Giter VIP home page Giter VIP logo

typesafe-api-endpoints's Introduction

typesafe-api-endpoints

This is a proof of concept I have created to see how far we can take TypeScripts type inference to create a fully typesafe endpoints schema.

Result: It works quiet well! (with TypeScript version 4.4.x)

Note: I have used it in a SvelteKit production environment for a few months but recently switched to tRPC because I currently have no time to maintain this project.

Usage:

  1. First you need to define a Schema containing all HTTP methods, it's endpoints, slugs, query params and return types.
  2. Then use the Schema to implement a Server-handler
  3. Call the endpoints from the Client

Schema

We can define a schema like:

type Schema = CreateTypesafeApiEndpointsSchema<{
   GET: {
      'products': Product[]
      'products?limit': Product[]
      'product/{id}': Product
   }
   POST: {
      'product': (product: Product) => boolean
   },
   PUT: {
      'product/{productId}': (product: Partial<Product>) => boolean
   }
}>

Server

And can use the Schema type for our server-handler that forces us to implement all methods and it's endpoints:

export const handler: ServerHandler<Schema, 'SvelteKit'> = {
   GET: {
      'products': async () => {
         return products
      },
      'products?limit': async ({ query: { limit = Infinity } }) => {
         return products.slice(0, +limit)
      },
      'product/{id}': async ({ slugs: { id } }) => {
         return products.find(({ id: productId }) => +id === productId)
      }
   },
   POST: {
      'product': async ({ body: product }) => {
         // create logic
         return true
      }
   },
   PUT: {
      'product/{productId}': async ({ slugs: { productId }, body: product }) => {
         // update logic
         return true
      }
   }
}

Client

We now can also create the client:

export const api = createTypesafeApiEndpointsClient<Schema>('api-endpoint', fetch)

Whenever we use api a request is made to the endpoint we have provided as the first argument (in this case : /api-endpoint)

We can now call all endpoints from the api-object and get fully typed

const loadData = async () => {
   const response = await api.GET('product/{id}', { slugs: { id: '123' }, query: undefined })

   if (response.error) {
      throw response.error
   }

   return response.data
}

const data = await loadData()
// => 'data' is of type 'Product'

I could not get rid of needing to pass also undefined in some cases. I think the type definitions are to complex for TypeScript to resolve everything correctly.

Examples

You can take a look ath the examples folder to see an implementation of this library

typesafe-api-endpoints's People

Contributors

gh-action-bump-version avatar ivanhofer avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

leonardssh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.