-
Notifications
You must be signed in to change notification settings - Fork 7
/
router_body.test.ts
92 lines (86 loc) · 1.84 KB
/
router_body.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import * as z from "../index";
import { openApi } from "../openapi";
test("router with body", async () => {
const pet = z.reference(
"Pets",
z.object({
id: z.integer("int64"),
name: z.string(),
tag: z.string().optional(),
})
);
const schema = z.endpoints([
z.endpoint({
name: "C",
method: "POST",
path: [z.literal("pets")],
body: z.body({
type: "application/json",
content: pet,
}),
responses: [
z.response({
status: 201,
headers: {},
description: "Post with body",
}),
],
}),
]);
const api: z.Api<typeof schema> = {
C: () =>
Promise.resolve({
status: 201,
headers: {},
}),
};
const res = await api["C"]({
method: "POST",
path: ["pets"],
query: {},
headers: {},
body: { type: "application/json", content: { id: 1, name: "Joe" } },
});
expect(res).toEqual({
status: 201,
headers: {},
});
const open = openApi(schema);
expect(open).toEqual({
components: undefined,
info: {
title: "No title",
version: "1.0.0",
},
openapi: "3.0.0",
paths: {
"/pets": {
post: {
operationId: "C",
parameters: undefined,
requestBody: {
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/Pets",
},
},
},
},
responses: {
201: {
content: undefined,
description: "Post with body",
headers: undefined,
},
},
summary: undefined,
tags: undefined,
},
},
},
servers: [],
});
});