Skip to content

PickResponseByStatusCode

reference
1 min readUpdated

Kind: Type

Source: src/client/types.ts

Part of: Client

Keep only specific status code responses from all routes of an app. Useful when error responses are handled centrally (e.g., via custom fetch) and you want the client to only expose success response types.

PickResponseByStatusCode filters an app's route response definitions to keep only responses matching the supplied status-code type. Use it when a shared HTTP layer handles certain responses and client-facing route types should expose only the remaining responses.

Definition

ts
App extends HonoBase<infer E, infer _ extends Schema, infer B> ? PickSchema<ExtractSchema<App>, U> extends infer S extends Schema ? Hono<E, S, B> : never : never

Diagram

mermaid
graph LR
  A[App route contract] --> C[PickResponseByStatusCode]
  B[Allowed status-code type] --> C
  C --> D[Filtered route response definitions]
  D --> E[Client-facing route types]

Usage

ts
import type { PickResponseByStatusCode } from '@ts-rest/core';
import { contract } from './contract';
import type { SuccessStatusCode } from './http-status';

type ClientRoutes = PickResponseByStatusCode<
  typeof contract,
  SuccessStatusCode
>;

// Use ClientRoutes when building a client whose shared fetch layer
// handles excluded responses before they reach application code.

AI Coding Instructions

  • Pass the full app contract type as the first generic argument and the allowed status-code union as the second.
  • Define shared status-code unions near the HTTP or fetch integration when multiple clients need the same filtering behavior.
  • Keep centrally handled responses out of the selected status-code union so they do not appear in client response types.
  • Apply this type to contract definitions before deriving client types that should only expose selected responses.

Was this page helpful?

Download as PDF
PickResponseByStatusCode — Hono (narrator proof)