pg-mvc-service 2.0.2 → 2.0.4

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.
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Base64Client = void 0;
16
16
  const pdf_lib_1 = require("pdf-lib");
17
17
  const sharp_1 = __importDefault(require("sharp"));
18
+ const type_utils_n_daira_1 = require("type-utils-n-daira");
18
19
  class Base64Client {
19
20
  constructor() { }
20
21
  // public encode(text: string): string {
@@ -150,5 +151,62 @@ class Base64Client {
150
151
  return Buffer.from(pdfBytes);
151
152
  });
152
153
  }
154
+ static isJpeg(value) {
155
+ if (type_utils_n_daira_1.ValidateStringUtil.isBase64(value) === false) {
156
+ return false;
157
+ }
158
+ if (value.startsWith('data:')) {
159
+ if (value.startsWith('data:image/jpeg,') === false && value.startsWith('data:image/jpg,') === false) {
160
+ return false;
161
+ }
162
+ const valueParts = value.split(',');
163
+ if (valueParts.length !== 2) {
164
+ return false;
165
+ }
166
+ return valueParts[1].startsWith(this.PREFIX_JPEG_DATA);
167
+ }
168
+ return value.startsWith(this.PREFIX_JPEG_DATA);
169
+ }
170
+ static isPng(value) {
171
+ if (type_utils_n_daira_1.ValidateStringUtil.isBase64(value) === false) {
172
+ return false;
173
+ }
174
+ if (value.startsWith('data:')) {
175
+ if (value.startsWith('data:image/png,') === false) {
176
+ return false;
177
+ }
178
+ const valueParts = value.split(',');
179
+ if (valueParts.length !== 2) {
180
+ return false;
181
+ }
182
+ return valueParts[1].startsWith(this.PREFIX_PNG_DATA);
183
+ }
184
+ return value.startsWith(this.PREFIX_PNG_DATA);
185
+ }
186
+ static tryConvertToPng(base64Value) {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ if (type_utils_n_daira_1.ValidateStringUtil.isBase64(base64Value) === false) {
189
+ return false;
190
+ }
191
+ const base64Data = base64Value.startsWith('data:') ? base64Value.split(',')[1] : base64Value;
192
+ if (this.isPng(base64Data)) {
193
+ return base64Data;
194
+ }
195
+ else if (this.isJpeg(base64Data)) {
196
+ const buffer = Buffer.from(base64Data, 'base64');
197
+ try {
198
+ const pngBuffer = yield (0, sharp_1.default)(buffer)
199
+ .ensureAlpha().png().toBuffer();
200
+ return pngBuffer.toString('base64');
201
+ }
202
+ catch (e) {
203
+ return false;
204
+ }
205
+ }
206
+ return false;
207
+ });
208
+ }
153
209
  }
154
210
  exports.Base64Client = Base64Client;
211
+ Base64Client.PREFIX_JPEG_DATA = '/9j/';
212
+ Base64Client.PREFIX_PNG_DATA = 'iVBORw0KGgo';
@@ -339,7 +339,7 @@ class TableModel {
339
339
  }
340
340
  throw new Exception_1.UnprocessableException(code, message);
341
341
  }
