Skip to content
/ fetchdts Public

A suite of type utilities for building strongly-typed APIs

License

Notifications You must be signed in to change notification settings

unjs/fetchdts

Repository files navigation

fetchdts

npm version npm downloads Github Actions Codecov

A suite of type utilities for building strongly-typed APIs

🚧 Under active development

Features

  • 💪 Strongly-typed query, body, headers, response.
  • 🗺️ Static path segments, as well as dynamic and wildcard parameters.
  • 📦 Exposes core utilities for building typed fetch functions.

Usage

Install package:

# npm
npm install fetchdts

# pnpm
pnpm install fetchdts
import type { DynamicParam, Endpoint, TypedFetchInput, TypedFetchResponse, WildcardParam } from 'fetchdts'

interface Schema {
  '/api': {
    '/test': {
      [Endpoint]: {
        POST: {
          body: { foo: string }
          response: { bar: string }
        }
      }
    }
    [DynamicParam]: {
      [Endpoint]: {
        GET: {
          body: { id: boolean }
          response: string
        }
      }
    }
  }
}

async function typedFetch<T extends TypedFetchInput<Schema>>(_input: T) {
  return {} as Promise<TypedFetchResponse<Schema, T>>
}

const _res = await typedFetch('/api/foo')
// typed as string

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Made with ❤️

Published under MIT License.

About

A suite of type utilities for building strongly-typed APIs

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published