A simple manifest-based fetch()
API client building utility.
It is obvious in the sense it uses a staightforward, nearly obvious pattern to automatically build API clients from route definitions in a minimalist manifest. Conceived originally as part of fastify-api
, which already provides a manifest compatible with manifetch
based on your API route definitions.
$ npm i manifetch --save
const fetch = require('undici-fetch')
const manifetch = require('manifetch')
const prefix = 'https://jsonplaceholder.typicode.com'
const get = manifetch({ fetch, prefix })
const manifest = {
getPosts: ['GET', '/posts'],
getPost: ['GET', '/posts/:id'],
nestedDemonstration: {
getPosts: ['GET', '/posts'],
getPost: ['GET', '/posts/:id'],
},
}
const client = new Proxy(manifest, { get })
async function main () {
const { json: posts } = await client.getPosts()
const { json: post } = await client.getPost({ id: 10 })
console.log('First post out of collection:', posts[0])
console.log('Tenth post, individually requested', post)
}
main().then(() => process.exit())
- Work seamlessly on Node, Deno and on the browser
- Facilitate
fetch()
usage with minor, sensible API enhancements - Automatically construct API clients based on a minimalist API manifest
- Support both a minimalist manifest and the OpenAPI specification
- Basic Node implementation using undici-fetch for
fetch()
- Write comprehensive test suite based on node-tap.
- Write comprehensive usage examples for
fastify-api
andfastify-vite
- Optimize
applyParams()
implementation with new fast-apply-params package - Memoize
Proxy
instances on the client, prepopulate all wrappers on the server
MIT