GithubHelp home page GithubHelp logo

ts-rest

Incrementally adoptable RPC-like client and server helpers for a magical end to end typed experience πŸͺ„

GitHub Repo stars License Bundle Size

Join us on Discord for help, feedback, and discussions!


Discord Shield

Introduction

ts-rest offers a simple way to define a contract for your API, which can be both consumed and implemented by your application, giving you end to end type safety without the hassle or code generation.

Features

  • End-to-end type safety πŸ›Ÿ
  • RPC-like client side API ⚑️
  • Small Bundle Size πŸ“‰
  • No Code Generation πŸƒβ€β™€οΈ
  • Zod support for runtime validation πŸ”’
  • Full optional OpenAPI integration πŸ“

πŸ‘‰ Start reading the official Quickstart Guide πŸ‘ˆ

Super Simple Example

Easily define your API contract somewhere shared

const contract = c.router({
  getPosts: {
    method: 'GET',
    path: '/posts',
    query: z.object({
      skip: z.number(),
      take: z.number(),
    }), // <-- Zod schema
    responses: {
      200: c.type<Post[]>(), // <-- OR normal TS types
    },
    headers: z.object({
      'x-pagination-page': z.coerce.number().optional(),
    }),
  },
});

Fulfill the contract on your server, with a type-safe router:

const router = s.router(contract, {
  getPosts: async ({ params: { id } }) => {
    return {
      status: 200,
      body: prisma.post.findUnique({ where: { id } }),
    };
  },
});

Consume the api on the client with a RPC-like interface:

const result = await client.getPosts({
  headers: { 'x-pagination-page': 1 },
  query: { skip: 0, take: 10 },
  // ^-- Fully typed!
});

Quickstart

Create a contract, implement it on your server then consume it in your client. Incrementally adopt, trial it with your team, then get shipping faster.

πŸ‘‰ Start reading the official Quickstart Guide πŸ‘ˆ

Contributors ✨

MASSIVE Thanks to all of these wonderful people (emoji key), who have helped make ts-rest possible:

Youssef Gaber
Youssef Gaber

πŸ’» πŸ€” ⚠️
Per Hermansson
Per Hermansson

πŸ“– πŸ’»
GrΓ©gory Houllier
GrΓ©gory Houllier

πŸ“–
Michael Angelo
Michael Angelo

πŸ“–
Pieter Venter
Pieter Venter

πŸ“–
Rifaldhi AW
Rifaldhi AW

πŸ“–
Jonathan White
Jonathan White

πŸ’» πŸ“–
Max Brosnahan
Max Brosnahan

πŸ’» πŸ€”
Oliver Butler
Oliver Butler

πŸ’» πŸ€” πŸ“– πŸš‡ 🚧
Adrian Barylski
Adrian Barylski

πŸ’» πŸ“– ⚠️
Neil A. Dobson
Neil A. Dobson

πŸ’»
Eric Do
Eric Do

πŸ“–
Ben
Ben

πŸ’» πŸ“– ⚠️
LW
LW

πŸ’» πŸ›
Andrew Vance
Andrew Vance

πŸ“–

Star History

Since our first commit in 2022 we've been growing steadily. We're proud of our progress and we're excited about the future.

Join us on Discord for help, feedback, and discussions!


Discord Shield

ts-rest's Projects

ts-rest icon ts-rest

RPC-like client, contract, and server implementation for a pure REST API

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.