pg-mvc-service 2.0.104 → 2.0.105

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.
@@ -59,7 +59,7 @@ const createSwagger = (services, name, url, params = []) => {
59
59
  }
60
60
  }
61
61
  yml += service.Request.createSwagger(service.Method);
62
- yml += service.Response.createSwagger();
62
+ yml += service.Response.createSwagger(service.Request.getInputErrorLost(service.Method, service.ApiCode));
63
63
  endpontSwaggerYml[service.Endpoint][service.Method] = yml;
64
64
  }
65
65
  let swaggerInfo = `openapi: 3.0.0
@@ -67,27 +67,6 @@ class RequestType extends ReqResType_1.default {
67
67
  };
68
68
  this.ERROR_MESSAGE = process.env.TZ === 'Asia/Tokyo' ? this.ERROR_MESSAGE_JAPAN : this.ERROR_MESSAGE_ENGLISH;
69
69
  this.paramProperties = [];
70
- // private makeSwaggerPropertyFromDictionary(keys: Array<string | number>, tabCount: number): string {
71
- // const property = this.getProperty(keys).properties;
72
- // const space = ' '.repeat(tabCount);
73
- // let ymlString = `${space}items:\n`;
74
- // ymlString += `${space} type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
75
- // if ((property.description ?? '') !== '') {
76
- // const descJoin = `\n${space} `;
77
- // ymlString += `${space} description: |${descJoin}${property.description.replaceAll('\n', descJoin)}\n`;
78
- // }
79
- // switch (property.type) {
80
- // case 'object':
81
- // case 'object?':
82
- // ymlString += this.makeSwaggerProperyFromObject([...keys, 0], tabCount + 1);
83
- // break;
84
- // case 'array':
85
- // case 'array?':
86
- // ymlString += this.makeSwaggerPropertyFromArray([...keys, 0], tabCount + 1);
87
- // break;
88
- // }
89
- // return ymlString;
90
- // }
91
70
  }
92
71
  get paramPath() {
93
72
  return this.paramProperties.map(property => `/{${property.key}}`).join("");
@@ -1034,5 +1013,46 @@ class RequestType extends ReqResType_1.default {
1034
1013
  }
1035
1014
  return ymlString;
1036
1015
  }
1016
+ getInputErrorLost(method, code) {
1017
+ const errorList = [];
1018
+ if (method === 'GET' || method === 'DELETE') {
1019
+ const tabCount = 4;
1020
+ const space = ' '.repeat(tabCount);
1021
+ let ymlString = '';
1022
+ for (const [key, property] of Object.entries(this.properties)) {
1023
+ if (property.type.endsWith('?') === false) {
1024
+ errorList.push({
1025
+ status: 400,
1026
+ code: code + '',
1027
+ description: `${key}:未入力エラー(未指定、null、空文字の場合にエラー)`
1028
+ });
1029
+ }
1030
+ // ymlString += `${space}- name: ${key}\n`;
1031
+ // ymlString += `${space} in: query\n`;
1032
+ // const descJoin = `\n${space} `;
1033
+ // if (property.type === 'enum' || property.type === 'enum?') {
1034
+ // ymlString += `${space} description: |${property.description === undefined ? '' : `${descJoin}${property.description.replaceAll("\n", descJoin)}`}`;
1035
+ // ymlString += `${descJoin}enum list`;
1036
+ // ymlString += `${Object.entries(property.enums).map(([key, value]) => `${descJoin}- ${key}: ${value}`)}\n`;
1037
+ // } else if (property.description !== undefined) {
1038
+ // ymlString += `${space} description: |\n${space} ${property.description.replaceAll("\n", descJoin)}\n`;
1039
+ // }
1040
+ // ymlString += `${space} required: ${property.type.endsWith('?') ? 'false' : 'true'}\n`;
1041
+ // ymlString += `${space} schema:\n`;
1042
+ // ymlString += `${space} type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
1043
+ // if (property.type === 'enum' || property.type === 'enum?') {
1044
+ // ymlString += `${space} nullable: ${property.enumType.endsWith('?')}\n`;
1045
+ // ymlString += `${space} enum:\n`;
1046
+ // for (const type of Object.keys(property.enumType)) {
1047
+ // ymlString += `${space} - ${type}\n`;
1048
+ // }
1049
+ // if (property.enumType.endsWith('?')) {
1050
+ // ymlString += `${space} - null\n`;
1051
+ // }
1052
+ // }
1053
+ }
1054
+ }
1055
+ return errorList;
1056
+ }
1037
1057
  }
1038
1058
  exports.RequestType = RequestType;
