-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
createSchema.ts
101 lines (93 loc) · 1.95 KB
/
createSchema.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
93
94
95
96
97
98
99
100
101
import fs from 'fs';
import path from 'path';
import { stringify } from 'yaml';
import { type ZodOpenApiOperationObject, createDocument } from '../../src';
import {
CreateJobRequestSchema,
CreateJobResponseSchema,
GetJobQuerySchema,
GetJobResponseSchema,
} from './types';
const getJobOperation: ZodOpenApiOperationObject = {
operationId: 'getJob',
summary: 'Get Job',
requestParams: {
query: GetJobQuerySchema,
},
responses: {
'200': {
description: 'Successful operation',
content: {
'application/json': {
schema: GetJobResponseSchema,
},
},
},
},
};
const createJobOperation: ZodOpenApiOperationObject = {
operationId: 'createJob',
summary: 'Create Job',
requestBody: {
content: {
'application/json': {
schema: CreateJobRequestSchema,
},
},
},
responses: {
'201': {
description: 'Successful creation',
content: {
'application/json': {
schema: CreateJobResponseSchema,
},
},
},
},
};
const document = createDocument({
openapi: '3.1.0',
info: {
title: 'Simple API',
version: '1.0.0',
description: 'A simple demo for zod-openapi',
license: {
name: 'MIT',
},
},
components: {
securitySchemes: {
s2sauth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'An s2s token issued by an allow listed consumer',
},
},
},
servers: [
{
url: 'http://example.com/dev',
description: 'Dev Endpoint',
},
{
url: 'http://example.com/prod',
description: 'Prod Endpoint',
},
],
security: [
{
s2sauth: [],
},
],
paths: {
'/job': {
get: getJobOperation,
post: createJobOperation,
},
},
});
const yaml = stringify(document, { aliasDuplicateObjects: false });
// eslint-disable-next-line no-sync
fs.writeFileSync(path.join(__dirname, 'openapi.yml'), yaml);