Comparing version 0.5.1 to 0.6.0
67
index.js
@@ -1,7 +0,9 @@ | ||
/** @typedef {import('net').AddressInfo} AddressInfo */ | ||
/** @typedef {import('http').Server} Server */ | ||
/** @typedef {import('http').IncomingMessage} IncomingMessage */ | ||
/** @typedef {import('http').ServerResponse} ServerResponse */ | ||
/** @typedef {import('axios').AxiosAdapter} AxiosAdapter */ | ||
/** @typedef {import('axios').AxiosInstance} AxiosInstance */ | ||
/** | ||
* @typedef {import('net').AddressInfo} AddressInfo | ||
* @typedef {import('http').Server} Server | ||
* @typedef {import('http').IncomingMessage} IncomingMessage | ||
* @typedef {import('http').ServerResponse} ServerResponse | ||
* @typedef {import('axios').AxiosAdapter} AxiosAdapter | ||
* @typedef {import('axios').AxiosInstance} AxiosInstance | ||
*/ | ||
@@ -23,15 +25,6 @@ const http = require('http') | ||
/** | ||
* Create the adapter of the request callback, used for your own axios instance. | ||
* ``` | ||
* axiosist(callback) | ||
* ``` | ||
* is equal to | ||
* ``` | ||
* axios.create({ adapter: axiosist.createAdapter(callback) }) | ||
* ``` | ||
* | ||
* @param {Handler} handler A handler for axiosist, may be a request listener or a http server. | ||
* @returns {AxiosAdapter} The axios adapter would used in adapter options of axios. | ||
*/ | ||
const createAdapter = handler => config => { | ||
const createAdapter = handler => config => new Promise((resolve, reject) => { | ||
const urlObject = url.parse(config.url || '') | ||
@@ -54,10 +47,9 @@ | ||
return new Promise((resolve, reject) => { | ||
server.on('error', reject) | ||
if (listening) { | ||
resolve() | ||
} else { | ||
server.listen(0, '127.0.0.1', resolve) | ||
} | ||
}).then(() => { | ||
server.on('error', reject) | ||
let promise = listening | ||
? Promise.resolve() | ||
: new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) | ||
promise = promise.then(() => { | ||
const address = /** @type {AddressInfo} */(server.address()) | ||
@@ -67,20 +59,15 @@ urlObject.port = address.port.toString() | ||
return defaultAdapter(config) | ||
}).then(value => new Promise(resolve => { | ||
if (listening) { | ||
resolve(value) | ||
} else { | ||
server.close(() => resolve(value)) | ||
} | ||
}), reason => new Promise((resolve, reject) => { | ||
if (listening) { | ||
reject(reason) | ||
} else { | ||
server.close(() => reject(reason)) | ||
} | ||
})) | ||
} | ||
}) | ||
if (listening) { | ||
promise.then(resolve, reject) | ||
} else { | ||
promise.then( | ||
value => server.close(() => resolve(value)), | ||
reason => server.close(() => reject(reason)) | ||
) | ||
} | ||
}) | ||
/** | ||
* Create an axios instance with adapter of the request callback, and treat all HTTP statuses as fulfilled. | ||
* | ||
* @param {Handler} handler A handler, may be a request listener or a http server. | ||
@@ -87,0 +74,0 @@ * @returns {AxiosInstance} The axios instance would use for test. |
{ | ||
"name": "axiosist", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Convert node.js request handler to axios adapter", | ||
@@ -39,7 +39,7 @@ "main": "index.js", | ||
"@types/node": ">=8.9.0", | ||
"ava": "^1.2.1", | ||
"nyc": "^14.0.0", | ||
"ava": "^1.4.1", | ||
"nyc": "^14.1.0", | ||
"standard": "^12.0.1", | ||
"typescript": "^3.3.3333" | ||
"typescript": "^3.4.5" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7816
68