Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@ts-rest/nest

ts-rest140.4kMIT3.52.1TypeScript support: included

Nest server integration for @ts-rest

rpc, rest, typescript, typescript-rest, nestjs, http, api, type safe

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.

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


Discord Shield

changelog

@ts-rest/nest

3.52.1

3.52.0

Minor Changes

  • 23567fa: Change multi-handler to not use @All decorator, now creates dummy methods for each route within a multi handler at runtime. This means we no longer need our own route matching alogirithm (we can utilise nest entirely) and we now don't have collisions with other controllers with the same path but different methods
  • 964f7c6: Support optional path params in the type system, and ensure multiple levels of query params are dealt with in nest
  • 9a444c5: Added NestJS v11 to dependencies

3.51.1

3.51.0

3.50.0

3.49.4

3.49.3

3.49.2

3.49.1

Patch Changes

  • b31454f: All ts-rest libraries are now packaged in a better way to be compatible with as many bundlers as possible. The @tanstack/react-query No QueryClient set error should also occur much less now.

3.49.0

3.48.1

3.48.0

3.47.0

3.46.0

Minor Changes

  • d763d45: Export RequestValidationErrorSchema for default request validation error responses.

3.45.2

3.45.1

3.45.0

3.44.1

3.44.0

3.43.0

Minor Changes

  • 308b966: TsRestResponseError can be thrown from any server package

3.42.0

3.41.2

3.41.1

3.41.0

3.40.1

3.40.0

3.39.2

3.39.1

3.39.0

Minor Changes

  • 860e402: Add contract definition for an absent body and handle accordingly on the server

3.38.0

3.37.0

Patch Changes

  • 85b262b: Fix return types for Nest handlers

3.36.0

Minor Changes

  • 3e8701c: Add functionality to be able to set configuration options globally using TsRestModule

Breaking Change

  • The deprecated @JsonQuery decorator has been removed. Use the existing options argument on @TsRest instead.

3.35.1

Patch Changes

  • b3fde9b: Fix Nest.js interceptor returninng promise when using new Nest.js handlers
  • 14f9416: Allow Nest.js handlers to return non-promise responses

3.35.0

3.34.0

3.33.1

3.33.0

3.32.0

3.31.0

Minor Changes

  • dcf40a6: feat: @ts-rest/nest allow TsRestException to be handled by NestJS exception filters
  • 89941a2: feat: @ts-rest/nest Adds support to provide a cause to TsRestException

3.30.5

