8000 fix(client): Return a union of success types when multiple are defined · aklinker1/zeta@e5bdddc · GitHub
[go: up one dir, main page]

Skip to content

Commit e5bdddc

Browse files
committed
fix(client): Return a union of success types when multiple are defined
1 parent 83a4059 commit e5bdddc

5 files changed

Lines changed: 116 additions & 5 deletions

File tree

‎src/__tests__/app.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { createTestAppClient } from "../testing";
55
import { expectTypeOf } from "expect-type";
66
import type { AnyDef, GetAppData } from "../types";
77

8+
// Silence console.error logs
9+
globalThis.console.error = mock();
10+
811
describe("App", () => {
912
describe("fetch", () => {
1013
describe("response type inference", () => {

‎src/__tests__/client.test.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { describe, it, expect } from "bun:test";
1+
import { describe, it, expect, mock } from "bun:test";
22
import { createApp } from "../app";
33
import { createTestAppClient } from "../testing";
44
import type { App } from "../types";
55
import type { GetClientRoutes } from "../client";
66
import { expectTypeOf } from "expect-type";
77
import { z } from "zod/v4";
8+
import { HttpStatus } from "../status";
9+
import { ErrorResponse, NoResponse } from "../custom-responses";
10+
11+
// Silence console.error logs
12+
globalThis.console.error = mock();
813

914
describe("Client", () => {
1015
describe("inputs", () => {
@@ -89,6 +94,50 @@ describe("Client", () => {
8994
});
9095
});
9196

97+
describe("response types", () => {
98+
describe("single response", () => {
99+
it("should return the responses type", async () => {
100+
const app = createApp().get(
101+
"/health",
102+
{ responses: z.object({ status: z.string() }) },
103+
() => ({ status: "ok" }),
104+
);
105+
const client = createTestAppClient(app);
106+
107+
const response = await client.fetch("GET", "/health", {});
108+
expectTypeOf(response).toEqualTypeOf<{ status: string }>();
109+
expect(response).toEqual({ status: "ok" });
110+
});
111+
});
112+
113+
describe("multiple responses", () => {
114+
it("should return a union of the 2XX responses", async () => {
115+
const app = createApp().put(
116+
"/users/:id",
117+
{
118+
params: z.object({ id: z.string() }),
119+
responses: {
120+
[200]: z.object({ id: z.string(), name: z.string() }),
121+
[HttpStatus.NoContent]: NoResponse,
122+
[HttpStatus.BadRequest]: ErrorResponse,
123+
},
124+
},
125+
({ status }) => status(HttpStatus.NoContent, undefined),
126+
);
127+
const client = createTestAppClient(app);
128+
129+
const response = await client.fetch("PUT", "/users/:id", {
130+
params: { id: "123" },
131+
});
132+
expectTypeOf(response).toEqualTypeOf<void | {
133+
id: string;
134+
name: string;
135+
}>();
136+
expect(response).toEqual(undefined);
137+
});
138+
});
139+
});
140+
92141
describe("GetClientRoutes", () => {
93142
it("should convert app data with prefix to absolute routes", () => {
94143
type MyApp = App<{

‎src/__tests__/types.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type * as t from "../types";
44
import { z } from "zod/v4";
55
import type { StandardSchemaV1 } from "@standard-schema/spec";
66
import { ErrorResponse } from "../custom-responses";
7+
import type { HttpStatus } from "../status";
78

89
describe("Types", () => {
910
describe("MergeRoutes", () => {
@@ -277,7 +278,7 @@ describe("Types", () => {
277278
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
278279
});
279280

280-
it("should return the input response type", () => {
281+
it("should return the output response type", () => {
281282
type Def = {
282283
responses: z.ZodObject<{ id: z.ZodString }>;
283284
};
@@ -288,6 +289,36 @@ describe("Types", () => {
288289
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
289290
});
290291

292+
it("should return a union of successful output response types when multiple are defined using number literals", () => {
293+
type Def = {
294+
responses: {
295+
200: z.ZodObject<{ id: z.ZodString }>;
296+
204: z.ZodVoid;
297+
404: z.ZodObject<{ error: z.ZodString }>;
298+
};
299+
};
300+
type Expected = { id: string } | void;
301+
302+
type Actual = t.GetResponseOutputFromDef<Def>;
303+
304+
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
305+
});
306+
307+
it("should return a union of successful output response types when multiple are defined using HttpStatus enum", () => {
308+
type Def = {
309+
responses: {
310+
[HttpStatus.Ok]: z.ZodObject<{ id: z.ZodString }>;
311+
[HttpStatus.Accepted]: z.ZodVoid;
312+
[HttpStatus.BadRequest]: z.ZodObject<{ error: z.ZodString }>;
313+
};
314+
};
315+
type Expected = { id: string } | void;
316+
317+
type Actual = t.GetResponseOutputFromDef<Def>;
318+
319+
expectTypeOf<Actual>().toEqualTypeOf<Expected>();
320+
});
321+
291322
it("should return any when using AnyDef", () => {
292323
type Def = t.AnyDef;
293324
type Expected = any;

‎src/open-api.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function buildOpenApiDocs(
1111
): { type: "success"; docs: any } | { type: "error"; error: unknown } {
1212
try {
1313
if (!options?.schemaAdapter)
14-
throw Error("OpenAPI docs require a schema adapter");
14+
return { type: "error", error: "OpenAPI docs require a schema adapter" };
1515
const adapter = options.schemaAdapter;
1616

1717
const userDoc = options.openApi ?? {};

‎src/types.ts‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import type { StandardSchemaV1 } from "@standard-schema/spec";
1010
import type { OpenAPI } from "openapi-types";
1111
import type { IsStatusResult } from "./internal/utils";
12+
import type { HttpStatus } from "./status";
1213

1314
//
1415
// APP
@@ -940,6 +941,28 @@ export type GetRequestParamsOutput<
940941
? GetRequestParamsOutputFromDef<TRoutes[TMethod][TRoute]>
941942
: never;
942943

944+
type SuccessStatusCodes =
945+
| 200
946+
| HttpStatus.Ok
947+
| 201
948+
| HttpStatus.Created
949+
| 202
950+
| HttpStatus.Accepted
951+
| 203
952+
| HttpStatus.NonAuthoritativeInformation
953+
| 204
954+
| HttpStatus.NoContent
955+
| 205
956+
| HttpStatus.ResetContent
957+
| 206
958+
| HttpStatus.PartialContent
959+
| 207
960+
| HttpStatus.MultiStatus
961+
| 208
962+
| HttpStatus.AlreadyReported
963+
| 226
964+
| HttpStatus.ImUsed;
965+
943966
/**
944967
* Given a `RouteDef`, return a union of all possible handler return values.
945968
*
@@ -955,7 +978,12 @@ export type GetResponseOutputFromDef<TRouteDef extends RouteDef> =
955978
? undefined
956979
: TRouteDef["responses"] extends StandardSchemaV1
957980
? StandardSchemaV1.InferOutput<TRouteDef["responses"]>
958-
: never;
981+
: {
982+
[key in SuccessStatusCodes &
983+
keyof TRouteDef["responses"]]: TRouteDef["responses"][key] extends StandardSchemaV1
984+
? StandardSchemaV1.InferOutput<TRouteDef["responses"][key]>
985+
: unknown;
986+
}[SuccessStatusCodes & keyof TRouteDef["responses"]];
959987

960988
/**
961989
* Given a set of routes, a method, and a route, return the output type of the
@@ -967,7 +995,7 @@ export type GetResponseOutput<
967995
TPath extends keyof TRoutes[TMethod],
968996
> = TPath extends BasePath
969997
? GetResponseOutputFromDef<TRoutes[TMethod][TPath]>
970-
: never;
998+
: 1;
971999

9721000
type InputParams = "headers" | "params" | "query" | "body";
9731001

0 commit comments

Comments
 (0)
0