badmfck-api-server 3.3.9 → 3.4.1

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.
@@ -95,7 +95,7 @@ async function Initializer(services) {
95
95
  exports.Initializer = Initializer;
96
96
  class APIService extends BaseService_1.BaseService {
97
97
  static nextLogID = 0;
98
- version = "3.3.8";
98
+ version = "3.4.1";
99
99
  options;
100
100
  monitor = null;
101
101
  started = new Date();
@@ -146,7 +146,7 @@ class APIService extends BaseService_1.BaseService {
146
146
  ep = ep.replaceAll("//", "/");
147
147
  if (ep === req.endpoint) {
148
148
  req.internalCall = true;
149
- const response = await i.execute(req);
149
+ const response = await i.__execute(req);
150
150
  if (response.error)
151
151
  return response.error;
152
152
  return response.data;
@@ -286,7 +286,7 @@ class APIService extends BaseService_1.BaseService {
286
286
  if (!ignoreInterceptor) {
287
287
  let interceptorResult;
288
288
  if (this.options.interceptor) {
289
- interceptorResult = await this.options.interceptor.execute(httpRequest);
289
+ interceptorResult = await this.options.interceptor.__execute(httpRequest);
290
290
  if (interceptorResult.error && !allowInterceptorError) {
291
291
  this.sendResponse(req.get("Referer") ?? "", res, interceptorResult, tme, ep, log, httpRequest);
292
292
  return;
@@ -294,13 +294,13 @@ class APIService extends BaseService_1.BaseService {
294
294
  httpRequest.interceptorResult = interceptorResult;
295
295
  }
296
296
  }
297
- const precheck = await i.precheck(httpRequest);
297
+ const precheck = await i.__precheck(httpRequest);
298
298
  if (precheck && precheck.error) {
299
299
  this.sendResponse(req.get("Referer") ?? "", res, precheck, tme, ep, log, httpRequest);
300
300
  return;
301
301
  }
302
302
  httpRequest.precheck = precheck;
303
- result = await i.execute(httpRequest);
303
+ result = await i.__execute(httpRequest);
304
304
  }
305
305
  catch (e) {
306
306
  console.error(e);
@@ -1,8 +1,8 @@
1
1
  import { HTTPRequestVO, TransferPacketVO } from "./structures/Interfaces";
2
2
  export interface IBaseEndpoint {
3
3
  endpoints?: IEndpointHandler[];
4
- execute: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
5
- precheck: (req: HTTPRequestVO) => Promise<TransferPacketVO<any> | null>;
4
+ __execute: (req: HTTPRequestVO) => Promise<TransferPacketVO<any>>;
5
+ __precheck: (req: HTTPRequestVO) => Promise<TransferPacketVO<any> | null>;
6
6
  init: () => Promise<void>;
7
7
  ignoreHttpLogging: boolean;
8
8
  }
@@ -48,8 +48,8 @@ export declare class BaseEndpoint implements IBaseEndpoint {
48
48
  registerEndpoints(endpoints: IEndpointHandler[]): void;
49
49
  init(): Promise<void>;
50
50
  getInitalizedEndpointNames(): String[];
51
- precheck(req: HTTPRequestVO): Promise<TransferPacketVO<any> | null>;
52
- execute(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
51
+ __precheck(req: HTTPRequestVO): Promise<TransferPacketVO<any> | null>;
52
+ __execute(req: HTTPRequestVO): Promise<TransferPacketVO<any>>;
53
53
  protected validateStructure(structure: any, req: HTTPRequestVO): Promise<void>;
54
54
  protected createStream(req: HTTPRequestVO): HTTPRequestVO;
55
55
  }
@@ -67,8 +67,8 @@ class BaseEndpoint {
67
67
  getInitalizedEndpointNames() {
68
68
  return this.inializedEndpointNames;
69
69
  }
70
- async precheck(req) { return null; }
71
- async execute(req) {
70
+ async __precheck(req) { return null; }
71
+ async __execute(req) {
72
72
  if (this.endpoints && this.endpoints.length > 0) {
73
73
  for (let i of this.endpoints) {
74
74
  let targetEP = BaseEndpoint.entrypoint + i.endpoint;
@@ -386,6 +386,8 @@ class MysqlAdapter {
386
386
  if (fields) {
387
387
  for (let i in fields) {
388
388
  const name = i;
389
+ if (name.startsWith("$__"))
390
+ continue;
389
391
  let queryField = fields[i];
390
392
  if (queryField === undefined)
391
393
  continue;
@@ -409,6 +411,8 @@ class MysqlAdapter {
409
411
  let insertFieldValues = [];
410
412
  for (let i in fields) {
411
413
  let f = fields[i];
414
+ if (f.name?.startsWith("$__"))
415
+ continue;
412
416
  if (f.ignoreInInsert)
413
417
  continue;
414
418
  insertFieldNames.push('`' + f.name + '`');
@@ -423,6 +427,8 @@ class MysqlAdapter {
423
427
  let oninsertValues = [];
424
428
  for (let i in fields) {
425
429
  let f = fields[i];
430
+ if (f.name?.startsWith("$__"))
431
+ continue;
426
432
  if (f.ignoreInInsert)
427
433
  continue;
428
434
  oninsertNames.push(f.name);
@@ -434,6 +440,8 @@ class MysqlAdapter {
434
440
  let onUpdate = [];
435
441
  for (let i in fields) {
436
442
  let f = fields[i];
443
+ if (f.name?.startsWith("$__"))
444
+ continue;
437
445
  if (f.ignoreInUpdate)
438
446
  continue;
439
447
  onUpdate.push('`' + f.name + '` = ' + f.__parsedValue);
@@ -444,6 +452,8 @@ class MysqlAdapter {
444
452
  let onDuplicate = [];
445
453
  for (let i in fields) {
446
454
  let f = fields[i];
455
+ if (f.name?.startsWith("$__"))
456
+ continue;
447
457
  if (!f.useInReplace)
448
458
  continue;
449
459
  onDuplicate.push('`' + f.name + f.__parsedValue);
@@ -126,6 +126,22 @@ class Validator {
126
126
  errors.push("wrong value for field '" + i + "', expected " + expected.join(", ") + " got: " + value);
127
127
  }
128
128
  }
129
+ if (structure['$__' + i + "_values"]) {
130
+ const values = structure['$__' + i + "_values"];
131
+ if (Array.isArray(values) && values.length) {
132
+ if (!values.includes(object[i])) {
133
+ let caseValue = null;
134
+ for (let i of values) {
135
+ const v = i.toLowerCase();
136
+ if (v === (object[i] + "").toLowerCase()) {
137
+ caseValue = object[i];
138
+ break;
139
+ }
140
+ }
141
+ errors.push("Wrong value for field '" + i + "'" + (caseValue ? ", check case for: " + caseValue : ""));
142
+ }
143
+ }
144
+ }
129
145
  if (typeof structure[i] === "string" && structure["$__" + i + "_regex"]) {
130
146
  const regex = structure["$__" + i + "_regex"];
131
147
  const reg = new RegExp(regex);
@@ -145,6 +161,12 @@ class Validator {
145
161
  }
146
162
  }
147
163
  if (typeof structure[i] === "number") {
164
+ if (typeof object[i] === "string") {
165
+ if (object[i].indexOf("."))
166
+ object[i] = parseFloat(object[i]);
167
+ else
168
+ object[i] = parseInt(object[i]);
169
+ }
148
170
  if (isNaN(object[i]))
149
171
  errors.push("wrong value for field '" + i + "', expected number got " + object[i]);
150
172
  if (structure[i] != 0) {
@@ -170,6 +192,16 @@ class Validator {
170
192
  if (object[i] > max)
171
193
  errors.push("value for field '" + i + "' is too big, expected less than " + max + " got: " + object[i]);
172
194
  }
195
+ if (typeof object[i] === "string" && structure["$__" + i + "_min"]) {
196
+ const min = structure["$__" + i + "_min"];
197
+ if (object[i].length < min)
198
+ errors.push("value length for field '" + i + "' is too small, expected more than " + min + " got: " + object[i].length);
199
+ }
200
+ if (typeof object[i] === "string" && structure["$__" + i + "_max"]) {
201
+ const max = structure["$__" + i + "_max"];
202
+ if (object[i].length > max)
203
+ errors.push("value length for field '" + i + "' is too big, expected less than " + max + " got: " + object[i].length);
204
+ }
173
205
  foundKeys.push(i);
174
206
  }
175
207
  if (errors.length > 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "3.3.9",
3
+ "version": "3.4.1",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",