Skip to content

Commit

Permalink
refactor: expose interceptor as is
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Feb 29, 2024
1 parent 9048eb5 commit 7656e1b
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 128 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports.DecoratorHandler = DecoratorHandler
module.exports.RedirectHandler = RedirectHandler
module.exports.createRedirectInterceptor = createRedirectInterceptor
module.exports.interceptors = {
proxy: require('./lib/interceptor/proxy'),
Proxy: require('./lib/interceptor/proxy'),
redirect: require('./lib/interceptor/redirect'),
retry: require('./lib/interceptor/retry')
}
Expand Down
44 changes: 25 additions & 19 deletions lib/interceptor/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,32 @@

const { InvalidArgumentError } = require('../core/errors')
const ProxyAgent = require('../dispatcher/proxy-agent')
const DispatcherBase = require('../dispatcher/dispatcher-base')
const Dispatcher = require('../dispatcher/dispatcher')

class ProxyInterceptor extends DispatcherBase {
class ProxyInterceptor extends Dispatcher {
constructor (dispatcher, opts) {
if (dispatcher == null) {
throw new InvalidArgumentError(
'Dispatcher instance is mandatory for ProxyInterceptor'
)
}

if (typeof opts === 'string') {
opts = { uri: opts }
}

if (!opts || (!opts.uri && !(opts instanceof URL))) {
throw new InvalidArgumentError(
'Proxy opts.uri or instance of URL is mandatory'
)
}

if (opts.auth && opts.token) {
throw new InvalidArgumentError(
'opts.auth cannot be used in combination with opts.token'
)
}

super()
this.dispatcher = dispatcher
this.agent = new ProxyAgent(opts)
Expand All @@ -20,20 +42,4 @@ class ProxyInterceptor extends DispatcherBase {
}
}

module.exports = opts => {
if (typeof opts === 'string') {
opts = { uri: opts }
}

if (!opts || (!opts.uri && !(opts instanceof URL))) {
throw new InvalidArgumentError('Proxy opts.uri or instance of URL is mandatory')
}

if (opts.auth && opts.token) {
throw new InvalidArgumentError(
'opts.auth cannot be used in combination with opts.token'
)
}

return dispatcher => new ProxyInterceptor(dispatcher, opts)
}
module.exports = ProxyInterceptor
2 changes: 1 addition & 1 deletion lib/interceptor/redirect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { InvalidArgumentError } = require('../core/errors')
const Dispatcher = require('../dispatcher/dispatcher-base')
const Dispatcher = require('../dispatcher/dispatcher')
const RedirectHandler = require('../handler/RedirectHandler')

class RedirectDispatcher extends Dispatcher {
Expand Down
2 changes: 1 addition & 1 deletion lib/interceptor/retry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Dispatcher = require('../dispatcher/dispatcher-base')
const Dispatcher = require('../dispatcher/dispatcher')
const RetryHandler = require('../handler/RetryHandler')

class RetryDispatcher extends Dispatcher {
Expand Down
Loading

0 comments on commit 7656e1b

Please sign in to comment.