8000 feat!: Allow customizing the response content type on OpenAPI docs · aklinker1/zeta@2721e7f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2721e7f

Browse files
committed
feat!: Allow customizing the response content type on OpenAPI docs
1 parent 471232d commit 2721e7f

5 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ const app = createApp().get("/path", {}, ({ set }) => {
277277

278278
> As of right now, you can't add custom status codes to the OpenAPI docs or as apart of the response schema. The OpenAPI spec will always show `200 OK` as the response.
279279
280+
By default, Zeta will show `application/json` as the content type in the OpenAPI docs. you can override this by setting the `contentType` metadata on your schema:
281+
282+
```ts
283+
app.get("/csv", {
284+
response: z.string().meta({ contentType: "text/csv" })
285+
}, () => "...")
286+
```
287+
280288
## Life Cycle Hooks
281289

282290
You can use any of the following APIs to hook into the request lifecycle:

‎example.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ const app = createApp({
9595

9696
return entry;
9797
},
98+
)
99+
100+
.get(
101+
"/api/csv",
102+
{
103+
response: z.string().meta({ contentType: "text/csv" }),
104+
},
105+
() => "test",
98106
);
99107

100108
console.log(

‎src/adapters/zod-schema-adapter.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ export const zodSchemaAdapter: SchemaAdapter = {
4040
}),
4141
);
4242
},
43+
getResponseContentType: (schema: any) => {
44+
const meta = z.globalRegistry.get(schema);
45+
return meta?.contentType ?? meta?.["content-type"];
46+
},
4347
};

‎src/open-api.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ export function buildOpenApiDocs(
6262
requestBody: body
6363
? {
6464
content: {
65-
"application/json": {
66-
schema: adapter.toJsonSchema(body),
67-
},
65+
[adapter.getResponseContentType(body) ?? "application/json"]:
66+
{
67+
schema: adapter.toJsonSchema(body),
68+
},
6869
},
6970
}
7071
: undefined,
@@ -78,7 +79,8 @@ export function buildOpenApiDocs(
7879
200: {
7980
description: "OK",
8081
content: {
81-
"application/json": {
82+
[adapter.getResponseContentType(response) ??
83+
"application/json"]: {
8284
schema: adapter.toJsonSchema(response),
8385
},
8486
},

‎src/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ export interface SchemaAdapter {
956956
schema: StandardSchemaV1;
957957
description?: string;
958958
}>;
959+
getResponseContentType: (schema: StandardSchemaV1) => string | undefined;
959960
}
960961

961962
//

0 commit comments

Comments
 (0)
0