@lifeomic/axios-fetch
Advanced tools
Comparing version 2.0.5 to 2.0.6-pr-101-1436700965-1636405616.0
{ | ||
"name": "@lifeomic/axios-fetch", | ||
"version": "2.0.5", | ||
"version": "2.0.6-pr-101-1436700965-1636405616.0", | ||
"description": "A WebAPI Fetch implementation backed by an Axios client", | ||
"main": "src/index.js", | ||
"types": "src/index.ts", | ||
"types": "src/index.d.ts", | ||
"files": [ | ||
"src/**/*" | ||
], | ||
"scripts": { | ||
"test": "nyc ava", | ||
"lint": "eslint . --ext .js,.ts -f codeframe && tsc --noEmit", | ||
"lint": "eslint . --ext .js,.ts -f codeframe", | ||
"postlint": "yarn tsc --noEmit", | ||
"pretest": "yarn lint", | ||
@@ -37,14 +41,14 @@ "coverage": "nyc report --reporter=text-lcov > ./.nyc_output/lcov.info", | ||
"@lifeomic/eslint-plugin-node": "^2.0.1", | ||
"@types/sinon": "^10.0.0", | ||
"@typescript-eslint/eslint-plugin": "^4.22.0", | ||
"@typescript-eslint/parser": "^4.22.0", | ||
"@types/sinon": "^10.0.4", | ||
"@typescript-eslint/eslint-plugin": "^5.1.0", | ||
"@typescript-eslint/parser": "^5.1.0", | ||
"ava": "^3.15.0", | ||
"axios": "^0.21.4", | ||
"coveralls": "^3.1.0", | ||
"eslint": "^6.0.0", | ||
"nock": "^11.3.3", | ||
"nyc": "^15.0.0", | ||
"sinon": "^10.0.1", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.2.4" | ||
"axios": "^0.24.0", | ||
"coveralls": "^3.1.1", | ||
"eslint": "^7.32.0", | ||
"nock": "^13.1.4", | ||
"nyc": "^15.1.0", | ||
"sinon": "^11.1.2", | ||
"ts-node": "^10.3.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
@@ -51,0 +55,0 @@ "dependencies": { |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildAxiosFetch = void 0; | ||
const node_fetch_1 = require("node-fetch"); | ||
const form_data_1 = __importDefault(require("form-data")); | ||
const typeUtils_1 = require("./typeUtils"); | ||
/** | ||
* A Fetch WebAPI implementation based on the Axios client | ||
*/ | ||
async function axiosFetch(axios, | ||
const axiosFetch = (axios, | ||
// Convert the `fetch` style arguments into a Axios style config | ||
transformer, input, init = {}) { | ||
const rawHeaders = init.headers || {}; | ||
const lowerCasedHeaders = Object.keys(rawHeaders).filter(key => key && rawHeaders[key]) | ||
transformer = (config) => config) => async (input, init) => { | ||
const rawHeaders = (0, typeUtils_1.createAxiosHeaders)(init === null || init === void 0 ? void 0 : init.headers); | ||
const lowerCasedHeaders = Object.keys(rawHeaders) | ||
.reduce((acc, key) => { | ||
acc[key.toLowerCase()] = rawHeaders[key]; | ||
const value = rawHeaders[key]; | ||
if (value) { | ||
acc[key.toLowerCase()] = value; | ||
} | ||
return acc; | ||
@@ -25,5 +25,5 @@ }, {}); | ||
const rawConfig = { | ||
url: input, | ||
method: init.method || 'GET', | ||
data: typeof init.body === 'undefined' || init.body instanceof form_data_1.default ? init.body : String(init.body), | ||
url: (0, typeUtils_1.getUrl)(input), | ||
method: (init === null || init === void 0 ? void 0 : init.method) || 'GET', | ||
data: init === null || init === void 0 ? void 0 : init.body, | ||
headers: lowerCasedHeaders, | ||
@@ -36,3 +36,3 @@ // Force the response to an arraybuffer type. Without this, the Response | ||
}; | ||
const config = transformer ? transformer(rawConfig, input, init) : rawConfig; | ||
const config = transformer(rawConfig, input, init); | ||
let result; | ||
@@ -50,13 +50,12 @@ try { | ||
} | ||
const fetchHeaders = new node_fetch_1.Headers(result.headers); | ||
return new node_fetch_1.Response(result.data, { | ||
status: result.status, | ||
statusText: result.statusText, | ||
headers: fetchHeaders | ||
headers: (0, typeUtils_1.createFetchHeaders)(result.headers) | ||
}); | ||
} | ||
}; | ||
function buildAxiosFetch(axios, transformer) { | ||
return axiosFetch.bind(undefined, axios, transformer); | ||
return axiosFetch(axios, trans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XN1bHQuaGVhZGVycylcbiAgICB9KTtcbiAgfTtcblxuZXhwb3J0IGZ1bmN0aW9uIGJ1aWxkQXhpb3NGZXRjaDxJbml0IGV4dGVuZHMgUmVxdWVzdEluaXQgPSBSZXF1ZXN0SW5pdD4gKGF4aW9zOiBBeGlvc0luc3RhbmNlLCB0cmFuc2Zvcm1lcj86IEF4aW9zVHJhbnNmb3JtZXI8SW5pdD4pIHtcbiAgcmV0dXJuIGF4aW9zRmV0Y2goYXhpb3MsIHRyYW5zZm9ybWVyKTtcbn1cbiJdfQ== |
115
src/index.ts
@@ -1,76 +0,69 @@ | ||
import { Response, Headers as FetchHeaders } from 'node-fetch'; | ||
import FormData from 'form-data'; | ||
import { AxiosInstance, AxiosRequestConfig } from './types'; | ||
import { Response, RequestInfo, RequestInit } from 'node-fetch'; | ||
import { createAxiosHeaders, createFetchHeaders, getUrl } from './typeUtils'; | ||
import { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
export interface FetchInit extends Record<string, any> { | ||
headers?: Record<string, string>; | ||
method?: AxiosRequestConfig['method']; | ||
body?: FormData | any; | ||
extra?: any; | ||
} | ||
export type AxiosTransformer<Init extends RequestInit = RequestInit> = (config: AxiosRequestConfig, input: RequestInfo, init?: Init) => AxiosRequestConfig; | ||
export type AxiosTransformer = (config: AxiosRequestConfig, input: string | undefined, init: FetchInit) => AxiosRequestConfig; | ||
export type AxiosFetch = (input?: string, init?: FetchInit) => Promise<Response>; | ||
/** | ||
* A Fetch WebAPI implementation based on the Axios client | ||
*/ | ||
async function axiosFetch ( | ||
const axiosFetch = <Init extends RequestInit = RequestInit>( | ||
axios: AxiosInstance, | ||
// Convert the `fetch` style arguments into a Axios style config | ||
transformer?: AxiosTransformer, | ||
input?: string, | ||
init: FetchInit = {} | ||
) { | ||
const rawHeaders: Record<string, string> = init.headers || {}; | ||
const lowerCasedHeaders = Object.keys(rawHeaders).filter(key => key && rawHeaders[key]) | ||
.reduce<Record<string, string>>( | ||
(acc, key) => { | ||
acc[key.toLowerCase()] = rawHeaders[key] as string; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
transformer: AxiosTransformer<Init> = (config) => config | ||
) => async ( | ||
input: RequestInfo, | ||
init?: Init | ||
) => { | ||
const rawHeaders = createAxiosHeaders(init?.headers); | ||
const lowerCasedHeaders = Object.keys(rawHeaders) | ||
.reduce<Record<string, string>>( | ||
(acc, key) => { | ||
const value = rawHeaders[key]; | ||
if (value) { | ||
acc[key.toLowerCase()] = value; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
if (!('content-type' in lowerCasedHeaders)) { | ||
lowerCasedHeaders['content-type'] = 'text/plain;charset=UTF-8'; | ||
} | ||
if (!('content-type' in lowerCasedHeaders)) { | ||
lowerCasedHeaders['content-type'] = 'text/plain;charset=UTF-8'; | ||
} | ||
const rawConfig: AxiosRequestConfig = { | ||
url: input, | ||
method: init.method || 'GET', | ||
data: typeof init.body === 'undefined' || init.body instanceof FormData ? init.body : String(init.body), | ||
headers: lowerCasedHeaders, | ||
// Force the response to an arraybuffer type. Without this, the Response | ||
// object will try to guess the content type and add headers that weren't in | ||
// the response. | ||
// NOTE: Don't use 'stream' because it's not supported in the browser | ||
responseType: 'arraybuffer' | ||
}; | ||
const rawConfig: AxiosRequestConfig = { | ||
url: getUrl(input), | ||
method: (init?.method as AxiosRequestConfig['method']) || 'GET', | ||
data: init?.body, | ||
headers: lowerCasedHeaders, | ||
// Force the response to an arraybuffer type. Without this, the Response | ||
// object will try to guess the content type and add headers that weren't in | ||
// the response. | ||
// NOTE: Don't use 'stream' because it's not supported in the browser | ||
responseType: 'arraybuffer' | ||
}; | ||
const config = transformer ? transformer(rawConfig, input, init) : rawConfig; | ||
const config = transformer(rawConfig, input, init); | ||
let result; | ||
try { | ||
result = await axios.request(config); | ||
} catch (err: any) { | ||
if (err.response) { | ||
result = err.response; | ||
} else { | ||
throw err; | ||
let result; | ||
try { | ||
result = await axios.request(config); | ||
} catch (err: any) { | ||
if (err.response) { | ||
result = err.response; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
const fetchHeaders = new FetchHeaders(result.headers); | ||
return new Response(result.data, { | ||
status: result.status, | ||
statusText: result.statusText, | ||
headers: createFetchHeaders(result.headers) | ||
}); | ||
}; | ||
return new Response(result.data, { | ||
status: result.status, | ||
statusText: result.statusText, | ||
headers: fetchHeaders | ||
}); | ||
export function buildAxiosFetch<Init extends RequestInit = RequestInit> (axios: AxiosInstance, transformer?: AxiosTransformer<Init>) { | ||
return axiosFetch(axios, transformer); | ||
} | ||
export function buildAxiosFetch (axios: AxiosInstance, transformer?: AxiosTransformer): AxiosFetch { | ||
return axiosFetch.bind(undefined, axios, transformer); | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
21663
208
2
1