@faasjs/http 0.0.4-beta.6 → 0.0.4-beta.8

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.
package/dist/index.js CHANGED
@@ -135,14 +135,14 @@ var Cookie = class {
135
135
  invoke(cookie, logger) {
136
136
  this.content = /* @__PURE__ */ Object.create(null);
137
137
  if (cookie)
138
- cookie.split(";").forEach((x) => {
139
- x = x.trim();
140
- const k = /([^=]+)/.exec(x);
138
+ for (const x of cookie.split(";")) {
139
+ const trimX = x.trim();
140
+ const k = /([^=]+)/.exec(trimX);
141
141
  if (k !== null)
142
142
  this.content[k[0]] = decodeURIComponent(
143
- x.replace(`${k[0]}=`, "").replace(/;$/, "")
143
+ trimX.replace(`${k[0]}=`, "").replace(/;$/, "")
144
144
  );
145
- });
145
+ }
146
146
  this.setCookie = /* @__PURE__ */ Object.create(null);
147
147
  this.session.invoke(this.read(this.session.config.key), logger);
148
148
  return this;
@@ -445,8 +445,11 @@ var Http = class {
445
445
  await provider.deploy(this.type, data, config);
446
446
  }
447
447
  async onMount(data, next) {
448
- var _a;
449
- data.logger.debug("[onMount] merge config");
448
+ var _a, _b;
449
+ const logger$1 = new logger.Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
450
+ if (!logger$1.label.endsWith(this.name))
451
+ logger$1.label = `${logger$1.label}] [${this.name}`;
452
+ logger$1.debug("[onMount] merge config");
450
453
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
451
454
  for (let key in process.env)
452
455
  if (key.startsWith(prefix)) {
@@ -455,25 +458,27 @@ var Http = class {
455
458
  if (key.includes("_")) {
456
459
  let config = this.config;
457
460
  const keys = key.split("_");
458
- keys.slice(0, keys.length - 1).forEach((k) => {
461
+ for (const k of keys.slice(0, keys.length - 1)) {
459
462
  if (!config[k])
460
463
  config[k] = /* @__PURE__ */ Object.create(null);
461
464
  config = config[k];
462
- });
465
+ }
463
466
  config[keys[keys.length - 1]] = value;
464
467
  } else
465
468
  this.config[key] = value;
466
469
  }
467
- if ((_a = data.config.plugins) == null ? void 0 : _a[this.name || this.type])
470
+ if (!data.config)
471
+ throw Error(`[${this.name}] Config not found.`);
472
+ if ((_b = data.config.plugins) == null ? void 0 : _b[this.name || this.type])
468
473
  this.config = deep_merge.deepMerge(
469
474
  this.config,
470
475
  data.config.plugins[this.name || this.type].config
471
476
  );
472
- data.logger.debug("[onMount] prepare cookie & session");
473
- this.cookie = new Cookie(this.config.cookie || {}, data.logger);
477
+ logger$1.debug("[onMount] prepare cookie & session");
478
+ this.cookie = new Cookie(this.config.cookie || {}, logger$1);
474
479
  this.session = this.cookie.session;
475
480
  if (this.validatorOptions) {
476
- data.logger.debug("[onMount] prepare validator");
481
+ logger$1.debug("[onMount] prepare validator");
477
482
  this.validator = new Validator(
478
483
  this.validatorOptions
479
484
  );
@@ -481,39 +486,46 @@ var Http = class {
481
486
  await next();
482
487
  }
483
488
  async onInvoke(data, next) {
484
- var _a, _b;
489
+ var _a, _b, _c, _d;
490
+ const logger$1 = new logger.Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
491
+ if (!((_b = logger$1.label) == null ? void 0 : _b.endsWith(this.name)))
492
+ logger$1.label = `${logger$1.label}] [${this.name}`;
485
493
  this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
486
494
  this.body = data.event.body;
487
495
  this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
488
496
  this.response = { headers: /* @__PURE__ */ Object.create(null) };
489
497
  if (data.event.body) {
490
- if (((_a = this.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
491
- data.logger.debug("[onInvoke] Parse params from json body");
498
+ if (((_c = this.headers["content-type"]) == null ? void 0 : _c.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
499
+ logger$1.debug("[onInvoke] Parse params from json body");
492
500
  try {
493
501
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
494
502
  } catch (error) {
495
- data.logger.error(
503
+ logger$1.error(
496
504
  "[onInvoke] Parse params from json body failed: %s",
497
505
  error.message
498
506
  );
499
507
  }
500
508
  } else {
501
- data.logger.debug("[onInvoke] Parse params from raw body");
509
+ logger$1.debug("[onInvoke] Parse params from raw body");
502
510
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
503
511
  }
504
512
  if (this.params && typeof this.params === "object" && this.params._)
505
513
  delete this.params._;
506
514
  data.event.params = deepClone(this.params);
507
- data.logger.debug("[onInvoke] Params: %j", this.params);
515
+ logger$1.debug("[onInvoke] Params: %j", this.params);
508
516
  }
509
- this.cookie.invoke(this.headers.cookie, data.logger);
517
+ this.cookie.invoke(this.headers.cookie, logger$1);
510
518
  if (this.headers.cookie) {
511
- data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
512
- data.logger.debug("[onInvoke] Session: %j", this.session.content);
519
+ logger$1.debug("[onInvoke] Cookie: %j", this.cookie.content);
520
+ logger$1.debug(
521
+ "[onInvoke] Session: %s %j",
522
+ this.session.config.key,
523
+ this.session.content
524
+ );
513
525
  }
514
526
  try {
515
527
  if (this.validator) {
516
- data.logger.debug("[onInvoke] Valid request");
528
+ logger$1.debug("[onInvoke] Valid request");
517
529
  await this.validator.valid(
518
530
  {
519
531
  headers: this.headers,
@@ -521,7 +533,7 @@ var Http = class {
521
533
  cookie: this.cookie,
522
534
  session: this.session
523
535
  },
524
- data.logger
536
+ logger$1
525
537
  );
526
538
  }
527
539
  await next();
@@ -530,8 +542,8 @@ var Http = class {
530
542
  }
531
543
  this.session.update();
532
544
  if (data.response)
533
- if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
534
- data.logger.error(data.response);
545
+ if (data.response instanceof Error || ((_d = data.response.constructor) == null ? void 0 : _d.name) === "Error") {
546
+ logger$1.error(data.response);
535
547
  this.response.body = JSON.stringify({
536
548
  error: { message: data.response.message }
537
549
  });
package/dist/index.mjs CHANGED
@@ -133,14 +133,14 @@ var Cookie = class {
133
133
  invoke(cookie, logger) {
134
134
  this.content = /* @__PURE__ */ Object.create(null);
135
135
  if (cookie)
136
- cookie.split(";").forEach((x) => {
137
- x = x.trim();
138
- const k = /([^=]+)/.exec(x);
136
+ for (const x of cookie.split(";")) {
137
+ const trimX = x.trim();
138
+ const k = /([^=]+)/.exec(trimX);
139
139
  if (k !== null)
140
140
  this.content[k[0]] = decodeURIComponent(
141
- x.replace(`${k[0]}=`, "").replace(/;$/, "")
141
+ trimX.replace(`${k[0]}=`, "").replace(/;$/, "")
142
142
  );
143
- });
143
+ }
144
144
  this.setCookie = /* @__PURE__ */ Object.create(null);
145
145
  this.session.invoke(this.read(this.session.config.key), logger);
146
146
  return this;
@@ -443,8 +443,11 @@ var Http = class {
443
443
  await provider.deploy(this.type, data, config);
444
444
  }
445
445
  async onMount(data, next) {
446
- var _a;
447
- data.logger.debug("[onMount] merge config");
446
+ var _a, _b;
447
+ const logger = new Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
448
+ if (!logger.label.endsWith(this.name))
449
+ logger.label = `${logger.label}] [${this.name}`;
450
+ logger.debug("[onMount] merge config");
448
451
  const prefix = `SECRET_${this.name.toUpperCase()}_`;
449
452
  for (let key in process.env)
450
453
  if (key.startsWith(prefix)) {
@@ -453,25 +456,27 @@ var Http = class {
453
456
  if (key.includes("_")) {
454
457
  let config = this.config;
455
458
  const keys = key.split("_");
456
- keys.slice(0, keys.length - 1).forEach((k) => {
459
+ for (const k of keys.slice(0, keys.length - 1)) {
457
460
  if (!config[k])
458
461
  config[k] = /* @__PURE__ */ Object.create(null);
459
462
  config = config[k];
460
- });
463
+ }
461
464
  config[keys[keys.length - 1]] = value;
462
465
  } else
463
466
  this.config[key] = value;
464
467
  }
465
- if ((_a = data.config.plugins) == null ? void 0 : _a[this.name || this.type])
468
+ if (!data.config)
469
+ throw Error(`[${this.name}] Config not found.`);
470
+ if ((_b = data.config.plugins) == null ? void 0 : _b[this.name || this.type])
466
471
  this.config = deepMerge(
467
472
  this.config,
468
473
  data.config.plugins[this.name || this.type].config
469
474
  );
470
- data.logger.debug("[onMount] prepare cookie & session");
471
- this.cookie = new Cookie(this.config.cookie || {}, data.logger);
475
+ logger.debug("[onMount] prepare cookie & session");
476
+ this.cookie = new Cookie(this.config.cookie || {}, logger);
472
477
  this.session = this.cookie.session;
473
478
  if (this.validatorOptions) {
474
- data.logger.debug("[onMount] prepare validator");
479
+ logger.debug("[onMount] prepare validator");
475
480
  this.validator = new Validator(
476
481
  this.validatorOptions
477
482
  );
@@ -479,39 +484,46 @@ var Http = class {
479
484
  await next();
480
485
  }
481
486
  async onInvoke(data, next) {
482
- var _a, _b;
487
+ var _a, _b, _c, _d;
488
+ const logger = new Logger(((_a = data.logger) == null ? void 0 : _a.label) || this.name);
489
+ if (!((_b = logger.label) == null ? void 0 : _b.endsWith(this.name)))
490
+ logger.label = `${logger.label}] [${this.name}`;
483
491
  this.headers = data.event.headers || /* @__PURE__ */ Object.create(null);
484
492
  this.body = data.event.body;
485
493
  this.params = data.event.queryString || /* @__PURE__ */ Object.create(null);
486
494
  this.response = { headers: /* @__PURE__ */ Object.create(null) };
487
495
  if (data.event.body) {
488
- if (((_a = this.headers["content-type"]) == null ? void 0 : _a.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
489
- data.logger.debug("[onInvoke] Parse params from json body");
496
+ if (((_c = this.headers["content-type"]) == null ? void 0 : _c.includes("application/json")) && typeof data.event.body === "string" && data.event.body.length > 1) {
497
+ logger.debug("[onInvoke] Parse params from json body");
490
498
  try {
491
499
  this.params = Object.keys(this.params).length ? Object.assign(this.params, JSON.parse(data.event.body)) : JSON.parse(data.event.body);
492
500
  } catch (error) {
493
- data.logger.error(
501
+ logger.error(
494
502
  "[onInvoke] Parse params from json body failed: %s",
495
503
  error.message
496
504
  );
497
505
  }
498
506
  } else {
499
- data.logger.debug("[onInvoke] Parse params from raw body");
507
+ logger.debug("[onInvoke] Parse params from raw body");
500
508
  this.params = data.event.body || /* @__PURE__ */ Object.create(null);
501
509
  }
502
510
  if (this.params && typeof this.params === "object" && this.params._)
503
511
  delete this.params._;
504
512
  data.event.params = deepClone(this.params);
505
- data.logger.debug("[onInvoke] Params: %j", this.params);
513
+ logger.debug("[onInvoke] Params: %j", this.params);
506
514
  }
507
- this.cookie.invoke(this.headers.cookie, data.logger);
515
+ this.cookie.invoke(this.headers.cookie, logger);
508
516
  if (this.headers.cookie) {
509
- data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
510
- data.logger.debug("[onInvoke] Session: %j", this.session.content);
517
+ logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
518
+ logger.debug(
519
+ "[onInvoke] Session: %s %j",
520
+ this.session.config.key,
521
+ this.session.content
522
+ );
511
523
  }
512
524
  try {
513
525
  if (this.validator) {
514
- data.logger.debug("[onInvoke] Valid request");
526
+ logger.debug("[onInvoke] Valid request");
515
527
  await this.validator.valid(
516
528
  {
517
529
  headers: this.headers,
@@ -519,7 +531,7 @@ var Http = class {
519
531
  cookie: this.cookie,
520
532
  session: this.session
521
533
  },
522
- data.logger
534
+ logger
523
535
  );
524
536
  }
525
537
  await next();
@@ -528,8 +540,8 @@ var Http = class {
528
540
  }
529
541
  this.session.update();
530
542
  if (data.response)
531
- if (data.response instanceof Error || ((_b = data.response.constructor) == null ? void 0 : _b.name) === "Error") {
532
- data.logger.error(data.response);
543
+ if (data.response instanceof Error || ((_d = data.response.constructor) == null ? void 0 : _d.name) === "Error") {
544
+ logger.error(data.response);
533
545
  this.response.body = JSON.stringify({
534
546
  error: { message: data.response.message }
535
547
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/http",
3
- "version": "0.0.4-beta.6",
3
+ "version": "0.0.4-beta.8",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,9 +21,13 @@
21
21
  "files": [
22
22
  "dist"
23
23
  ],
24
- "dependencies": {
25
- "@faasjs/func": "0.0.4-beta.6",
26
- "@faasjs/logger": "0.0.4-beta.6"
24
+ "peerDependencies": {
25
+ "@faasjs/func": "0.0.4-beta.8",
26
+ "@faasjs/logger": "0.0.4-beta.8"
27
+ },
28
+ "devDependencies": {
29
+ "@faasjs/func": "0.0.4-beta.8",
30
+ "@faasjs/logger": "0.0.4-beta.8"
27
31
  },
28
32
  "engines": {
29
33
  "npm": ">=9.0.0",