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

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/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,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,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,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,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,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
@@ -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/
@@ -159,8 +159,8 @@ let SwaggerExplorer = class SwaggerExplorer {
159
159
  if (headers.length > 0) {
160
160
  headers = headers.map(item => item.metadata);
161
161
  }
162
- // 过滤出安全信息
163
- const security = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
162
+ // 过滤出安全信息(方法优先)
163
+ const classSecurity = metaForClass.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
164
164
  // 初始化路径对象
165
165
  const paths = {};
166
166
  // 如果存在路由信息,则遍历生成路径
@@ -171,6 +171,7 @@ let SwaggerExplorer = class SwaggerExplorer {
171
171
  url = replaceUrl(url, parseParamsInPath(url));
172
172
  // 方法元数据
173
173
  const metaForMethods = core_1.MetadataManager.getMetadata(constants_1.DECORATORS_METHOD_METADATA, target, webRouter.method) || [];
174
+ const methodSecurity = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_SECURITY);
174
175
  // 判断是否忽略当前路由
175
176
  const endpoints = metaForMethods.filter(item => item.key === constants_1.DECORATORS.API_EXCLUDE_ENDPOINT &&
176
177
  item.propertyName === webRouter.method);
@@ -215,20 +216,26 @@ let SwaggerExplorer = class SwaggerExplorer {
215
216
  Object.assign(paths[url][webRouter.requestMethod], e.metadata);
216
217
  }
217
218
  }
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
- }
219
+ // 优先级处理:method exclude > method security > class exclude > class security
220
+ const hasMethodExclude = metaForMethods.find(item => item.key === constants_1.DECORATORS.API_EXCLUDE_SECURITY);
221
+ const hasMethodSecurity = methodSecurity.length > 0;
222
+ const hasClassExclude = metaForClass.find(item => item.key === constants_1.DECORATORS.API_EXCLUDE_SECURITY);
223
+ const hasClassSecurity = classSecurity.length > 0;
224
+ if (hasMethodExclude) {
225
+ paths[url][webRouter.requestMethod].security = [];
226
+ }
227
+ else if (hasMethodSecurity) {
228
+ paths[url][webRouter.requestMethod].security = methodSecurity
229
+ .map(s => s.metadata)
230
+ .filter(Boolean);
231
+ }
232
+ else if (hasClassExclude) {
233
+ paths[url][webRouter.requestMethod].security = [];
234
+ }
235
+ else if (hasClassSecurity) {
236
+ paths[url][webRouter.requestMethod].security = classSecurity
237
+ .map(s => s.metadata)
238
+ .filter(Boolean);
232
239
  }
233
240
  }
234
241
  }
@@ -513,7 +520,7 @@ let SwaggerExplorer = class SwaggerExplorer {
513
520
  }
514
521
  else {
515
522
  tt.content = {
516
- 'text/plan': {
523
+ 'text/plain': {
517
524
  schema: {
518
525
  type: convertSchemaType(tt.type),
519
526
  },
@@ -847,52 +854,40 @@ let SwaggerExplorer = class SwaggerExplorer {
847
854
  if (!opts) {
848
855
  return;
849
856
  }
850
- const authType = opts.authType;
851
- delete opts.authType;
852
- // TODO 加 security
857
+ const { authType, name = '', addSecurityRequirements = false, ...otherOptions } = opts;
858
+ if (!authType) {
859
+ return;
860
+ }
853
861
  switch (authType) {
854
- case 'basic':
855
- {
856
- const name = opts.name;
857
- delete opts.name;
858
- this.documentBuilder.addBasicAuth(opts, name);
859
- }
862
+ case 'basic': {
863
+ this.documentBuilder.addBasicAuth(otherOptions, name);
860
864
  break;
861
- case 'bearer':
862
- {
863
- const name = opts.name;
864
- delete opts.name;
865
- this.documentBuilder.addBearerAuth(opts, name);
866
- }
865
+ }
866
+ case 'bearer': {
867
+ this.documentBuilder.addBearerAuth(otherOptions, name);
867
868
  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
- }
869
+ }
870
+ case 'cookie': {
871
+ const { cookieName, securityName, ...options } = otherOptions;
872
+ this.documentBuilder.addCookieAuth(cookieName, options, securityName);
876
873
  break;
877
- case 'oauth2':
878
- {
879
- const name = opts.name;
880
- delete opts.name;
881
- this.documentBuilder.addOAuth2(opts, name);
882
- }
874
+ }
875
+ case 'oauth2': {
876
+ this.documentBuilder.addOAuth2(otherOptions, name);
883
877
  break;
884
- case 'apikey':
885
- {
886
- const name = opts.name;
887
- delete opts.name;
888
- this.documentBuilder.addApiKey(opts, name);
889
- }
878
+ }
879
+ case 'apikey': {
880
+ this.documentBuilder.addApiKey(otherOptions, name);
890
881
  break;
891
- case 'custom':
892
- {
893
- this.documentBuilder.addSecurity(opts?.name, opts);
894
- }
882
+ }
883
+ case 'custom': {
884
+ this.documentBuilder.addSecurity(name, otherOptions);
895
885
  break;
886
+ }
887
+ }
888
+ if (addSecurityRequirements) {
889
+ // 添加安全要求
890
+ this.documentBuilder.addSecurityRequirements(name);
896
891
  }
897
892
  }
898
893
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/swagger",
3
- "version": "4.0.0-alpha.1",
3
+ "version": "4.0.0-beta.2",
4
4
  "main": "dist/index.js",
5
5
  "typings": "index.d.ts",
6
6
  "files": [
@@ -10,11 +10,12 @@
10
10
  "index.html"
11
11
  ],
12
12
  "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"
13
+ "@apidevtools/swagger-parser": "11.0.1",
14
+ "@midwayjs/core": "^4.0.0-beta.2",
15
+ "@midwayjs/koa": "^4.0.0-beta.2",
16
+ "@midwayjs/mock": "^4.0.0-beta.2",
17
+ "@midwayjs/validate": "^4.0.0-beta.2",
18
+ "swagger-ui-dist": "5.18.3"
18
19
  },
19
20
  "author": "Kurten Chan <chinkurten@gmail.com>",
20
21
  "license": "MIT",
@@ -28,5 +29,5 @@
28
29
  "type": "git",
29
30
  "url": "https://github.com/midwayjs/midway.git"
30
31
  },
31
- "gitHead": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
32
+ "gitHead": "53bfef4c5279da5f09025e4610bdbf64f94f60bd"
32
33
  }