This package assumes your using a REST API with JSON requests and responses. And if you are, it will handle setting headers for content-type and authorization, serialing and parsing JSON and some other nice tricks like caching responses.
npm install treeder/apiThen import:
import { api, API, apiInit } from 'api'import { api, API, apiInit } from 'https://cdn.jsdelivr.net/gh/treeder/api@1/api.js'- api is a generic API instance, ready to use just by calling
api(url, opts)instead offetch(url, opts) - API is a class letting you instantiate and customize.
- apiInit lets you customize the default API instance.
Then just use it in place of fetch:
let r = await api(`/v1/users`)Use with models package to get even better JSON parsing capabilities
let r = await api(`/v1/user/123`)
let user = parseModel(r.user, User)apiInit({ apiURL: apiURL })- apiURL will be prefixed to any calls.
Add headers to the API instance that will be used for all subsequent calls.
apiInit({ headers: { Authorization: `apiKey ${process.env.API_KEY}` } })If you have different API's you are talking to, you can create new instances:
const api2 = new API({ apiURL: 'https://somewhere.com' })
// then use it with:
let r = await api2.fetch('/abc')Any errors will throw an APIError which has a status field on it to check the code.
try {
let r = await api('https://x.com/abc')
} catch (e) {
return Response.json({ error: e }, { status: e.status })
}This is exported too so you can use it in your API's to return nice errors.
Caching can really help reduce network traffic and increase performance depending on your use case. This will only cache GET requests.
To use caching, you have to create an api object and use fetchAndCache.
let api = new API()
let r = await api.fetchAndCache('https://somewhere.com/my/stuff')