MCPcopy
hub / github.com/ts-rest/ts-rest

github.com/ts-rest/ts-rest @v3.52.1 sqlite

repository ↗ · DeepWiki ↗ · release v3.52.1 ↗
518 symbols 1,558 edges 288 files 2 documented · 0%
README

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.

<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=ts-rest/ts-rest&type=Timeline" />

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

Discord Shield

<a href="https://vercel.com/?utm_source=ts-rest&utm_campaign=oss" target="_blank" rel="noreferrer"

<img
  src="https://ts-rest.com/img/powered-by-vercel.svg"
  alt="Powered by Vercel"
  height="40"
/>

Extension points exported contracts — how you extend this code

User (Interface)
(no doc)
apps/example-express/src/authenticated-app.ts
State (Interface)
(no doc)
apps/example-next/state.ts
Post (Interface)
(no doc)
libs/example-contracts/src/lib/contract-blog.ts
SchemaObject (Interface)
(no doc)
libs/ts-rest/open-api/src/lib/ts-rest-open-api.ts
Form (Interface)
(no doc)
apps/example-next/pages/post/new.tsx
PostTs (Interface)
(no doc)
libs/example-contracts/src/lib/contract-ts.ts
AppRouteOptions (Interface)
(no doc)
libs/ts-rest/serverless/src/lib/types.ts
Form (Interface)
(no doc)
apps/example-next/pages/post/[id]/edit.tsx

Core symbols most depended-on inside this repo

initContract
called by 76
libs/ts-rest/core/src/lib/dsl.ts
TsRest
called by 57
libs/ts-rest/nest/src/lib/ts-rest.decorator.ts
TsRestHandler
called by 50
libs/ts-rest/nest/src/lib/ts-rest-nest-handler.ts
tsRestHandler
called by 50
libs/ts-rest/nest/src/lib/ts-rest-nest-handler.ts
TsRestRequest
called by 40
libs/ts-rest/nest/src/lib/ts-rest-request.decorator.ts
useQuery
called by 39
libs/ts-rest/react-query-v5/src/v5/types/hooks.ts
checkZodSchema
called by 32
libs/ts-rest/core/src/lib/zod-utils.ts
isAppRoute
called by 19
libs/ts-rest/core/src/lib/dsl.ts

Shape

Function 240
Method 148
Class 114
Interface 16

Languages

TypeScript100%

Modules by API surface

libs/ts-rest/serverless/src/lib/router-builder/index.ts26 symbols
libs/ts-rest/nest/src/lib/ts-rest-nest-handler.spec.ts25 symbols
libs/ts-rest/nest/src/lib/ts-rest-nest-handler.ts18 symbols
libs/ts-rest/nest/src/lib/ts-rest-nest.spec.ts17 symbols
libs/ts-rest/core/src/lib/client.ts12 symbols
libs/ts-rest/serverless/src/lib/router.ts11 symbols
libs/ts-rest/react-query-v5/src/v5/types/query-client-functions.ts11 symbols
libs/ts-rest/react-query-v5/src/v5/types/hooks.ts11 symbols
libs/ts-rest/next/src/lib/ts-rest-next.ts11 symbols
libs/ts-rest/serverless/src/lib/types.ts10 symbols
libs/ts-rest/react-query/src/v4/react-query.ts10 symbols
libs/ts-rest/open-api/src/lib/ts-rest-open-api.ts10 symbols

Dependencies from manifests, versioned

@actions/core1.10.0 · 1×
@anatine/zod-openapi2.0.1 · 1×
@azure/functions4.5.0 · 1×
@babel/core7.14.5 · 1×
@babel/preset-react7.14.5 · 1×
@babel/runtime7.20.6 · 1×
@changesets/cli2.26.2 · 1×
@cloudflare/workers-types4.20240405.0 · 1×
@docusaurus/core2.4.1 · 1×
@docusaurus/module-type-aliases2.4.1 · 1×
@docusaurus/preset-classic2.4.1 · 1×
@eslint/eslintrc3.2.0 · 1×

For agents

$ claude mcp add ts-rest \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact