@faasjs/http 0.0.2-beta.26 → 0.0.2-beta.260

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/lib/cookie.d.ts CHANGED
@@ -9,11 +9,9 @@ export interface CookieOptions {
9
9
  session?: SessionOptions;
10
10
  [key: string]: any;
11
11
  }
12
- export declare class Cookie {
13
- session: Session;
14
- content: {
15
- [key: string]: any;
16
- };
12
+ export declare class Cookie<C, S> {
13
+ session: Session<S, C>;
14
+ content: C;
17
15
  readonly config: {
18
16
  domain?: string;
19
17
  path: string;
@@ -25,19 +23,17 @@ export declare class Cookie {
25
23
  };
26
24
  private setCookie;
27
25
  constructor(config: CookieOptions);
28
- invoke(cookie: string | undefined): this;
26
+ invoke(cookie: string | undefined): Cookie<C, S>;
29
27
  read(key: string): any;
30
- write(key: string, value: any, opts?: {
28
+ write(key: string, value: string, opts?: {
31
29
  domain?: string;
32
30
  path?: string;
33
31
  expires?: number | string;
34
32
  secure?: boolean;
35
33
  httpOnly?: boolean;
36
34
  sameSite?: 'Strict' | 'Lax' | 'None';
37
- }): this;
35
+ }): Cookie<C, S>;
38
36
  headers(): {
39
- 'Set-Cookie'?: undefined;
40
- } | {
41
- 'Set-Cookie': string[];
37
+ 'Set-Cookie'?: string[];
42
38
  };
43
39
  }
package/lib/index.d.ts CHANGED
@@ -1,59 +1,76 @@
1
- import { Plugin, InvokeData, MountData, DeployData, Next } from '@faasjs/func';
1
+ import { Plugin, InvokeData, MountData, DeployData, Next, UseifyPlugin } from '@faasjs/func';
2
2
  import { Cookie, CookieOptions } from './cookie';
3
3
  import { Session, SessionOptions } from './session';
4
- import { Validator, ValidatorOptions, ValidatorRuleOptions } from './validator';
4
+ import { Validator, ValidatorOptions, ValidatorRuleOptions, ValidatorConfig } from './validator';
5
5
  export { Cookie, CookieOptions, Session, SessionOptions, Validator, ValidatorOptions, ValidatorRuleOptions };
6
6
  export declare const ContentType: {
7
7
  [key: string]: string;
8
8
  };
9
- export interface HttpConfig {
9
+ export declare type HttpConfig<TParams = {
10
+ [key: string]: any;
11
+ }, TCookie = {
12
+ [key: string]: any;
13
+ }, TSession = {
14
+ [key: string]: any;
15
+ }> = {
10
16
  [key: string]: any;
11
17
  name?: string;
12
18
  config?: {
13
19
  [key: string]: any;
14
- method?: string;
20
+ method?: 'BEGIN' | 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PUT' | 'OPTIONS' | 'TRACE' | 'PATCH' | 'ANY';
15
21
  timeout?: number;
22
+ path?: string;
23
+ ignorePathPrefix?: string;
16
24
  functionName?: string;
17
25
  cookie?: CookieOptions;
18
26
  };
19
- validator?: {
20
- params?: ValidatorOptions;
21
- cookie?: ValidatorOptions;
22
- session?: ValidatorOptions;
23
- };
24
- }
27
+ validator?: ValidatorConfig<TParams, TCookie, TSession>;
28
+ };
25
29
  export interface Response {
26
30
  statusCode?: number;
27
- headers: {
28
- [key: string]: any;
31
+ headers?: {
32
+ [key: string]: string;
29
33
  };
30
34
  body?: string;
35
+ message?: string;
31
36
  }
32
- export declare class Http implements Plugin {
37
+ export declare class HttpError extends Error {
38
+ readonly statusCode: number;
39
+ readonly message: string;
40
+ constructor({ statusCode, message }: {
41
+ statusCode?: number;
42
+ message: string;
43
+ });
44
+ }
45
+ export declare class Http<TParams = {
46
+ [key: string]: any;
47
+ }, TCookie = {
48
+ [key: string]: string;
49
+ }, TSession = {
50
+ [key: string]: any;
51
+ }> implements Plugin {
33
52
  readonly type: string;
34
- name?: string;
53
+ readonly name: string;
35
54
  headers: {
36
55
  [key: string]: string;
37
56
  };
38
- params: any;
39
- cookie: Cookie;
40
- session: Session;
41
- config: {
42
- [key: string]: any;
43
- method?: number;
44
- timeout?: number;
45
- functionName?: string;
46
- cookie?: CookieOptions;
47
- };
48
- private validatorOptions?;
57
+ params: TParams;
58
+ cookie: Cookie<TCookie, TSession>;
59
+ session: Session<TSession, TCookie>;
60
+ config: HttpConfig<TParams, TCookie, TSession>;
61
+ private readonly validatorOptions?;
49
62
  private response?;
50
63
  private validator?;
51
- private logger;
64
+ private readonly logger;
52
65
  /**
53
66
  * 创建 Http 插件实例
54
67
  * @param config {object} 配置项
55
68
  * @param config.name {string} 配置名
56
69
  * @param config.config {object} 网关配置
70
+ * @param config.config.method {string} 请求方法,默认为 POST
71
+ * @param config.config.path {string} 请求路径,默认为云函数文件路径
72
+ * @param config.config.ignorePathPrefix {string} 排除的路径前缀,当设置 path 时无效
73
+ * @param config.config.cookie {object} Cookie 配置
57
74
  * @param config.validator {object} 入参校验配置
58
75
  * @param config.validator.params {object} params 校验配置
59
76
  * @param config.validator.params.whitelist {string} 白名单配置
@@ -67,8 +84,9 @@ export declare class Http implements Plugin {
67
84
  * @param config.validator.session.whitelist {string} 白名单配置
68
85
  * @param config.validator.session.onError {function} 自定义报错
69
86
  * @param config.validator.session.rules {object} 参数校验规则
87
+ * @param config.validator.before {function} 参数校验前自定义函数
70
88
  */
71
- constructor(config?: HttpConfig);
89
+ constructor(config?: HttpConfig<TParams, TCookie, TSession>);
72
90
  onDeploy(data: DeployData, next: Next): Promise<void>;
73
91
  onMount(data: MountData, next: Next): Promise<void>;
74
92
  onInvoke(data: InvokeData, next: Next): Promise<void>;
@@ -77,21 +95,28 @@ export declare class Http implements Plugin {
77
95
  * @param key {string} key
78
96
  * @param value {*} value
79
97
  */
80
- setHeader(key: string, value: any): Http;
98
+ setHeader(key: string, value: string): Http<TParams, TCookie, TSession>;
81
99
  /**
82
100
  * 设置 Content-Type
83
101
  * @param type {string} 类型
84
102
  * @param charset {string} 编码
85
103
  */
86
- setContentType(type: string, charset?: string): Http;
104
+ setContentType(type: string, charset?: string): Http<TParams, TCookie, TSession>;
87
105
  /**
88
106
  * 设置状态码
89
107
  * @param code {number} 状态码
90
108
  */
91
- setStatusCode(code: number): Http;
109
+ setStatusCode(code: number): Http<TParams, TCookie, TSession>;
92
110
  /**
93
111
  * 设置 body
94
112
  * @param body {*} 内容
95
113
  */
96
- setBody(body: string): Http;
114
+ setBody(body: string): Http<TParams, TCookie, TSession>;
97
115
  }
116
+ export declare function useHttp<TParams = {
117
+ [key: string]: any;
118
+ }, TCookie = {
119
+ [key: string]: any;
120
+ }, TSession = {
121
+ [key: string]: any;
122
+ }>(config?: HttpConfig<TParams, TCookie, TSession>): Http<TParams, TCookie, TSession> & UseifyPlugin;