badmfck-api-server 4.0.5 → 4.0.7

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.
@@ -65,6 +65,7 @@ export declare class APIService<TInterceptorResult = any, TInternalCallParams =
65
65
  private monitor;
66
66
  private started;
67
67
  private requestsCount;
68
+ private noCors;
68
69
  netLog: APIServiceNetworkLogItem[];
69
70
  constructor(options: APIServiceOptions);
70
71
  init(): Promise<void>;
@@ -95,11 +95,12 @@ async function Initializer(services) {
95
95
  }
96
96
  exports.Initializer = Initializer;
97
97
  class APIService extends BaseService_1.BaseService {
98
- version = "4.0.5";
98
+ version = "4.0.7";
99
99
  options;
100
100
  monitor = null;
101
101
  started = new Date();
102
102
  requestsCount = 0;
103
+ noCors = false;
103
104
  netLog = [];
104
105
  constructor(options) {
105
106
  super('HTTP Service');
@@ -109,6 +110,10 @@ class APIService extends BaseService_1.BaseService {
109
110
  const self = "http://localhost:" + this.options.port;
110
111
  if (!this.options.corsHostWhiteList.find(val => val === self))
111
112
  this.options.corsHostWhiteList.push(self);
113
+ if (this.options.corsHostWhiteList.length === 0)
114
+ this.noCors = true;
115
+ if (this.options.corsHostWhiteList.find(x => x === "*"))
116
+ this.noCors = true;
112
117
  const list = [];
113
118
  for (let h of this.options.corsHostWhiteList) {
114
119
  h = h.replace(/\/$/, "");
@@ -234,6 +239,8 @@ class APIService extends BaseService_1.BaseService {
234
239
  const originHeader = req.headers.origin;
235
240
  if (!originHeader)
236
241
  return next();
242
+ if (this.noCors)
243
+ return next();
237
244
  let originNorm;
238
245
  try {
239
246
  originNorm = new URL(String(originHeader)).origin.replace(/\/$/, "");
@@ -243,6 +250,7 @@ class APIService extends BaseService_1.BaseService {
243
250
  error: { ...DefaultErrors_1.default.FORBIDDEN, details: "Invalid Origin header" },
244
251
  data: null,
245
252
  httpStatus: 403,
253
+ version: this.options.appVersion
246
254
  });
247
255
  return;
248
256
  }
@@ -251,6 +259,7 @@ class APIService extends BaseService_1.BaseService {
251
259
  error: { ...DefaultErrors_1.default.FORBIDDEN, details: `Origin not allowed: ${originNorm}` },
252
260
  data: null,
253
261
  httpStatus: 403,
262
+ version: this.options.appVersion
254
263
  });
255
264
  return;
256
265
  }
@@ -48,6 +48,7 @@ export interface IDBError {
48
48
  export interface IDBResult {
49
49
  data?: any | null;
50
50
  error?: IDBError | null;
51
+ total?: number;
51
52
  execution: {
52
53
  started_at: number;
53
54
  finished_at: number;
@@ -445,8 +445,21 @@ class MysqlAdapter {
445
445
  let result = null;
446
446
  if (this.options.maxQueryTimeout && this.options.maxQueryTimeout > 0)
447
447
  result = await this.withTimeout(conn.query(query), this.options.maxQueryTimeout, query);
448
- else
448
+ else {
449
449
  result = await conn.query(query);
450
+ }
451
+ let total = null;
452
+ ;
453
+ if (request.calculateCount) {
454
+ const cres = await conn.query(this.prepareCountQuery(query));
455
+ if (cres && cres[0]) {
456
+ const res = cres[0];
457
+ if (Array.isArray(res) && res.length > 0 && "count" in res[0])
458
+ total = res[0].count;
459
+ else if ("count" in res)
460
+ total = res.count;
461
+ }
462
+ }
450
463
  this.lastSuccessQueryTime = +new Date();
451
464
  if (!request.transactionID && doCloseConnection)
452
465
  this.finalizeConnection(conn);
@@ -455,6 +468,7 @@ class MysqlAdapter {
455
468
  return {
456
469
  data: result[0],
457
470
  error: null,
471
+ total: total ? total : undefined,
458
472
  execution: {
459
473
  started_at: executionStartTime,
460
474
  finished_at: Date.now()
@@ -630,14 +644,11 @@ class MysqlAdapter {
630
644
  return value;
631
645
  }
632
646
  prepareCountQuery(query) {
633
- let countQuery = query;
634
- if (countQuery.indexOf("LIMIT") === -1)
635
- countQuery = countQuery.substring(0, countQuery.indexOf("LIMIT"));
636
- if (countQuery.indexOf("ORDER") !== -1)
637
- countQuery = countQuery.substring(0, countQuery.indexOf("ORDER"));
638
- countQuery = countQuery.substring(countQuery.indexOf("FROM"));
639
- countQuery = "SELECT COUNT(1) as `count` " + countQuery;
640
- return countQuery;
647
+ let cleanQuery = query
648
+ .replace(/ORDER\s+BY\s+.*$/is, "")
649
+ .replace(/\bLIMIT\s+\d+(\s+OFFSET\s+\d+)?/is, "")
650
+ .trim();
651
+ return `SELECT COUNT(1) AS \`count\` FROM (${cleanQuery}) AS \`temp_count_table\``;
641
652
  }
642
653
  async commit(trx) {
643
654
  try {
@@ -70,7 +70,7 @@ class Documentation extends BaseEndpoint_1.BaseEndpoint {
70
70
  async checkAuthorization(req) {
71
71
  const users = await APIService_1.REQ_DOC_USERS.request();
72
72
  if (!users || users.length == 0)
73
- throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No users found for documentation access" };
73
+ throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No users found for documentation access, please set up users first." };
74
74
  let header = req.headers['authorization'];
75
75
  if (!header)
76
76
  throw { ...DefaultErrors_1.default.UNAUTHORIZED, details: "No authorization header found" };