Merge pull request #25737 from VincentLanglet/react-native/fetch

[react-native] Add missing GlobalFetch / RequestInfo definitions
This commit is contained in:
Eloy Durán
2018-05-16 15:41:07 +02:00
committed by GitHub
3 changed files with 35 additions and 2 deletions

View File

@@ -25,7 +25,11 @@ declare function fetchBundle(bundleId: number, callback: (error?: Error | null)
// Fetch API
//
declare function fetch(input: Request | string, init?: RequestInit): Promise<Response>;
declare interface GlobalFetch {
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
}
declare function fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
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;

View File

@@ -0,0 +1,26 @@
const fetchCopy: GlobalFetch['fetch'] = fetch;
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);
})

View File

@@ -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"
]
}
}