@faasjs/http 0.0.3-beta.85 → 0.0.3-beta.87

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.
@@ -0,0 +1,209 @@
1
+ import { Plugin, DeployData, Next, MountData, InvokeData, UseifyPlugin } from '@faasjs/func';
2
+ import { Logger } from '@faasjs/logger';
3
+
4
+ type SessionOptions = {
5
+ key: string;
6
+ secret: string;
7
+ salt?: string;
8
+ signedSalt?: string;
9
+ keylen?: number;
10
+ iterations?: number;
11
+ digest?: string;
12
+ cipherName?: string;
13
+ };
14
+ type SessionContent = string | number | {
15
+ [key: string]: any;
16
+ } | null | undefined;
17
+ declare class Session<S extends Record<string, string> = any, C extends Record<string, string> = any> {
18
+ content: Record<string, string | number>;
19
+ readonly config: {
20
+ key: string;
21
+ secret: string;
22
+ salt: string;
23
+ signedSalt: string;
24
+ keylen: number;
25
+ iterations: number;
26
+ digest: string;
27
+ cipherName: string;
28
+ };
29
+ private readonly secret;
30
+ private readonly signedSecret;
31
+ private readonly cookie;
32
+ private changed?;
33
+ constructor(cookie: Cookie<C, S>, config: SessionOptions);
34
+ invoke(cookie?: string): void;
35
+ encode(text: SessionContent): string;
36
+ decode<TData = any>(text: string): TData | SessionContent;
37
+ read(key: string): string | number;
38
+ write(key: string, value?: string | number | null): Session<S, C>;
39
+ update(): Session<S, C>;
40
+ }
41
+
42
+ type CookieOptions = {
43
+ domain?: string;
44
+ path?: string;
45
+ expires?: number;
46
+ secure?: boolean;
47
+ httpOnly?: boolean;
48
+ sameSite?: 'Strict' | 'Lax' | 'None';
49
+ session?: SessionOptions;
50
+ [key: string]: any;
51
+ };
52
+ declare class Cookie<C extends Record<string, string> = any, S extends Record<string, string> = any> {
53
+ session: Session<S, C>;
54
+ content: Record<string, string>;
55
+ readonly config: {
56
+ domain?: string;
57
+ path: string;
58
+ expires: number;
59
+ secure: boolean;
60
+ httpOnly: boolean;
61
+ sameSite?: 'Strict' | 'Lax' | 'None';
62
+ session: SessionOptions;
63
+ };
64
+ private setCookie;
65
+ constructor(config: CookieOptions);
66
+ invoke(cookie: string | undefined): Cookie<C, S>;
67
+ read(key: string): any;
68
+ write(key: string, value: string, opts?: {
69
+ domain?: string;
70
+ path?: string;
71
+ expires?: number | string;
72
+ secure?: boolean;
73
+ httpOnly?: boolean;
74
+ sameSite?: 'Strict' | 'Lax' | 'None';
75
+ }): Cookie<C, S>;
76
+ headers(): {
77
+ 'Set-Cookie'?: string[];
78
+ };
79
+ }
80
+
81
+ type ValidatorRuleOptionsType = 'string' | 'number' | 'boolean' | 'object' | 'array';
82
+ type ValidatorRuleOptions = {
83
+ type?: ValidatorRuleOptionsType;
84
+ required?: boolean;
85
+ in?: any[];
86
+ default?: any;
87
+ config?: Partial<ValidatorOptions>;
88
+ regexp?: RegExp;
89
+ };
90
+ type ValidatorOptions<Content = Record<string, any>> = {
91
+ whitelist?: 'error' | 'ignore';
92
+ rules: {
93
+ [k in keyof Content]?: ValidatorRuleOptions;
94
+ };
95
+ onError?: (type: string, key: string | string[], value?: any) => {
96
+ statusCode?: number;
97
+ message: any;
98
+ } | void;
99
+ };
100
+ type Request<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
101
+ headers: {
102
+ [key: string]: string;
103
+ };
104
+ params?: TParams;
105
+ cookie?: Cookie<TCookie, TSession>;
106
+ session?: Session<TSession, TCookie>;
107
+ };
108
+ type BeforeOption<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = (request: Request<TParams, TCookie, TSession>) => Promise<void | {
109
+ statusCode?: number;
110
+ message: string;
111
+ }>;
112
+ type ValidatorConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
113
+ params?: ValidatorOptions<TParams>;
114
+ cookie?: ValidatorOptions<TCookie>;
115
+ session?: ValidatorOptions<TSession>;
116
+ before?: BeforeOption;
117
+ };
118
+ declare class Validator<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> {
119
+ before?: BeforeOption<TParams, TCookie, TSession>;
120
+ paramsConfig?: ValidatorOptions<TParams>;
121
+ cookieConfig?: ValidatorOptions<TCookie>;
122
+ sessionConfig?: ValidatorOptions<TSession>;
123
+ private request;
124
+ constructor(config: ValidatorConfig<TParams, TCookie, TSession>);
125
+ valid(request: Request<TParams, TCookie, TSession>, logger: Logger): Promise<void>;
126
+ validContent(type: string, params: {
127
+ [key: string]: any;
128
+ }, baseKey: string, config: ValidatorOptions, logger: Logger): void;
129
+ }
130
+
131
+ declare const ContentType: {
132
+ [key: string]: string;
133
+ };
134
+ type HttpConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
135
+ [key: string]: any;
136
+ name?: string;
137
+ config?: {
138
+ [key: string]: any;
139
+ /** POST as default */
140
+ method?: 'BEGIN' | 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PUT' | 'OPTIONS' | 'TRACE' | 'PATCH' | 'ANY';
141
+ timeout?: number;
142
+ /** file relative path as default */
143
+ path?: string;
144
+ ignorePathPrefix?: string;
145
+ functionName?: string;
146
+ cookie?: CookieOptions;
147
+ };
148
+ validator?: ValidatorConfig<TParams, TCookie, TSession>;
149
+ };
150
+ type Response = {
151
+ statusCode?: number;
152
+ headers?: {
153
+ [key: string]: string;
154
+ };
155
+ body?: string;
156
+ message?: string;
157
+ };
158
+ declare class HttpError extends Error {
159
+ readonly statusCode: number;
160
+ readonly message: string;
161
+ constructor({ statusCode, message }: {
162
+ statusCode?: number;
163
+ message: string;
164
+ });
165
+ }
166
+ declare class Http<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> implements Plugin {
167
+ readonly type: string;
168
+ readonly name: string;
169
+ headers: {
170
+ [key: string]: string;
171
+ };
172
+ body: any;
173
+ params: TParams;
174
+ cookie: Cookie<TCookie, TSession>;
175
+ session: Session<TSession, TCookie>;
176
+ config: HttpConfig<TParams, TCookie, TSession>;
177
+ private readonly validatorOptions?;
178
+ private response?;
179
+ private validator?;
180
+ constructor(config?: HttpConfig<TParams, TCookie, TSession>);
181
+ onDeploy(data: DeployData, next: Next): Promise<void>;
182
+ onMount(data: MountData, next: Next): Promise<void>;
183
+ onInvoke(data: InvokeData, next: Next): Promise<void>;
184
+ /**
185
+ * set header
186
+ * @param key {string} key
187
+ * @param value {*} value
188
+ */
189
+ setHeader(key: string, value: string): Http<TParams, TCookie, TSession>;
190
+ /**
191
+ * set Content-Type
192
+ * @param type {string} 类型
193
+ * @param charset {string} 编码
194
+ */
195
+ setContentType(type: string, charset?: string): Http<TParams, TCookie, TSession>;
196
+ /**
197
+ * set status code
198
+ * @param code {number} 状态码
199
+ */
200
+ setStatusCode(code: number): Http<TParams, TCookie, TSession>;
201
+ /**
202
+ * set body
203
+ * @param body {*} 内容
204
+ */
205
+ setBody(body: string): Http<TParams, TCookie, TSession>;
206
+ }
207
+ declare function useHttp<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any>(config?: HttpConfig<TParams, TCookie, TSession>): UseifyPlugin<Http<TParams, TCookie, TSession>>;
208
+
209
+ export { ContentType, Cookie, CookieOptions, Http, HttpConfig, HttpError, Response, Session, SessionOptions, Validator, ValidatorConfig, ValidatorOptions, ValidatorRuleOptions, useHttp };
package/dist/index.js CHANGED
@@ -30,35 +30,7 @@ __export(src_exports, {
30
30
  });
