JSPM

  • Created
  • Published
  • Downloads 121616
  • Score
    100M100P100Q161897F
  • License MIT

Package Exports

  • @ts-rest/core
  • @ts-rest/core/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ts-rest/core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ts-rest-core

Motivation

ts-rest-core aims to allow for the creation of type safe contracts to be upheld by producers and consumers of the contract.

Some end-to-end type safe libraries such as tRPC are amazing, however, they aren't able to separate the contract from the implementation, in situations with published packages or wanting to avoid unnecessary rebuilds with tools such as NX this is a problem.

One example of this is with NX, in NX you can rebuild only "affected" packages, however, if you export your contract (e.g. tRPC) from the backend, your front end will need to be rebuilt as well.

Contract Abstraction

Unlike tRPC, ts-rest-core aims to separate the contract from the server-side implementation, this adds another "jump" to go client->contract->server, however, this additional step provides a much better dev experience in Monorepos with NX.

  • Contracts should have no knowledge of the implementation
  • Contracts should exist within their own package, or within a shared library within a monorepo
  • Contracts should define all the types to be shared with the consumers

Implementation - API

Contract

import { initTsCont } from 'ts-rest-core';

const c = initTsCont();

export type Post = {
  id: number;
  title: string;
  body: string;
};

export const router = c.router({
  posts: c.router({
    getPost: c.query({
      method: 'GET',
      path: ({ id }: { id: string }) => `/posts/${id}`,
      response: c.response<Post>(),
    }),
    getPosts: c.query({
      method: 'GET',
      path: () => '/posts',
      response: c.response<Post[]>(),
    }),
  }),
});

Client

const client = initClient(router, {
  api: fetchApi,
  baseUrl: 'http://localhost:3333',
  baseHeaders: {},
});

const { data } = await client.posts.getPosts();

Server

const server = initServer(router, {
  api: fetchApi,
  baseUrl: 'http://localhost:3333',
  baseHeaders: {},
});

// TRPC-like type-safe generation or just extract input/output types