npm install treeder/api
Then import with:
import { api, apiInit } from 'api'
import { api, apiInit } from 'https://cdn.jsdelivr.net/gh/treeder/api@0/api.js'
apiInit({ apiURL: apiURL })
Then just use it in place of fetch:
let r = await api(`/v1/posts`)
To add an Authorization
header, specify a getToken() function.
This is an example you can use on the browser side if you are using Firebase Auth.
apiInit({ getToken: () => auth.currentUser.getIdToken() })
On the server, you do the same thing, but your token probably comes from an env var:
apiInit({ getToken: () => 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', getToken: () => this.options.apiToken })