From a72c9959063eb9c62d39d99cb8e81809f7d046ed Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 13 May 2018 01:05:59 +0200 Subject: [PATCH 1/3] Add GlobalFetch / RequestInfo definitions --- types/react-native/globals.d.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/types/react-native/globals.d.ts b/types/react-native/globals.d.ts index 92ae988ecb..2110ab7278 100644 --- a/types/react-native/globals.d.ts +++ b/types/react-native/globals.d.ts @@ -25,7 +25,11 @@ declare function fetchBundle(bundleId: number, callback: (error?: Error | null) // Fetch API // -declare function fetch(input: Request | string, init?: RequestInit): Promise; +declare interface GlobalFetch { + fetch(input: RequestInfo, init?: RequestInit): Promise; +} + +declare function fetch(input: RequestInfo, init?: RequestInit): Promise; interface Blob {} @@ -85,6 +89,8 @@ declare var Request: { new(input: Request | string, init?: RequestInit): Request; }; +declare type RequestInfo = Request | string; + declare interface ResponseInit { headers?: HeadersInit_; status?: number; From 8b7bccf8a1145f2628e6fb4f0d8db1f0478868e4 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 15 May 2018 00:00:05 +0200 Subject: [PATCH 2/3] Add tests --- types/react-native/test/globals.tsx | 3 +++ types/react-native/tsconfig.json | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 types/react-native/test/globals.tsx diff --git a/types/react-native/test/globals.tsx b/types/react-native/test/globals.tsx new file mode 100644 index 0000000000..a6488cef82 --- /dev/null +++ b/types/react-native/test/globals.tsx @@ -0,0 +1,3 @@ +const fetchCopy: GlobalFetch['fetch'] = fetch; + +const requestInfoAsString: RequestInfo = 'request'; diff --git a/types/react-native/tsconfig.json b/types/react-native/tsconfig.json index 16c6bf9275..c577fb0d8b 100644 --- a/types/react-native/tsconfig.json +++ b/types/react-native/tsconfig.json @@ -20,9 +20,10 @@ "files": [ "index.d.ts", "test/index.tsx", + "test/globals.tsx", "test/animated.tsx", "test/init-example.tsx", "test/ART.tsx", "test/legacy-properties.tsx" ] -} \ No newline at end of file +} From c20e3f91b0c2d161b0a0a0a553ae301785aadef8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 15 May 2018 15:57:16 +0200 Subject: [PATCH 3/3] Improve tests --- types/react-native/test/globals.tsx | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/types/react-native/test/globals.tsx b/types/react-native/test/globals.tsx index a6488cef82..b5eae800b1 100644 --- a/types/react-native/test/globals.tsx +++ b/types/react-native/test/globals.tsx @@ -1,3 +1,26 @@ const fetchCopy: GlobalFetch['fetch'] = fetch; -const requestInfoAsString: RequestInfo = 'request'; +const myHeaders = new Headers(); +myHeaders.append('Content-Type', 'image/jpeg'); + +const myInit: RequestInit = { + method: 'GET', + headers: myHeaders, + mode: 'cors', +}; + +const myRequest = new Request('flowers.jpg'); + +fetch(myRequest, myInit).then((response) => { + console.log(response.type); + console.log(response.url); + console.log(response.status); + console.log(response.ok); + console.log(response.statusText); + console.log(response.headers); + + return response.blob(); +}).then((blob) => { + const init = { status: 200, statusText: 'SuperSmashingGreat!' } + const myResponse = new Response(blob, init); +})