-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from lifeomic/followNodeFetchSpec
Follow node fetch spec
- Loading branch information
Showing
11 changed files
with
254 additions
and
134 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
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ node_modules/ | |
.idea/ | ||
src/**/*.js | ||
src/**/*.d.ts | ||
!src/axios-types.d.ts | ||
test/**/*.js |
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
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 |
---|---|---|
@@ -1,76 +1,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 = (config: AxiosRequestConfig, input: string | undefined, init: FetchInit) => AxiosRequestConfig; | ||
|
||
export type AxiosFetch = (input?: string, init?: FetchInit) => Promise<Response>; | ||
export type AxiosTransformer<Init extends RequestInit = RequestInit> = | ||
(config: AxiosRequestConfig, input: RequestInfo, init?: Init) => AxiosRequestConfig; | ||
|
||
/** | ||
* 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: fetchHeaders | ||
}); | ||
} | ||
return new Response(result.data, { | ||
status: result.status, | ||
statusText: result.statusText, | ||
headers: createFetchHeaders(result.headers) | ||
}); | ||
}; | ||
|
||
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); |
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,53 @@ | ||
import { Headers as NodeHeaders } from 'node-fetch'; | ||
|
||
export type HeadersLike = string[][] | Record<string, string | undefined> | Headers | NodeHeaders; | ||
|
||
export type UrlLike = string | { | ||
href?: string; | ||
url?: string; | ||
} | ||
|
||
export function createFetchHeaders (axiosHeaders: Record<string, string> = {}): string[][] { | ||
const headers: string[][] = []; | ||
Object.entries(axiosHeaders).forEach(([name, value]) => { | ||
headers.push([name, value]); | ||
}); | ||
return headers; | ||
} | ||
|
||
const isHeaders = (headers: HeadersLike): headers is Headers => headers.constructor.name === 'Headers'; | ||
|
||
export function createAxiosHeaders (headers: HeadersLike = {}): Record<string, string> { | ||
const rawHeaders: Record<string, string> = {}; | ||
|
||
if (isHeaders(headers)) { | ||
headers.forEach((value, name) => { | ||
rawHeaders[name] = value; | ||
}); | ||
} else if (Array.isArray(headers)) { | ||
headers.forEach(([name, value]) => { | ||
if (value) { | ||
rawHeaders[name!] = value; | ||
} | ||
}); | ||
} else { | ||
Object.entries(headers).forEach(([name, value]) => { | ||
if (value) { | ||
rawHeaders[name] = value; | ||
} | ||
}); | ||
} | ||
return rawHeaders; | ||
} | ||
|
||
export function getUrl (input?: UrlLike): string | undefined { | ||
let url: string | undefined; | ||
if (typeof input === 'string') { | ||
url = input; | ||
} else if (input?.href) { | ||
url = input.href; | ||
} else if (input?.url) { | ||
url = input.url; | ||
} | ||
return url; | ||
} |
Oops, something went wrong.