diff --git a/examples/typescript/index.ts b/examples/typescript/index.ts index acac13b..ab8e9da 100755 --- a/examples/typescript/index.ts +++ b/examples/typescript/index.ts @@ -1,15 +1,39 @@ -import fetch from 'cross-fetch'; - -fetch('https://api.github.com/users/lquixada') - .then(res => { - if (res.status >= 400) { - throw new Error("Bad response from server"); - } - return res.json(); - }) - .then(user => { - console.log(user); - }) - .catch(err => { - console.error(err); - }); +/** This is a more complex setup to test type resolutions */ +import fetch, { Request, Response, Headers } from 'cross-fetch'; + +const headers = new Headers({ + 'Content-Type': 'application/json', + Accept: 'application/json' +}) + +const customFetch = async (input: RequestInfo, init: RequestInit): Promise => { + const req = new Request(input) + + return fetch(req, init) + .then((res: Response): Promise => { + if (res.status >= 400) { + throw new Error("Bad response from server"); + } + return res.json(); + }) + .then((user: any): Promise => { + // Clone user just for the sake of using `new Response` + const json = JSON.stringify(user); + const res = new Response(json, { headers }) + + return res.json() + }) + .then((user: any): void => { + console.log(user); + }) + .catch((err: Error): void => { + console.error(err); + }); +}; + +customFetch('https://api.github.com/users/lquixada', { + method: 'GET', + headers +}) + + diff --git a/examples/typescript/tsconfig.json b/examples/typescript/tsconfig.json index 3a8ca5a..a999a13 100644 --- a/examples/typescript/tsconfig.json +++ b/examples/typescript/tsconfig.json @@ -4,6 +4,8 @@ "target": "es6", "module": "commonjs", "noEmit": true, - "allowSyntheticDefaultImports": true - } + "allowSyntheticDefaultImports": true, + "strict": true + }, + "files": ["index.ts"] }