@faasjs/http 0.0.4-beta.1 → 0.0.4-beta.11

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/README.md CHANGED
@@ -64,15 +64,15 @@ ___
64
64
 
65
65
  ### HttpConfig
66
66
 
67
- Ƭ **HttpConfig**<`TParams`, `TCookie`, `TSession`\>: `Object`
67
+ Ƭ **HttpConfig**\<`TParams`, `TCookie`, `TSession`\>: `Object`
68
68
 
69
69
  #### Type parameters
70
70
 
71
71
  | Name | Type |
72
72
  | :------ | :------ |
73
- | `TParams` | extends `Record`<`string`, `any`\> = `any` |
74
- | `TCookie` | extends `Record`<`string`, `string`\> = `any` |
75
- | `TSession` | extends `Record`<`string`, `string`\> = `any` |
73
+ | `TParams` | extends `Record`\<`string`, `any`\> = `any` |
74
+ | `TCookie` | extends `Record`\<`string`, `string`\> = `any` |
75
+ | `TSession` | extends `Record`\<`string`, `string`\> = `any` |
76
76
 
77
77
  #### Index signature
78
78
 
@@ -82,7 +82,7 @@ ___
82
82
 
83
83
  | Name | Type |
84
84
  | :------ | :------ |
85
- | `config?` | { `[key: string]`: `any`; `cookie?`: [`CookieOptions`](#cookieoptions) ; `functionName?`: `string` ; `ignorePathPrefix?`: `string` ; `method?`: ``"BEGIN"`` \| ``"GET"`` \| ``"POST"`` \| ``"DELETE"`` \| ``"HEAD"`` \| ``"PUT"`` \| ``"OPTIONS"`` \| ``"TRACE"`` \| ``"PATCH"`` \| ``"ANY"`` ; `path?`: `string` ; `timeout?`: `number` } |
85
+ | `config?` | \{ `[key: string]`: `any`; `cookie?`: [`CookieOptions`](#cookieoptions) ; `functionName?`: `string` ; `ignorePathPrefix?`: `string` ; `method?`: ``"BEGIN"`` \| ``"GET"`` \| ``"POST"`` \| ``"DELETE"`` \| ``"HEAD"`` \| ``"PUT"`` \| ``"OPTIONS"`` \| ``"TRACE"`` \| ``"PATCH"`` \| ``"ANY"`` ; `path?`: `string` ; `timeout?`: `number` } |
86
86
  | `config.cookie?` | [`CookieOptions`](#cookieoptions) |
87
87
  | `config.functionName?` | `string` |
88
88
  | `config.ignorePathPrefix?` | `string` |
@@ -90,7 +90,7 @@ ___
90
90
  | `config.path?` | `string` |
91
91
  | `config.timeout?` | `number` |
92
92
  | `name?` | `string` |
93
- | `validator?` | [`ValidatorConfig`](#validatorconfig)<`TParams`, `TCookie`, `TSession`\> |
93
+ | `validator?` | [`ValidatorConfig`](#validatorconfig)\<`TParams`, `TCookie`, `TSession`\> |
94
94
 
95
95
  ___
96
96
 
@@ -103,7 +103,7 @@ ___
103
103
  | Name | Type |
104
104
  | :------ | :------ |
105
105
  | `body?` | `string` |
106
- | `headers?` | { `[key: string]`: `string`; } |
106
+ | `headers?` | \{ `[key: string]`: `string`; } |
107
107
  | `message?` | `string` |
108
108
  | `statusCode?` | `number` |
109
109
 
@@ -130,43 +130,43 @@ ___
130
130
 
131
131
  ### ValidatorConfig
132
132
 
133
- Ƭ **ValidatorConfig**<`TParams`, `TCookie`, `TSession`\>: `Object`
133
+ Ƭ **ValidatorConfig**\<`TParams`, `TCookie`, `TSession`\>: `Object`
134
134
 
135
135
  #### Type parameters
136
136
 
137
137
  | Name | Type |
138
138
  | :------ | :------ |
139
- | `TParams` | extends `Record`<`string`, `any`\> = `any` |
140
- | `TCookie` | extends `Record`<`string`, `string`\> = `any` |
141
- | `TSession` | extends `Record`<`string`, `string`\> = `any` |
139
+ | `TParams` | extends `Record`\<`string`, `any`\> = `any` |
140
+ | `TCookie` | extends `Record`\<`string`, `string`\> = `any` |
141
+ | `TSession` | extends `Record`\<`string`, `string`\> = `any` |
142
142
 
143
143
  #### Type declaration
144
144
 
145
145
  | Name | Type |
146
146
  | :------ | :------ |
147
147
  | `before?` | `BeforeOption` |
148
- | `cookie?` | [`ValidatorOptions`](#validatoroptions)<`TCookie`\> |
149
- | `params?` | [`ValidatorOptions`](#validatoroptions)<`TParams`\> |
150
- | `session?` | [`ValidatorOptions`](#validatoroptions)<`TSession`\> |
148
+ | `cookie?` | [`ValidatorOptions`](#validatoroptions)\<`TCookie`\> |
149
+ | `params?` | [`ValidatorOptions`](#validatoroptions)\<`TParams`\> |
150
+ | `session?` | [`ValidatorOptions`](#validatoroptions)\<`TSession`\> |
151
151
 
152
152
  ___
153
153
 
154
154
  ### ValidatorOptions
155
155
 
156
- Ƭ **ValidatorOptions**<`Content`\>: `Object`
156
+ Ƭ **ValidatorOptions**\<`Content`\>: `Object`
157
157
 
158
158
  #### Type parameters
159
159
 
160
160
  | Name | Type |
161
161
  | :------ | :------ |
162
- | `Content` | `Record`<`string`, `any`\> |
162
+ | `Content` | `Record`\<`string`, `any`\> |
163
163
 
164
164
  #### Type declaration
165
165
 
166
166
  | Name | Type |
167
167
  | :------ | :------ |
168
- | `onError?` | (`type`: `string`, `key`: `string` \| `string`[], `value?`: `any`) => { `message`: `any` ; `statusCode?`: `number` } \| `void` |
169
- | `rules` | { [k in keyof Content]?: ValidatorRuleOptions } |
168
+ | `onError?` | (`type`: `string`, `key`: `string` \| `string`[], `value?`: `any`) => \{ `message`: `any` ; `statusCode?`: `number` } \| `void` |
169
+ | `rules` | \{ [k in keyof Content]?: ValidatorRuleOptions } |
170
170
  | `whitelist?` | ``"error"`` \| ``"ignore"`` |
171
171
 
172
172
  ___
@@ -179,7 +179,7 @@ ___
179
179
 
180
180
  | Name | Type |
181
181
  | :------ | :------ |
182
- | `config?` | `Partial`<[`ValidatorOptions`](#validatoroptions)\> |
182
+ | `config?` | `Partial`\<[`ValidatorOptions`](#validatoroptions)\> |
183
183
  | `default?` | `any` |
184
184
  | `in?` | `any`[] |
185
185
  | `regexp?` | `RegExp` |
@@ -200,22 +200,22 @@ ___
200
200
 
201
201
  ### useHttp
202
202
 
203
- ▸ **useHttp**<`TParams`, `TCookie`, `TSession`\>(`config?`): `UseifyPlugin`<[`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession`\>\>
203
+ ▸ **useHttp**\<`TParams`, `TCookie`, `TSession`\>(`config?`): `UseifyPlugin`\<[`Http`](classes/Http.md)\<`TParams`, `TCookie`, `TSession`\>\>
204
204
 
205
205
  #### Type parameters
206
206
 
207
207
  | Name | Type |
208
208
  | :------ | :------ |
209
- | `TParams` | extends `Record`<`string`, `any`\> = `any` |
210
- | `TCookie` | extends `Record`<`string`, `string`\> = `any` |
211
- | `TSession` | extends `Record`<`string`, `string`\> = `any` |
209
+ | `TParams` | extends `Record`\<`string`, `any`\> = `any` |
210
+ | `TCookie` | extends `Record`\<`string`, `string`\> = `any` |
211
+ | `TSession` | extends `Record`\<`string`, `string`\> = `any` |
212
212
 
213
213
  #### Parameters
214
214
 
215
215
  | Name | Type |
216
216
  | :------ | :------ |
217
- | `config?` | [`HttpConfig`](#httpconfig)<`TParams`, `TCookie`, `TSession`\> |
217
+ | `config?` | [`HttpConfig`](#httpconfig)\<`TParams`, `TCookie`, `TSession`\> |
218
218
 
219
219
  #### Returns
220
220
 
221
- `UseifyPlugin`<[`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession`\>\>
221
+ `UseifyPlugin`\<[`Http`](classes/Http.md)\<`TParams`, `TCookie`, `TSession`\>\>
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.1",
3
+ "version": "0.0.4-beta.11",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,12 +22,12 @@
22
22
  "dist"
23
23
  ],
24
24
  "peerDependencies": {
25
- "@faasjs/func": "0.0.3-beta.109",
26
- "@faasjs/logger": "0.0.3-beta.109"
25
+ "@faasjs/func": "0.0.4-beta.11",
26
+ "@faasjs/logger": "0.0.4-beta.11"
27
27
  },
28
28
  "devDependencies": {
29
- "@faasjs/func": "0.0.4-beta.1",
30
- "@faasjs/logger": "0.0.4-beta.1"
29
+ "@faasjs/func": "0.0.4-beta.11",
30
+ "@faasjs/logger": "0.0.4-beta.11"
31
31
  },
32
32
  "engines": {
33
33
  "npm": ">=9.0.0",