-
Notifications
You must be signed in to change notification settings - Fork 104
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
Showing
2 changed files
with
43 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> => { | ||
const req = new Request(input) | ||
|
||
return fetch(req, init) | ||
.then((res: Response): Promise<any> => { | ||
if (res.status >= 400) { | ||
throw new Error("Bad response from server"); | ||
} | ||
return res.json(); | ||
}) | ||
.then((user: any): Promise<Response> => { | ||
// 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 | ||
}) | ||
|
||
|
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