https://github.com/anisjonischkeit/typed-fetch
https://github.com/anisjonischkeit/typed-fetch
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/anisjonischkeit/typed-fetch
- Owner: anisjonischkeit
- Created: 2018-08-28T13:26:29.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-17T16:04:10.000Z (over 6 years ago)
- Last Synced: 2025-03-26T16:56:11.146Z (2 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/typed-fetch
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Typed Fetch
Typed Fetch addresses the issue of flow not being able to validate the types of the data that an API call responds with. The package exposes the `typedFetch` function which mimics the fetch API but additionally accepts a decoder. If the response data's type is the expected type, the response is a promise of the decoded object. If the response json is of a different structure, an exception is raised that should be caught and handled.
## Examples
```javascript
import typedFetch from "typed-fetch";
import { object, string } from "decoders";// Our response should include a title
const todosDecoder = object({ title: string });typedFetch("https://jsonplaceholder.typicode.com/todos/1", todosDecoder)
.then(todos => {
// todos has the flow type {| title: string |} as specified by the decoderconst myTodos: { title: string } = todos; // passes a flow check
const myFoo: { foo: string } = todos; // fails a flow check
})
```For more examples on how decoders work, [go to the decoders github page](https://github.com/nvie/decoders)