342
- validateOptions(options, isInsert) {
342
+ validateOptions(options, isInsert, pkOrId) {
343
343
  return __awaiter(this, void 0, void 0, function* () {
344
344
  if (Object.keys(options).length === 0) {
345
345
  throw new Error('At least one key-value pair is required in options.');
@@ -432,7 +432,7 @@ class TableModel {
432
432
  update(pkOrId, options) {
433
433
  return __awaiter(this, void 0, void 0, function* () {
434
434
  var _a;
435
- yield this.validateOptions(options, false);
435
+ yield this.validateOptions(options, false, pkOrId);
436
436
  const updateSetQuery = UpdateExpression_1.default.createUpdateSet(this, options);
437
437
  let whereQuery;
438
438
  if (typeof pkOrId === 'string' || typeof pkOrId === 'number' || typeof pkOrId === 'boolean') {
@@ -114,55 +114,6 @@ class ReqResType {
114
114
  return false;
115
115
  }
116
116
  }
117
- /**
118
- * 値がメールアドレス形式であるかどうかを検証します
119
- * Validates if the given value is in the format of an email address
120
- * @param value - 検証する値, The value to be validated
121
- * @returns {boolean} - 値がメールアドレス形式であるかどうか, Whether the value is in the format of an email address
122
- */
123
- isMail(value) {
124
- if (typeof value !== 'string') {
125
- return false;
126
- }
127
- const pattern = new RegExp('^[a-zA-Z0-9_%+-]+([.][a-zA-Z0-9_%+-]+)*@[a-zA-Z0-9]+([-.]?[a-zA-Z0-9]+)*\\.[a-zA-Z]{2,}$');
128
- return pattern.test(value);
129
- }
130
- /**
131
- * 値がHTTPS URLであるかどうかを検証します
132
- * Validates if the given value is an HTTPS URL
133
- * @param value - 検証する値, The value to be validated
134
- * @returns {boolean} - 値がHTTPS URLであるかどうか, Whether the value is an HTTPS URL
135
- */
136
- isHttps(value) {
137
- if (typeof value !== 'string') {
138
- return false;
139
- }
140
- const urlPattern = new RegExp('^(https?:\\/\\/[^\\s/$.?#].[^\\s]*)$');
141
- return urlPattern.test(value);
142
- }
143
- /**
144
- * 値がBase64形式であるかどうかを検証します
145
- * Validates if the given value is in Base64 format
146
- * @param value - 検証する値, The value to be validated
147
- * @returns {boolean} - 値がBase64形式であるかどうか, Whether the value is in Base64 format
148
- */
149
- isBase64(value) {
150
- if (typeof value !== 'string') {
151
- return false;
152
- }
153
- // base64は4倍の長さである必要がある
154
- if (value.length % 4 !== 0) {
155
- return false;
156
- }
157
- // 基本的なbase64パターン
158
- // 使用可能な文字
159
- // ・ アルファベット(A-Z, a-z)
160
- // ・ 数字(0-9)
161
- // ・ +と/(基本文字)
162
- // ・ =(パディング文字)
163
- const base64Pattern = /^[A-Za-z0-9+/]*={0,2}$/;
164
- return base64Pattern.test(value);
165
- }
166
117
  /**
167
118
  * プロパティの型をSwagger形式に変換します
168
119
  * Converts the property type to Swagger format
@@ -7,6 +7,7 @@ exports.RequestType = void 0;
7
7
  const ReqResType_1 = __importDefault(require("./ReqResType"));
8
8
  const Exception_1 = require("../exceptions/Exception");
9
9
  const StringUtil_1 = __importDefault(require("../Utils/StringUtil"));
10
+ const type_utils_n_daira_1 = require("type-utils-n-daira");
10
11
  class RequestType extends ReqResType_1.default {
11
12
  constructor() {
12
13
  super(...arguments);
@@ -532,7 +533,7 @@ class RequestType extends ReqResType_1.default {
532
533
  this.throwInputError(isRequestBody ? "UUID_21" : "UUID_91", keys, value);
533
534
  case 'mail':
534
535
  case 'mail?':
535
- if (this.isMail(value)) {
536
+ if (type_utils_n_daira_1.ValidateStringUtil.isMail(value)) {
536
537
  return value;
537
538
  }
538
539
  this.throwInputError(isRequestBody ? "MAIL_21" : "MAIL_91", keys, value);
@@ -565,13 +566,13 @@ class RequestType extends ReqResType_1.default {
565
566
  return value.replace('T', ' ');
566
567
  case 'https':
567
568
  case 'https?':
568
- if (this.isHttps(value)) {
569
+ if (type_utils_n_daira_1.ValidateStringUtil.isHttps(value)) {
569
570
  return value;
570
571
  }
571
572
  this.throwInputError(isRequestBody ? "HTTPS_21" : "HTTPS_91", keys, value);
572
573
  case 'base64':
573
574
  case 'base64?':
574
- if (this.isBase64(value)) {
575
+ if (type_utils_n_daira_1.ValidateStringUtil.isBase64(value)) {
575
576
  return value;
576
577
  }
577
578
  this.throwInputError(isRequestBody ? "BASE64_21" : "BASE64_91", keys, value);
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.ResponseType = void 0;
7
+ const type_utils_n_daira_1 = require("type-utils-n-daira");
7
8
  const StringUtil_1 = __importDefault(require("../Utils/StringUtil"));
8
9
  const ReqResType_1 = __importDefault(require("./ReqResType"));
9
10
  class ResponseType extends ReqResType_1.default {
@@ -210,7 +211,7 @@ class ResponseType extends ReqResType_1.default {
210
211
  return undefined;
211
212
  case 'mail':
212
213
  case 'mail?':
213
- if (this.isMail(value)) {
214
+ if (type_utils_n_daira_1.ValidateStringUtil.isMail(value)) {
214
215
  return value;
215
216
  }
216
217
  return undefined;
@@ -252,13 +253,13 @@ class ResponseType extends ReqResType_1.default {
252
253
  return undefined;
253
254
  case 'https':
254
255
  case 'https?':
255
- if (this.isHttps(value)) {
256
+ if (type_utils_n_daira_1.ValidateStringUtil.isHttps(value)) {
256
257
  return value;
257
258
  }
258
259
  return undefined;
259
260
  case 'base64':
260
261
  case 'base64?':
261
- if (this.isBase64(value)) {
262
+ if (type_utils_n_daira_1.ValidateStringUtil.isBase64(value)) {
262
263
  return value;
263
264
  }
264
265
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-mvc-service",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/n-daira/npm-pack_mvc-service#readme",
6
6
  "bugs": {
@@ -31,6 +31,7 @@
31
31
  "axios": "1.10.0",
32
32
  "crypto": "1.0.1",
33
33
  "pdf-lib": "1.17.1",
34
- "sharp": "0.34.1"
34
+ "sharp": "0.34.1",
35
+ "type-utils-n-daira": "1.0.11"
35
36
  }
36
37
  }
@@ -1,5 +1,6 @@
1
1
  import { PDFDocument } from 'pdf-lib';
2
2
  import sharp from 'sharp';
3
+ import { ValidateStringUtil } from 'type-utils-n-daira';
3
4
 
4
5
  export type TPng = 'image/png';
5
6
  export type TJpeg = 'image/jpeg';
@@ -10,6 +11,9 @@ export type TPdf = 'application/pdf';
10
11
  export type TJson = 'application/json';
11
12
 
12
13
  export class Base64Client {
14
+ public static readonly PREFIX_JPEG_DATA = '/9j/';
15
+ public static readonly PREFIX_PNG_DATA = 'iVBORw0KGgo';
16
+
13
17
  constructor() { }
14
18
 
15
19
  // public encode(text: string): string {
@@ -152,4 +156,68 @@ export class Base64Client {
152
156
  const pdfBytes = await pdfDoc.save();
153
157
  return Buffer.from(pdfBytes);
154
158
  }
159
+
160
+ public static isJpeg(value: any): value is string {
161
+ if (ValidateStringUtil.isBase64(value) === false) {
162
+ return false
163
+ }
164
+
165
+ if (value.startsWith('data:')) {
166
+ if (value.startsWith('data:image/jpeg,') === false && value.startsWith('data:image/jpg,') === false) {
167
+ return false;
168
+ }
169
+
170
+ const valueParts = value.split(',');
171
+ if (valueParts.length !== 2) {
172
+ return false;
173
+ }
174
+
175
+ return valueParts[1].startsWith(this.PREFIX_JPEG_DATA);
176
+ }
177
+
178
+ return value.startsWith(this.PREFIX_JPEG_DATA);
179
+ }
180
+
181
+ public static isPng(value: any): value is string {
182
+ if (ValidateStringUtil.isBase64(value) === false) {
183
+ return false
184
+ }
185
+
186
+ if (value.startsWith('data:')) {
187
+ if (value.startsWith('data:image/png,') === false) {
188
+ return false;
189
+ }
190
+
191
+ const valueParts = value.split(',');
192
+ if (valueParts.length !== 2) {
193
+ return false;
194
+ }
195
+
196
+ return valueParts[1].startsWith(this.PREFIX_PNG_DATA);
197
+ }
198
+
199
+ return value.startsWith(this.PREFIX_PNG_DATA);
200
+ }
201
+
202
+ public static async tryConvertToPng(base64Value: any): Promise<string | false> {
203
+ if (ValidateStringUtil.isBase64(base64Value) === false) {
204
+ return false;
205
+ }
206
+
207
+ const base64Data = base64Value.startsWith('data:') ? base64Value.split(',')[1] : base64Value;
208
+ if (this.isPng(base64Data)) {
209
+ return base64Data;
210
+ } else if (this.isJpeg(base64Data)) {
211
+ const buffer = Buffer.from(base64Data, 'base64');
212
+ try {
213
+ const pngBuffer = await sharp(buffer)
214
+ .ensureAlpha().png().toBuffer();
215
+ return pngBuffer.toString('base64');
216
+ } catch (e) {
217
+ return false;
218
+ }
219
+ }
220
+
221
+ return false;
222
+ }
155
223
  }
@@ -400,7 +400,7 @@ export class TableModel {
400
400
  throw new UnprocessableException(code, message);
401
401
  }
402
402
 
403
- protected async validateOptions(options: {[key: string]: any}, isInsert: boolean): Promise<void> {
403
+ protected async validateOptions(options: {[key: string]: any}, isInsert: boolean, pkOrId?: string | number | boolean | {[key: string]: any}): Promise<void> {
404
404
  if (Object.keys(options).length === 0) {
405
405
  throw new Error('At least one key-value pair is required in options.');
406
406
  }
@@ -502,7 +502,7 @@ export class TableModel {
502
502
  }
503
503
 
504
504
  public async update(pkOrId: string | number | boolean | {[key: string]: any}, options: {[key: string]: any}) : Promise<void> {
505
- await this.validateOptions(options, false);
505
+ await this.validateOptions(options, false, pkOrId);
506
506
 
507
507
  const updateSetQuery = UpdateExpression.createUpdateSet(this, options);
508
508
  let whereQuery: TQuery;
@@ -151,62 +151,6 @@ export default class ReqResType {
151
151
  }
152
152
  }
153
153
 
154
- /**
155
- * 値がメールアドレス形式であるかどうかを検証します
156
- * Validates if the given value is in the format of an email address
157
- * @param value - 検証する値, The value to be validated
158
- * @returns {boolean} - 値がメールアドレス形式であるかどうか, Whether the value is in the format of an email address
159
- */
160
- protected isMail(value: any) {
161
- if (typeof value !== 'string') {
162
- return false;
163
- }
164
-
165
- const pattern = new RegExp('^[a-zA-Z0-9_%+-]+([.][a-zA-Z0-9_%+-]+)*@[a-zA-Z0-9]+([-.]?[a-zA-Z0-9]+)*\\.[a-zA-Z]{2,}$');
166
- return pattern.test(value);
167
- }
168
-
169
- /**
170
- * 値がHTTPS URLであるかどうかを検証します
171
- * Validates if the given value is an HTTPS URL
172
- * @param value - 検証する値, The value to be validated
173
- * @returns {boolean} - 値がHTTPS URLであるかどうか, Whether the value is an HTTPS URL
174
- */
175
- protected isHttps(value: any) {
176
- if (typeof value !== 'string') {
177
- return false;
178
- }
179
-
180
- const urlPattern = new RegExp('^(https?:\\/\\/[^\\s/$.?#].[^\\s]*)$');
181
- return urlPattern.test(value);
182
- }
183
-
184
- /**
185
- * 値がBase64形式であるかどうかを検証します
186
- * Validates if the given value is in Base64 format
187
- * @param value - 検証する値, The value to be validated
188
- * @returns {boolean} - 値がBase64形式であるかどうか, Whether the value is in Base64 format
189
- */
190
- protected isBase64(value: any) {
191
- if (typeof value !== 'string') {
192
- return false;
193
- }
194
-
195
- // base64は4倍の長さである必要がある
196
- if (value.length % 4 !== 0) {
197
- return false;
198
- }
199
-
200
- // 基本的なbase64パターン
201
- // 使用可能な文字
202
- // ・ アルファベット(A-Z, a-z)
203
- // ・ 数字(0-9)
204
- // ・ +と/(基本文字)
205
- // ・ =(パディング文字)
206
- const base64Pattern = /^[A-Za-z0-9+/]*={0,2}$/;
207
- return base64Pattern.test(value);
208
- }
209
-
210
154
  /**
211
155
  * プロパティの型をSwagger形式に変換します
212
156
  * Converts the property type to Swagger format
@@ -2,6 +2,7 @@ import { Request } from 'express';
2
2
  import ReqResType, { EnumType, PrimitiveType, PropertyType } from "./ReqResType";
3
3
  import { InputErrorException } from '../exceptions/Exception';
4
4
  import StringUtil from '../Utils/StringUtil';
5
+ import { ValidateStringUtil } from 'type-utils-n-daira';
5
6
 
6
7
  // エラーメッセージの型定義
7
8
  export interface ErrorMessageType {
@@ -575,7 +576,7 @@ export class RequestType extends ReqResType {
575
576
  this.throwInputError(isRequestBody ? "UUID_21" : "UUID_91", keys, value);
576
577
  case 'mail':
577
578
  case 'mail?':
578
- if (this.isMail(value)) {
579
+ if (ValidateStringUtil.isMail(value)) {
579
580
  return value;
580
581
  }
581
582
  this.throwInputError(isRequestBody ? "MAIL_21" : "MAIL_91", keys, value);
@@ -611,13 +612,13 @@ export class RequestType extends ReqResType {
611
612
  return value.replace('T', ' ');
612
613
  case 'https':
613
614
  case 'https?':
614
- if (this.isHttps(value)) {
615
+ if (ValidateStringUtil.isHttps(value)) {
615
616
  return value;
616
617
  }
617
618
  this.throwInputError(isRequestBody ? "HTTPS_21" : "HTTPS_91", keys, value);
618
619
  case 'base64':
619
620
  case 'base64?':
620
- if (this.isBase64(value)) {
621
+ if (ValidateStringUtil.isBase64(value)) {
621
622
  return value;
622
623
  }
623
624
  this.throwInputError(isRequestBody ? "BASE64_21" : "BASE64_91", keys, value);
@@ -1,3 +1,4 @@
1
+ import { ValidateStringUtil } from "type-utils-n-daira";
1
2
  import StringUtil from "../Utils/StringUtil";
2
3
  import ReqResType from "./ReqResType";
3
4
 
@@ -226,7 +227,7 @@ export class ResponseType extends ReqResType {
226
227
  return undefined;
227
228
  case 'mail':
228
229
  case 'mail?':
229
- if (this.isMail(value)) {
230
+ if (ValidateStringUtil.isMail(value)) {
230
231
  return value;
231
232
  }
232
233
  return undefined;
@@ -274,13 +275,13 @@ export class ResponseType extends ReqResType {
274
275
  return undefined;
275
276
  case 'https':
276
277
  case 'https?':
277
- if (this.isHttps(value)) {
278
+ if (ValidateStringUtil.isHttps(value)) {
278
279
  return value;
279
280
  }
280
281
  return undefined;
281
282
  case 'base64':
282
283
  case 'base64?':
283
- if (this.isBase64(value)) {
284
+ if (ValidateStringUtil.isBase64(value)) {
284
285
  return value;
285
286
  }
286
287
  return undefined;