vls-openapi-generator 1.0.2 → 1.2.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.
@@ -47,65 +47,48 @@ 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'));
53
- existingOpenAPIFile.paths = {};
54
- existingOpenAPIFile.components = {
55
- parameters: {},
56
- schemas: {}
57
- };
58
54
  for (const handler of handlerFiles) {
59
55
  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(() => ({})));
56
+ const { bodySchema, queryParametersSchema, eventResponseParametersSchema, eventResponseModulesSchema, OPENAPI_CONFIG: openAPIConfig } = (await Promise.resolve(`${path.join(SCHEMAS_DIR, fileName + '.js')}`).then(s => __importStar(require(s))).catch(() => ({})));
62
57
  const bodyComponent = (0, zod_openapi_1.generateSchema)(bodySchema ?? zod_1.z.never(), undefined, '3.0');
63
58
  const queryParametersComponent = (0, zod_openapi_1.generateSchema)(queryParametersSchema ?? zod_1.z.never(), undefined, '3.0');
64
59
  const eventResponseComponent = (0, zod_openapi_1.generateSchema)(zod_1.z.object({
65
- ...(eventResponseParametersSchema ? { params: eventResponseParametersSchema } : {}),
66
- ...(eventResponseModulesSchema ? { modules: eventResponseModulesSchema } : {})
60
+ ...(eventResponseParametersSchema
61
+ ? { params: eventResponseParametersSchema }
62
+ : { params: zod_1.z.object({}) }),
63
+ ...(eventResponseModulesSchema
64
+ ? { modules: eventResponseModulesSchema }
65
+ : { modules: zod_1.z.array(zod_1.z.unknown()) }),
66
+ fallback: zod_1.z.boolean().default(false)
67
67
  }), undefined, '3.0');
68
- if (bodySchema) {
69
- existingOpenAPIFile.components.schemas[`${fileName}-request-body`] = bodyComponent;
70
- }
71
- const queryParameterKeys = [];
68
+ const queryParameters = [];
72
69
  if (queryParametersSchema) {
73
70
  for (const key in queryParametersComponent.properties) {
74
71
  const properties = queryParametersComponent?.properties;
75
72
  if (!properties || !properties[key]) {
76
73
  continue;
77
74
  }
78
- const queryParameterKey = `${fileName}-${key}-query-parameter`;
79
- queryParameterKeys.push(queryParameterKey);
80
- existingOpenAPIFile.components.parameters[queryParameterKey] = {
75
+ queryParameters.push({
81
76
  name: key,
82
77
  in: 'query',
83
- required: true,
84
78
  schema: properties[key]
85
- };
79
+ });
86
80
  }
87
81
  }
