pg-mvc-service 1.0.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.
Files changed (56) hide show
  1. package/README.md +1 -0
  2. package/dist/PoolManager.js +57 -0
  3. package/dist/Service.js +257 -0
  4. package/dist/clients/AwsS3Client.js +249 -0
  5. package/dist/clients/Base64Client.js +153 -0
  6. package/dist/clients/EncryptClient.js +85 -0
  7. package/dist/clients/StringClient.js +13 -0
  8. package/dist/documents/Swagger.js +94 -0
  9. package/dist/exceptions/Exception.js +53 -0
  10. package/dist/index.js +16 -0
  11. package/dist/models/MigrateDatabase.js +138 -0
  12. package/dist/models/MigrateRollback.js +146 -0
  13. package/dist/models/MigrateTable.js +51 -0
  14. package/dist/models/SqlUtils/SelectExpression.js +92 -0
  15. package/dist/models/SqlUtils/ValidateValueUtil.js +250 -0
  16. package/dist/models/SqlUtils/WhereExpression.js +256 -0
  17. package/dist/models/TableDoc.js +353 -0
  18. package/dist/models/TableModel.js +636 -0
  19. package/dist/models/Type.js +2 -0
  20. package/dist/models/Utils/DateTimeUtil.js +134 -0
  21. package/dist/models/Utils/NumberUtil.js +28 -0
  22. package/dist/models/Utils/StringUtil.js +31 -0
  23. package/dist/models/ValidateClient.js +164 -0
  24. package/dist/models/index.js +14 -0
  25. package/dist/reqestResponse/ReqResType.js +196 -0
  26. package/dist/reqestResponse/RequestType.js +742 -0
  27. package/dist/reqestResponse/ResponseType.js +380 -0
  28. package/index.d.ts +306 -0
  29. package/package.json +36 -0
  30. package/src/PoolManager.ts +48 -0
  31. package/src/Service.ts +251 -0
  32. package/src/clients/AwsS3Client.ts +229 -0
  33. package/src/clients/Base64Client.ts +155 -0
  34. package/src/clients/EncryptClient.ts +100 -0
  35. package/src/clients/StringClient.ts +14 -0
  36. package/src/documents/Swagger.ts +111 -0
  37. package/src/exceptions/Exception.ts +54 -0
  38. package/src/index.ts +7 -0
  39. package/src/models/MigrateDatabase.ts +135 -0
  40. package/src/models/MigrateRollback.ts +151 -0
  41. package/src/models/MigrateTable.ts +56 -0
  42. package/src/models/SqlUtils/SelectExpression.ts +97 -0
  43. package/src/models/SqlUtils/ValidateValueUtil.ts +270 -0
  44. package/src/models/SqlUtils/WhereExpression.ts +286 -0
  45. package/src/models/TableDoc.ts +360 -0
  46. package/src/models/TableModel.ts +713 -0
  47. package/src/models/Type.ts +59 -0
  48. package/src/models/Utils/DateTimeUtil.ts +146 -0
  49. package/src/models/Utils/NumberUtil.ts +23 -0
  50. package/src/models/Utils/StringUtil.ts +33 -0
  51. package/src/models/ValidateClient.ts +182 -0
  52. package/src/models/index.ts +7 -0
  53. package/src/reqestResponse/ReqResType.ts +242 -0
  54. package/src/reqestResponse/RequestType.ts +851 -0
  55. package/src/reqestResponse/ResponseType.ts +418 -0
  56. package/tsconfig.json +14 -0
