@faasjs/http 0.0.2-beta.39 → 0.0.2-beta.390

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
@@ -1,9 +1,221 @@
1
1
  # @faasjs/http
2
2
 
3
- http 插件
4
-
5
- [![License: MIT](https://img.shields.io/npm/l/@faasjs/http.svg)](https://github.com/faasjs/faasjs/blob/master/packages/faasjs/http/LICENSE)
3
+ [![License: MIT](https://img.shields.io/npm/l/@faasjs/http.svg)](https://github.com/faasjs/faasjs/blob/main/packages/faasjs/http/LICENSE)
6
4
  [![NPM Stable Version](https://img.shields.io/npm/v/@faasjs/http/stable.svg)](https://www.npmjs.com/package/@faasjs/http)
7
5
  [![NPM Beta Version](https://img.shields.io/npm/v/@faasjs/http/beta.svg)](https://www.npmjs.com/package/@faasjs/http)
8
6
 
9
- https://faasjs.com/doc/http.html
7
+ FaasJS's http plugin.
8
+
9
+ ## Install
10
+
11
+ npm install @faasjs/http
12
+
13
+ ## Modules
14
+
15
+ ### Classes
16
+
17
+ - [Cookie](classes/Cookie.md)
18
+ - [Http](classes/Http.md)
19
+ - [HttpError](classes/HttpError.md)
20
+ - [Session](classes/Session.md)
21
+ - [Validator](classes/Validator.md)
22
+
23
+ ### Type aliases
24
+
25
+ - [CookieOptions](#cookieoptions)
26
+ - [HttpConfig](#httpconfig)
27
+ - [Response](#response)
28
+ - [SessionOptions](#sessionoptions)
29
+ - [ValidatorConfig](#validatorconfig)
30
+ - [ValidatorOptions](#validatoroptions)
31
+ - [ValidatorRuleOptions](#validatorruleoptions)
32
+
33
+ ### Variables
34
+
35
+ - [ContentType](#contenttype)
36
+
37
+ ### Functions
38
+
39
+ - [useHttp](#usehttp)
40
+
41
+ ## Type aliases
42
+
43
+ ### CookieOptions
44
+
45
+ Ƭ **CookieOptions**: `Object`
46
+
47
+ #### Index signature
48
+
49
+ ▪ [key: `string`]: `any`
50
+
51
+ #### Type declaration
52
+
53
+ | Name | Type |
54
+ | :------ | :------ |
55
+ | `domain?` | `string` |
56
+ | `expires?` | `number` |
57
+ | `httpOnly?` | `boolean` |
58
+ | `path?` | `string` |
59
+ | `sameSite?` | ``"Strict"`` \| ``"Lax"`` \| ``"None"`` |
60
+ | `secure?` | `boolean` |
61
+ | `session?` | [`SessionOptions`](#sessionoptions) |
62
+
63
+ ___
64
+
65
+ ### HttpConfig
66
+
67
+ Ƭ **HttpConfig**<`TParams`, `TCookie`, `TSession`\>: `Object`
68
+
69
+ #### Type parameters
70
+
71
+ | Name | Type |
72
+ | :------ | :------ |
73
+ | `TParams` | extends `Record`<`string`, `any`\> = `any` |
74
+ | `TCookie` | extends `Record`<`string`, `string`\> = `any` |
75
+ | `TSession` | extends `Record`<`string`, `string`\> = `any` |
76
+
77
+ #### Index signature
78
+
79
+ ▪ [key: `string`]: `any`
80
+
81
+ #### Type declaration
82
+
83
+ | Name | Type |
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` } |
86
+ | `config.cookie?` | [`CookieOptions`](#cookieoptions) |
87
+ | `config.functionName?` | `string` |
88
+ | `config.ignorePathPrefix?` | `string` |
89
+ | `config.method?` | ``"BEGIN"`` \| ``"GET"`` \| ``"POST"`` \| ``"DELETE"`` \| ``"HEAD"`` \| ``"PUT"`` \| ``"OPTIONS"`` \| ``"TRACE"`` \| ``"PATCH"`` \| ``"ANY"`` |
90
+ | `config.path?` | `string` |
91
+ | `config.timeout?` | `number` |
92
+ | `name?` | `string` |
93
+ | `validator?` | [`ValidatorConfig`](#validatorconfig)<`TParams`, `TCookie`, `TSession`\> |
94
+
95
+ ___
96
+
97
+ ### Response
98
+
99
+ Ƭ **Response**: `Object`
100
+
101
+ #### Type declaration
102
+
103
+ | Name | Type |
104
+ | :------ | :------ |
105
+ | `body?` | `string` |
106
+ | `headers?` | { `[key: string]`: `string`; } |
107
+ | `message?` | `string` |
108
+ | `statusCode?` | `number` |
109
+
110
+ ___
111
+
112
+ ### SessionOptions
113
+
114
+ Ƭ **SessionOptions**: `Object`
115
+
116
+ #### Type declaration
117
+
118
+ | Name | Type |
119
+ | :------ | :------ |
120
+ | `cipherName?` | `string` |
121
+ | `digest?` | `string` |
122
+ | `iterations?` | `number` |
123
+ | `key` | `string` |
124
+ | `keylen?` | `number` |
125
+ | `salt?` | `string` |
126
+ | `secret` | `string` |
127
+ | `signedSalt?` | `string` |
128
+
129
+ ___
130
+
131
+ ### ValidatorConfig
132
+
133
+ Ƭ **ValidatorConfig**<`TParams`, `TCookie`, `TSession`\>: `Object`
134
+
135
+ #### Type parameters
136
+
137
+ | Name | Type |
138
+ | :------ | :------ |
139
+ | `TParams` | extends `Record`<`string`, `any`\> = `any` |
140
+ | `TCookie` | extends `Record`<`string`, `string`\> = `any` |
141
+ | `TSession` | extends `Record`<`string`, `string`\> = `any` |
142
+
143
+ #### Type declaration
144
+
145
+ | Name | Type |
146
+ | :------ | :------ |
147
+ | `before?` | `BeforeOption` |
148
+ | `cookie?` | [`ValidatorOptions`](#validatoroptions)<`TCookie`\> |
149
+ | `params?` | [`ValidatorOptions`](#validatoroptions)<`TParams`\> |
150
+ | `session?` | [`ValidatorOptions`](#validatoroptions)<`TSession`\> |
151
+
152
+ ___
153
+
154
+ ### ValidatorOptions
155
+
156
+ Ƭ **ValidatorOptions**<`Content`\>: `Object`
157
+
158
+ #### Type parameters
159
+
160
+ | Name | Type |
161
+ | :------ | :------ |
162
+ | `Content` | `Record`<`string`, `any`\> |
163
+
164
+ #### Type declaration
165
+
166
+ | Name | Type |
167
+ | :------ | :------ |
168
+ | `rules` | { [k in keyof Content]?: ValidatorRuleOptions } |
169
+ | `whitelist?` | ``"error"`` \| ``"ignore"`` |
170
+ | `onError?` | (`type`: `string`, `key`: `string` \| `string`[], `value?`: `any`) => `void` \| { `message`: `any` ; `statusCode?`: `number` } |
171
+
172
+ ___
173
+
174
+ ### ValidatorRuleOptions
175
+
176
+ Ƭ **ValidatorRuleOptions**: `Object`
177
+
178
+ #### Type declaration
179
+
180
+ | Name | Type |
181
+ | :------ | :------ |
182
+ | `config?` | `Partial`<[`ValidatorOptions`](#validatoroptions)\> |
183
+ | `default?` | `any` |
184
+ | `in?` | `any`[] |
185
+ | `regexp?` | `RegExp` |
186
+ | `required?` | `boolean` |
187
+ | `type?` | `ValidatorRuleOptionsType` |
188
+
189
+ ## Variables
190
+
191
+ ### ContentType
192
+
193
+ • `Const` **ContentType**: `Object`
194
+
195
+ #### Index signature
196
+
197
+ ▪ [key: `string`]: `string`
198
+
199
+ ## Functions
200
+
201
+ ### useHttp
202
+
203
+ ▸ **useHttp**<`TParams`, `TCookie`, `TSession`\>(`config?`): [`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession`\> & `UseifyPlugin`
204
+
205
+ #### Type parameters
206
+
207
+ | Name | Type |
208
+ | :------ | :------ |
209
+ | `TParams` | extends `Record`<`string`, `any`\> = `any` |
210
+ | `TCookie` | extends `Record`<`string`, `string`\> = `any` |
211
+ | `TSession` | extends `Record`<`string`, `string`\> = `any` |
212
+
213
+ #### Parameters
214
+
215
+ | Name | Type |
216
+ | :------ | :------ |
217
+ | `config?` | [`HttpConfig`](#httpconfig)<`TParams`, `TCookie`, `TSession`\> |
218
+
219
+ #### Returns
220
+
221
+ [`Http`](classes/Http.md)<`TParams`, `TCookie`, `TSession`\> & `UseifyPlugin`
@@ -0,0 +1,238 @@
1
+ import { Plugin, DeployData, Next, MountData, InvokeData, UseifyPlugin } from '@faasjs/func';
2
+ import { Logger } from '@faasjs/logger';
3
+
4
+ declare 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
+ declare 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
+ declare 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
+ declare type ValidatorRuleOptionsType = 'string' | 'number' | 'boolean' | 'object' | 'array';
82
+ declare type ValidatorRuleOptions = {
83
+ type?: ValidatorRuleOptionsType;
84
+ required?: boolean;
85
+ in?: any[];
86
+ default?: any;
87
+ config?: Partial<ValidatorOptions>;
88
+ regexp?: RegExp;
89
+ };
90
+ declare 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
+ declare 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
+ declare 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
+ declare 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
+ private readonly logger;
125
+ constructor(config: {
126
+ params?: ValidatorOptions<TParams>;
127
+ cookie?: ValidatorOptions<TCookie>;
128
+ session?: ValidatorOptions<TSession>;
129
+ before?: BeforeOption<TParams, TCookie, TSession>;
130
+ }, logger: Logger);
131
+ valid(request: Request<TParams, TCookie, TSession>): Promise<void>;
132
+ validContent(type: string, params: {
133
+ [key: string]: any;
134
+ }, baseKey: string, config: ValidatorOptions): void;
135
+ }
136
+
137
+ declare const ContentType: {
138
+ [key: string]: string;
139
+ };
140
+ declare type HttpConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
141
+ [key: string]: any;
142
+ name?: string;
143
+ config?: {
144
+ [key: string]: any;
145
+ method?: 'BEGIN' | 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PUT' | 'OPTIONS' | 'TRACE' | 'PATCH' | 'ANY';
146
+ timeout?: number;
147
+ path?: string;
148
+ ignorePathPrefix?: string;
149
+ functionName?: string;
150
+ cookie?: CookieOptions;
151
+ };
152
+ validator?: ValidatorConfig<TParams, TCookie, TSession>;
153
+ };
154
+ declare type Response = {
155
+ statusCode?: number;
156
+ headers?: {
157
+ [key: string]: string;
158
+ };
159
+ body?: string;
160
+ message?: string;
161
+ };
162
+ declare class HttpError extends Error {
163
+ readonly statusCode: number;
164
+ readonly message: string;
165
+ constructor({ statusCode, message }: {
166
+ statusCode?: number;
167
+ message: string;
168
+ });
169
+ }
170
+ declare class Http<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> implements Plugin {
171
+ readonly type: string;
172
+ readonly name: string;
173
+ headers: {
174
+ [key: string]: string;
175
+ };
176
+ body: any;
177
+ params: TParams;
178
+ cookie: Cookie<TCookie, TSession>;
179
+ session: Session<TSession, TCookie>;
180
+ config: HttpConfig<TParams, TCookie, TSession>;
181
+ private readonly validatorOptions?;
182
+ private response?;
183
+ private validator?;
184
+ private readonly logger;
185
+ /**
186
+ * 创建 Http 插件实例
187
+ * @param config {object} 配置项
188
+ * @param config.name {string} 配置名
189
+ * @param config.config {object} 网关配置
190
+ * @param config.config.method {string} 请求方法,默认为 POST
191
+ * @param config.config.path {string} 请求路径,默认为云函数文件路径
192
+ * @param config.config.ignorePathPrefix {string} 排除的路径前缀,当设置 path 时无效
193
+ * @param config.config.cookie {object} Cookie 配置
194
+ * @param config.validator {object} 入参校验配置
195
+ * @param config.validator.params {object} params 校验配置
196
+ * @param config.validator.params.whitelist {string} 白名单配置
197
+ * @param config.validator.params.onError {function} 自定义报错
198
+ * @param config.validator.params.rules {object} 参数校验规则
199
+ * @param config.validator.cookie {object} cookie 校验配置
200
+ * @param config.validator.cookie.whitelist {string} 白名单配置
201
+ * @param config.validator.cookie.onError {function} 自定义报错
202
+ * @param config.validator.cookie.rules {object} 参数校验规则
203
+ * @param config.validator.session {object} session 校验配置
204
+ * @param config.validator.session.whitelist {string} 白名单配置
205
+ * @param config.validator.session.onError {function} 自定义报错
206
+ * @param config.validator.session.rules {object} 参数校验规则
207
+ * @param config.validator.before {function} 参数校验前自定义函数
208
+ */
209
+ constructor(config?: HttpConfig<TParams, TCookie, TSession>);
210
+ onDeploy(data: DeployData, next: Next): Promise<void>;
211
+ onMount(data: MountData, next: Next): Promise<void>;
212
+ onInvoke(data: InvokeData, next: Next): Promise<void>;
213
+ /**
214
+ * 设置 header
215
+ * @param key {string} key
216
+ * @param value {*} value
217
+ */
218
+ setHeader(key: string, value: string): Http<TParams, TCookie, TSession>;
219
+ /**
220
+ * 设置 Content-Type
221
+ * @param type {string} 类型
222
+ * @param charset {string} 编码
223
+ */
224
+ setContentType(type: string, charset?: string): Http<TParams, TCookie, TSession>;
225
+ /**
226
+ * 设置状态码
227
+ * @param code {number} 状态码
228
+ */
229
+ setStatusCode(code: number): Http<TParams, TCookie, TSession>;
230
+ /**
231
+ * 设置 body
232
+ * @param body {*} 内容
233
+ */
234
+ setBody(body: string): Http<TParams, TCookie, TSession>;
235
+ }
236
+ 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>): Http<TParams, TCookie, TSession> & UseifyPlugin;
237
+
238
+ export { ContentType, Cookie, CookieOptions, Http, HttpConfig, HttpError, Response, Session, SessionOptions, Validator, ValidatorConfig, ValidatorOptions, ValidatorRuleOptions, useHttp };