-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50956e2
commit fbb8080
Showing
7 changed files
with
34 additions
and
22 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
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,3 +1,6 @@ | ||
import { FinalRequestOptions, UnifiedResponse } from "types"; | ||
|
||
|
||
export abstract class Fetch { | ||
abstract call(url: string, options?: RequestInit): Promise<Response>; | ||
abstract call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse>; | ||
} |
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,7 +1,15 @@ | ||
import { FinalRequestOptions, UnifiedResponse } from 'types'; | ||
import { Fetch } from './BaseFetch'; | ||
|
||
export class BrowserFetch extends Fetch { | ||
call(url: string, options?: RequestInit): Promise<Response> { | ||
return fetch(url, options); | ||
call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> { | ||
const controller = new AbortController(); | ||
|
||
return fetch(url, { | ||
method: options?.method, | ||
headers: options?.headers as HeadersInit, | ||
body: options.body ? JSON.stringify(options.body) : undefined, | ||
signal: controller.signal, | ||
}); | ||
} | ||
} |
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,11 +1,15 @@ | ||
// import { RequestInit as NodeRequestInit } from 'node-fetch'; | ||
|
||
import { FinalRequestOptions, UnifiedResponse } from "types"; | ||
import { Fetch } from "./BaseFetch"; | ||
|
||
export class NodeFetch extends Fetch { | ||
async call(url: string, options?: RequestInit): Promise<Response> { | ||
const nodeFetch = (await import("node-fetch")).default; | ||
// @ts-ignore | ||
return nodeFetch(url, options); | ||
async call(url: string, options: FinalRequestOptions): Promise<UnifiedResponse> { | ||
const nodeFetchModule = await import("node-fetch"); | ||
const nodeFetch = nodeFetchModule.default; | ||
|
||
return nodeFetch(url, { | ||
method: options?.method, | ||
headers: options.headers as Record<string, string>, | ||
body: options?.body ? JSON.stringify(options.body) : undefined, | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -34,4 +34,5 @@ export { | |
type HTTPMethod, | ||
type DefaultQuery, | ||
type Headers, | ||
type UnifiedResponse, | ||
} from './API'; |