-
Notifications
You must be signed in to change notification settings - Fork 31
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 #68 from lifeomic/convertToTypescript
Update to typescript, and publish the types
- Loading branch information
Showing
8 changed files
with
486 additions
and
189 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": [ | ||
"plugin:@lifeomic/node/recommended", | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint"], | ||
"rules": { | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/ban-ts-ignore": "off", | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
"@typescript-eslint/no-non-null-assertion": "off", | ||
"@typescript-eslint/no-var-requires": "off", | ||
"lodash/import-scope": "off" | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,72 @@ | ||
import { Response, Headers as FetchHeaders } 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 type AxiosTransformer = (config: AxiosRequestConfig, input: string | undefined, init: FetchInit) => AxiosRequestConfig; | ||
|
||
export type AxiosFetch = (input?: string, init?: FetchInit) => Response; | ||
|
||
/** | ||
* A Fetch WebAPI implementation based on the Axios client | ||
*/ | ||
async function axiosFetch ( | ||
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) | ||
.reduce<Record<string, string>>( | ||
(acc, key) => { | ||
acc[key.toLowerCase()] = rawHeaders[key]; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
|
||
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 config = transformer ? transformer(rawConfig, input, init) : rawConfig; | ||
|
||
let result; | ||
try { | ||
result = await axios.request(config); | ||
} catch (err) { | ||
result = err.response; | ||
} | ||
|
||
const fetchHeaders = new FetchHeaders(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); | ||
} |
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,45 @@ | ||
// Copied and edited from axios@0.21.1 because types aren't available in version 0.17 | ||
|
||
export type Method = | ||
| 'get' | 'GET' | ||
| 'delete' | 'DELETE' | ||
| 'head' | 'HEAD' | ||
| 'options' | 'OPTIONS' | ||
| 'post' | 'POST' | ||
| 'put' | 'PUT' | ||
| 'patch' | 'PATCH' | ||
| 'purge' | 'PURGE' | ||
| 'link' | 'LINK' | ||
| 'unlink' | 'UNLINK'; | ||
|
||
export type ResponseType = | ||
| 'arraybuffer' | ||
| 'blob' | ||
| 'document' | ||
| 'json' | ||
| 'text' | ||
| 'stream'; | ||
|
||
export interface AxiosRequestConfig { | ||
url?: string; | ||
method?: Method; | ||
headers?: any; | ||
data?: any; | ||
responseType?: ResponseType; | ||
[key: string]: any; | ||
} | ||
|
||
export interface AxiosResponse<T = any> { | ||
data: T; | ||
status: number; | ||
statusText: string; | ||
headers: any; | ||
config: AxiosRequestConfig; | ||
request?: any; | ||
} | ||
|
||
export type AxiosPromise<T = any> = Promise<AxiosResponse<T>> & any | ||
|
||
export interface AxiosInstance { | ||
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>; | ||
} |
Oops, something went wrong.