@@ -0,0 +1,742 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RequestType = void 0;
7
+ const ReqResType_1 = __importDefault(require("./ReqResType"));
8
+ const Exception_1 = require("../exceptions/Exception");
9
+ class RequestType extends ReqResType_1.default {
10
+ constructor() {
11
+ super(...arguments);
12
+ // *****************************************
13
+ // Input Error Message
14
+ // Please make changes to error messages in the subclass
15
+ // エラー文言
16
+ // エラーメッセージの変更はサブクラスで行ってください
17
+ // *****************************************
18
+ this.INVALID_PATH_PARAM_UUID_ERROR_MESSAGE = 'The {property} in the URL must be a UUID. ({value})';
19
+ this.REQUIRED_ERROR_MESSAGE = '{property} is required.';
20
+ this.UNNECESSARY_INPUT_ERROR_MESSAGE = "{property} is unnecessary input. ";
21
+ this.INVALID_OBJECT_ERROR_MESSAGE = '{property} must be of type Object. ({value})';
22
+ this.INVALID_ARRAY_ERROR_MESSAGE = '{property} must be of type Array. ({value})';
23
+ this.INVALID_NUMBER_ERROR_MESSAGE = '{property} must be of type number. ({value})';
24
+ this.INVALID_BOOL_ERROR_MESSAGE = '{property} must be of type bool or a string with true, false, or a number with 0, 1. ({value})';
25
+ this.INVALID_STRING_ERROR_MESSAGE = '{property} must be of type string. ({value})';
26
+ this.INVALID_UUID_ERROR_MESSAGE = '{property} must be a UUID. ({value})';
27
+ this.INVALID_MAIL_ERROR_MESSAGE = '{property} must be an email. ({value})';
28
+ this.INVALID_HTTPS_ERROR_MESSAGE = '{property} must be an https or http URL. ({value})';
29
+ this.INVALID_DATE_ERROR_MESSAGE = '{property} must be a string in "YYYY-MM-DD" format and a valid date. ({value})';
30
+ this.INVALID_TIME_ERROR_MESSAGE = '{property} must be a string in "hh:mi" format and a valid time. ({value})';
31
+ this.INVALID_DATETIME_ERROR_MESSAGE = '{property} must be a string in "YYYY-MM-DD hh:mi:ss" or "YYYY-MM-DDThh:mi:ss" format and a valid date and time. ({value})';
32
+ this.INVALID_BASE64_ERROR_MESSAGE = '{property} must be in Base64 format. ({value})';
33
+ this.INVALID_ENUM_ERROR_MESSAGE = '{property} must be in {enums}. ({value})';
34
+ }
35
+ get Params() {
36
+ var _a;
37
+ if (this.params === undefined) {
38
+ throw new Error("Request data must be set using setRequest method before accessing Req.");
39
+ }
40
+ return (_a = this.params) !== null && _a !== void 0 ? _a : {};
41
+ }
42
+ get Data() {
43
+ var _a;
44
+ if (this.data === undefined) {
45
+ throw new Error("Request data must be set using setRequest method before accessing Req.");
46
+ }
47
+ return (_a = this.data) !== null && _a !== void 0 ? _a : {};
48
+ }
49
+ get Headers() {
50
+ if (this.headers === undefined) {
51
+ throw new Error("Request data must be set using setRequest method before accessing Req.");
52
+ }
53
+ return this.headers;
54
+ }
55
+ get RemoteAddress() { return this.remoteAddress; }
56
+ get Authorization() {
57
+ var _a;
58
+ const authorization = (_a = this.Headers['authorization']) !== null && _a !== void 0 ? _a : '';
59
+ if (authorization.startsWith('Bearer ') === false) {
60
+ return null;
61
+ }
62
+ return authorization.replace(/^Bearer\s/, '');
63
+ }
64
+ setRequest(request) {
65
+ var _a, _b, _c;
66
+ this.createBody(request);
67
+ if (request.params !== undefined) {
68
+ for (const [key, value] of Object.entries(request.params)) {
69
+ if (key.includes("id") || key.includes("Id")) {
70
+ if (this.isUUID(value) === false) {
71
+ throw new Exception_1.InputErrorException("990", this.ErrorMessage("990", [key], value));
72
+ }
73
+ }
74
+ }
75
+ }
76
+ this.params = (_a = request.params) !== null && _a !== void 0 ? _a : {};
77
+ this.headers = (_b = request.headers) !== null && _b !== void 0 ? _b : {};
78
+ this.remoteAddress = (_c = request.socket) === null || _c === void 0 ? void 0 : _c.remoteAddress;
79
+ }
80
+ /**
81
+ * Generates an error message based on the provided code, keys, and value.
82
+ * 指定されたコード、キー、および値に基づいてエラーメッセージを生成します。
83
+ * @param {string} code - The error code. エラーコード
84
+ * @param {Array<string | number>} keys - The keys indicating the property path. プロパティパスを示すキー
85
+ * @param {any} value - The value that caused the error. エラーを引き起こした値
86
+ * @returns {string} The generated error message. 生成されたエラーメッセージ
87
+ */
88
+ ErrorMessage(code, keys, value) {
89
+ const list = {
90
+ "990": this.INVALID_PATH_PARAM_UUID_ERROR_MESSAGE,
91
+ "000": this.REQUIRED_ERROR_MESSAGE,
92
+ "001": this.REQUIRED_ERROR_MESSAGE,
93
+ "101": this.REQUIRED_ERROR_MESSAGE,
94
+ "301": this.REQUIRED_ERROR_MESSAGE,
95
+ "002": this.INVALID_OBJECT_ERROR_MESSAGE,
96
+ "102": this.INVALID_OBJECT_ERROR_MESSAGE,
97
+ "003": this.INVALID_ARRAY_ERROR_MESSAGE,
98
+ "103": this.INVALID_ARRAY_ERROR_MESSAGE,
99
+ "004": this.UNNECESSARY_INPUT_ERROR_MESSAGE,
100
+ "104": this.UNNECESSARY_INPUT_ERROR_MESSAGE,
101
+ "201": this.INVALID_NUMBER_ERROR_MESSAGE,
102
+ "211": this.INVALID_BOOL_ERROR_MESSAGE,
103
+ "212": this.INVALID_BOOL_ERROR_MESSAGE,
104
+ "213": this.INVALID_BOOL_ERROR_MESSAGE,
105
+ "221": this.INVALID_STRING_ERROR_MESSAGE,
106
+ "231": this.INVALID_UUID_ERROR_MESSAGE,
107
+ "241": this.INVALID_MAIL_ERROR_MESSAGE,
108
+ "251": this.INVALID_DATE_ERROR_MESSAGE,
109
+ "252": this.INVALID_DATE_ERROR_MESSAGE,
110
+ "261": this.INVALID_TIME_ERROR_MESSAGE,
111
+ "271": this.INVALID_DATETIME_ERROR_MESSAGE,
112
+ "272": this.INVALID_DATETIME_ERROR_MESSAGE,
113
+ "281": this.INVALID_HTTPS_ERROR_MESSAGE,
114
+ "291": this.INVALID_BASE64_ERROR_MESSAGE,
115
+ "411": this.INVALID_NUMBER_ERROR_MESSAGE,
116
+ "421": this.INVALID_BOOL_ERROR_MESSAGE,
117
+ "422": this.INVALID_BOOL_ERROR_MESSAGE,
118
+ "423": this.INVALID_BOOL_ERROR_MESSAGE,
119
+ "431": this.INVALID_STRING_ERROR_MESSAGE,
120
+ };
121
+ let errorMessage = '';
122
+ if (code === "401" || code === "402") {
123
+ const property = this.getProperty(keys);
124
+ errorMessage = this.INVALID_ENUM_ERROR_MESSAGE.replace('{enums}', Object.keys(property.enums).join(','));
125
+ }
126
+ else {
127
+ errorMessage = list[code];
128
+ }
129
+ errorMessage = errorMessage.replace("{property}", keys.join('.')).replace("{value}", value);
130
+ return errorMessage;
131
+ }
132
+ /**
133
+ * Sets the values of the request body to the class properties.
134
+ * リクエストボディの値をクラスのプロパティにセットします。
135
+ *
136
+ * Note: This method is implemented as a separate method rather than in the constructor.
137
+ * This is because if executed in the constructor, the properties of the inheriting class
138
+ * are not yet initialized, and the values cannot be set correctly.
139
+ * 注意: このメソッドはコンストラクタではなく別メソッドとして実装されています。
140
+ * これは、コンストラクタ内で実行すると継承先のクラスのプロパティが
141
+ * まだ初期化されていないため、正しく値をセットできないためです。
142
+ *
143
+ * @param {Object} body - Request body object, リクエストボディオブジェクト
144
+ * @throws {InputErrorException} Thrown when the input value is invalid, 入力値が不正な場合にスローされます
145
+ */
146
+ createBody(request) {
147
+ if (request.method === 'GET' || request.method === 'DELETE') {
148
+ this.data = request.query;
149
+ }
150
+ else {
151
+ this.data = request.body;
152
+ }
153
+ if (this.data === undefined) {
154
+ // ここは基本通ることはないと思いますが...
155
+ throw new Error(`リクエストBodyがundefinedです。`);
156
+ }
157
+ for (const key of Object.keys(this.properties)) {
158
+ // NULLチェック
159
+ if (key in this.data === false || this.data[key] === null || this.data[key] === "") {
160
+ if (this.properties[key].type === 'array' && ['GET', 'DELETE'].includes(request.method)) {
161
+ // GET,DELETEメソッドの場合、?array=1&array=2で配列となるが、
162
+ // ?array=1のみで終わる場合は配列にならないため、直接配列にしている
163
+ // この処理で空文字やnullが入った場合の対処をここで行う
164
+ const itemProperty = this.properties[key].properties;
165
+ if (itemProperty.type.endsWith('?')) {
166
+ const tempValue = this.data[key];
167
+ this.data[key] = [];
168
+ if (tempValue !== undefined) {
169
+ if (itemProperty.type === 'string?') {
170
+ this.data[key][0] = tempValue;
171
+ }
172
+ else {
173
+ this.data[key][0] = null;
174
+ }
175
+ }
176
+ continue;
177
+ }
178
+ else {
179
+ throw new Exception_1.InputErrorException("000", this.ErrorMessage("000", [key, 0], ""));
180
+ }
181
+ }
182
+ else {
183
+ if (this.properties[key].type.endsWith('?')) {
184
+ this.changeBody([key], null);
185
+ continue;
186
+ }
187
+ else {
188
+ throw new Exception_1.InputErrorException("001", this.ErrorMessage("001", [key], ""));
189
+ }
190
+ }
191
+ }
192
+ const value = this.data[key];
193
+ switch (this.properties[key].type) {
194
+ case 'object':
195
+ case 'object?':
196
+ if (typeof value === 'object') {
197
+ this.setObject([key], value);
198
+ }
199
+ else {
200
+ throw new Exception_1.InputErrorException("002", this.ErrorMessage("002", [key], value));
201
+ }
202
+ break;
203
+ case 'array':
204
+ case 'array?':
205
+ if (Array.isArray(value)) {
206
+ this.setArray([key], value);
207
+ }
208
+ else {
209
+ if (request.method === 'GET' || request.method === 'DELETE') {
210
+ // GET,DELETEメソッドの場合、?array=1&array=2で配列となるが、
211
+ // ?array=1のみで終わる場合は配列にならないため、直接配列にしている
212
+ this.data[key] = [this.convertValue(this.properties[key].properties.type, value, [key, 0])];
213
+ }
214
+ else {
215
+ throw new Exception_1.InputErrorException("003", this.ErrorMessage("003", [key], value));
216
+ }
217
+ }
218
+ break;
219
+ case 'enum':
220
+ case 'enum?':
221
+ this.setEnum([key], value);
222
+ break;
223
+ default:
224
+ this.convertInput([key], value);
225
+ break;
226
+ }
227
+ }
228
+ // 不要項目チェック
229
+ for (const [key, value] of Object.entries(this.data)) {
230
+ if (key in this.properties === false) {
231
+ throw new Exception_1.InputErrorException("004", this.ErrorMessage("004", [key], value));
232
+ }
233
+ }
234
+ }
235
+ /**
236
+ * Sets the value for an enum type based on the specified keys.
237
+ * 指定されたキーに基づいて列挙型の値を設定します。
238
+ * @param {Array<string | number>} keys - Path to the target (array of strings or index numbers)
239
+ * 処理対象のパス(文字列またはインデックス番号の配列)
240
+ * @param {any} value - Value to be set
241
+ * 設定する値
242
+ * @throws {InputErrorException} Thrown when the type does not match
243
+ * 型が一致しない場合にスローされます
244
+ */
245
+ setEnum(keys, value) {
246
+ const property = this.getProperty(keys);
247
+ const enumType = property.enumType;
248
+ if (value === undefined || value === null || (typeof value === 'string' && value === '')) {
249
+ if (enumType.endsWith('?')) {
250
+ this.changeBody(keys, null);
251
+ }
252
+ else {
253
+ throw new Exception_1.InputErrorException("401", this.ErrorMessage("401", keys, value));
254
+ }
255
+ }
256
+ switch (enumType) {
257
+ case 'number':
258
+ case 'number?':
259
+ if (this.isNumber(value) === false) {
260
+ throw new Exception_1.InputErrorException("411", this.ErrorMessage("411", keys, value));
261
+ }
262
+ value = Number(value);
263
+ break;
264
+ case 'string':
265
+ case 'string?':
266
+ switch (typeof value) {
267
+ case 'number':
268
+ value = value.toString();
269
+ break;
270
+ case 'string':
271
+ value = value;
272
+ break;
273
+ default:
274
+ throw new Exception_1.InputErrorException("431", this.ErrorMessage("431", keys, value));
275
+ }
276
+ break;
277
+ }
278
+ if (Object.keys(property.enums).includes(value.toString()) === false) {
279
+ throw new Exception_1.InputErrorException("402", this.ErrorMessage("402", keys, value));
280
+ }
281
+ this.changeBody(keys, value);
282
+ }
283
+ /**
284
+ * Recursively processes array type values.
285
+ * Validates each element of the array and converts it to the appropriate type.
286
+ * 配列型の値を再帰的に処理します。
287
+ * 配列の各要素を検証し、適切な型に変換します。
288
+ *
289
+ * @param {Array<string | number>} keys - Current processing path (array of strings or index numbers)
290
+ * 現在の処理パス(文字列またはインデックス番号の配列)
291
+ * @param {any[]} values - Array to be processed
292
+ * 処理対象の配列
293
+ * @throws {InputErrorException} Thrown when the type of an array element is invalid
294
+ * 配列要素の型が不正な場合にスローされます
295
+ */
296
+ setArray(keys, values) {
297
+ const property = this.getProperty(keys);
298
+ for (let i = 0; i < values.length; i++) {
299
+ // NULL Check
300
+ if (values[i] === undefined || values[i] === null || (property.properties.type.replace("?", "") !== "string" && values[i] === "")) {
301
+ if (property.properties.type.endsWith('?')) {
302
+ this.changeBody([...keys, i], values[i] === undefined ? undefined : null);
303
+ continue;
304
+ }
305
+ else {
306
+ throw new Exception_1.InputErrorException("301", this.ErrorMessage("301", [...keys, i], ""));
307
+ }
308
+ }
309
+ switch (property.properties.type) {
310
+ case 'object':
311
+ case 'object?':
312
+ this.setObject([...keys, i], values[i]);
313
+ break;
314
+ case 'array':
315
+ case 'array?':
316
+ this.setArray([...keys, i], values[i]);
317
+ break;
318
+ case 'enum':
319
+ case 'enum?':
320
+ for (const value of values) {
321
+ this.setEnum([...keys, i], value);
322
+ }
323
+ break;
324
+ default:
325
+ this.convertInput([...keys, i], values[i]);
326
+ break;
327
+ }
328
+ }
329
+ }
330
+ /**
331
+ * Retrieve the property definition corresponding to the specified key path.
332
+ * 指定されたキーパスに対応するプロパティ定義を取得します。
333
+ * @param {Array<string | number>} keys - Access path to the property (array of strings or index numbers)
334
+ * プロパティへのアクセスパス(文字列またはインデックス番号の配列)
335
+ * @returns {BaseType} Property definition object
336
+ * プロパティ定義オブジェクト
337
+ */
338
+ getProperty(keys) {
339
+ let property = this.properties;
340
+ for (let i = 0; i < keys.length; i++) {
341
+ const key = keys[i];
342
+ if (typeof key === 'number') {
343
+ property = property.properties;
344
+ continue;
345
+ }
346
+ if (i === 0) {
347
+ property = property[key];
348
+ }
349
+ else {
350
+ property = property.properties[key];
351
+ }
352
+ }
353
+ return property;
354
+ }
355
+ /**
356
+ * Set the value of the request body to the specified path.
357
+ * Automatically create intermediate objects or arrays as needed.
358
+ * リクエストボディの値を指定されたパスに設定します。
359
+ * 必要に応じて中間のオブジェクトや配列を自動的に作成します。
360
+ * @param {Array<string | number>} keys - Path to the destination (array of strings or index numbers)
361
+ * 設定先へのパス(文字列またはインデックス番号の配列)
362
+ * @param {any} value - Value to be set
363
+ * 設定する値
364
+ */
365
+ changeBody(keys, value) {
366
+ let body = this.data;
367
+ // 最後のキーを除いて順番にオブジェクトを辿る
368
+ for (let i = 0; i < keys.length - 1; i++) {
369
+ const key = keys[i];
370
+ const nextKey = keys[i + 1];
371
+ // 次のキーが数値型の場合は配列、そうでない場合はオブジェクトとして初期化
372
+ if (!(key in body)) {
373
+ body[key] = typeof nextKey === 'number' ? [] : {};
374
+ }
375
+ body = body[key];
376
+ }
377
+ // 最後のキーに対して値を設定
378
+ body[keys[keys.length - 1]] = value;
379
+ }
380
+ /**
381
+ * Process object type values recursively.
382
+ * Validate object properties and convert them to appropriate types.
383
+ * オブジェクト型の値を再帰的に処理します。
384
+ * オブジェクトのプロパティを検証し、適切な型に変換します。
385
+ * @param {Array<string | number>} keys - Current processing path (array of strings or index numbers)
386
+ * 現在の処理パス(文字列またはインデックス番号の配列)
387
+ * @param {object} values - Object to be processed
388
+ * 処理対象のオブジェクト
389
+ * @throws {InputErrorException} Thrown when the property type is invalid
390
+ * プロパティの型が不正な場合にスローされます
391
+ */
392
+ setObject(keys, values) {
393
+ const property = this.getProperty(keys);
394
+ for (const key of Object.keys(property.properties)) {
395
+ // NULL Check
396
+ if (key in values === false || values[key] === null || values[key] === "") {
397
+ if (property.properties[key].type.endsWith('?')) {
398
+ this.changeBody([...keys, key], null);
399
+ continue;
400
+ }
401
+ else {
402
+ throw new Exception_1.InputErrorException("101", this.ErrorMessage("101", [...keys, key], ""));
403
+ }
404
+ }
405
+ const value = values[key];
406
+ switch (property.properties[key].type) {
407
+ case 'object':
408
+ case 'object?':
409
+ if (typeof value === 'object') {
410
+ this.setObject([...keys, key], value);
411
+ }
412
+ else {
413
+ throw new Exception_1.InputErrorException("102", this.ErrorMessage("102", [...keys, key], value));
414
+ }
415
+ break;
416
+ case 'array':
417
+ case 'array?':
418
+ if (Array.isArray(value)) {
419
+ this.setArray([...keys, key], value);
420
+ }
421
+ else {
422
+ throw new Exception_1.InputErrorException("103", this.ErrorMessage("103", [...keys, key], value));
423
+ }
424
+ break;
425
+ case 'enum':
426
+ case 'enum?':
427
+ this.setEnum([...keys, key], value);
428
+ break;
429
+ default:
430
+ this.convertInput([...keys, key], value);
431
+ break;
432
+ }
433
+ }
434
+ // unnecessary input check
435
+ for (const [key, value] of Object.entries(values)) {
436
+ if (key in property.properties === false) {
437
+ throw new Exception_1.InputErrorException("104", this.ErrorMessage("104", [...keys, key], value));
438
+ }
439
+ }
440
+ }
441
+ /**
442
+ * Convert the input value based on the specified type.
443
+ * Throws an exception if type conversion fails.
444
+ * 指定された型に基づいて入力値を変換します。
445
+ * 型変換に失敗した場合は例外をスローします。
446
+ *
447
+ * @param {string} type - The type to convert to (e.g., 'number', 'boolean', 'string', 'date', 'time', 'datetime')
448
+ * 変換する型(例: 'number', 'boolean', 'string', 'date', 'time', 'datetime')
449
+ * @param {any} value - The value to convert
450
+ * 変換する値
451
+ * @param {Array<string | number>} keys - The path to the target (array of strings or index numbers)
452
+ * 処理対象のパス(文字列またはインデックス番号の配列)
453
+ * @returns {any} The converted value, 変換された値
454
+ * @throws {InputErrorException} Thrown if type conversion fails, 型変換に失敗した場合にスローされます
455
+ */
456
+ convertValue(type, value, keys) {
457
+ switch (type) {
458
+ case 'number':
459
+ case 'number?':
460
+ if (this.isNumber(value) === false) {
461
+ throw new Exception_1.InputErrorException("201", this.ErrorMessage("201", keys, value));
462
+ }
463
+ return Number(value);
464
+ case 'boolean':
465
+ case 'boolean?':
466
+ switch (typeof value) {
467
+ case 'boolean':
468
+ return value;
469
+ case 'number':
470
+ if (value !== 0 && value !== 1) {
471
+ throw new Exception_1.InputErrorException("211", this.ErrorMessage("211", keys, value));
472
+ }
473
+ return value === 1 ? true : false;
474
+ case 'string':
475
+ if (value !== 'true' && value !== 'false') {
476
+ throw new Exception_1.InputErrorException("212", this.ErrorMessage("212", keys, value));
477
+ }
478
+ return value === 'true' ? true : false;
479
+ default:
480
+ throw new Exception_1.InputErrorException("213", this.ErrorMessage("213", keys, value));
481
+ }
482
+ case 'string':
483
+ case 'string?':
484
+ switch (typeof value) {
485
+ case 'number':
486
+ return value.toString();
487
+ case 'string':
488
+ return value;
489
+ default:
490
+ throw new Exception_1.InputErrorException("221", this.ErrorMessage("221", keys, value));
491
+ }
492
+ case 'uuid':
493
+ case 'uuid?':
494
+ if (this.isUUID(value)) {
495
+ return value;
496
+ }
497
+ throw new Exception_1.InputErrorException("231", this.ErrorMessage("231", keys, value));
498
+ case 'mail':
499
+ case 'mail?':
500
+ if (this.isMail(value)) {
501
+ return value;
502
+ }
503
+ throw new Exception_1.InputErrorException("241", this.ErrorMessage("241", keys, value));
504
+ case 'date':
505
+ case 'date?':
506
+ if (this.isYYYYMMDD(value) === false) {
507
+ throw new Exception_1.InputErrorException("251", this.ErrorMessage("251", keys, value));
508
+ }
509
+ if (this.isErrorDateTime(value)) {
510
+ throw new Exception_1.InputErrorException("252", this.ErrorMessage("252", keys, value));
511
+ }
512
+ return value;
513
+ case 'time':
514
+ case 'time?':
515
+ if (this.isHHMM(value)) {
516
+ return `${value}`;
517
+ }
518
+ throw new Exception_1.InputErrorException("261", this.ErrorMessage("261", keys, value));
519
+ case 'datetime':
520
+ case 'datetime?':
521
+ if (this.isYYYYMMDDhhmi(value)) {
522
+ value += ':00';
523
+ }
524
+ if (this.isYYYYMMDDhhmiss(value) === false) {
525
+ throw new Exception_1.InputErrorException("271", this.ErrorMessage("271", keys, value));
526
+ }
527
+ if (this.isErrorDateTime(value)) {
528
+ throw new Exception_1.InputErrorException("272", this.ErrorMessage("272", keys, value));
529
+ }
530
+ return value.replace('T', ' ');
531
+ case 'https':
532
+ case 'https?':
533
+ if (this.isHttps(value)) {
534
+ return value;
535
+ }
536
+ throw new Exception_1.InputErrorException("281", this.ErrorMessage("281", keys, value));
537
+ case 'base64':
538
+ case 'base64?':
539
+ if (this.isBase64(value)) {
540
+ return value;
541
+ }
542
+ throw new Exception_1.InputErrorException("291", this.ErrorMessage("291", keys, value));
543
+ }
544
+ return value;
545
+ }
546
+ /**
547
+ * Convert the input value to the specified type.
548
+ * Throws an exception if type conversion fails.
549
+ * 入力値を指定された型に変換します。
550
+ * 型変換に失敗した場合は例外をスローします。
551
+ * @param {Array<string | number>} keys - Path to the target (array of strings or index numbers)
552
+ * 処理対象のパス(文字列またはインデックス番号の配列)
553
+ * @param {any} value - Value to be converted
554
+ * 変換する値
555
+ * @throws {InputErrorException} Thrown when type conversion fails
556
+ * 型変換に失敗した場合にスローされます
557
+ */
558
+ convertInput(keys, value) {
559
+ const property = this.getProperty(keys);
560
+ this.changeBody(keys, this.convertValue(property.type, value, keys));
561
+ }
562
+ // ****************************************************************************
563
+ // for create swagger
564
+ // ****************************************************************************
565
+ /**
566
+ * Generates a Swagger YAML definition from the request body.
567
+ * リクエストボディからSwaggerのYAML定義を生成します。
568
+ * @returns {string} Swagger format YAML definition
569
+ * Swagger形式のYAML定義
570
+ */
571
+ createSwagger(method) {
572
+ if (method === 'GET' || method === 'DELETE') {
573
+ const tabCount = 4;
574
+ const space = ' '.repeat(tabCount);
575
+ let ymlString = '';
576
+ for (const [key, property] of Object.entries(this.properties)) {
577
+ ymlString += `${space}- name: ${key}\n`;
578
+ ymlString += `${space} in: query\n`;
579
+ const descJoin = `\n${space} `;
580
+ if (property.type === 'enum' || property.type === 'enum?') {
581
+ ymlString += `${space} description: |${property.description === undefined ? '' : `${descJoin}${property.description.replaceAll("\n", descJoin)}`}`;
582
+ ymlString += `${descJoin}enum list`;
583
+ ymlString += `${Object.entries(property.enums).map(([key, value]) => `${descJoin}- ${key}: ${value}`)}\n`;
584
+ }
585
+ else if (property.description !== undefined) {
586
+ ymlString += `${space} description: |\n${space} ${property.description.replaceAll("\n", descJoin)}\n`;
587
+ }
588
+ ymlString += `${space} required: ${property.type.endsWith('?') ? 'false' : 'true'}\n`;
589
+ ymlString += `${space} schema:\n`;
590
+ ymlString += `${space} type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
591
+ if (property.type === 'enum' || property.type === 'enum?') {
592
+ ymlString += `${space} nullable: ${property.enumType.endsWith('?')}\n`;
593
+ ymlString += `${space} enum:\n`;
594
+ for (const type of Object.keys(property.enumType)) {
595
+ ymlString += `${space} - ${type}\n`;
596
+ }
597
+ if (property.enumType.endsWith('?')) {
598
+ ymlString += `${space} - null\n`;
599
+ }
600
+ }
601
+ }
602
+ return ymlString;
603
+ }
604
+ else {
605
+ const tabCount = 8;
606
+ const space = ' '.repeat(tabCount);
607
+ let componentYml = '\n';
608
+ let requiredList = [];
609
+ for (const [key, property] of Object.entries(this.properties)) {
610
+ if (property.type.endsWith('?') === false) {
611
+ requiredList.push(key);
612
+ }
613
+ componentYml += `${space}${key}:\n`;
614
+ componentYml += ` ${space}type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
615
+ const descJoin = `\n${space} `;
616
+ if (property.type === 'enum' || property.type === 'enum?') {
617
+ componentYml += `${space} description: |${property.description === undefined ? '' : `${descJoin}${property.description.replaceAll("\n", descJoin)}`}`;
618
+ componentYml += `${descJoin}enum list`;
619
+ componentYml += `${Object.entries(property.enums).map(([key, value]) => `${descJoin}- ${key}: ${value}`)}\n`;
620
+ }
621
+ else if (property.description !== undefined) {
622
+ componentYml += `${space} description: |${descJoin}${property.description.replaceAll("\n", descJoin)}\n`;
623
+ }
624
+ if (property.type === 'enum' || property.type === 'enum?') {
625
+ componentYml += `${space} nullable: ${property.enumType.endsWith('?')}\n`;
626
+ componentYml += `${space} enum:\n`;
627
+ for (const type of Object.keys(property.enums)) {
628
+ componentYml += `${space} - ${type}\n`;
629
+ }
630
+ if (property.enumType.endsWith('?')) {
631
+ componentYml += `${space} - null\n`;
632
+ }
633
+ }
634
+ switch (property.type) {
635
+ case 'object':
636
+ case 'object?':
637
+ componentYml += this.makeSwaggerProperyFromObject([key], tabCount + 1);
638
+ break;
639
+ case 'array':
640
+ case 'array?':
641
+ componentYml += this.makeSwaggerPropertyFromArray([key], tabCount + 1);
642
+ break;
643
+ }
644
+ }
645
+ if (requiredList.length > 0) {
646
+ componentYml += ` required:\n`;
647
+ componentYml += ` - ${requiredList.join(`\n - `)}\n`;
648
+ }
649
+ let ymlString = ` requestBody:\n`;
650
+ ymlString += ` content:\n`;
651
+ ymlString += ` application/json:\n`;
652
+ ymlString += ` schema:\n`;
653
+ ymlString += ` type: object\n`;
654
+ ymlString += ` properties:${componentYml}`;
655
+ ymlString += ` application/x-www-form-urlencoded:\n`;
656
+ ymlString += ` schema:\n`;
657
+ ymlString += ` type: object\n`;
658
+ ymlString += ` properties:${componentYml}`;
659
+ return ymlString;
660
+ }
661
+ }
662
+ /**
663
+ * Generates Swagger properties from object type properties
664
+ * オブジェクト型のプロパティからSwaggerのプロパティを生成
665
+ * @param {Array.<string|number>} keys - Path to the properties
666
+ * プロパティへのパス
667
+ * @returns {string} Swagger format property definition
668
+ * Swagger形式のプロパティ定義
669
+ */
670
+ makeSwaggerProperyFromObject(keys, tabCount) {
671
+ var _a;
672
+ const space = ' '.repeat(tabCount);
673
+ let ymlString = `${space}properties:\n`;
674
+ const properties = this.getProperty(keys).properties;
675
+ for (const key of Object.keys(properties)) {
676
+ const property = properties[key];
677
+ ymlString += `${space} ${key}:\n`;
678
+ ymlString += `${space} type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
679
+ const descJoin = `\n${space} `;
680
+ if (property.type === 'enum' || property.type === 'enum?') {
681
+ ymlString += `${space} description: |${property.description === undefined ? '' : `${descJoin}${property.description.replaceAll('\n', descJoin)}`}`;
682
+ ymlString += `${descJoin}enum list`;
683
+ ymlString += `${Object.entries(property.enums).map(([key, value]) => `${descJoin}- ${key}: ${value}`)}\n`;
684
+ }
685
+ else if (((_a = property.description) !== null && _a !== void 0 ? _a : '') !== '') {
686
+ ymlString += `${space} description: |${descJoin}${property.description.replaceAll('\n', descJoin)}\n`;
687
+ }
688
+ if (property.type === 'enum' || property.type === 'enum?') {
689
+ ymlString += `${space} nullable: ${property.enumType.endsWith('?')}\n`;
690
+ ymlString += `${space} enum:\n`;
691
+ for (const type of Object.keys(property.enumType)) {
692
+ ymlString += `${space} - ${type}\n`;
693
+ }
694
+ if (property.enumType.endsWith('?')) {
695
+ ymlString += `${space} - null\n`;
696
+ }
697
+ }
698
+ switch (property.type) {
699
+ case 'object':
700
+ case 'object?':
701
+ ymlString += this.makeSwaggerProperyFromObject([...keys, key], tabCount + 2);
702
+ break;
703
+ case 'array':
704
+ case 'array?':
705
+ ymlString += this.makeSwaggerPropertyFromArray([...keys, key], tabCount + 2);
706
+ break;
707
+ }
708
+ }
709
+ return ymlString;
710
+ }
711
+ /**
712
+ * Generates Swagger properties from array type properties
713
+ * 配列型のプロパティからSwaggerのプロパティを生成
714
+ * @param {Array.<string|number>} keys - Path to the properties
715
+ * プロパティへのパス
716
+ * @returns {string} Swagger format property definition
717
+ * Swagger形式のプロパティ定義
718
+ */
719
+ makeSwaggerPropertyFromArray(keys, tabCount) {
720
+ var _a;
721
+ const property = this.getProperty(keys).properties;
722
+ const space = ' '.repeat(tabCount);
723
+ let ymlString = `${space}items:\n`;
724
+ ymlString += `${space} type: ${this.replaceFromPropertyTypeToSwagger(property)}\n`;
725
+ if (((_a = property.description) !== null && _a !== void 0 ? _a : '') !== '') {
726
+ const descJoin = `\n${space} `;
727
+ ymlString += `${space} description: |${descJoin}${property.description.replaceAll('\n', descJoin)}\n`;
728
+ }
729
+ switch (property.type) {
730
+ case 'object':
731
+ case 'object?':
732
+ ymlString += this.makeSwaggerProperyFromObject([...keys, 0], tabCount + 1);
733
+ break;
734
+ case 'array':
735
+ case 'array?':
736
+ ymlString += this.makeSwaggerPropertyFromArray([...keys, 0], tabCount + 1);
737
+ break;
738
+ }
739
+ return ymlString;
740
+ }
741
+ }
742
+ exports.RequestType = RequestType;