0.0.3 • Published 3 years ago

manifetch v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

manifetch

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.

Install

$ npm i manifetch --save

Usage

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

See also example and test.

Goals

  • 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

Status

  • Basic Node implementation using undici-fetch for fetch()
  • Write comprehensive test suite based on node-tap.
  • Write comprehensive usage examples for fastify-api and fastify-vite
  • Optimize applyParams() implementation with new fast-apply-params package
  • Memoize Proxy instances on the client, prepopulate all wrappers on the server

License

MIT