Comparing version 0.4.0 to 0.5.1
46
index.js
@@ -1,2 +0,7 @@ | ||
'use strict' | ||
/** @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 */ | ||
@@ -7,6 +12,27 @@ const http = require('http') | ||
const axios = require('axios').default | ||
const defaultAdapter = axios.defaults.adapter | ||
const defaultAdapter = /** @type {AxiosAdapter} */(axios.defaults.adapter) | ||
/** | ||
* @callback RequestListener | ||
* @param {IncomingMessage} request | ||
* @param {ServerResponse} response | ||
*/ | ||
/** @typedef {RequestListener | Server} Handler */ | ||
/** | ||
* 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 urlObject = url.parse(config.url) | ||
const urlObject = url.parse(config.url || '') | ||
@@ -36,3 +62,4 @@ // Forcely set protocol to HTTP | ||
}).then(() => { | ||
urlObject.port = server.address().port | ||
const address = /** @type {AddressInfo} */(server.address()) | ||
urlObject.port = address.port.toString() | ||
config.url = url.format(urlObject) | ||
@@ -55,2 +82,8 @@ return defaultAdapter(config) | ||
/** | ||
* 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. | ||
* @returns {AxiosInstance} The axios instance would use for test. | ||
*/ | ||
const axiosist = handler => { | ||
@@ -64,4 +97,3 @@ return axios.create({ | ||
exports = module.exports = axiosist | ||
exports.default = axiosist | ||
exports.createAdapter = createAdapter | ||
module.exports = axiosist | ||
module.exports.createAdapter = createAdapter |
{ | ||
"name": "axiosist", | ||
"version": "0.4.0", | ||
"version": "0.5.1", | ||
"description": "Convert node.js request handler to axios adapter", | ||
@@ -22,2 +22,5 @@ "main": "index.js", | ||
], | ||
"files": [ | ||
"index.js" | ||
], | ||
"author": "George Cheng <Gerhut@GMail.com>", | ||
@@ -24,0 +27,0 @@ "license": "MIT", |
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
1
8240
4
84