31
31
  module.exports = __toCommonJS(src_exports);
32
32
  var import_func = require("@faasjs/func");
33
-
34
- // ../deep_merge/src/index.ts
35
- var shouldMerge = function(item) {
36
- const type = Object.prototype.toString.call(item);
37
- return type === "[object Object]" || type === "[object Array]";
38
- };
39
- function deepMerge(...sources) {
40
- let acc = /* @__PURE__ */ Object.create(null);
41
- for (const source of sources)
42
- if (source instanceof Array) {
43
- if (!(acc instanceof Array))
44
- acc = [];
45
- acc = [...new Set(source.concat(...acc))];
46
- } else if (shouldMerge(source))
47
- for (const [key, value] of Object.entries(source)) {
48
- let val;
49
- if (shouldMerge(value))
50
- val = deepMerge(acc[key], value);
51
- else
52
- val = value;
53
- acc = {
54
- ...acc,
55
- [key]: val
56
- };
57
- }
58
- return acc;
59
- }
60
-
61
- // src/index.ts
33
+ var import_deep_merge2 = require("@faasjs/deep_merge");
62
34
  var import_logger = require("@faasjs/logger");
63
35
 
64
36
  // src/session.ts
@@ -152,9 +124,10 @@ var Session = class {
152
124
  };
