@faasjs/http 0.0.2-beta.99 → 0.0.3-beta.2

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
+ | `onError?` | (`type`: `string`, `key`: `string` \| `string`[], `value?`: `any`) => { `message`: `any` ; `statusCode?`: `number` } \| `void` |
169
+ | `rules` | { [k in keyof Content]?: ValidatorRuleOptions } |
170
+ | `whitelist?` | ``"error"`` \| ``"ignore"`` |
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,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>): Http<TParams, TCookie, TSession> & UseifyPlugin;
208
+
209
+ export { ContentType, Cookie, CookieOptions, Http, HttpConfig, HttpError, Response, Session, SessionOptions, Validator, ValidatorConfig, ValidatorOptions, ValidatorRuleOptions, useHttp };