Patch Changes

  • 9bd7402: - @ts-rest/fastify fix: fastify deprecated routerPath property (fixes #392)
    • @ts-rest/open-api fix: Pass through contentType to OpenApi schema (#414)
    • @ts-rest/core fix: Content-type text/html returns blob body (fixes #418)
  • 8cc95c5: add changeset for latest changes

3.30.4

Patch Changes

3.30.3

3.30.2

3.30.1

3.30.0

3.29.0

Minor Changes

  • 19aeb0a: - feat: Add option to disable request validation
    • This is useful when a user wants to perform validation or handle validation errors themselves

Patch Changes

  • 5f7b236: - bump @ts-rest/react-query peer dependency @tanstack/react-query to ^4.0.0 (latest 4.33.0)
    • bump @ts-rest/react-query peer dependency zod to ^3.21.0
    • upgrades NX to 16.7 for project root

3.28.0

Minor Changes

  • 207e9c5: We now support the following versions of Nestjs:
    "@nestjs/common": "^9.0.0 || ^10.0.0",
    "@nestjs/core": "^9.0.0 || ^10.0.0",

3.27.0

Patch Changes

  • 55411ad: Upgrade zod to 3.21.4 Upgrade @anatine/zod-openapi to 2.0.1

3.26.4

Patch Changes

  • 62db9e0: fix: add support for trailing slashes in the nest multi-handler routing

3.26.3

Patch Changes

  • 5365f2a: Fix multi-handler breaking in Fastify with query parameters

3.26.2

Patch Changes

  • b3d3868: Remove extra peer dependencies from @ts-rest/nest

3.26.1

Patch Changes

  • 1afbf08: Add missing ts-rest-nest support for Fastify (thanks btravers for the GH issue)

3.26.0

Minor Changes

  • fcf877d: Allow defining non-json response types in the contract
  • 48b138d: Add new SingleHandler and MultiHandler API to @ts-rest/nest

3.25.1

Patch Changes

  • 81560d4: Fix ESM/CJS issues in package.json

3.25.0

Patch Changes

  • bf21a75: Internal refactor of types

3.24.0

3.23.0

Minor Changes

  • 74bb4a8: Implement strict mode at a contract level. Strict mode ensures that only known responses are allowed by the type system. This applies both on the server and client side. Enable this with strictStatusCodes: true when defining a contract. If you would like to have the vanilla client throw an error when the response status is not known then you will need to use throwOnUnknownStatus when initializing the client.

3.22.0

3.21.2

3.21.1

Patch Changes

  • 1b4ef1e: Fix incorrect detection of zod objects with nested ZodEffects and fix regression with validation

3.21.0

Patch Changes

  • 8729bb5: Fix node16 esm module resolution

3.20.0

Minor Changes

  • c1c1d31: Add type-safe header definitions to contracts

3.19.5

3.19.4

3.19.3

3.19.2

Patch Changes

  • 96ab6bd: Revert ESM fix, due to failed compilation on Next.js

3.19.1

Patch Changes

  • ecac73d: Fix compatibility with Node.js TS native ESM code

3.19.0

3.18.1

3.18.0

3.17.0

3.16.2

3.16.1

Patch Changes

  • 8a8b606: Increased type safety for the @TsRest decorator

3.16.0

Minor Changes

  • e490cf3: - Added server-side response validation feature
  • Deprecated @Api decorator, use @TsRest instead

3.15.0

Minor Changes

  • 0f1edf9: Rename some Nest functions and types, and deprecate old names

  • Fix Nest deprecation warning when passing Zod error to HttpException (#122)

  • Some internal helper types (NestControllerShapeFromAppRouter and NestAppRouteShape) that were previously exported are now kept internal. You can use NestControllerInterface and NestRequestShapes instead.

3.14.0

3.13.1

Patch Changes

  • d778e60: Rebuilt without code comments in the compiled JS

3.13.0

3.12.1

Patch Changes

  • e0164f6: Publish README

3.12.0

Minor Changes

  • a53a384: Add response shape shape helper to make allow you to extract the response from a Contract route
  • 5a13803: Allow typed query parameters by encoding them as JSON strings (disabled by default)

3.11.2

3.11.1

3.11.0

3.10.2

3.10.1

3.10.0

Minor Changes

  • 046e498: Use Nest.js req.query and req.params instead of parsing from URL

3.9.0

3.8.0

3.7.0

Minor Changes

  • 8a19717: Add pathParams transformations

3.6.1

3.6.0

Minor Changes

  • 6753c69: Build ESM and CommonJS for improved compatibility

3.5.0

Minor Changes

  • 068822d: Add support for multipart/form-data

3.4.2

3.4.1

3.4.0

3.3.0

3.2.2

3.2.1

3.2.0

3.1.1

3.1.0

3.0.0

Major Changes

  • 895112a: Migrate paths to a string rather than function

2.1.0

Minor Changes

  • 75f157a: Add Zod validation to @ts-rest/next

2.0.1

Patch Changes

  • 119aed6: Bump versions to 2.0.1

2.0.0

Major Changes

  • 4792b26: Change contract to support multiple responses, for different statuses
  • c88fb99: Rename data to body to be more HTTP spec compliant

Patch Changes

  • 4792b26: Add error handling support to express

1.3.0

Patch Changes

  • 077d57b: Update client to have zod response type
  • 5f87b1a: Add OpenAPI options for generateOpenApi

1.2.0

1.1.0

1.0.4

Patch Changes

  • 3adb9c8: Change build path

1.0.3

Patch Changes

  • 0d706a3: Sync package versions

1.0.2

Patch Changes

  • 1fe80ea: sync versions

1.0.0

Major Changes

  • 86a5cb7: add react-query and nest integration