8000 fix: Get meta from `ZetaSchema` when building responses · aklinker1/zeta@c596cd0 · GitHub
[go: up one dir, main page]

Skip to content

Commit c596cd0

Browse files
committed
fix: Get meta from ZetaSchema when building responses
1 parent 9170fe6 commit c596cd0

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

‎src/internal/call-handler.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
validateOutputSchema,
1212
} from "./utils";
1313
import { smartDeserialize, smartSerialize } from "./serialization";
14+
import { getMeta } from "../meta";
1415

1516
export async function callHandler(
1617
ctx: any,
@@ -92,7 +93,7 @@ export async function callHandler(
9293
route.data.def.responses,
9394
ctx.response,
9495
);
95-
responseMeta = schemaAdapter?.getMeta(route.data.def.responses);
96+
responseMeta = getMeta(schemaAdapter, route.data.def.responses);
9697
} else {
9798
if (!ctx.response || !isStatusResult(ctx.response)) {
9899
throw new Error(
@@ -107,7 +108,7 @@ export async function callHandler(
107108
}
108109
ctx.set.status = status;
109110
ctx.response = validateOutputSchema(schema, body);
110-
responseMeta = schemaAdapter?.getMeta(schema);
111+
responseMeta = getMeta(schemaAdapter, schema);
111112
}
112113
}
113114
}

‎src/meta.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { StandardSchemaV1 } from "@standard-schema/spec";
2+
import type { SchemaAdapter } from "./types";
3+
import type { ZetaSchema } from "./custom-responses";
4+
5+
/** Get metadata for either a ZetaSchema or a StandardSchemaV1. */
6+
export function getMeta(
7+
adapter: SchemaAdapter | undefined,
8+
schema: StandardSchemaV1 | ZetaSchema,
9+
): Record<string, any> {
10+
if ("~zeta" in schema) return schema.meta;
11+
12+
if (!adapter) return {};
13+
return adapter.getMeta(schema) ?? {};
14+
}

‎src/open-api.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { CreateAppOptions } from "./app";
44
import type { StandardSchemaV1 } from "@standard-schema/spec";
55
import { getHttpStatusName } from "./status";
66
import { ErrorResponseJsonSchema, type ZetaSchema } from "./custom-responses";
7+
import { getMeta } from "./meta";
78

89
export function buildOpenApiDocs(
910
options: CreateAppOptions<any> | undefined,
@@ -45,7 +46,7 @@ export function buildOpenApiDocs(
4546
requestBody: body
4647
? {
4748
content: {
48-
[adapter.getMeta(body)?.contentType ?? "application/json"]: {
49+
[getMeta(adapter, body)?.contentType ?? "application/json"]: {
4950
schema: adapter.toJsonSchema(body),
5051
},
5152
},
@@ -151,16 +152,18 @@ function buildResponse(
151152
schema: StandardSchemaV1 | ZetaSchema,
152153
adapter: SchemaAdapter,
153154
): NonNullable<OpenAPI.Operation["responses"]>[string] {
155+
const meta = getMeta(adapter, schema);
156+
154157
if ("~zeta" in schema) {
155158
const description =
156-
schema["~zeta"].meta?.responseDescription ??
157-
getHttpStatusName(status) ??
158-
"";
159+
meta?.responseDescription ?? getHttpStatusName(status) ?? "";
160+
159161
if (schema["~zeta"].type === "NoResponse") {
160162
return {
161163
description,
162164
};
163165
}
166+
164167
if (schema["~zeta"].type === "ErrorResponse") {
165168
return {
166169
description,
@@ -175,7 +178,6 @@ function buildResponse(
175178
}
176179
}
177180

178-
const meta = adapter.getMeta(schema);
179181
return {
180182
description: meta?.responseDescription ?? getHttpStatusName(status),
181183
content: {

0 commit comments

Comments
 (0)
0