-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Client.clients & Agent is a Client.
Refs: #616
- Loading branch information
Showing
13 changed files
with
301 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,63 @@ | ||
'use strict' | ||
|
||
const Client = require('./lib/core/client') | ||
const Client = require('./lib/client') | ||
const errors = require('./lib/core/errors') | ||
const Pool = require('./lib/client-pool') | ||
const { Agent, getGlobalAgent, setGlobalAgent } = require('./lib/agent') | ||
const Pool = require('./lib/pool') | ||
const Agent = require('./lib/agent') | ||
const util = require('./lib/core/util') | ||
const { InvalidArgumentError } = require('./lib/core/errors') | ||
const api = require('./lib/api') | ||
|
||
Object.assign(Client.prototype, api) | ||
Object.assign(Pool.prototype, api) | ||
Object.assign(Agent.prototype, api) | ||
|
||
function undici (url, opts) { | ||
return new Pool(url, opts) | ||
} | ||
|
||
module.exports = undici | ||
|
||
module.exports.Pool = Pool | ||
module.exports.Client = Client | ||
module.exports.errors = errors | ||
|
||
module.exports.Agent = Agent | ||
module.exports.setGlobalAgent = setGlobalAgent | ||
module.exports.getGlobalAgent = getGlobalAgent | ||
|
||
function dispatchFromAgent (fn) { | ||
return (url, { agent = getGlobalAgent(), method = 'GET', ...opts } = {}, ...additionalArgs) => { | ||
let globalClient = new Agent() | ||
|
||
function setGlobalClient (agent) { | ||
if (!agent || typeof agent.dispatch !== 'function') { | ||
throw new InvalidArgumentError('Argument agent must implement Agent') | ||
} | ||
globalClient = agent | ||
} | ||
|
||
function getGlobalClient () { | ||
return globalClient | ||
} | ||
|
||
function makeDispatch (fn) { | ||
return (url, { agent, client = getGlobalClient(), method = 'GET', ...opts } = {}, ...additionalArgs) => { | ||
if (opts.path != null) { | ||
throw new InvalidArgumentError('unsupported opts.path') | ||
} | ||
|
||
if (agent) { | ||
throw new InvalidArgumentError('unsupported opts.agent. Did you mean opts.client?') | ||
} | ||
|
||
const { origin, pathname, search } = util.parseURL(url) | ||
const path = search ? `${pathname}${search}` : pathname | ||
|
||
return fn.call(agent, { ...opts, origin, method, path }, ...additionalArgs) | ||
return fn.call(client, { ...opts, origin, method, path }, ...additionalArgs) | ||
} | ||
} | ||
|
||
module.exports.request = dispatchFromAgent(api.request) | ||
module.exports.stream = dispatchFromAgent(api.stream) | ||
module.exports.pipeline = dispatchFromAgent(api.pipeline) | ||
module.exports.connect = dispatchFromAgent(api.connect) | ||
module.exports.upgrade = dispatchFromAgent(api.upgrade) | ||
module.exports.setGlobalClient = setGlobalClient | ||
module.exports.getGlobalClient = getGlobalClient | ||
|
||
module.exports.request = makeDispatch(api.request) | ||
module.exports.stream = makeDispatch(api.stream) | ||
module.exports.pipeline = makeDispatch(api.pipeline) | ||
module.exports.connect = makeDispatch(api.connect) | ||
module.exports.upgrade = makeDispatch(api.upgrade) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./core/client') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.