-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenApiService.ts
More file actions
151 lines (141 loc) · 3.73 KB
/
Copy pathopenApiService.ts
File metadata and controls
151 lines (141 loc) · 3.73 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
export * as OpenApiService from "./openApiService";
import { OpenAPIRegistry } from "@asteasolutions/zod-to-openapi";
import { UpdateCustomer } from "./updateCustomer";
import { UpdateCustomerParams } from "./updateCustomerParams";
import { Customer } from "../types/customer";
import { OpenApiGeneratorV31 } from "@asteasolutions/zod-to-openapi";
import { z } from "zod";
import { UpdateCustomerQueryParams } from "./updateCustomerQueryParams";
import { ValidationErrorsHttp } from "./validationErrorsHttp";
import { ValidationErrorsRest } from "./validationErrorsRest";
export function createOpenApiRegistry(apiGatewayType: "http" | "rest") {
const registry = new OpenAPIRegistry();
const UpdateCustomerRequestSchema = registry.register(
"UpdateCustomerRequest",
UpdateCustomer
);
const UpdateCustomerParamsSchema = registry.register(
"UpdateCustomerParams",
UpdateCustomerParams
);
const UpdateCustomerQueryParamesSchema = registry.register(
"UpdateCustomerQueryParams",
UpdateCustomerQueryParams
);
const UpdateCustomerResponseSchema = registry.register(
"UpdateCustomerResponse",
Customer
);
let ValidationErrorsSchema: z.ZodType;
if (apiGatewayType === "http") {
ValidationErrorsSchema = registry.register(
"ValidationErrors",
ValidationErrorsHttp
);
} else {
ValidationErrorsSchema = registry.register(
"ValidationErrors",
ValidationErrorsRest
);
}
registry.registerPath({
method: "put",
path: "/customer/{id}",
summary: "PUT /customer/{id}",
description: "Update customer",
request: {
params: UpdateCustomerParamsSchema,
query: UpdateCustomerQueryParamesSchema,
body: {
content: {
"application/json": {
schema: UpdateCustomerRequestSchema,
},
},
},
},
responses: {
200: {
description: "Customer updated",
content: {
"application/json": {
schema: UpdateCustomerResponseSchema,
},
},
},
400: {
description: "Validation errors",
content: {
"application/json": {
schema: ValidationErrorsSchema,
},
},
},
},
});
registry.registerPath({
method: "get",
path: "/openapi.yaml",
summary: "GET /openapi.yaml",
description: "OpenAPI specification in YAML format",
responses: {
200: {
description: "OpenAPI specification",
content: {
"text/yaml": {
schema: z.string(),
},
},
},
},
});
registry.registerPath({
method: "get",
path: "/openapi.json",
summary: "GET /openapi.json",
description: "OpenAPI specification in JSON format",
responses: {
200: {
description: "OpenAPI specification",
content: {
"application/json": {
schema: z.string(),
},
},
},
},
});
registry.registerPath({
method: "get",
path: "/docs",
summary: "GET /docs",
description: "UI for OpenAPI schema",
responses: {
200: {
description: "UI for OpenAPI schema",
content: {
"application/json": {
schema: z.string(),
},
},
},
},
});
return registry;
}
export function createOpenApiSchema(
servers: { url: string; description: string }[],
apiGatewayType: "http" | "rest"
) {
const openApiRegistry = createOpenApiRegistry(apiGatewayType);
const generator = new OpenApiGeneratorV31(openApiRegistry.definitions);
const openApiDocs = generator.generateDocument({
openapi: "3.0.3",
info: {
version: "1.0.0",
title: "Customer API",
},
servers: servers,
});
return openApiDocs;
}