@lifeomic/axios-fetch
Advanced tools
Comparing version 2.0.6-pr-99-1374004398-1634941034.0 to 3.0.1-pr-102-1462920668-1636990839.0
{ | ||
"name": "@lifeomic/axios-fetch", | ||
"version": "2.0.6-pr-99-1374004398-1634941034.0", | ||
"version": "3.0.1-pr-102-1462920668-1636990839.0", | ||
"description": "A WebAPI Fetch implementation backed by an Axios client", | ||
@@ -12,3 +12,4 @@ "main": "src/index.js", | ||
"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", | ||
@@ -41,23 +42,20 @@ "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", | ||
"form-data": "^4.0.0", | ||
"nock": "^13.1.4", | ||
"node-fetch": "^2.6.1", | ||
"nyc": "^15.1.0", | ||
"sinon": "^11.1.2", | ||
"ts-node": "^10.3.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
"dependencies": { | ||
"@types/node-fetch": "^2.5.10", | ||
"form-data": "^2.5.0", | ||
"node-fetch": "^2.6.1" | ||
"@types/node-fetch": "^2.5.10" | ||
}, | ||
"eslintConfig": { | ||
"extends": "plugin:@lifeomic/node/recommended" | ||
}, | ||
"nyc": { | ||
@@ -64,0 +62,0 @@ "check-coverage": true, |
# Axios-Fetch | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/lifeomic/axios-fetch.svg)](https://greenkeeper.io/) | ||
[![Build Status](https://travis-ci.org/lifeomic/axios-fetch.svg?branch=master)](https://travis-ci.org/lifeomic/axios-fetch) | ||
[![npm](https://img.shields.io/npm/v/@lifeomic/axios-fetch.svg)](https://www.npmjs.com/package/@lifeomic/axios-fetch) | ||
[![Build Status](https://github.com/lifeomic/axios-fetch/actions/workflows/release.yaml/badge.svg)](https://github.com/lifeomic/axios-fetch/actions/workflows/release.yaml) | ||
[![Coverage Status](https://coveralls.io/repos/github/lifeomic/axios-fetch/badge.svg?branch=master)](https://coveralls.io/github/lifeomic/axios-fetch?branch=master) | ||
![Dependabot Badge](https://flat.badgen.net/dependabot/lifeomic/axios-fetch?icon=dependabot) | ||
This library exposes a Fetch WebAPI implementation backed by a Axios client | ||
This library exposes a Fetch WebAPI implementation backed by an Axios client | ||
instance. This allows a bridge between projects that have pre-configured Axios | ||
clients already to other libraries that require Fetch implementations. | ||
## Global Response object | ||
It is expected that the global Response object will be available. For testing we use the [node-fetch | ||
](https://www.npmjs.com/package/node-fetch) library. | ||
```typescript | ||
import { Response } from 'node-fetch'; | ||
// @ts-expect-error node-fetch doesn't exactly match the Response object, but close enough. | ||
global.Response = Response; | ||
``` | ||
## Example | ||
One library that wants a Fetch implementation is the [Apollo Link | ||
@@ -20,3 +34,3 @@ HTTP](https://www.apollographql.com/docs/link/links/http.html) library. If your | ||
const link = createHttpLink({ | ||
uri: "/graphql" | ||
uri: "/graphql", | ||
fetch: buildAxiosFetch(yourAxiosInstance) | ||
@@ -40,3 +54,3 @@ }); | ||
## Support for IE11 | ||
## Support for IE11 | ||
@@ -43,0 +57,0 @@ To Support IE11 add following dependencies |
@@ -1,12 +0,3 @@ | ||
import { Response } from 'node-fetch'; | ||
import FormData from 'form-data'; | ||
import { AxiosInstance, AxiosRequestConfig } from './types'; | ||
export interface FetchInit extends Record<string, any> { | ||
headers?: Record<string, string>; | ||
method?: AxiosRequestConfig['method']; | ||
body?: FormData | any; | ||
extra?: any; | ||
} | ||
export declare type AxiosTransformer = (config: AxiosRequestConfig, input: string | undefined, init: FetchInit) => AxiosRequestConfig; | ||
export declare type AxiosFetch = (input?: string, init?: FetchInit) => Promise<Response>; | ||
export declare function buildAxiosFetch(axios: AxiosInstance, transformer?: AxiosTransformer): AxiosFetch; | ||
import { AxiosInstance, AxiosRequestConfig } from './axios-types'; | ||
export declare type AxiosTransformer<Init extends RequestInit = RequestInit> = (config: AxiosRequestConfig, input: RequestInfo, init?: Init) => AxiosRequestConfig; | ||
export declare const buildAxiosFetch: <Init extends RequestInit = RequestInit>(axios: AxiosInstance, transformer?: AxiosTransformer<Init> | undefined) => (input: RequestInfo, init?: Init | undefined) => Promise<Response>; |
"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]) | ||
.reduce((acc, key) => { | ||
acc[key.toLowerCase()] = rawHeaders[key]; | ||
return acc; | ||
}, {}); | ||
transformer = (config) => config) => async (input, init) => { | ||
const rawHeaders = (0, typeUtils_1.createAxiosHeaders)(init === null || init === void 0 ? void 0 : init.headers); | ||
const lowerCasedHeaders = {}; | ||
Object.entries(rawHeaders).forEach(([name, value]) => { | ||
lowerCasedHeaders[name.toLowerCase()] = value; | ||
}); | ||
if (!('content-type' in lowerCasedHeaders)) { | ||
@@ -25,5 +20,5 @@ lowerCasedHeaders['content-type'] = 'text/plain;charset=UTF-8'; | ||
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 +31,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 +45,10 @@ try { | ||
} | ||
const fetchHeaders = new node_fetch_1.Headers(result.headers); | ||
return new node_fetch_1.Response(result.data, { | ||
return new 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); | ||
} | ||
}; | ||
const buildAxiosFetch = (axios, transformer) => axiosFetch(axios, transformer); | ||
exports.buildAxiosFetch = buildAxiosFetch; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJpbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQSwyQ0FBK0Q7QUFDL0QsMERBQWlDO0FBY2pDOztHQUVHO0FBQ0gsS0FBSyxVQUFVLFVBQVUsQ0FDdkIsS0FBb0I7QUFDcEIsZ0VBQWdFO0FBQ2hFLFdBQThCLEVBQzlCLEtBQWMsRUFDZCxPQUFrQixFQU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5IHtcbiAgICAgIHJlc3VsdCA9IGF3YWl0IGF4aW9zLnJlcXVlc3QoY29uZmlnKTtcbiAgICB9IGNhdGNoIChlcnI6IGFueSkge1xuICAgICAgaWYgKGVyci5yZXNwb25zZSkge1xuICAgICAgICByZXN1bHQgPSBlcnIucmVzcG9uc2U7XG4gICAgICB9IGVsc2Uge1xuICAgICAgICB0aHJvdyBlcnI7XG4gICAgICB9XG4gICAgfVxuXG4gICAgcmV0dXJuIG5ldyBSZXNwb25zZShyZXN1bHQuZGF0YSwge1xuICAgICAgc3RhdHVzOiByZXN1bHQuc3RhdHVzLFxuICAgICAgc3RhdHVzVGV4dDogcmVzdWx0LnN0YXR1c1RleHQsXG4gICAgICBoZWFkZXJzOiBjcmVhdGVGZXRjaEhlYWRlcnMocmVzdWx0LmhlYWRlcnMpXG4gICAgfSk7XG4gIH07XG5cbmV4cG9ydCBjb25zdCBidWlsZEF4aW9zRmV0Y2ggPSA8SW5pdCBleHRlbmRzIFJlcXVlc3RJbml0ID0gUmVxdWVzdEluaXQ+IChcbiAgYXhpb3M6IEF4aW9zSW5zdGFuY2UsXG4gIHRyYW5zZm9ybWVyPzogQXhpb3NUcmFuc2Zvcm1lcjxJbml0PlxuKSA9PiBheGlvc0ZldGNoKGF4aW9zLCB0cmFuc2Zvcm1lcik7XG4iXX0= |
115
src/index.ts
@@ -1,76 +0,67 @@ | ||
import { Response, Headers as FetchHeaders } from 'node-fetch'; | ||
import FormData from 'form-data'; | ||
import { AxiosInstance, AxiosRequestConfig } from './types'; | ||
import { | ||
createAxiosHeaders, | ||
createFetchHeaders, | ||
getUrl | ||
} from './typeUtils'; | ||
import { AxiosInstance, AxiosRequestConfig } from './axios-types'; | ||
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: Record<string, string> = {}; | ||
Object.entries(rawHeaders).forEach(([name, value]) => { | ||
lowerCasedHeaders[name.toLowerCase()] = value; | ||
}); | ||
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 (axios: AxiosInstance, transformer?: AxiosTransformer): AxiosFetch { | ||
return axiosFetch.bind(undefined, axios, transformer); | ||
} | ||
export const buildAxiosFetch = <Init extends RequestInit = RequestInit> ( | ||
axios: AxiosInstance, | ||
transformer?: AxiosTransformer<Init> | ||
) => axiosFetch(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
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
23016
1
10
257
71
15
1
- Removedform-data@^2.5.0
- Removednode-fetch@^2.6.1
- Removedform-data@2.5.2(transitive)
- Removednode-fetch@2.7.0(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedtr46@0.0.3(transitive)
- Removedwebidl-conversions@3.0.1(transitive)
- Removedwhatwg-url@5.0.0(transitive)