@@ -10,6 +10,7 @@ const ReqResType_1 = __importDefault(require("./ReqResType"));
10
10
  class ResponseType extends ReqResType_1.default {
11
11
  constructor() {
12
12
  super(...arguments);
13
+ this.errorList = [];
13
14
  /**
14
15
  * Property to store response data
15
16
  * レスポンスデータを格納するためのプロパティ
@@ -337,7 +338,7 @@ class ResponseType extends ReqResType_1.default {
337
338
  * @returns {string} Swagger format response definition
338
339
  * Swagger形式のレスポンス定義
339
340
  */
340
- createSwagger() {
341
+ createSwagger(requestErrorList) {
341
342
  let ymlString = ` responses:
342
343
  '200':
343
344
  description: 成功事レスポンス
@@ -375,6 +376,43 @@ class ResponseType extends ReqResType_1.default {
375
376
  break;
376
377
  }
377
378
  }
379
+ const errorList = [...this.errorList, ...requestErrorList];
380
+ errorList.push({
381
+ status: 500,
382
+ code: '',
383
+ description: 'サーバー内部エラー(予期せぬエラー)'
384
+ });
385
+ for (const error of errorList) {
386
+ ymlString += `
387
+ '${error.code}':
388
+ description: 【エラー】${error.description}
389
+ content:
390
+ application/json:
391
+ schema:
392
+ type: object
393
+ properties:`;
394
+ if ([400, 404, 409, 422].includes(error.status)) {
395
+ ymlString += `
396
+ errorCode:
397
+ type: string
398
+ description: エラーコード
399
+ errorMessage:
400
+ type: string
401
+ description: エラーメッセージ`;
402
+ }
403
+ else if (error.status === 401) {
404
+ ymlString += `
405
+ message:
406
+ type: string
407
+ description: Authentication expired. Please login again.`;
408
+ }
409
+ else if (error.status === 500) {
410
+ ymlString += `
411
+ message:
412
+ type: string
413
+ description: Internal Server Error`;
414
+ }
415
+ }
378
416
  return ymlString;
379
417
  }
380
418
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-mvc-service",
3
- "version": "2.0.104",
3
+ "version": "2.0.105",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/n-daira/npm-pack_mvc-service#readme",
6
6
  "bugs": {
@@ -72,7 +72,7 @@ export const createSwagger = (services: Service[], name: string, url: string, pa
72
72
  }
73
73
 
74
74
  yml += service.Request.createSwagger(service.Method);
75
- yml += service.Response.createSwagger();
75
+ yml += service.Response.createSwagger(service.Request.getInputErrorLost(service.Method, service.ApiCode));
76
76
 
77
77
  endpontSwaggerYml[service.Endpoint][service.Method] = yml;
78
78
  }
@@ -3,6 +3,7 @@ import ReqResType, { EnumType, NumberType, PrimitiveType, PropertyType, StringTy
3
3
  import { InputErrorException } from '../exceptions/Exception';
4
4
  import StringUtil from '../Utils/StringUtil';
5
5
  import { ValidateStringUtil } from 'type-utils-n-daira';
6
+ import { IError } from './ResponseType';
6
7
 
7
8
  // エラーメッセージの型定義
8
9
  export interface ErrorMessageType {
@@ -1099,32 +1100,52 @@ export class RequestType extends ReqResType {
1099
1100
  return ymlString;
1100
1101
  }
1101
1102
 
1102
- // private makeSwaggerPropertyFromDictionary(keys: Array<string | number>, tabCount: number): string {
1103
+ public getInputErrorLost(method: string, code: string): Array<IError> {
1104
+ const errorList: Array<IError> = [];
1103
1105
 
1104
- // const property = this.getProperty(keys).properties;
1105
-
1106
- // const space = ' '.repeat(tabCount);
1106
+ if (method === 'GET' || method === 'DELETE') {
1107
1107
 
1108
- // let ymlString = `${space}items:\n`;
1109
- // ymlString += `${space} type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
1110
- // if ((property.description ?? '') !== '') {
1111
- // const descJoin = `\n${space} `;
1112
- // ymlString += `${space} description: |${descJoin}${property.description.replaceAll('\n', descJoin)}\n`;
1113
- // }
1108
+ const tabCount = 4;
1109
+ const space = ' '.repeat(tabCount);
1114
1110
 
1115
- // switch (property.type) {
1116
- // case 'object':
1117
- // case 'object?':
1118
- // ymlString += this.makeSwaggerProperyFromObject([...keys, 0], tabCount + 1);
1119
- // break;
1120
- // case 'array':
1121
- // case 'array?':
1122
- // ymlString += this.makeSwaggerPropertyFromArray([...keys, 0], tabCount + 1);
1123
- // break;
1124
- // }
1111
+ let ymlString = '';
1112
+ for (const [key, property] of Object.entries(this.properties)) {
1113
+ if (property.type.endsWith('?') === false) {
1114
+ errorList.push({
1115
+ status: 400,
1116
+ code: code + '',
1117
+ description: `${key}:未入力エラー(未指定、null、空文字の場合にエラー)`
1118
+ });
1119
+ }
1120
+ // ymlString += `${space}- name: ${key}\n`;
1121
+ // ymlString += `${space} in: query\n`;
1122
+
1123
+ // const descJoin = `\n${space} `;
1124
+ // if (property.type === 'enum' || property.type === 'enum?') {
1125
+ // ymlString += `${space} description: |${property.description === undefined ? '' : `${descJoin}${property.description.replaceAll("\n", descJoin)}`}`;
1126
+ // ymlString += `${descJoin}enum list`;
1127
+ // ymlString += `${Object.entries(property.enums).map(([key, value]) => `${descJoin}- ${key}: ${value}`)}\n`;
1128
+ // } else if (property.description !== undefined) {
1129
+ // ymlString += `${space} description: |\n${space} ${property.description.replaceAll("\n", descJoin)}\n`;
1130
+ // }
1131
+ // ymlString += `${space} required: ${property.type.endsWith('?') ? 'false' : 'true'}\n`;
1132
+ // ymlString += `${space} schema:\n`;
1133
+ // ymlString += `${space} type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
1134
+ // if (property.type === 'enum' || property.type === 'enum?') {
1135
+ // ymlString += `${space} nullable: ${property.enumType.endsWith('?')}\n`;
1136
+ // ymlString += `${space} enum:\n`;
1137
+ // for (const type of Object.keys(property.enumType)) {
1138
+ // ymlString += `${space} - ${type}\n`;
1139
+ // }
1140
+ // if (property.enumType.endsWith('?')) {
1141
+ // ymlString += `${space} - null\n`;
1142
+ // }
1143
+ // }
1144
+ }
1145
+ }
1125
1146
 
1126
- // return ymlString;
1127
- // }
1147
+ return errorList;
1148
+ }
1128
1149
  }
1129
1150
 
1130
1151
  // Requestのheaderで定義されているIFをそのまま拝借
@@ -2,8 +2,16 @@ import { ValidateStringUtil } from "type-utils-n-daira";
2
2
  import StringUtil from "../Utils/StringUtil";
3
3
  import ReqResType, { PropertyType } from "./ReqResType";
4
4
 
5
+ export interface IError {
6
+ status: 400 | 401 | 404 | 409 | 422 | 500,
7
+ code: string;
8
+ description: string;
9
+ }
10
+
5
11
  export class ResponseType extends ReqResType {
6
12
 
13
+ protected readonly errorList: Array<IError> = [];
14
+
7
15
  /**
8
16
  * Property to store response data
9
17
  * レスポンスデータを格納するためのプロパティ
@@ -367,7 +375,7 @@ export class ResponseType extends ReqResType {
367
375
  * @returns {string} Swagger format response definition
368
376
  * Swagger形式のレスポンス定義
369
377
  */
370
- public createSwagger(): string {
378
+ public createSwagger(requestErrorList: Array<IError>): string {
371
379
  let ymlString = ` responses:
372
380
  '200':
373
381
  description: 成功事レスポンス
@@ -410,6 +418,42 @@ export class ResponseType extends ReqResType {
410
418
  }
411
419
  }
412
420
 
421
+ const errorList: Array<IError> = [...this.errorList, ...requestErrorList];
422
+ errorList.push({
423
+ status: 500,
424
+ code: '',
425
+ description: 'サーバー内部エラー(予期せぬエラー)'
426
+ })
427
+ for (const error of errorList) {
428
+ ymlString += `
429
+ '${error.code}':
430
+ description: 【エラー】${error.description}
431
+ content:
432
+ application/json:
433
+ schema:
434
+ type: object
435
+ properties:`;
436
+ if ([400, 404, 409, 422].includes(error.status)) {
437
+ ymlString += `
438
+ errorCode:
439
+ type: string
440
+ description: エラーコード
441
+ errorMessage:
442
+ type: string
443
+ description: エラーメッセージ`;
444
+ } else if (error.status === 401) {
445
+ ymlString += `
446
+ message:
447
+ type: string
448
+ description: Authentication expired. Please login again.`;
449
+ } else if (error.status === 500) {
450
+ ymlString += `
451
+ message:
452
+ type: string
453
+ description: Internal Server Error`;
454
+ }
455
+ }
456
+
413
457
  return ymlString;
414
458
  }
415
459