88
- if (eventResponseParametersSchema || eventResponseModulesSchema) {
89
- existingOpenAPIFile.components.schemas[`${fileName}-response`] = eventResponseComponent;
90
- }
91
82
  existingOpenAPIFile.paths['/' + fileName] = {
92
83
  post: {
93
84
  tags: openAPIConfig.tags,
94
- parameters: queryParametersSchema
95
- ? queryParameterKeys.map((x) => {
96
- return {
97
- $ref: `#/components/parameters/${x}`
98
- };
99
- })
100
- : undefined,
85
+ parameters: queryParametersSchema ? queryParameters : undefined,
101
86
  requestBody: bodySchema
102
87
  ? {
103
88
  required: true,
104
89
  content: {
105
90
  'application/json': {
106
- schema: {
107
- $ref: `#/components/schemas/${fileName}-request-body`
108
- }
91
+ schema: bodyComponent
109
92
  }
110
93
  }
111
94
  }
@@ -113,15 +96,11 @@ const generateTemplate = async () => {
113
96
  responses: {
114
97
  '200': {
115
98
  description: `Successful response.`,
116
- content: eventResponseParametersSchema || eventResponseModulesSchema
117
- ? {
118
- 'application/json': {
119
- schema: {
120
- $ref: `#/components/schemas/${fileName}-response`
121
- }
122
- }
99
+ content: {
100
+ 'application/json': {
101
+ schema: eventResponseComponent
123
102
  }
124
- : undefined
103
+ }
125
104
  }
126
105
  }
127
106
  }
@@ -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
- $ref: string;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vls-openapi-generator",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "main": "dist/generate-template.js",
5
5
  "types": "dist/lambda-type.d.ts",
6
6
  "scripts": {
@@ -18,31 +18,26 @@ 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);
24
25
  const existingOpenAPIFile = JSON.parse(await fs.readFile(OPENAPI_FILE, 'utf-8')) as OpenAPI;
25
26
 
26
- existingOpenAPIFile.paths = {};
27
- existingOpenAPIFile.components = {
28
- parameters: {},
29
- schemas: {}
30
- };
31
-
32
27
  for (const handler of handlerFiles) {
33
28
  const fileName = path.parse(handler).name;
34
29
 
35
- const { bodySchema, queryParametersSchema, eventResponseParametersSchema, eventResponseModulesSchema } =
36
- (await import(path.join(SCHEMAS_DIR, fileName + '.js')).catch(() => ({}))) as {
37
- bodySchema?: OpenApiZodAny;
38
- eventResponseParametersSchema?: OpenApiZodAny;
39
- eventResponseModulesSchema?: OpenApiZodAny;
40
- queryParametersSchema?: OpenApiZodAny;
41
- };
42
-
43
- const { OPENAPI_CONFIG: openAPIConfig } = (await import(path.join(HANDLERS_DIR, fileName + '.js')).catch(
44
- () => ({})
45
- )) as {
30
+ const {
31
+ bodySchema,
32
+ queryParametersSchema,
33
+ eventResponseParametersSchema,
34
+ eventResponseModulesSchema,
35
+ OPENAPI_CONFIG: openAPIConfig
36
+ } = (await import(path.join(SCHEMAS_DIR, fileName + '.js')).catch(() => ({}))) as {
37
+ bodySchema?: OpenApiZodAny;
38
+ eventResponseParametersSchema?: OpenApiZodAny;
39
+ eventResponseModulesSchema?: OpenApiZodAny;
40
+ queryParametersSchema?: OpenApiZodAny;
46
41
  OPENAPI_CONFIG: OpenAPIConfig;
47
42
  };
48
43
 
@@ -50,18 +45,19 @@ const generateTemplate = async (): Promise<void> => {
50
45
  const queryParametersComponent = generateSchema(queryParametersSchema ?? z.never(), undefined, '3.0');
51
46
  const eventResponseComponent = generateSchema(
52
47
  z.object({
53
- ...(eventResponseParametersSchema ? { params: eventResponseParametersSchema } : {}),
54
- ...(eventResponseModulesSchema ? { modules: eventResponseModulesSchema } : {})
48
+ ...(eventResponseParametersSchema
49
+ ? { params: eventResponseParametersSchema }
50
+ : { params: z.object({}) }),
51
+ ...(eventResponseModulesSchema
52
+ ? { modules: eventResponseModulesSchema }
53
+ : { modules: z.array(z.unknown()) }),
54
+ fallback: z.boolean().default(false)
55
55
  }),
56
56
  undefined,
57
57
  '3.0'
58
58
  );
59
59
 
60
- if (bodySchema) {
61
- existingOpenAPIFile.components.schemas[`${fileName}-request-body`] = bodyComponent;
62
- }
63
-
64
- const queryParameterKeys = [];
60
+ const queryParameters = [];
65
61
 
66
62
  if (queryParametersSchema) {
67
63
  for (const key in queryParametersComponent.properties) {
@@ -71,41 +67,24 @@ const generateTemplate = async (): Promise<void> => {
71
67
  continue;
72
68
  }
73
69
 
74
- const queryParameterKey = `${fileName}-${key}-query-parameter`;
75
-
76
- queryParameterKeys.push(queryParameterKey);
77
-
78
- existingOpenAPIFile.components.parameters[queryParameterKey] = {
70
+ queryParameters.push({
79
71
  name: key,
80
72
  in: 'query',
81
- required: true,
82
- schema: properties[key]
83
- };
73
+ schema: properties[key] as ReturnType<typeof generateSchema>
74
+ } as const);
84
75
  }
85
76
  }
86
77
 
87
- if (eventResponseParametersSchema || eventResponseModulesSchema) {
88
- existingOpenAPIFile.components.schemas[`${fileName}-response`] = eventResponseComponent;
89
- }
90
-
91
78
  existingOpenAPIFile.paths['/' + fileName] = {
92
79
  post: {
93
80
  tags: openAPIConfig.tags,
94
- parameters: queryParametersSchema
95
- ? queryParameterKeys.map((x) => {
96
- return {
97
- $ref: `#/components/parameters/${x}`
98
- };
99
- })
100
- : undefined,
81
+ parameters: queryParametersSchema ? queryParameters : undefined,
101
82
  requestBody: bodySchema
102
83
  ? {
103
84
  required: true,
104
85
  content: {
105
86
  'application/json': {
106
- schema: {
107
- $ref: `#/components/schemas/${fileName}-request-body`
108
- }
87
+ schema: bodyComponent
109
88
  }
110
89
  }
111
90
  }
@@ -113,16 +92,11 @@ const generateTemplate = async (): Promise<void> => {
113
92
  responses: {
114
93
  '200': {
115
94
  description: `Successful response.`,
116
- content:
117
- eventResponseParametersSchema || eventResponseModulesSchema
118
- ? {
119
- 'application/json': {
120
- schema: {
121
- $ref: `#/components/schemas/${fileName}-response`
122
- }
123
- }
124
- }
125
- : undefined
95
+ content: {
96
+ 'application/json': {
97
+ schema: eventResponseComponent
98
+ }
99
+ }
126
100
  }
127
101
  }
128
102
  }
@@ -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: { $ref: string }[] | undefined;
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;