Skip to content

treeder/api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

api function - an enhanced fetch

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.

Getting started

On the server

npm install treeder/api

Then import:

import { api, API, apiInit } from 'api'

In the browser

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 of fetch(url, opts)
  • API is a class letting you instantiate and customize.
  • apiInit lets you customize the default API instance.

Usage

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)

Configure default instance

apiInit({ apiURL: apiURL })
  • apiURL will be prefixed to any calls.

Authentication

Add headers to the API instance that will be used for all subsequent calls.

apiInit({ headers: { Authorization: `apiKey ${process.env.API_KEY}` } })

Multiple APIs

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')

Errors

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

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')

About

Fetch with auth and JSON as default.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •