Skip to content

Commit

Permalink
refactor: resolveFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Feb 25, 2022
1 parent 694c006 commit 52cc1ba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
9 changes: 2 additions & 7 deletions src/lib/StorageBucketApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DEFAULT_HEADERS } from './constants'
import { Fetch, get, post, put, remove } from './fetch'
import { getGlobalFetch } from './helpers'
import { resolveFetch } from './helpers'
import { Bucket } from './types'
import crossFetch from 'cross-fetch'

export class StorageBucketApi {
protected url: string
Expand All @@ -12,11 +11,7 @@ export class StorageBucketApi {
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
this.url = url
this.headers = { ...DEFAULT_HEADERS, ...headers }
if (fetch) {
this.fetch = fetch
} else {
this.fetch = getGlobalFetch() ?? ((crossFetch as unknown) as Fetch)
}
this.fetch = resolveFetch(fetch)
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/lib/StorageFileApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Fetch, FetchParameters, get, post, remove } from './fetch'
import { getGlobalFetch } from './helpers'
import { resolveFetch } from './helpers'
import { FileObject, FileOptions, SearchOptions } from './types'
import crossFetch from 'cross-fetch'

const DEFAULT_SEARCH_OPTIONS = {
limit: 100,
Expand Down Expand Up @@ -33,11 +32,7 @@ export class StorageFileApi {
this.url = url
this.headers = headers
this.bucketId = bucketId
if (fetch) {
this.fetch = fetch
} else {
this.fetch = getGlobalFetch() ?? ((crossFetch as unknown) as Fetch)
}
this.fetch = resolveFetch(fetch)
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export const getGlobalFetch = (): typeof fetch | undefined => {
if (typeof fetch === 'undefined') {
return undefined
import crossFetch from 'cross-fetch'

type Fetch = typeof fetch

export const resolveFetch = (customFetch?: Fetch): Fetch => {
if (customFetch) {
return customFetch
} else if (typeof fetch === 'undefined') {
return (crossFetch as unknown) as Fetch
} else {
return fetch
}
Expand Down

0 comments on commit 52cc1ba

Please sign in to comment.