vls-openapi-generator 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/generate-openapi.js +18 -34
- package/dist/openapi-type.d.ts +6 -7
- package/package.json +1 -1
- package/src/generate-openapi.ts +30 -50
- package/src/openapi-type.ts +11 -7
package/dist/generate-openapi.js
CHANGED
|
@@ -47,6 +47,7 @@ const SCHEMAS_DIR = path.join(process.cwd(), 'dist', 'src', 'schemas');
|
|
|
47
47
|
const OUTPUT_FILE = path.join(process.cwd(), 'openapi.json');
|
|
48
48
|
const generateTemplate = async () => {
|
|
49
49
|
console.info('Generating Open API documentation...');
|
|
50
|
+
await (0, util_1.promisify)(child_process_1.exec)('rm -rf ./dist');
|
|
50
51
|
await (0, util_1.promisify)(child_process_1.exec)('npx tsc');
|
|
51
52
|
const handlerFiles = await fs_1.promises.readdir(HANDLERS_DIR);
|
|
52
53
|
const existingOpenAPIFile = JSON.parse(await fs_1.promises.readFile(OPENAPI_FILE, 'utf-8'));
|
|
@@ -57,55 +58,42 @@ const generateTemplate = async () => {
|
|
|
57
58
|
};
|
|
58
59
|
for (const handler of handlerFiles) {
|
|
59
60
|
const fileName = path.parse(handler).name;
|
|
60
|
-
const { bodySchema, queryParametersSchema, eventResponseParametersSchema, eventResponseModulesSchema } = (await Promise.resolve(`${path.join(SCHEMAS_DIR, fileName + '.js')}`).then(s => __importStar(require(s))).catch(() => ({})));
|
|
61
|
-
const { OPENAPI_CONFIG: openAPIConfig } = (await Promise.resolve(`${path.join(HANDLERS_DIR, fileName + '.js')}`).then(s => __importStar(require(s))).catch(() => ({})));
|
|
61
|
+
const { bodySchema, queryParametersSchema, eventResponseParametersSchema, eventResponseModulesSchema, OPENAPI_CONFIG: openAPIConfig } = (await Promise.resolve(`${path.join(SCHEMAS_DIR, fileName + '.js')}`).then(s => __importStar(require(s))).catch(() => ({})));
|
|
62
62
|
const bodyComponent = (0, zod_openapi_1.generateSchema)(bodySchema ?? zod_1.z.never(), undefined, '3.0');
|
|
63
63
|
const queryParametersComponent = (0, zod_openapi_1.generateSchema)(queryParametersSchema ?? zod_1.z.never(), undefined, '3.0');
|
|
64
64
|
const eventResponseComponent = (0, zod_openapi_1.generateSchema)(zod_1.z.object({
|
|
65
|
-
...(eventResponseParametersSchema
|
|
66
|
-
|
|
65
|
+
...(eventResponseParametersSchema
|
|
66
|
+
? { params: eventResponseParametersSchema }
|
|
67
|
+
: { params: zod_1.z.object({}) }),
|
|
68
|
+
...(eventResponseModulesSchema
|
|
69
|
+
? { modules: eventResponseModulesSchema }
|
|
70
|
+
: { modules: zod_1.z.array(zod_1.z.unknown()) }),
|
|
71
|
+
fallback: zod_1.z.boolean().default(false)
|
|
67
72
|
}), undefined, '3.0');
|
|
68
|
-
|
|
69
|
-
existingOpenAPIFile.components.schemas[`${fileName}-request-body`] = bodyComponent;
|
|
70
|
-
}
|
|
71
|
-
const queryParameterKeys = [];
|
|
73
|
+
const queryParameters = [];
|
|
72
74
|
if (queryParametersSchema) {
|
|
73
75
|
for (const key in queryParametersComponent.properties) {
|
|
74
76
|
const properties = queryParametersComponent?.properties;
|
|
75
77
|
if (!properties || !properties[key]) {
|
|
76
78
|
continue;
|
|
77
79
|
}
|
|
78
|
-
|
|
79
|
-
queryParameterKeys.push(queryParameterKey);
|
|
80
|
-
existingOpenAPIFile.components.parameters[queryParameterKey] = {
|
|
80
|
+
queryParameters.push({
|
|
81
81
|
name: key,
|
|
82
82
|
in: 'query',
|
|
83
|
-
required: true,
|
|
84
83
|
schema: properties[key]
|
|
85
|
-
};
|
|
84
|
+
});
|
|
86
85
|
}
|
|
87
86
|
}
|
|
88
|
-
if (eventResponseParametersSchema || eventResponseModulesSchema) {
|
|
89
|
-
existingOpenAPIFile.components.schemas[`${fileName}-response`] = eventResponseComponent;
|
|
90
|
-
}
|
|
91
87
|
existingOpenAPIFile.paths['/' + fileName] = {
|
|
92
88
|
post: {
|
|
93
89
|
tags: openAPIConfig.tags,
|
|
94
|
-
parameters: queryParametersSchema
|
|
95
|
-
? queryParameterKeys.map((x) => {
|
|
96
|
-
return {
|
|
97
|
-
$ref: `#/components/parameters/${x}`
|
|
98
|
-
};
|
|
99
|
-
})
|
|
100
|
-
: undefined,
|
|
90
|
+
parameters: queryParametersSchema ? queryParameters : undefined,
|
|
101
91
|
requestBody: bodySchema
|
|
102
92
|
? {
|
|
103
93
|
required: true,
|
|
104
94
|
content: {
|
|
105
95
|
'application/json': {
|
|
106
|
-
schema:
|
|
107
|
-
$ref: `#/components/schemas/${fileName}-request-body`
|
|
108
|
-
}
|
|
96
|
+
schema: bodyComponent
|
|
109
97
|
}
|
|
110
98
|
}
|
|
111
99
|
}
|
|
@@ -113,15 +101,11 @@ const generateTemplate = async () => {
|
|
|
113
101
|
responses: {
|
|
114
102
|
'200': {
|
|
115
103
|
description: `Successful response.`,
|
|
116
|
-
content:
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
schema: {
|
|
120
|
-
$ref: `#/components/schemas/${fileName}-response`
|
|
121
|
-
}
|
|
122
|
-
}
|
|
104
|
+
content: {
|
|
105
|
+
'application/json': {
|
|
106
|
+
schema: eventResponseComponent
|
|
123
107
|
}
|
|
124
|
-
|
|
108
|
+
}
|
|
125
109
|
}
|
|
126
110
|
}
|
|
127
111
|
}
|
package/dist/openapi-type.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { generateSchema } from '@anatine/zod-openapi';
|
|
1
2
|
export type OpenAPI = {
|
|
2
3
|
openapi: '3.0.0';
|
|
3
4
|
info: {
|
|
@@ -18,15 +19,15 @@ export type OpenAPI = {
|
|
|
18
19
|
paths: Record<string, {
|
|
19
20
|
post: {
|
|
20
21
|
parameters: {
|
|
21
|
-
|
|
22
|
+
name: string;
|
|
23
|
+
in: 'query';
|
|
24
|
+
schema: ReturnType<typeof generateSchema>;
|
|
22
25
|
}[] | undefined;
|
|
23
26
|
requestBody: {
|
|
24
27
|
required: true;
|
|
25
28
|
content: {
|
|
26
29
|
'application/json': {
|
|
27
|
-
schema:
|
|
28
|
-
$ref: `#/components/schemas/${string}-request-body`;
|
|
29
|
-
};
|
|
30
|
+
schema: ReturnType<typeof generateSchema>;
|
|
30
31
|
};
|
|
31
32
|
};
|
|
32
33
|
} | undefined;
|
|
@@ -35,9 +36,7 @@ export type OpenAPI = {
|
|
|
35
36
|
description: string;
|
|
36
37
|
content: {
|
|
37
38
|
'application/json': {
|
|
38
|
-
schema:
|
|
39
|
-
$ref: `#/components/schemas/${string}-response`;
|
|
40
|
-
};
|
|
39
|
+
schema: ReturnType<typeof generateSchema>;
|
|
41
40
|
};
|
|
42
41
|
} | undefined;
|
|
43
42
|
};
|
package/package.json
CHANGED
package/src/generate-openapi.ts
CHANGED
|
@@ -18,6 +18,7 @@ const OUTPUT_FILE = path.join(process.cwd(), 'openapi.json');
|
|
|
18
18
|
const generateTemplate = async (): Promise<void> => {
|
|
19
19
|
console.info('Generating Open API documentation...');
|
|
20
20
|
|
|
21
|
+
await promisify(exec)('rm -rf ./dist');
|
|
21
22
|
await promisify(exec)('npx tsc');
|
|
22
23
|
|
|
23
24
|
const handlerFiles = await fs.readdir(HANDLERS_DIR);
|
|
@@ -32,17 +33,17 @@ const generateTemplate = async (): Promise<void> => {
|
|
|
32
33
|
for (const handler of handlerFiles) {
|
|
33
34
|
const fileName = path.parse(handler).name;
|
|
34
35
|
|
|
35
|
-
const {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
const {
|
|
37
|
+
bodySchema,
|
|
38
|
+
queryParametersSchema,
|
|
39
|
+
eventResponseParametersSchema,
|
|
40
|
+
eventResponseModulesSchema,
|
|
41
|
+
OPENAPI_CONFIG: openAPIConfig
|
|
42
|
+
} = (await import(path.join(SCHEMAS_DIR, fileName + '.js')).catch(() => ({}))) as {
|
|
43
|
+
bodySchema?: OpenApiZodAny;
|
|
44
|
+
eventResponseParametersSchema?: OpenApiZodAny;
|
|
45
|
+
eventResponseModulesSchema?: OpenApiZodAny;
|
|
46
|
+
queryParametersSchema?: OpenApiZodAny;
|
|
46
47
|
OPENAPI_CONFIG: OpenAPIConfig;
|
|
47
48
|
};
|
|
48
49
|
|
|
@@ -50,18 +51,19 @@ const generateTemplate = async (): Promise<void> => {
|
|
|
50
51
|
const queryParametersComponent = generateSchema(queryParametersSchema ?? z.never(), undefined, '3.0');
|
|
51
52
|
const eventResponseComponent = generateSchema(
|
|
52
53
|
z.object({
|
|
53
|
-
...(eventResponseParametersSchema
|
|
54
|
-
|
|
54
|
+
...(eventResponseParametersSchema
|
|
55
|
+
? { params: eventResponseParametersSchema }
|
|
56
|
+
: { params: z.object({}) }),
|
|
57
|
+
...(eventResponseModulesSchema
|
|
58
|
+
? { modules: eventResponseModulesSchema }
|
|
59
|
+
: { modules: z.array(z.unknown()) }),
|
|
60
|
+
fallback: z.boolean().default(false)
|
|
55
61
|
}),
|
|
56
62
|
undefined,
|
|
57
63
|
'3.0'
|
|
58
64
|
);
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
existingOpenAPIFile.components.schemas[`${fileName}-request-body`] = bodyComponent;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const queryParameterKeys = [];
|
|
66
|
+
const queryParameters = [];
|
|
65
67
|
|
|
66
68
|
if (queryParametersSchema) {
|
|
67
69
|
for (const key in queryParametersComponent.properties) {
|
|
@@ -71,41 +73,24 @@ const generateTemplate = async (): Promise<void> => {
|
|
|
71
73
|
continue;
|
|
72
74
|
}
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
queryParameterKeys.push(queryParameterKey);
|
|
77
|
-
|
|
78
|
-
existingOpenAPIFile.components.parameters[queryParameterKey] = {
|
|
76
|
+
queryParameters.push({
|
|
79
77
|
name: key,
|
|
80
78
|
in: 'query',
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
};
|
|
79
|
+
schema: properties[key] as ReturnType<typeof generateSchema>
|
|
80
|
+
} as const);
|
|
84
81
|
}
|
|
85
82
|
}
|
|
86
83
|
|
|
87
|
-
if (eventResponseParametersSchema || eventResponseModulesSchema) {
|
|
88
|
-
existingOpenAPIFile.components.schemas[`${fileName}-response`] = eventResponseComponent;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
84
|
existingOpenAPIFile.paths['/' + fileName] = {
|
|
92
85
|
post: {
|
|
93
86
|
tags: openAPIConfig.tags,
|
|
94
|
-
parameters: queryParametersSchema
|
|
95
|
-
? queryParameterKeys.map((x) => {
|
|
96
|
-
return {
|
|
97
|
-
$ref: `#/components/parameters/${x}`
|
|
98
|
-
};
|
|
99
|
-
})
|
|
100
|
-
: undefined,
|
|
87
|
+
parameters: queryParametersSchema ? queryParameters : undefined,
|
|
101
88
|
requestBody: bodySchema
|
|
102
89
|
? {
|
|
103
90
|
required: true,
|
|
104
91
|
content: {
|
|
105
92
|
'application/json': {
|
|
106
|
-
schema:
|
|
107
|
-
$ref: `#/components/schemas/${fileName}-request-body`
|
|
108
|
-
}
|
|
93
|
+
schema: bodyComponent
|
|
109
94
|
}
|
|
110
95
|
}
|
|
111
96
|
}
|
|
@@ -113,16 +98,11 @@ const generateTemplate = async (): Promise<void> => {
|
|
|
113
98
|
responses: {
|
|
114
99
|
'200': {
|
|
115
100
|
description: `Successful response.`,
|
|
116
|
-
content:
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
$ref: `#/components/schemas/${fileName}-response`
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
: undefined
|
|
101
|
+
content: {
|
|
102
|
+
'application/json': {
|
|
103
|
+
schema: eventResponseComponent
|
|
104
|
+
}
|
|
105
|
+
}
|
|
126
106
|
}
|
|
127
107
|
}
|
|
128
108
|
}
|
package/src/openapi-type.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { generateSchema } from '@anatine/zod-openapi';
|
|
2
|
+
|
|
1
3
|
export type OpenAPI = {
|
|
2
4
|
openapi: '3.0.0';
|
|
3
5
|
info: { title: string; version: string };
|
|
@@ -15,15 +17,19 @@ export type OpenAPI = {
|
|
|
15
17
|
string,
|
|
16
18
|
{
|
|
17
19
|
post: {
|
|
18
|
-
parameters:
|
|
20
|
+
parameters:
|
|
21
|
+
| {
|
|
22
|
+
name: string;
|
|
23
|
+
in: 'query';
|
|
24
|
+
schema: ReturnType<typeof generateSchema>;
|
|
25
|
+
}[]
|
|
26
|
+
| undefined;
|
|
19
27
|
requestBody:
|
|
20
28
|
| {
|
|
21
29
|
required: true;
|
|
22
30
|
content: {
|
|
23
31
|
'application/json': {
|
|
24
|
-
schema:
|
|
25
|
-
$ref: `#/components/schemas/${string}-request-body`;
|
|
26
|
-
};
|
|
32
|
+
schema: ReturnType<typeof generateSchema>;
|
|
27
33
|
};
|
|
28
34
|
};
|
|
29
35
|
}
|
|
@@ -34,9 +40,7 @@ export type OpenAPI = {
|
|
|
34
40
|
content:
|
|
35
41
|
| {
|
|
36
42
|
'application/json': {
|
|
37
|
-
schema:
|
|
38
|
-
$ref: `#/components/schemas/${string}-response`;
|
|
39
|
-
};
|
|
43
|
+
schema: ReturnType<typeof generateSchema>;
|
|
40
44
|
};
|
|
41
45
|
}
|
|
42
46
|
| undefined;
|