diff --git a/src/index.ts b/src/index.ts index e88c021..3f258a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,18 +3,21 @@ import fetch from 'cross-fetch' import { InitOptions, Options, - Headers + Headers, + Fetch } from './types' import { removeLeadingSlash } from './utils' export class createClient { private client_secret: string private options?: Options + private fetch?: Fetch constructor(options: InitOptions) { const { client_secret, ...others } = options this.client_secret = client_secret + this.fetch = options.fetch ? options.fetch : fetch this.options = { host: options.host ? options.host : 'api.shipengine.com', version: options.version ? options.version : 'v1', @@ -58,7 +61,7 @@ export class createClient { ? data : { body: JSON.stringify({ data }) } - const response = await fetch(uri, { + const response = await this.fetch(uri, { method, headers, ...(data && body) diff --git a/src/types.ts b/src/types.ts index 9e380a1..73d1519 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,6 @@ export interface InitOptions { client_secret: string + fetch?: Fetch version?: string application?: string currency?: string @@ -8,6 +9,7 @@ export interface InitOptions { } export interface Options { + fetch?: Fetch application?: string currency?: string host?: string @@ -18,3 +20,7 @@ export interface Options { export interface Headers { [key: string]: string } + +export interface Fetch { + (input?: Request | string, init?: RequestInit): Promise +}