From 49f1a6933cc22d1e703d631d5b861b8601ca2231 Mon Sep 17 00:00:00 2001 From: Fabio Berger <me@fabioberger.com> Date: Thu, 12 Jul 2018 23:13:47 +0200 Subject: Add fetchAsync util and RPCSubprovider --- packages/utils/src/fetchAsync.ts | 29 +++++++++++++++++++++++++++++ packages/utils/src/index.ts | 2 ++ 2 files changed, 31 insertions(+) create mode 100644 packages/utils/src/fetchAsync.ts (limited to 'packages/utils/src') diff --git a/packages/utils/src/fetchAsync.ts b/packages/utils/src/fetchAsync.ts new file mode 100644 index 000000000..7cb2c1759 --- /dev/null +++ b/packages/utils/src/fetchAsync.ts @@ -0,0 +1,29 @@ +import { FetchRequest } from '@0xproject/types'; +import 'isomorphic-fetch'; + +export const fetchAsync = async ( + endpoint: string, + options: FetchRequest, + timeoutMs: number = 20000, +): Promise<Response> => { + let finalOptions; + if ((process as any).browser === true) { + const controller = new AbortController(); + const signal = controller.signal; + setTimeout(() => { + controller.abort(); + }, timeoutMs); + finalOptions = { + signal, + ...options, + }; + } else { + finalOptions = { + timeout: timeoutMs, + ...options, + }; + } + + const response = await fetch(endpoint, finalOptions); + return response; +}; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index fd102cecb..48fd6152e 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -8,3 +8,5 @@ export { logUtils } from './log_utils'; export { abiUtils } from './abi_utils'; export { NULL_BYTES } from './constants'; export { errorUtils } from './error_utils'; +export { fetchAsync } from './fetchAsync'; +export { FetchRequest } from '@0xproject/types'; -- cgit v1.2.3 From 2e5ff53d7271509accc226e83d76d1785350b1d2 Mon Sep 17 00:00:00 2001 From: Fabio Berger <me@fabioberger.com> Date: Fri, 13 Jul 2018 12:42:01 +0200 Subject: -fetch'; --- packages/utils/src/fetchAsync.ts | 16 +++++++++------- packages/utils/src/index.ts | 1 - 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'packages/utils/src') diff --git a/packages/utils/src/fetchAsync.ts b/packages/utils/src/fetchAsync.ts index 7cb2c1759..a009f0c86 100644 --- a/packages/utils/src/fetchAsync.ts +++ b/packages/utils/src/fetchAsync.ts @@ -1,29 +1,31 @@ -import { FetchRequest } from '@0xproject/types'; import 'isomorphic-fetch'; export const fetchAsync = async ( endpoint: string, - options: FetchRequest, + options: RequestInit = {}, timeoutMs: number = 20000, ): Promise<Response> => { - let finalOptions; + let optionsWithAbortParam; if ((process as any).browser === true) { const controller = new AbortController(); const signal = controller.signal; setTimeout(() => { controller.abort(); }, timeoutMs); - finalOptions = { + optionsWithAbortParam = { signal, ...options, }; } else { - finalOptions = { + // HACK: the `timeout` param only exists in `node-fetch`, and not on the `isomorphic-fetch` + // `RequestInit` type. Since `isomorphic-fetch` conditionally wraps `node-fetch` when the + // execution environment is `Node.js`, we need to cast it to `any` in that scenario. + optionsWithAbortParam = { timeout: timeoutMs, ...options, - }; + } as any; } - const response = await fetch(endpoint, finalOptions); + const response = await fetch(endpoint, optionsWithAbortParam); return response; }; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 48fd6152e..b8e0b1775 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -9,4 +9,3 @@ export { abiUtils } from './abi_utils'; export { NULL_BYTES } from './constants'; export { errorUtils } from './error_utils'; export { fetchAsync } from './fetchAsync'; -export { FetchRequest } from '@0xproject/types'; -- cgit v1.2.3 From 4c7fd5a4e8b745bfdbfca20b5495f48332aae90d Mon Sep 17 00:00:00 2001 From: Fabio Berger <me@fabioberger.com> Date: Fri, 13 Jul 2018 15:03:13 +0200 Subject: Replace process.browser with detect-node library --- packages/utils/src/fetchAsync.ts | 3 ++- packages/utils/src/globals.d.ts | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'packages/utils/src') diff --git a/packages/utils/src/fetchAsync.ts b/packages/utils/src/fetchAsync.ts index a009f0c86..47d87815e 100644 --- a/packages/utils/src/fetchAsync.ts +++ b/packages/utils/src/fetchAsync.ts @@ -1,3 +1,4 @@ +import isNode = require('detect-node'); import 'isomorphic-fetch'; export const fetchAsync = async ( @@ -6,7 +7,7 @@ export const fetchAsync = async ( timeoutMs: number = 20000, ): Promise<Response> => { let optionsWithAbortParam; - if ((process as any).browser === true) { + if (isNode) { const controller = new AbortController(); const signal = controller.signal; setTimeout(() => { diff --git a/packages/utils/src/globals.d.ts b/packages/utils/src/globals.d.ts index 94e63a32d..98a4b56fc 100644 --- a/packages/utils/src/globals.d.ts +++ b/packages/utils/src/globals.d.ts @@ -4,3 +4,7 @@ declare module '*.json' { export default json; /* tslint:enable */ } + +declare module 'detect-node' { + export const isNode: boolean; +} -- cgit v1.2.3 From 350989bbec59b731a11c69237bec8625f2331ce2 Mon Sep 17 00:00:00 2001 From: Fabio Berger <me@fabioberger.com> Date: Fri, 13 Jul 2018 15:25:37 +0200 Subject: Move type defs to typescript-typingsd --- packages/utils/src/globals.d.ts | 4 ---- 1 file changed, 4 deletions(-) (limited to 'packages/utils/src') diff --git a/packages/utils/src/globals.d.ts b/packages/utils/src/globals.d.ts index 98a4b56fc..94e63a32d 100644 --- a/packages/utils/src/globals.d.ts +++ b/packages/utils/src/globals.d.ts @@ -4,7 +4,3 @@ declare module '*.json' { export default json; /* tslint:enable */ } - -declare module 'detect-node' { - export const isNode: boolean; -} -- cgit v1.2.3 From 2f41ed50c198b2952b1af50de55df2f19d645033 Mon Sep 17 00:00:00 2001 From: Fabio Berger <me@fabioberger.com> Date: Fri, 13 Jul 2018 16:47:32 +0200 Subject: Switch conditional --- packages/utils/src/fetchAsync.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/utils/src') diff --git a/packages/utils/src/fetchAsync.ts b/packages/utils/src/fetchAsync.ts index 47d87815e..8295ff5ec 100644 --- a/packages/utils/src/fetchAsync.ts +++ b/packages/utils/src/fetchAsync.ts @@ -7,7 +7,7 @@ export const fetchAsync = async ( timeoutMs: number = 20000, ): Promise<Response> => { let optionsWithAbortParam; - if (isNode) { + if (!isNode) { const controller = new AbortController(); const signal = controller.signal; setTimeout(() => { -- cgit v1.2.3 From 80071beaacd4303bd5c7edb699d202861fd98b50 Mon Sep 17 00:00:00 2001 From: Fabio Berger <me@fabioberger.com> Date: Fri, 13 Jul 2018 19:38:35 +0200 Subject: Add assertion to make sure caller to fetchAsync isn't trying to set timeout in a context-specific way --- packages/utils/src/fetchAsync.ts | 3 +++ 1 file changed, 3 insertions(+) (limited to 'packages/utils/src') diff --git a/packages/utils/src/fetchAsync.ts b/packages/utils/src/fetchAsync.ts index 8295ff5ec..6ae2ba1a4 100644 --- a/packages/utils/src/fetchAsync.ts +++ b/packages/utils/src/fetchAsync.ts @@ -6,6 +6,9 @@ export const fetchAsync = async ( options: RequestInit = {}, timeoutMs: number = 20000, ): Promise<Response> => { + if (options.signal || (options as any).timeout) { + throw new Error('Cannot call fetchAsync with options.signal or options.timeout. To set a timeout, please use the supplied "timeoutMs" parameter.'); + } let optionsWithAbortParam; if (!isNode) { const controller = new AbortController(); -- cgit v1.2.3 From 2f0a9148387b66d75b9ee3856e68f0ed3aa149de Mon Sep 17 00:00:00 2001 From: Fabio Berger <me@fabioberger.com> Date: Fri, 13 Jul 2018 19:53:49 +0200 Subject: Fix linter --- packages/utils/src/fetchAsync.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'packages/utils/src') diff --git a/packages/utils/src/fetchAsync.ts b/packages/utils/src/fetchAsync.ts index 6ae2ba1a4..c02e5baba 100644 --- a/packages/utils/src/fetchAsync.ts +++ b/packages/utils/src/fetchAsync.ts @@ -7,7 +7,9 @@ export const fetchAsync = async ( timeoutMs: number = 20000, ): Promise<Response> => { if (options.signal || (options as any).timeout) { - throw new Error('Cannot call fetchAsync with options.signal or options.timeout. To set a timeout, please use the supplied "timeoutMs" parameter.'); + throw new Error( + 'Cannot call fetchAsync with options.signal or options.timeout. To set a timeout, please use the supplied "timeoutMs" parameter.', + ); } let optionsWithAbortParam; if (!isNode) { -- cgit v1.2.3