@midwayjs/swagger 4.0.0-alpha.1 → 4.0.0-beta.10

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.
Files changed (32) hide show
  1. package/README.md +1 -1
  2. package/dist/common/enum.utils.js +5 -5
  3. package/dist/configuration.js +2 -0
  4. package/dist/decorators/api-basic.decorator.d.ts +1 -1
  5. package/dist/decorators/api-basic.decorator.js +1 -2
  6. package/dist/decorators/api-bearer.decorator.d.ts +1 -1
  7. package/dist/decorators/api-bearer.decorator.js +1 -2
  8. package/dist/decorators/api-body.decorator.js +2 -2
  9. package/dist/decorators/api-cookie.decorator.d.ts +1 -1
  10. package/dist/decorators/api-cookie.decorator.js +1 -2
  11. package/dist/decorators/api-exclude-controller.decorator.js +1 -2
  12. package/dist/decorators/api-exclude-endpoint.decorator.js +1 -2
  13. package/dist/decorators/api-extension.decorator.js +1 -2
  14. package/dist/decorators/api-extra-model.decorator.js +1 -2
  15. package/dist/decorators/api-header.decorator.js +2 -2
  16. package/dist/decorators/api-oauth2.decorator.d.ts +1 -1
  17. package/dist/decorators/api-oauth2.decorator.js +1 -2
  18. package/dist/decorators/api-operation.decorator.js +1 -2
  19. package/dist/decorators/api-param.decorator.js +1 -2
  20. package/dist/decorators/api-property.decorator.js +4 -5
  21. package/dist/decorators/api-query.decorator.js +1 -2
  22. package/dist/decorators/api-response.decorator.js +2 -2
  23. package/dist/decorators/api-security.decorator.d.ts +2 -2
  24. package/dist/decorators/api-security.decorator.js +2 -3
  25. package/dist/decorators/api-tags.decorator.js +1 -2
  26. package/dist/decorators/helpers.js +5 -6
  27. package/dist/documentBuilder.js +13 -15
  28. package/dist/interfaces/index.d.ts +4 -0
  29. package/dist/swaggerExplorer.js +54 -61
  30. package/dist/swaggerMiddleware.js +4 -0
  31. package/dist/ui/render.js +6 -6
  32. package/package.json +9 -7
