Skip to content

Commit

Permalink
feat: update to support pluggable WebAPI Fetch implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Grohs authored and Adam Grohs committed Apr 11, 2019
1 parent cecff04 commit ade787f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface InitOptions {
client_secret: string
fetch?: Fetch
version?: string
application?: string
currency?: string
Expand All @@ -8,6 +9,7 @@ export interface InitOptions {
}

export interface Options {
fetch?: Fetch
application?: string
currency?: string
host?: string
Expand All @@ -18,3 +20,7 @@ export interface Options {
export interface Headers {
[key: string]: string
}

export interface Fetch {
(input?: Request | string, init?: RequestInit): Promise<Response>
}

0 comments on commit ade787f

Please sign in to comment.