@moostjs/swagger 0.3.39 → 0.3.40

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/index.cjs CHANGED
@@ -4,7 +4,7 @@ var moost = require('moost');
4
4
  var eventHttp = require('@moostjs/event-http');
5
5
  var zod = require('@moostjs/zod');
6
6
  var httpStatic = require('@wooksjs/http-static');
7
- var path = require('path');
7
+ var Path = require('path');
8
8
  var swaggerUiDist = require('swagger-ui-dist');
9
9
  var zodParser = require('zod-parser');
10
10
 
@@ -44,6 +44,9 @@ function SwaggerRequestBody(opt) {
44
44
  return meta;
45
45
  });
46
46
  }
47
+ function SwaggerParam(opts) {
48
+ return getSwaggerMate().decorate('swaggerParams', opts, true);
49
+ }
47
50
 
48
51
  /******************************************************************************
49
52
  Copyright (c) Microsoft Corporation.
@@ -119,22 +122,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
119
122
  for (const [code, responseConfigs] of Object.entries(hmeta.swaggerResponses)) {
120
123
  const newCode = code === '0' ? getDefaultStatusCode(handlerMethod) : code;
121
124
  for (const [contentType, type] of Object.entries(responseConfigs)) {
122
- let schema;
123
- let zt;
124
- if (type instanceof zod.z.ZodType) {
125
- zt = type;
126
- }
127
- else if (typeof type === 'function') {
128
- zt = zod.getZodType({
129
- type,
130
- });
131
- }
132
- if (zt) {
133
- const parsed = myParseZod(zt);
134
- if (['ZodString', 'ZodNumber', 'ZodObject', 'ZodArray', 'ZodBoolean'].includes(parsed.$type)) {
135
- schema = getSwaggerSchema(parsed);
136
- }
137
- }
125
+ const schema = getSwaggerSchemaFromSwaggerConfigType(type);
138
126
  if (schema) {
139
127
  responses = responses || {};
140
128
  responses[newCode] = { content: { [contentType]: { schema } } };
@@ -194,6 +182,15 @@ function mapToSwaggerSpec(metadata, options, logger) {
194
182
  responses,
195
183
  };
196
184
  const endpointSpec = swaggerSpec.paths[handlerPath][handlerMethod];
185
+ for (const param of hmeta?.swaggerParams || []) {
186
+ endpointSpec.parameters.push({
187
+ name: param.name,
188
+ in: param.in,
189
+ description: param.description,
190
+ required: !!param.required,
191
+ schema: getSwaggerSchemaFromSwaggerConfigType(param.type) || { type: 'string' },
192
+ });
193
+ }
197
194
  for (const paramName of handler.registeredAs[0].args) {
198
195
  const paramIndex = handler.meta.params.findIndex(param => param.paramSource === 'ROUTE' && param.paramName === paramName);
199
196
  const paramMeta = handler.meta.params[paramIndex];
@@ -232,7 +229,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
232
229
  }, undefined, logger);
233
230
  const parsed = myParseZod(zodType);
234
231
  const schema = getSwaggerSchema(parsed, true);
235
- if (paramMeta.paramSource == 'QUERY_ITEM') {
232
+ if (paramMeta.paramSource === 'QUERY_ITEM') {
236
233
  endpointSpec.parameters.push({
237
234
  name: paramMeta.paramName || '',
238
235
  in: 'query',
@@ -241,7 +238,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
241
238
  schema: schema || { type: 'string' },
242
239
  });
243
240
  }
244
- else if (paramMeta.paramSource == 'QUERY' && parsed.$type === 'ZodObject') {
241
+ else if (paramMeta.paramSource === 'QUERY' && parsed.$type === 'ZodObject') {
245
242
  for (const [key, value] of Object.entries(parsed.$inner)) {
246
243
  const schema = getSwaggerSchema(value, true);
247
244
  if (schema) {
@@ -533,6 +530,28 @@ function getDefaultStatusCode(httpMethod) {
533
530
  };
534
531
  return defaultStatusCodes[httpMethod.toUpperCase()] || 200;
535
532
  }
533
+ function getSwaggerSchemaFromSwaggerConfigType(type) {
534
+ let schema;
535
+ let zt;
536
+ if (type instanceof zod.z.ZodType) {
537
+ zt = type;
538
+ }
539
+ else if (typeof type === 'function') {
540
+ zt = zod.getZodType({
541
+ type,
542
+ });
543
+ }
544
+ if (zt) {
545
+ const parsed = myParseZod(zt);
546
+ if (['ZodString', 'ZodNumber', 'ZodObject', 'ZodArray', 'ZodBoolean'].includes(parsed.$type)) {
547
+ schema = getSwaggerSchema(parsed);
548
+ }
549
+ }
550
+ else if (type.type || type.$ref) {
551
+ schema = type;
552
+ }
553
+ return schema;
554
+ }
536
555
 
537
556
  exports.SwaggerController = class SwaggerController {
538
557
  constructor(title = 'Moost API') {
@@ -542,7 +561,7 @@ exports.SwaggerController = class SwaggerController {
542
561
  'serveIndex'(url, location, status) {
543
562
  if (!url.endsWith('index.html') && !url.endsWith('/')) {
544
563
  status.value = 302;
545
- location.value = path.join(url, '/');
564
+ location.value = Path.join(url, '/');
546
565
  return '';
547
566
  }
548
567
  return `<!DOCTYPE html>
@@ -648,6 +667,7 @@ exports.SwaggerController = __decorate([
648
667
 
649
668
  exports.SwaggerDescription = SwaggerDescription;
650
669
  exports.SwaggerExclude = SwaggerExclude;
670
+ exports.SwaggerParam = SwaggerParam;
651
671
  exports.SwaggerRequestBody = SwaggerRequestBody;
652
672
  exports.SwaggerResponse = SwaggerResponse;
653
673
  exports.SwaggerTag = SwaggerTag;
package/dist/index.d.ts CHANGED
@@ -49,16 +49,23 @@ interface TSwaggerMate {
49
49
  swaggerTags: string[];
50
50
  swaggerExclude: boolean;
51
51
  swaggerDescription: string;
52
- swaggerResponses: Record<number, Record<string, TSwaggerResponseConfigValue>>;
53
- swaggerRequestBody: Record<string, TSwaggerResponseConfigValue>;
52
+ swaggerResponses: Record<number, Record<string, TSwaggerConfigType>>;
53
+ swaggerRequestBody: Record<string, TSwaggerConfigType>;
54
+ swaggerParams: Array<{
55
+ name: string;
56
+ in: 'query' | 'header' | 'path' | 'formData' | 'body';
57
+ description?: string;
58
+ required?: boolean;
59
+ type?: TSwaggerConfigType;
60
+ }>;
54
61
  }
55
- type TSwaggerResponseConfigValue = TFunction | z.ZodType | TSwaggerSchema;
62
+ type TSwaggerConfigType = TFunction | z.ZodType | TSwaggerSchema;
56
63
  interface TSwaggerResponseConfig {
57
64
  contentType?: string;
58
65
  description?: string;
59
- response: TSwaggerResponseConfigValue;
66
+ response: TSwaggerConfigType;
60
67
  }
61
- type TSwaggerResponseOpts = TSwaggerResponseConfigValue | TSwaggerResponseConfig;
68
+ type TSwaggerResponseOpts = TSwaggerConfigType | TSwaggerResponseConfig;
62
69
 
63
70
  declare const SwaggerTag: (tag: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
64
71
  declare const SwaggerExclude: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
@@ -66,6 +73,7 @@ declare const SwaggerDescription: (descr: string) => MethodDecorator & ClassDeco
66
73
  declare function SwaggerResponse(opts: TSwaggerResponseOpts): MethodDecorator;
67
74
  declare function SwaggerResponse(code: number, opts: TSwaggerResponseOpts): MethodDecorator;
68
75
  declare function SwaggerRequestBody(opt: TSwaggerResponseOpts): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
76
+ declare function SwaggerParam(opts: TSwaggerMate['swaggerParams'][number]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
69
77
 
70
78
  declare class SwaggerController {
71
79
  protected title: string;
@@ -79,4 +87,4 @@ declare class SwaggerController {
79
87
  'serve'(path: string): Promise<unknown>;
80
88
  }
81
89
 
82
- export { SwaggerController, SwaggerDescription, SwaggerExclude, SwaggerRequestBody, SwaggerResponse, SwaggerTag, type TSwaggerMate, type TSwaggerResponseConfigValue, type TSwaggerResponseOpts, getSwaggerMate };
90
+ export { SwaggerController, SwaggerDescription, SwaggerExclude, SwaggerParam, SwaggerRequestBody, SwaggerResponse, SwaggerTag, type TSwaggerConfigType, type TSwaggerMate, type TSwaggerResponseOpts, getSwaggerMate };
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { getMoostMate, Controller, Const, useEventLogger, useControllerContext, Moost } from 'moost';
2
2
  import { Get, SetHeader, Url, HeaderHook, StatusHook } from '@moostjs/event-http';
3
- import { z, getZodType, getZodTypeForProp, ZodSkip } from '@moostjs/zod';
3
+ import { getZodType, z, getZodTypeForProp, ZodSkip } from '@moostjs/zod';
4
4
  import { serveFile } from '@wooksjs/http-static';
5
- import { join } from 'path';
5
+ import Path from 'path';
6
6
  import { getAbsoluteFSPath } from 'swagger-ui-dist';
7
7
  import { parseZodType } from 'zod-parser';
8
8
 
@@ -42,6 +42,9 @@ function SwaggerRequestBody(opt) {
42
42
  return meta;
43
43
  });
44
44
  }
45
+ function SwaggerParam(opts) {
46
+ return getSwaggerMate().decorate('swaggerParams', opts, true);
47
+ }
45
48
 
46
49
  /******************************************************************************
47
50
  Copyright (c) Microsoft Corporation.
@@ -117,22 +120,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
117
120
  for (const [code, responseConfigs] of Object.entries(hmeta.swaggerResponses)) {
118
121
  const newCode = code === '0' ? getDefaultStatusCode(handlerMethod) : code;
119
122
  for (const [contentType, type] of Object.entries(responseConfigs)) {
120
- let schema;
121
- let zt;
122
- if (type instanceof z.ZodType) {
123
- zt = type;
124
- }
125
- else if (typeof type === 'function') {
126
- zt = getZodType({
127
- type,
128
- });
129
- }
130
- if (zt) {
131
- const parsed = myParseZod(zt);
132
- if (['ZodString', 'ZodNumber', 'ZodObject', 'ZodArray', 'ZodBoolean'].includes(parsed.$type)) {
133
- schema = getSwaggerSchema(parsed);
134
- }
135
- }
123
+ const schema = getSwaggerSchemaFromSwaggerConfigType(type);
136
124
  if (schema) {
137
125
  responses = responses || {};
138
126
  responses[newCode] = { content: { [contentType]: { schema } } };
@@ -192,6 +180,15 @@ function mapToSwaggerSpec(metadata, options, logger) {
192
180
  responses,
193
181
  };
194
182
  const endpointSpec = swaggerSpec.paths[handlerPath][handlerMethod];
183
+ for (const param of hmeta?.swaggerParams || []) {
184
+ endpointSpec.parameters.push({
185
+ name: param.name,
186
+ in: param.in,
187
+ description: param.description,
188
+ required: !!param.required,
189
+ schema: getSwaggerSchemaFromSwaggerConfigType(param.type) || { type: 'string' },
190
+ });
191
+ }
195
192
  for (const paramName of handler.registeredAs[0].args) {
196
193
  const paramIndex = handler.meta.params.findIndex(param => param.paramSource === 'ROUTE' && param.paramName === paramName);
197
194
  const paramMeta = handler.meta.params[paramIndex];
@@ -230,7 +227,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
230
227
  }, undefined, logger);
231
228
  const parsed = myParseZod(zodType);
232
229
  const schema = getSwaggerSchema(parsed, true);
233
- if (paramMeta.paramSource == 'QUERY_ITEM') {
230
+ if (paramMeta.paramSource === 'QUERY_ITEM') {
234
231
  endpointSpec.parameters.push({
235
232
  name: paramMeta.paramName || '',
236
233
  in: 'query',
@@ -239,7 +236,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
239
236
  schema: schema || { type: 'string' },
240
237
  });
241
238
  }
242
- else if (paramMeta.paramSource == 'QUERY' && parsed.$type === 'ZodObject') {
239
+ else if (paramMeta.paramSource === 'QUERY' && parsed.$type === 'ZodObject') {
243
240
  for (const [key, value] of Object.entries(parsed.$inner)) {
244
241
  const schema = getSwaggerSchema(value, true);
245
242
  if (schema) {
@@ -531,6 +528,28 @@ function getDefaultStatusCode(httpMethod) {
531
528
  };
532
529
  return defaultStatusCodes[httpMethod.toUpperCase()] || 200;
533
530
  }
531
+ function getSwaggerSchemaFromSwaggerConfigType(type) {
532
+ let schema;
533
+ let zt;
534
+ if (type instanceof z.ZodType) {
535
+ zt = type;
536
+ }
537
+ else if (typeof type === 'function') {
538
+ zt = getZodType({
539
+ type,
540
+ });
541
+ }
542
+ if (zt) {
543
+ const parsed = myParseZod(zt);
544
+ if (['ZodString', 'ZodNumber', 'ZodObject', 'ZodArray', 'ZodBoolean'].includes(parsed.$type)) {
545
+ schema = getSwaggerSchema(parsed);
546
+ }
547
+ }
548
+ else if (type.type || type.$ref) {
549
+ schema = type;
550
+ }
551
+ return schema;
552
+ }
534
553
 
535
554
  let SwaggerController = class SwaggerController {
536
555
  constructor(title = 'Moost API') {
@@ -540,7 +559,7 @@ let SwaggerController = class SwaggerController {
540
559
  'serveIndex'(url, location, status) {
541
560
  if (!url.endsWith('index.html') && !url.endsWith('/')) {
542
561
  status.value = 302;
543
- location.value = join(url, '/');
562
+ location.value = Path.join(url, '/');
544
563
  return '';
545
564
  }
546
565
  return `<!DOCTYPE html>
@@ -644,4 +663,4 @@ SwaggerController = __decorate([
644
663
  __metadata("design:paramtypes", [Object])
645
664
  ], SwaggerController);
646
665
 
647
- export { SwaggerController, SwaggerDescription, SwaggerExclude, SwaggerRequestBody, SwaggerResponse, SwaggerTag, getSwaggerMate };
666
+ export { SwaggerController, SwaggerDescription, SwaggerExclude, SwaggerParam, SwaggerRequestBody, SwaggerResponse, SwaggerTag, getSwaggerMate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/swagger",
3
- "version": "0.3.39",
3
+ "version": "0.3.40",
4
4
  "description": "@moostjs/swagger",
5
5
  "sideEffects": false,
6
6
  "main": "dist/index.cjs",
@@ -37,13 +37,14 @@
37
37
  },
38
38
  "homepage": "https://github.com/moostjs/moostjs/tree/main/packages/swagger#readme",
39
39
  "peerDependencies": {
40
- "moost": "0.3.39",
41
- "@moostjs/event-http": "0.3.39"
40
+ "moost": "0.3.40",
41
+ "@moostjs/event-http": "0.3.40"
42
42
  },
43
43
  "dependencies": {
44
+ "@wooksjs/event-http": "^0.4.35",
44
45
  "swagger-ui-dist": "^5.10.5",
45
46
  "zod-parser": "^0.0.3",
46
- "@moostjs/zod": "0.3.39",
47
+ "@moostjs/zod": "0.3.40",
47
48
  "@wooksjs/http-static": "^0.4.35"
48
49
  },
49
50
  "devDependencies": {