package/README.md CHANGED
@@ -8,4 +8,4 @@ Document: [https://midwayjs.org](https://midwayjs.org)
8
8
 
9
9
  ## License
10
10
 
11
- [MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
11
+ [MIT](https://github.com/midwayjs/midway/blob/master/LICENSE)
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isEnumMetadata = exports.isEnumDefined = exports.isEnumArray = exports.addEnumSchema = exports.addEnumArraySchema = exports.getEnumType = exports.getEnumValues = void 0;
3
+ exports.isEnumMetadata = exports.isEnumDefined = exports.isEnumArray = void 0;
4
+ exports.getEnumValues = getEnumValues;
5
+ exports.getEnumType = getEnumType;
6
+ exports.addEnumArraySchema = addEnumArraySchema;
7
+ exports.addEnumSchema = addEnumSchema;
4
8
  function getEnumValues(enumType) {
5
9
  if (Array.isArray(enumType)) {
6
10
  return enumType;
@@ -13,12 +17,10 @@ function getEnumValues(enumType) {
13
17
  .map(key => enumType[key]);
14
18
  return Array.from(new Set(values));
15
19
  }
16
- exports.getEnumValues = getEnumValues;
17
20
  function getEnumType(values) {
18
21
  const hasString = values.filter(s => typeof s === 'string').length > 0;
19
22
  return hasString ? 'string' : 'number';
20
23
  }
21
- exports.getEnumType = getEnumType;
22
24
  function addEnumArraySchema(paramDefinition, decoratorOptions) {
23
25
  const paramSchema = paramDefinition.schema || {};
24
26
  paramDefinition.schema = paramSchema;
@@ -33,7 +35,6 @@ function addEnumArraySchema(paramDefinition, decoratorOptions) {
33
35
  paramDefinition.enumName = decoratorOptions.enumName;
34
36
  }
35
37
  }
36
- exports.addEnumArraySchema = addEnumArraySchema;
37
38
  function addEnumSchema(paramDefinition, decoratorOptions) {
38
39
  const paramSchema = paramDefinition.schema || {};
39
40
  const enumValues = getEnumValues(decoratorOptions.enum);
@@ -44,7 +45,6 @@ function addEnumSchema(paramDefinition, decoratorOptions) {
44
45
  paramDefinition.enumName = decoratorOptions.enumName;
45
46
  }
46
47
  }
47
- exports.addEnumSchema = addEnumSchema;
48
48
  const isEnumArray = (obj) => obj.isArray && obj.enum;
49
49
  exports.isEnumArray = isEnumArray;
50
50
  const isEnumDefined = (obj) => obj.enum;
@@ -14,6 +14,8 @@ const core_1 = require("@midwayjs/core");
14
14
  const _1 = require(".");
15
15
  const DefaultConfig = require("./config/config.default");
16
16
  let SwaggerConfiguration = class SwaggerConfiguration {
17
+ applicationManager;
18
+ configService;
17
19
  async onReady(container) {
18
20
  const apps = this.applicationManager.getApplications([
19
21
  'express',
@@ -1,2 +1,2 @@
1
- export declare function ApiBasicAuth(name?: string): ClassDecorator;
1
+ export declare function ApiBasicAuth(name?: string): ClassDecorator & MethodDecorator;
2
2
  //# sourceMappingURL=api-basic.decorator.d.ts.map
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiBasicAuth = void 0;
3
+ exports.ApiBasicAuth = ApiBasicAuth;
4
4
  const api_security_decorator_1 = require("./api-security.decorator");
5
5
  function ApiBasicAuth(name = 'basic') {
6
6
  return (0, api_security_decorator_1.ApiSecurity)(name);
7
7
  }
8
- exports.ApiBasicAuth = ApiBasicAuth;
9
8
  //# sourceMappingURL=api-basic.decorator.js.map
@@ -1,2 +1,2 @@
1
- export declare function ApiBearerAuth(name?: string): ClassDecorator;
1
+ export declare function ApiBearerAuth(name?: string): ClassDecorator & MethodDecorator;
2
2
  //# sourceMappingURL=api-bearer.decorator.d.ts.map
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiBearerAuth = void 0;
3
+ exports.ApiBearerAuth = ApiBearerAuth;
4
4
  const api_security_decorator_1 = require("./api-security.decorator");
5
5
  function ApiBearerAuth(name = 'bearer') {
6
6
  return (0, api_security_decorator_1.ApiSecurity)(name);
7
7
  }
8
- exports.ApiBearerAuth = ApiBearerAuth;
9
8
  //# sourceMappingURL=api-bearer.decorator.js.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiBody = exports.BodyContentType = void 0;
3
+ exports.BodyContentType = void 0;
4
+ exports.ApiBody = ApiBody;
4
5
  const enum_utils_1 = require("../common/enum.utils");
5
6
  const helpers_1 = require("./helpers");
6
7
  var BodyContentType;
@@ -31,5 +32,4 @@ function ApiBody(options) {
31
32
  }
32
33
  return (0, helpers_1.createParamDecorator)(param, defaultBodyMetadata);
33
34
  }
34
- exports.ApiBody = ApiBody;
35
35
  //# sourceMappingURL=api-body.decorator.js.map
@@ -1,2 +1,2 @@
1
- export declare function ApiCookieAuth(name?: string): ClassDecorator;
1
+ export declare function ApiCookieAuth(name?: string): ClassDecorator & MethodDecorator;
2
2
  //# sourceMappingURL=api-cookie.decorator.d.ts.map
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiCookieAuth = void 0;
3
+ exports.ApiCookieAuth = ApiCookieAuth;
4
4
  const api_security_decorator_1 = require("./api-security.decorator");
5
5
  function ApiCookieAuth(name = 'cookie') {
6
6
  return (0, api_security_decorator_1.ApiSecurity)(name);
7
7
  }
8
- exports.ApiCookieAuth = ApiCookieAuth;
9
8
  //# sourceMappingURL=api-cookie.decorator.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiExcludeController = void 0;
3
+ exports.ApiExcludeController = ApiExcludeController;
4
4
  const core_1 = require("@midwayjs/core");
5
5
  const constants_1 = require("../constants");
6
6
  function ApiExcludeController(disable = true) {
@@ -8,5 +8,4 @@ function ApiExcludeController(disable = true) {
8
8
  core_1.MetadataManager.defineMetadata(constants_1.DECORATORS.API_EXCLUDE_CONTROLLER, { disable }, target);
9
9
  };
10
10
  }
11
- exports.ApiExcludeController = ApiExcludeController;
12
11
  //# sourceMappingURL=api-exclude-controller.decorator.js.map
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiExcludeEndpoint = void 0;
3
+ exports.ApiExcludeEndpoint = ApiExcludeEndpoint;
4
4
  const constants_1 = require("../constants");
5
5
  const helpers_1 = require("./helpers");
6
6
  function ApiExcludeEndpoint(disable = true) {
7
7
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_EXCLUDE_ENDPOINT, true);
8
8
  }
9
- exports.ApiExcludeEndpoint = ApiExcludeEndpoint;
10
9
  //# sourceMappingURL=api-exclude-endpoint.decorator.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiExtension = void 0;
3
+ exports.ApiExtension = ApiExtension;
4
4
  const constants_1 = require("../constants");
5
5
  const helpers_1 = require("./helpers");
6
6
  function ApiExtension(extensionKey, extensionProperties) {
@@ -12,5 +12,4 @@ function ApiExtension(extensionKey, extensionProperties) {
12
12
  };
13
13
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_EXTENSION, extensionObject);
14
14
  }
15
- exports.ApiExtension = ApiExtension;
16
15
  //# sourceMappingURL=api-extension.decorator.js.map
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiExtraModel = void 0;
3
+ exports.ApiExtraModel = ApiExtraModel;
4
4
  const constants_1 = require("../constants");
5
5
  const helpers_1 = require("./helpers");
6
6
  function ApiExtraModel(models) {
7
7
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_EXTRA_MODEL, models);
8
8
  }
9
- exports.ApiExtraModel = ApiExtraModel;
10
9
  //# sourceMappingURL=api-extra-model.decorator.js.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiHeaders = exports.ApiHeader = void 0;
3
+ exports.ApiHeaders = void 0;
4
+ exports.ApiHeader = ApiHeader;
4
5
  const constants_1 = require("../constants");
5
6
  const enum_utils_1 = require("../common/enum.utils");
6
7
  const helpers_1 = require("./helpers");
@@ -32,7 +33,6 @@ function ApiHeader(options) {
32
33
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_HEADERS, param)(target, undefined, undefined);
33
34
  };
34
35
  }
35
- exports.ApiHeader = ApiHeader;
36
36
  const ApiHeaders = (headers) => {
37
37
  return (target, key, descriptor) => {
38
38
  headers.forEach(options => ApiHeader(options)(target, key, descriptor));
@@ -1,2 +1,2 @@
1
- export declare function ApiOAuth2(scopes: string[], name?: string): ClassDecorator;
1
+ export declare function ApiOAuth2(scopes: string[], name?: string): ClassDecorator & MethodDecorator;
2
2
  //# sourceMappingURL=api-oauth2.decorator.d.ts.map
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiOAuth2 = void 0;
3
+ exports.ApiOAuth2 = ApiOAuth2;
4
4
  const api_security_decorator_1 = require("./api-security.decorator");
5
5
  function ApiOAuth2(scopes, name = 'oauth2') {
6
6
  return (0, api_security_decorator_1.ApiSecurity)(name, scopes);
7
7
  }
8
- exports.ApiOAuth2 = ApiOAuth2;
9
8
  //# sourceMappingURL=api-oauth2.decorator.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiOperation = void 0;
3
+ exports.ApiOperation = ApiOperation;
4
4
  const constants_1 = require("../constants");
5
5
  const helpers_1 = require("./helpers");
6
6
  const defaultOperationOptions = {
@@ -12,5 +12,4 @@ function ApiOperation(options) {
12
12
  ...options,
13
13
  });
14
14
  }
15
- exports.ApiOperation = ApiOperation;
16
15
  //# sourceMappingURL=api-operation.decorator.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiParam = void 0;
3
+ exports.ApiParam = ApiParam;
4
4
  const helpers_1 = require("./helpers");
5
5
  const enum_utils_1 = require("../common/enum.utils");
6
6
  const defaultParamOptions = {
@@ -26,5 +26,4 @@ function ApiParam(options) {
26
26
  }
27
27
  return (0, helpers_1.createParamDecorator)(param, defaultParamOptions);
28
28
  }
29
- exports.ApiParam = ApiParam;
30
29
  //# sourceMappingURL=api-param.decorator.js.map
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiResponseProperty = exports.ApiPropertyOptional = exports.createApiPropertyDecorator = exports.ApiProperty = void 0;
3
+ exports.ApiProperty = ApiProperty;
4
+ exports.createApiPropertyDecorator = createApiPropertyDecorator;
5
+ exports.ApiPropertyOptional = ApiPropertyOptional;
6
+ exports.ApiResponseProperty = ApiResponseProperty;
4
7
  const constants_1 = require("../constants");
5
8
  const enum_utils_1 = require("../common/enum.utils");
6
9
  const helpers_1 = require("./helpers");
@@ -8,7 +11,6 @@ const isEnumArray = (obj) => obj.isArray && !!obj.enum;
8
11
  function ApiProperty(options = {}) {
9
12
  return createApiPropertyDecorator(options);
10
13
  }
11
- exports.ApiProperty = ApiProperty;
12
14
  function createApiPropertyDecorator(options = {}) {
13
15
  const [type, isArray] = (0, helpers_1.getTypeIsArrayTuple)(options.type, options.isArray);
14
16
  options = {
@@ -37,19 +39,16 @@ function createApiPropertyDecorator(options = {}) {
37
39
  }
38
40
  return (0, helpers_1.createPropertyDecorator)(constants_1.DECORATORS.API_MODEL_PROPERTIES, options);
39
41
  }
40
- exports.createApiPropertyDecorator = createApiPropertyDecorator;
41
42
  function ApiPropertyOptional(options = {}) {
42
43
  return ApiProperty({
43
44
  ...options,
44
45
  required: false,
45
46
  });
46
47
  }
47
- exports.ApiPropertyOptional = ApiPropertyOptional;
48
48
  function ApiResponseProperty(options = {}) {
49
49
  return ApiProperty({
50
50
  readOnly: true,
51
51
  ...options,
52
52
  });
53
53
  }
54
- exports.ApiResponseProperty = ApiResponseProperty;
55
54
  //# sourceMappingURL=api-property.decorator.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiQuery = void 0;
3
+ exports.ApiQuery = ApiQuery;
4
4
  const enum_utils_1 = require("../common/enum.utils");
5
5
  const helpers_1 = require("./helpers");
6
6
  const defaultQueryOptions = {
@@ -27,5 +27,4 @@ function ApiQuery(options) {
27
27
  }
28
28
  return (0, helpers_1.createParamDecorator)(param, defaultQueryOptions);
29
29
  }
30
- exports.ApiQuery = ApiQuery;
31
30
  //# sourceMappingURL=api-query.decorator.js.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiDefaultResponse = exports.ApiUnsupportedMediaTypeResponse = exports.ApiUnprocessableEntityResponse = exports.ApiServiceUnavailableResponse = exports.ApiRequestTimeoutResponse = exports.ApiPayloadTooLargeResponse = exports.ApiPreconditionFailedResponse = exports.ApiNotImplementedResponse = exports.ApiNotAcceptableResponse = exports.ApiMethodNotAllowedResponse = exports.ApiGoneResponse = exports.ApiGatewayTimeoutResponse = exports.ApiForbiddenResponse = exports.ApiConflictResponse = exports.ApiBadGatewayResponse = exports.ApiInternalServerErrorResponse = exports.ApiNotFoundResponse = exports.ApiTooManyRequestsResponse = exports.ApiUnauthorizedResponse = exports.ApiBadRequestResponse = exports.ApiFoundResponse = exports.ApiMovedPermanentlyResponse = exports.ApiNoContentResponse = exports.ApiAcceptedResponse = exports.ApiCreatedResponse = exports.ApiOkResponse = exports.ApiResponse = void 0;
3
+ exports.ApiDefaultResponse = exports.ApiUnsupportedMediaTypeResponse = exports.ApiUnprocessableEntityResponse = exports.ApiServiceUnavailableResponse = exports.ApiRequestTimeoutResponse = exports.ApiPayloadTooLargeResponse = exports.ApiPreconditionFailedResponse = exports.ApiNotImplementedResponse = exports.ApiNotAcceptableResponse = exports.ApiMethodNotAllowedResponse = exports.ApiGoneResponse = exports.ApiGatewayTimeoutResponse = exports.ApiForbiddenResponse = exports.ApiConflictResponse = exports.ApiBadGatewayResponse = exports.ApiInternalServerErrorResponse = exports.ApiNotFoundResponse = exports.ApiTooManyRequestsResponse = exports.ApiUnauthorizedResponse = exports.ApiBadRequestResponse = exports.ApiFoundResponse = exports.ApiMovedPermanentlyResponse = exports.ApiNoContentResponse = exports.ApiAcceptedResponse = exports.ApiCreatedResponse = exports.ApiOkResponse = void 0;
4
+ exports.ApiResponse = ApiResponse;
4
5
  const constants_1 = require("../constants");
5
6
  const helpers_1 = require("./helpers");
6
7
  const httpStatus_1 = require("../common/httpStatus");
@@ -14,7 +15,6 @@ function ApiResponse(options) {
14
15
  };
15
16
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_RESPONSE, groupedMetadata);
16
17
  }
17
- exports.ApiResponse = ApiResponse;
18
18
  const ApiOkResponse = (options = {}) => ApiResponse({
19
19
  ...options,
20
20
  status: httpStatus_1.HttpStatus.OK,
@@ -1,4 +1,4 @@
1
1
  import { SecurityRequirementObject } from '../interfaces';
2
- export declare function ApiSecurity(name: string | SecurityRequirementObject, requirements?: string[]): ClassDecorator;
3
- export declare function ApiExcludeSecurity(): MethodDecorator;
2
+ export declare function ApiSecurity(name: string | SecurityRequirementObject, requirements?: string[]): ClassDecorator & MethodDecorator;
3
+ export declare function ApiExcludeSecurity(): ClassDecorator & MethodDecorator;
4
4
  //# sourceMappingURL=api-security.decorator.d.ts.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiExcludeSecurity = exports.ApiSecurity = void 0;
3
+ exports.ApiSecurity = ApiSecurity;
4
+ exports.ApiExcludeSecurity = ApiExcludeSecurity;
4
5
  const constants_1 = require("../constants");
5
6
  const helpers_1 = require("./helpers");
6
7
  function ApiSecurity(name, requirements = []) {
@@ -13,9 +14,7 @@ function ApiSecurity(name, requirements = []) {
13
14
  }
14
15
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_SECURITY, metadata);
15
16
  }
16
- exports.ApiSecurity = ApiSecurity;
17
17
  function ApiExcludeSecurity() {
18
18
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_EXCLUDE_SECURITY, true);
19
19
  }
20
- exports.ApiExcludeSecurity = ApiExcludeSecurity;
21
20
  //# sourceMappingURL=api-security.decorator.js.map
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiTags = void 0;
3
+ exports.ApiTags = ApiTags;
4
4
  const constants_1 = require("../constants");
5
5
  const helpers_1 = require("./helpers");
6
6
  function ApiTags(tags) {
7
7
  return (0, helpers_1.createMixedDecorator)(constants_1.DECORATORS.API_TAGS, tags);
8
8
  }
9
- exports.ApiTags = ApiTags;
10
9
  //# sourceMappingURL=api-tags.decorator.js.map
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSchemaPath = exports.getTypeIsArrayTuple = exports.createParamDecorator = exports.createMixedDecorator = exports.createPropertyDecorator = void 0;
3
+ exports.createPropertyDecorator = createPropertyDecorator;
4
+ exports.createMixedDecorator = createMixedDecorator;
5
+ exports.createParamDecorator = createParamDecorator;
6
+ exports.getTypeIsArrayTuple = getTypeIsArrayTuple;
7
+ exports.getSchemaPath = getSchemaPath;
4
8
  const core_1 = require("@midwayjs/core");
5
9
  const constants_1 = require("../constants");
6
10
  function createPropertyDecorator(metakey, metadata) {
7
11
  return core_1.DecoratorManager.createCustomPropertyDecorator(metakey, metadata, false);
8
12
  }
9
- exports.createPropertyDecorator = createPropertyDecorator;
10
13
  function createMixedDecorator(metakey, metadata) {
11
14
  return (target, methodName) => {
12
15
  if (methodName) {
@@ -24,14 +27,12 @@ function createMixedDecorator(metakey, metadata) {
24
27
  }
25
28
  };
26
29
  }
27
- exports.createMixedDecorator = createMixedDecorator;
28
30
  function createParamDecorator(metadata, initial) {
29
31
  return createMixedDecorator(constants_1.DECORATORS.API_PARAMETERS, {
30
32
  ...initial,
31
33
  ...metadata,
32
34
  });
33
35
  }
34
- exports.createParamDecorator = createParamDecorator;
35
36
  function getTypeIsArrayTuple(input, isArrayFlag) {
36
37
  if (!input) {
37
38
  return [input, isArrayFlag];
@@ -43,7 +44,6 @@ function getTypeIsArrayTuple(input, isArrayFlag) {
43
44
  const type = isInputArray ? input[0] : input;
44
45
  return [type, isInputArray];
45
46
  }
46
- exports.getTypeIsArrayTuple = getTypeIsArrayTuple;
47
47
  function getSchemaPath(clzz) {
48
48
  let str = clzz;
49
49
  if (typeof clzz !== 'string') {
@@ -51,5 +51,4 @@ function getSchemaPath(clzz) {
51
51
  }
52
52
  return `#/components/schemas/${str}`;
53
53
  }
54
- exports.getSchemaPath = getSchemaPath;
55
54
  //# sourceMappingURL=helpers.js.map
@@ -2,21 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DocumentBuilder = void 0;
4
4
  class DocumentBuilder {
5
- constructor() {
6
- this.document = {
7
- openapi: '3.0.1',
8
- info: {
9
- title: '',
10
- description: '',
11
- version: '1.0.0',
12
- contact: {},
13
- },
14
- tags: [],
15
- servers: [],
16
- components: {},
17
- paths: {},
18
- };
19
- }
5
+ document = {
6
+ openapi: '3.0.1',
7
+ info: {
8
+ title: '',
9
+ description: '',
10
+ version: '1.0.0',
11
+ contact: {},
12
+ },
13
+ tags: [],
14
+ servers: [],
15
+ components: {},
16
+ paths: {},
17
+ };
20
18
  setTitle(title) {
21
19
  this.document.info.title = title;
22
20
  return this;
@@ -280,6 +280,10 @@ export interface AuthOptions extends Omit<SecuritySchemeObject, 'type'> {
280
280
  * authType = cookie 时可以修改,cookie 的名称
281
281
  */
282
282
  cookieName?: string;
283
+ /**
284
+ * 添加全局默认要求
285
+ */
286
+ addSecurityRequirements?: boolean;
283
287
  }
284
288
  /**
285
289
  * see https://swagger.io/specification/
@@ -16,11 +16,9 @@ const documentBuilder_1 = require("./documentBuilder");
16
16
  const _1 = require(".");
17
17
  const enum_utils_1 = require("./common/enum.utils");
18
18
  let SwaggerExplorer = class SwaggerExplorer {
19
- constructor() {
20
- this.swaggerConfig = {};
21
- this.documentBuilder = new documentBuilder_1.DocumentBuilder();
22
- this.operationIdFactory = (controllerKey, webRouter) => `${controllerKey.toLowerCase()}_${webRouter.method.toLocaleLowerCase()}`;
23
- }
19
+ swaggerConfig = {};
20
+ documentBuilder = new documentBuilder_1.DocumentBuilder();
21
+ operationIdFactory = (controllerKey, webRouter) => `${controllerKey.toLowerCase()}_${webRouter.method.toLocaleLowerCase()}`;
24
22
  async init() {
25
23
  this.documentBuilder.setTitle(this.swaggerConfig.title);
26
24
  this.documentBuilder.setVersion(this.swaggerConfig.version);
@@ -159,8 +157,8 @@ let SwaggerExplorer = class SwaggerExplorer {
159
157
  if (headers.length > 0) {
160
158
  headers = headers.map(item => item.metadata);
161
159
  }
162
- // 过滤出安全信息
163
- const security = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
160
+ // 过滤出安全信息(方法优先)
161
+ const classSecurity = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
164
162
  // 初始化路径对象
165
163
  const paths = {};
166
164
  // 如果存在路由信息,则遍历生成路径
@@ -171,6 +169,7 @@ let SwaggerExplorer = class SwaggerExplorer {
171
169
  url = replaceUrl(url, parseParamsInPath(url));
172
170
  // 方法元数据
173
171
  const metaForMethods = core_1.MetadataManager.getMetadata(constants_1.DECORATORS_METHOD_METADATA, target, webRouter.method) || [];
172
+ const methodSecurity = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
174
173
  // 判断是否忽略当前路由
175
174
  const endpoints = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
176
175
  item.propertyName === webRouter.method);
@@ -215,20 +214,26 @@ let SwaggerExplorer = class SwaggerExplorer {
215
214
  Object.assign(paths[url][webRouter.requestMethod], e.metadata);
216
215
  }
217
216
  }
218
- const excludeSecurity = metaForMethods.find(item => {
219
- return item.key === constants_1.DECORATORS.API_EXCLUDE_SECURITY;
220
- });
221
- // 如果存在安全信息,则将其添加到路径中
222
- if (security.length > 0 && !excludeSecurity) {
223
- if (!paths[url][webRouter.requestMethod].security) {
224
- paths[url][webRouter.requestMethod].security = [];
225
- }
226
- for (const s of security) {
227
- if (!s.metadata) {
228
- continue;
229
- }
230
- paths[url][webRouter.requestMethod].security.push(s.metadata);
231
- }
217
+ // 优先级处理:method exclude > method security > class exclude > class security
218
+ const hasMethodExclude = metaForMethods.find(item => item.key === constants_1.DECORATORS.API_EXCLUDE_SECURITY);
219
+ const hasMethodSecurity = methodSecurity.length > 0;
220
+ const hasClassExclude = metaForClass.find(item => item.key === constants_1.DECORATORS.API_EXCLUDE_SECURITY);
221
+ const hasClassSecurity = classSecurity.length > 0;
222
+ if (hasMethodExclude) {
223
+ paths[url][webRouter.requestMethod].security = [];
224
+ }
225
+ else if (hasMethodSecurity) {
226
+ paths[url][webRouter.requestMethod].security = methodSecurity
227
+ .map(s => s.metadata)
228
+ .filter(Boolean);
229
+ }
230
+ else if (hasClassExclude) {
231
+ paths[url][webRouter.requestMethod].security = [];
232
+ }
233
+ else if (hasClassSecurity) {
234
+ paths[url][webRouter.requestMethod].security = classSecurity
235
+ .map(s => s.metadata)
236
+ .filter(Boolean);
232
237
  }
233
238
  }
234
239
  }
@@ -513,7 +518,7 @@ let SwaggerExplorer = class SwaggerExplorer {
513
518
  }
514
519
  else {
515
520
  tt.content = {
516
- 'text/plan': {
521
+ 'text/plain': {
517
522
  schema: {
518
523
  type: convertSchemaType(tt.type),
519
524
  },
@@ -847,52 +852,40 @@ let SwaggerExplorer = class SwaggerExplorer {
847
852
  if (!opts) {
848
853
  return;
849
854
  }
850
- const authType = opts.authType;
851
- delete opts.authType;
852
- // TODO 加 security
855
+ const { authType, name = '', addSecurityRequirements = false, ...otherOptions } = opts;
856
+ if (!authType) {
857
+ return;
858
+ }
853
859
  switch (authType) {
854
- case 'basic':
855
- {
856
- const name = opts.name;
857
- delete opts.name;
858
- this.documentBuilder.addBasicAuth(opts, name);
859
- }
860
+ case 'basic': {
861
+ this.documentBuilder.addBasicAuth(otherOptions, name);
860
862
  break;
861
- case 'bearer':
862
- {
863
- const name = opts.name;
864
- delete opts.name;
865
- this.documentBuilder.addBearerAuth(opts, name);
866
- }
863
+ }
864
+ case 'bearer': {
865
+ this.documentBuilder.addBearerAuth(otherOptions, name);
867
866
  break;
868
- case 'cookie':
869
- {
870
- const cname = opts.cookieName;
871
- const secName = opts.securityName;
872
- delete opts.cookieName;
873
- delete opts.securityName;
874
- this.documentBuilder.addCookieAuth(cname, opts, secName);
875
- }
867
+ }
868
+ case 'cookie': {
869
+ const { cookieName, securityName, ...options } = otherOptions;
870
+ this.documentBuilder.addCookieAuth(cookieName, options, securityName);
876
871
  break;
877
- case 'oauth2':
878
- {
879
- const name = opts.name;
880
- delete opts.name;
881
- this.documentBuilder.addOAuth2(opts, name);
882
- }
872
+ }
873
+ case 'oauth2': {
874
+ this.documentBuilder.addOAuth2(otherOptions, name);
883
875
  break;
884
- case 'apikey':
885
- {
886
- const name = opts.name;
887
- delete opts.name;
888
- this.documentBuilder.addApiKey(opts, name);
889
- }
876
+ }
877
+ case 'apikey': {
878
+ this.documentBuilder.addApiKey(otherOptions, name);
890
879
  break;
891
- case 'custom':
892
- {
893
- this.documentBuilder.addSecurity(opts?.name, opts);
894
- }
880
+ }
881
+ case 'custom': {
882
+ this.documentBuilder.addSecurity(name, otherOptions);
895
883
  break;
884
+ }
885
+ }
886
+ if (addSecurityRequirements) {
887
+ // 添加安全要求
888
+ this.documentBuilder.addSecurityRequirements(name);
896
889
  }
897
890
  }
898
891
  };
@@ -13,6 +13,10 @@ exports.SwaggerMiddleware = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
14
  const swaggerExplorer_1 = require("./swaggerExplorer");
15
15
  let SwaggerMiddleware = class SwaggerMiddleware {
16
+ swaggerConfig;
17
+ swaggerRender;
18
+ swaggerExplorer;
19
+ environmentService;
16
20
  async init() {
17
21
  if (typeof this.swaggerConfig.swaggerUIRender !== 'function') {
18
22
  throw new core_1.MidwayInvalidConfigPropertyError('swagger.swaggerRender');
package/dist/ui/render.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.renderSwaggerUIRemote = exports.renderJSON = exports.renderSwaggerUIDist = void 0;
3
+ exports.renderSwaggerUIDist = renderSwaggerUIDist;
4
+ exports.renderJSON = renderJSON;
5
+ exports.renderSwaggerUIRemote = renderSwaggerUIRemote;
4
6
  const core_1 = require("@midwayjs/core");
5
7
  const fs_1 = require("fs");
6
8
  const path_1 = require("path");
@@ -21,8 +23,9 @@ function renderSwaggerUIDist(swaggerConfig, swaggerExplorer) {
21
23
  function replaceInfo(content) {
22
24
  let str = `location.href.replace('${swaggerConfig.swaggerPath}/index.html', '${swaggerConfig.swaggerPath}/index.json'),\n validatorUrl: null,`;
23
25
  if (swaggerConfig.displayOptions) {
24
- Object.keys(swaggerConfig.displayOptions).forEach(key => {
25
- str += `\n${key}: ${swaggerConfig.displayOptions[key]},`;
26
+ Object.entries(swaggerConfig.displayOptions).forEach(([key, value]) => {
27
+ const variable = typeof value === 'string' ? `'${value}'` : value;
28
+ str += `\n${key}: ${variable},`;
26
29
  });
27
30
  }
28
31
  return content.replace('"https://petstore.swagger.io/v2/swagger.json",', str);
@@ -54,7 +57,6 @@ function renderSwaggerUIDist(swaggerConfig, swaggerExplorer) {
54
57
  return { ext, content };
55
58
  };
56
59
  }
57
- exports.renderSwaggerUIDist = renderSwaggerUIDist;
58
60
  function renderJSON(swaggerConfig, swaggerExplorer) {
59
61
  return async (pathname) => {
60
62
  if (pathname.indexOf(swaggerConfig.swaggerPath) === -1) {
@@ -68,7 +70,6 @@ function renderJSON(swaggerConfig, swaggerExplorer) {
68
70
  return;
69
71
  };
70
72
  }
71
- exports.renderJSON = renderJSON;
72
73
  function renderSwaggerUIRemote(swaggerConfig, swaggerExplorer) {
73
74
  const swaggerRenderOptions = swaggerConfig.swaggerUIRenderOptions || {};
74
75
  const indexPagePath = swaggerRenderOptions?.indexPagePath || (0, path_1.join)(__dirname, '../../index.html');
@@ -91,5 +92,4 @@ function renderSwaggerUIRemote(swaggerConfig, swaggerExplorer) {
91
92
  return;
92
93
  };
93
94
  }
94
- exports.renderSwaggerUIRemote = renderSwaggerUIRemote;
95
95
  //# sourceMappingURL=render.js.map
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-beta.10",
4
+ "description": "Midway Component for Swagger API Documentation",
4
5
  "main": "dist/index.js",
5
6
  "typings": "index.d.ts",
6
7
  "files": [
@@ -10,11 +11,12 @@
10
11
  "index.html"
11
12
  ],
12
13
  "devDependencies": {
13
- "@midwayjs/core": "^4.0.0-alpha.1",
14
- "@midwayjs/koa": "^4.0.0-alpha.1",
15
- "@midwayjs/mock": "^4.0.0-alpha.1",
16
- "@midwayjs/validate": "^4.0.0-alpha.1",
17
- "swagger-ui-dist": "5.18.2"
14
+ "@apidevtools/swagger-parser": "11.0.1",
15
+ "@midwayjs/core": "^4.0.0-beta.10",
16
+ "@midwayjs/koa": "^4.0.0-beta.10",
17
+ "@midwayjs/mock": "^4.0.0-beta.10",
18
+ "@midwayjs/validate": "^4.0.0-beta.10",
19
+ "swagger-ui-dist": "5.18.3"
18
20
  },
19
21
  "author": "Kurten Chan <chinkurten@gmail.com>",
20
22
  "license": "MIT",
@@ -28,5 +30,5 @@
28
30
  "type": "git",
29
31
  "url": "https://github.com/midwayjs/midway.git"
30
32
  },
31
- "gitHead": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
33
+ "gitHead": "1b1856629913703f67304155aaf611ec936a81ac"
32
34
  }