153
125
 
154
126
  // src/cookie.ts
127
+ var import_deep_merge = require("@faasjs/deep_merge");
155
128
  var Cookie = class {
156
129
  constructor(config) {
157
- this.config = deepMerge({
130
+ this.config = (0, import_deep_merge.deepMerge)({
158
131
  path: "/",
159
132
  expires: 31536e3,
160
133
  secure: true,
@@ -411,14 +384,14 @@ var ContentType = {
411
384
  json: "application/json",
412
385
  jsonp: "application/javascript"
413
386
  };
414
- var HttpError = class extends Error {
387
+ var HttpError = class _HttpError extends Error {
415
388
  constructor({
416
389
  statusCode,
417
390
  message
418
391
  }) {
419
392
  super(message);
420
393
  if (Error.captureStackTrace)
421
- Error.captureStackTrace(this, HttpError);
394
+ Error.captureStackTrace(this, _HttpError);
422
395
  this.statusCode = statusCode || 500;
423
396
  this.message = message;
424
397
  }
@@ -440,7 +413,7 @@ var Http = class {
440
413
  const logger = new import_logger.Logger(this.name);
441
414
  logger.debug("Generate api gateway's config");
442
415
  logger.debug("%j", data);
443
- const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
416
+ const config = data.config.plugins ? (0, import_deep_merge2.deepMerge)(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
444
417
  if (!config.config.path) {
445
418
  config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
446
419
  if (config.config.path === "/index")
@@ -476,7 +449,7 @@ var Http = class {
476
449
  this.config[key] = value;
477
450
  }
478
451
  if (data.config.plugins && data.config.plugins[this.name || this.type])
479
- this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
452
+ this.config = (0, import_deep_merge2.deepMerge)(this.config, data.config.plugins[this.name || this.type].config);
480
453
  data.logger.debug("[onMount] prepare cookie & session");
481
454
  this.cookie = new Cookie(this.config.cookie || {});
482
455
  this.session = this.cookie.session;
@@ -572,10 +545,20 @@ var Http = class {
572
545
  delete data.response.headers["Content-Encoding"];
573
546
  }
574
547
  }
548
+ /**
549
+ * set header
550
+ * @param key {string} key
551
+ * @param value {*} value
552
+ */
575
553
  setHeader(key, value) {
576
554
  this.response.headers[key] = value;
577
555
  return this;
578
556
  }
557
+ /**
558
+ * set Content-Type
559
+ * @param type {string} 类型
560
+ * @param charset {string} 编码
561
+ */
579
562
  setContentType(type, charset = "utf-8") {
580
563
  if (ContentType[type])
581
564
  this.setHeader("Content-Type", `${ContentType[type]}; charset=${charset}`);
@@ -583,10 +566,18 @@ var Http = class {
583
566
  this.setHeader("Content-Type", `${type}; charset=${charset}`);
584
567
  return this;
585
568
  }
569
+ /**
570
+ * set status code
571
+ * @param code {number} 状态码
572
+ */
586
573
  setStatusCode(code) {
587
574
  this.response.statusCode = code;
588
575
  return this;
589
576
  }
577
+ /**
578
+ * set body
579
+ * @param body {*} 内容
580
+ */
590
581
  setBody(body) {
591
582
  this.response.body = body;
592
583
  return this;
package/dist/index.mjs CHANGED
@@ -3,42 +3,14 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
3
3
  }) : x)(function(x) {
4
4
  if (typeof require !== "undefined")
5
5
  return require.apply(this, arguments);
6
- throw new Error('Dynamic require of "' + x + '" is not supported');
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
7
  });
8
8
 
9
9
  // src/index.ts
10
10
  import {
11
11
  usePlugin
12
12
  } from "@faasjs/func";
13
-
14
- // ../deep_merge/src/index.ts
15
- var shouldMerge = function(item) {
16
- const type = Object.prototype.toString.call(item);
17
- return type === "[object Object]" || type === "[object Array]";
18
- };
19
- function deepMerge(...sources) {
20
- let acc = /* @__PURE__ */ Object.create(null);
21
- for (const source of sources)
22
- if (source instanceof Array) {
23
- if (!(acc instanceof Array))
24
- acc = [];
25
- acc = [...new Set(source.concat(...acc))];
26
- } else if (shouldMerge(source))
27
- for (const [key, value] of Object.entries(source)) {
28
- let val;
29
- if (shouldMerge(value))
30
- val = deepMerge(acc[key], value);
31
- else
32
- val = value;
33
- acc = {
34
- ...acc,
35
- [key]: val
36
- };
37
- }
38
- return acc;
39
- }
40
-
41
- // src/index.ts
13
+ import { deepMerge as deepMerge2 } from "@faasjs/deep_merge";
42
14
  import { Logger } from "@faasjs/logger";
43
15
 
44
16
  // src/session.ts
@@ -138,6 +110,7 @@ var Session = class {
138
110
  };
139
111
 
140
112
  // src/cookie.ts
113
+ import { deepMerge } from "@faasjs/deep_merge";
141
114
  var Cookie = class {
142
115
  constructor(config) {
143
116
  this.config = deepMerge({
@@ -401,14 +374,14 @@ var ContentType = {
401
374
  json: "application/json",
402
375
  jsonp: "application/javascript"
403
376
  };
404
- var HttpError = class extends Error {
377
+ var HttpError = class _HttpError extends Error {
405
378
  constructor({
406
379
  statusCode,
407
380
  message
408
381
  }) {
409
382
  super(message);
410
383
  if (Error.captureStackTrace)
411
- Error.captureStackTrace(this, HttpError);
384
+ Error.captureStackTrace(this, _HttpError);
412
385
  this.statusCode = statusCode || 500;
413
386
  this.message = message;
414
387
  }
@@ -430,7 +403,7 @@ var Http = class {
430
403
  const logger = new Logger(this.name);
431
404
  logger.debug("Generate api gateway's config");
432
405
  logger.debug("%j", data);
433
- const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
406
+ const config = data.config.plugins ? deepMerge2(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
434
407
  if (!config.config.path) {
435
408
  config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
436
409
  if (config.config.path === "/index")
@@ -466,7 +439,7 @@ var Http = class {
466
439
  this.config[key] = value;
467
440
  }
468
441
  if (data.config.plugins && data.config.plugins[this.name || this.type])
469
- this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
442
+ this.config = deepMerge2(this.config, data.config.plugins[this.name || this.type].config);
470
443
  data.logger.debug("[onMount] prepare cookie & session");
471
444
  this.cookie = new Cookie(this.config.cookie || {});
472
445
  this.session = this.cookie.session;
@@ -562,10 +535,20 @@ var Http = class {
562
535
  delete data.response.headers["Content-Encoding"];
563
536
  }
564
537
  }
538
+ /**
539
+ * set header
540
+ * @param key {string} key
541
+ * @param value {*} value
542
+ */
565
543
  setHeader(key, value) {
566
544
  this.response.headers[key] = value;
567
545
  return this;
568
546
  }
547
+ /**
548
+ * set Content-Type
549
+ * @param type {string} 类型
550
+ * @param charset {string} 编码
551
+ */
569
552
  setContentType(type, charset = "utf-8") {
570
553
  if (ContentType[type])
571
554
  this.setHeader("Content-Type", `${ContentType[type]}; charset=${charset}`);
@@ -573,10 +556,18 @@ var Http = class {
573
556
  this.setHeader("Content-Type", `${type}; charset=${charset}`);
574
557
  return this;
575
558
  }
559
+ /**
560
+ * set status code
561
+ * @param code {number} 状态码
562
+ */
576
563
  setStatusCode(code) {
577
564
  this.response.statusCode = code;
578
565
  return this;
579
566
  }
567
+ /**
568
+ * set body
569
+ * @param body {*} 内容
570
+ */
580
571
  setBody(body) {
581
572
  this.response.body = body;
582
573
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/http",
3
- "version": "0.0.3-beta.85",
3
+ "version": "0.0.3-beta.87",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,14 +16,14 @@
16
16
  },
17
17
  "funding": "https://github.com/sponsors/faasjs",
18
18
  "scripts": {
19
- "build": "tsup-node src/index.ts --format esm,cjs --dts --clean"
19
+ "build": "tsup-node src/index.ts --config ../../tsup.config.json"
20
20
  },
21
21
  "files": [
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@faasjs/func": "^0.0.3-beta.85",
26
- "@faasjs/logger": "^0.0.3-beta.85"
25
+ "@faasjs/func": "^0.0.3-beta.87",
26
+ "@faasjs/logger": "^0.0.3-beta.87"
27
27
  },
28
28
  "engines": {
29
29
  "npm": ">=8.0.0",