@faasjs/http 3.7.0 → 4.0.0
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 +0 -4
- package/dist/index.d.mts +5 -65
- package/dist/index.d.ts +5 -65
- package/dist/index.js +0 -202
- package/dist/index.mjs +1 -202
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
- [Http](classes/Http.md)
|
|
12
12
|
- [HttpError](classes/HttpError.md)
|
|
13
13
|
- [Session](classes/Session.md)
|
|
14
|
-
- [Validator](classes/Validator.md)
|
|
15
14
|
|
|
16
15
|
## Type Aliases
|
|
17
16
|
|
|
@@ -20,9 +19,6 @@
|
|
|
20
19
|
- [HttpFuncHandler](type-aliases/HttpFuncHandler.md)
|
|
21
20
|
- [Response](type-aliases/Response.md)
|
|
22
21
|
- [SessionOptions](type-aliases/SessionOptions.md)
|
|
23
|
-
- [ValidatorConfig](type-aliases/ValidatorConfig.md)
|
|
24
|
-
- [ValidatorOptions](type-aliases/ValidatorOptions.md)
|
|
25
|
-
- [ValidatorRuleOptions](type-aliases/ValidatorRuleOptions.md)
|
|
26
22
|
|
|
27
23
|
## Variables
|
|
28
24
|
|
package/dist/index.d.mts
CHANGED
|
@@ -80,66 +80,10 @@ declare class Cookie<C extends Record<string, string> = any, S extends Record<st
|
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
/** @deprecated */
|
|
84
|
-
type ValidatorRuleOptionsType = 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
85
|
-
/** @deprecated */
|
|
86
|
-
type ValidatorRuleOptions = {
|
|
87
|
-
type?: ValidatorRuleOptionsType;
|
|
88
|
-
required?: boolean;
|
|
89
|
-
in?: any[];
|
|
90
|
-
default?: any;
|
|
91
|
-
config?: Partial<ValidatorOptions>;
|
|
92
|
-
regexp?: RegExp;
|
|
93
|
-
};
|
|
94
|
-
/** @deprecated */
|
|
95
|
-
type ValidatorOptions<Content = Record<string, any>> = {
|
|
96
|
-
whitelist?: 'error' | 'ignore';
|
|
97
|
-
rules: {
|
|
98
|
-
[k in keyof Content]?: ValidatorRuleOptions;
|
|
99
|
-
};
|
|
100
|
-
onError?: (type: string, key: string | string[], value?: any) => {
|
|
101
|
-
statusCode?: number;
|
|
102
|
-
message: any;
|
|
103
|
-
} | void;
|
|
104
|
-
};
|
|
105
|
-
type Request<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
|
|
106
|
-
headers: {
|
|
107
|
-
[key: string]: string;
|
|
108
|
-
};
|
|
109
|
-
params?: TParams;
|
|
110
|
-
cookie?: Cookie<TCookie, TSession>;
|
|
111
|
-
session?: Session<TSession, TCookie>;
|
|
112
|
-
};
|
|
113
|
-
/** @deprecated */
|
|
114
|
-
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 | {
|
|
115
|
-
statusCode?: number;
|
|
116
|
-
message: string;
|
|
117
|
-
}>;
|
|
118
|
-
/** @deprecated */
|
|
119
|
-
type ValidatorConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
|
|
120
|
-
params?: ValidatorOptions<TParams>;
|
|
121
|
-
cookie?: ValidatorOptions<TCookie>;
|
|
122
|
-
session?: ValidatorOptions<TSession>;
|
|
123
|
-
before?: BeforeOption;
|
|
124
|
-
};
|
|
125
|
-
/** @deprecated */
|
|
126
|
-
declare class Validator<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> {
|
|
127
|
-
before?: BeforeOption<TParams, TCookie, TSession>;
|
|
128
|
-
paramsConfig?: ValidatorOptions<TParams>;
|
|
129
|
-
cookieConfig?: ValidatorOptions<TCookie>;
|
|
130
|
-
sessionConfig?: ValidatorOptions<TSession>;
|
|
131
|
-
private request;
|
|
132
|
-
constructor(config: ValidatorConfig<TParams, TCookie, TSession>);
|
|
133
|
-
valid(request: Request<TParams, TCookie, TSession>, logger: Logger): Promise<void>;
|
|
134
|
-
validContent(type: string, params: {
|
|
135
|
-
[key: string]: any;
|
|
136
|
-
}, baseKey: string, config: ValidatorOptions, logger: Logger): void;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
83
|
declare const ContentType: {
|
|
140
84
|
[key: string]: string;
|
|
141
85
|
};
|
|
142
|
-
type HttpConfig
|
|
86
|
+
type HttpConfig = {
|
|
143
87
|
[key: string]: any;
|
|
144
88
|
name?: string;
|
|
145
89
|
config?: {
|
|
@@ -153,8 +97,6 @@ type HttpConfig<TParams extends Record<string, any> = any, TCookie extends Recor
|
|
|
153
97
|
functionName?: string;
|
|
154
98
|
cookie?: CookieOptions;
|
|
155
99
|
};
|
|
156
|
-
/** @deprecated */
|
|
157
|
-
validator?: ValidatorConfig<TParams, TCookie, TSession>;
|
|
158
100
|
};
|
|
159
101
|
type Response = {
|
|
160
102
|
statusCode?: number;
|
|
@@ -182,11 +124,9 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
182
124
|
params: TParams;
|
|
183
125
|
cookie: Cookie<TCookie, TSession>;
|
|
184
126
|
session: Session<TSession, TCookie>;
|
|
185
|
-
config: HttpConfig
|
|
186
|
-
private readonly validatorOptions?;
|
|
127
|
+
config: HttpConfig;
|
|
187
128
|
private response?;
|
|
188
|
-
|
|
189
|
-
constructor(config?: HttpConfig<TParams, TCookie, TSession>);
|
|
129
|
+
constructor(config?: HttpConfig);
|
|
190
130
|
onMount(data: MountData, next: Next): Promise<void>;
|
|
191
131
|
onInvoke(data: InvokeData, next: Next): Promise<void>;
|
|
192
132
|
/**
|
|
@@ -212,7 +152,7 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
212
152
|
*/
|
|
213
153
|
setBody(body: string): Http<TParams, TCookie, TSession>;
|
|
214
154
|
}
|
|
215
|
-
declare function useHttp<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any>(config?: HttpConfig
|
|
155
|
+
declare function useHttp<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any>(config?: HttpConfig): UseifyPlugin<Http<TParams, TCookie, TSession>>;
|
|
216
156
|
type HttpFuncHandler<TParams extends Record<string, any> = Record<string, any>, TCookie extends Record<string, string> = Record<string, string>, TSession extends Record<string, any> = Record<string, any>, TResult = any> = (data: InvokeData<{
|
|
217
157
|
[key: string]: any;
|
|
218
158
|
params?: TParams;
|
|
@@ -242,4 +182,4 @@ declare function useHttpFunc<TParams extends Record<string, any> = Record<string
|
|
|
242
182
|
params?: TParams;
|
|
243
183
|
}, any, TResult>;
|
|
244
184
|
|
|
245
|
-
export { ContentType, Cookie, type CookieOptions, Http, type HttpConfig, HttpError, type HttpFuncHandler, type Response, Session, type SessionOptions,
|
|
185
|
+
export { ContentType, Cookie, type CookieOptions, Http, type HttpConfig, HttpError, type HttpFuncHandler, type Response, Session, type SessionOptions, useHttp, useHttpFunc };
|
package/dist/index.d.ts
CHANGED
|
@@ -80,66 +80,10 @@ declare class Cookie<C extends Record<string, string> = any, S extends Record<st
|
|
|
80
80
|
};
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
/** @deprecated */
|
|
84
|
-
type ValidatorRuleOptionsType = 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
85
|
-
/** @deprecated */
|
|
86
|
-
type ValidatorRuleOptions = {
|
|
87
|
-
type?: ValidatorRuleOptionsType;
|
|
88
|
-
required?: boolean;
|
|
89
|
-
in?: any[];
|
|
90
|
-
default?: any;
|
|
91
|
-
config?: Partial<ValidatorOptions>;
|
|
92
|
-
regexp?: RegExp;
|
|
93
|
-
};
|
|
94
|
-
/** @deprecated */
|
|
95
|
-
type ValidatorOptions<Content = Record<string, any>> = {
|
|
96
|
-
whitelist?: 'error' | 'ignore';
|
|
97
|
-
rules: {
|
|
98
|
-
[k in keyof Content]?: ValidatorRuleOptions;
|
|
99
|
-
};
|
|
100
|
-
onError?: (type: string, key: string | string[], value?: any) => {
|
|
101
|
-
statusCode?: number;
|
|
102
|
-
message: any;
|
|
103
|
-
} | void;
|
|
104
|
-
};
|
|
105
|
-
type Request<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
|
|
106
|
-
headers: {
|
|
107
|
-
[key: string]: string;
|
|
108
|
-
};
|
|
109
|
-
params?: TParams;
|
|
110
|
-
cookie?: Cookie<TCookie, TSession>;
|
|
111
|
-
session?: Session<TSession, TCookie>;
|
|
112
|
-
};
|
|
113
|
-
/** @deprecated */
|
|
114
|
-
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 | {
|
|
115
|
-
statusCode?: number;
|
|
116
|
-
message: string;
|
|
117
|
-
}>;
|
|
118
|
-
/** @deprecated */
|
|
119
|
-
type ValidatorConfig<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> = {
|
|
120
|
-
params?: ValidatorOptions<TParams>;
|
|
121
|
-
cookie?: ValidatorOptions<TCookie>;
|
|
122
|
-
session?: ValidatorOptions<TSession>;
|
|
123
|
-
before?: BeforeOption;
|
|
124
|
-
};
|
|
125
|
-
/** @deprecated */
|
|
126
|
-
declare class Validator<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any> {
|
|
127
|
-
before?: BeforeOption<TParams, TCookie, TSession>;
|
|
128
|
-
paramsConfig?: ValidatorOptions<TParams>;
|
|
129
|
-
cookieConfig?: ValidatorOptions<TCookie>;
|
|
130
|
-
sessionConfig?: ValidatorOptions<TSession>;
|
|
131
|
-
private request;
|
|
132
|
-
constructor(config: ValidatorConfig<TParams, TCookie, TSession>);
|
|
133
|
-
valid(request: Request<TParams, TCookie, TSession>, logger: Logger): Promise<void>;
|
|
134
|
-
validContent(type: string, params: {
|
|
135
|
-
[key: string]: any;
|
|
136
|
-
}, baseKey: string, config: ValidatorOptions, logger: Logger): void;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
83
|
declare const ContentType: {
|
|
140
84
|
[key: string]: string;
|
|
141
85
|
};
|
|
142
|
-
type HttpConfig
|
|
86
|
+
type HttpConfig = {
|
|
143
87
|
[key: string]: any;
|
|
144
88
|
name?: string;
|
|
145
89
|
config?: {
|
|
@@ -153,8 +97,6 @@ type HttpConfig<TParams extends Record<string, any> = any, TCookie extends Recor
|
|
|
153
97
|
functionName?: string;
|
|
154
98
|
cookie?: CookieOptions;
|
|
155
99
|
};
|
|
156
|
-
/** @deprecated */
|
|
157
|
-
validator?: ValidatorConfig<TParams, TCookie, TSession>;
|
|
158
100
|
};
|
|
159
101
|
type Response = {
|
|
160
102
|
statusCode?: number;
|
|
@@ -182,11 +124,9 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
182
124
|
params: TParams;
|
|
183
125
|
cookie: Cookie<TCookie, TSession>;
|
|
184
126
|
session: Session<TSession, TCookie>;
|
|
185
|
-
config: HttpConfig
|
|
186
|
-
private readonly validatorOptions?;
|
|
127
|
+
config: HttpConfig;
|
|
187
128
|
private response?;
|
|
188
|
-
|
|
189
|
-
constructor(config?: HttpConfig<TParams, TCookie, TSession>);
|
|
129
|
+
constructor(config?: HttpConfig);
|
|
190
130
|
onMount(data: MountData, next: Next): Promise<void>;
|
|
191
131
|
onInvoke(data: InvokeData, next: Next): Promise<void>;
|
|
192
132
|
/**
|
|
@@ -212,7 +152,7 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
212
152
|
*/
|
|
213
153
|
setBody(body: string): Http<TParams, TCookie, TSession>;
|
|
214
154
|
}
|
|
215
|
-
declare function useHttp<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any>(config?: HttpConfig
|
|
155
|
+
declare function useHttp<TParams extends Record<string, any> = any, TCookie extends Record<string, string> = any, TSession extends Record<string, string> = any>(config?: HttpConfig): UseifyPlugin<Http<TParams, TCookie, TSession>>;
|
|
216
156
|
type HttpFuncHandler<TParams extends Record<string, any> = Record<string, any>, TCookie extends Record<string, string> = Record<string, string>, TSession extends Record<string, any> = Record<string, any>, TResult = any> = (data: InvokeData<{
|
|
217
157
|
[key: string]: any;
|
|
218
158
|
params?: TParams;
|
|
@@ -242,4 +182,4 @@ declare function useHttpFunc<TParams extends Record<string, any> = Record<string
|
|
|
242
182
|
params?: TParams;
|
|
243
183
|
}, any, TResult>;
|
|
244
184
|
|
|
245
|
-
export { ContentType, Cookie, type CookieOptions, Http, type HttpConfig, HttpError, type HttpFuncHandler, type Response, Session, type SessionOptions,
|
|
185
|
+
export { ContentType, Cookie, type CookieOptions, Http, type HttpConfig, HttpError, type HttpFuncHandler, type Response, Session, type SessionOptions, useHttp, useHttpFunc };
|
package/dist/index.js
CHANGED
|
@@ -177,183 +177,6 @@ var Cookie = class {
|
|
|
177
177
|
}
|
|
178
178
|
};
|
|
179
179
|
|
|
180
|
-
// src/validator.ts
|
|
181
|
-
var Validator = class {
|
|
182
|
-
before;
|
|
183
|
-
paramsConfig;
|
|
184
|
-
cookieConfig;
|
|
185
|
-
sessionConfig;
|
|
186
|
-
request;
|
|
187
|
-
constructor(config) {
|
|
188
|
-
this.paramsConfig = config.params;
|
|
189
|
-
this.cookieConfig = config.cookie;
|
|
190
|
-
this.sessionConfig = config.session;
|
|
191
|
-
this.before = config.before;
|
|
192
|
-
}
|
|
193
|
-
async valid(request, logger) {
|
|
194
|
-
if (this.before) {
|
|
195
|
-
const result = await this.before(request);
|
|
196
|
-
if (result) throw new HttpError(result);
|
|
197
|
-
}
|
|
198
|
-
this.request = request;
|
|
199
|
-
if (this.paramsConfig && request.params) {
|
|
200
|
-
logger.debug("Valid Params");
|
|
201
|
-
this.validContent("params", request.params, "", this.paramsConfig, logger);
|
|
202
|
-
}
|
|
203
|
-
if (this.cookieConfig && request.cookie) {
|
|
204
|
-
logger.debug("Valid Cookie");
|
|
205
|
-
if (request.cookie == null) throw Error("Not found Cookie");
|
|
206
|
-
this.validContent(
|
|
207
|
-
"cookie",
|
|
208
|
-
request.cookie.content,
|
|
209
|
-
"",
|
|
210
|
-
this.cookieConfig,
|
|
211
|
-
logger
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
if (this.sessionConfig && request.session) {
|
|
215
|
-
logger.debug("Valid Session");
|
|
216
|
-
if (request.session == null) throw Error("Not found Session");
|
|
217
|
-
this.validContent(
|
|
218
|
-
"session",
|
|
219
|
-
request.session.content,
|
|
220
|
-
"",
|
|
221
|
-
this.sessionConfig,
|
|
222
|
-
logger
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
validContent(type, params, baseKey, config, logger) {
|
|
227
|
-
if (config.whitelist) {
|
|
228
|
-
const paramsKeys = Object.keys(params);
|
|
229
|
-
const rulesKeys = Object.keys(config.rules);
|
|
230
|
-
const diff = paramsKeys.filter((k) => !rulesKeys.includes(k));
|
|
231
|
-
if (diff.length > 0) {
|
|
232
|
-
if (config.whitelist === "error") {
|
|
233
|
-
const diffKeys = diff.map((k) => `${baseKey}${k}`);
|
|
234
|
-
const error = Error(
|
|
235
|
-
`[${type}] Not permitted keys: ${diffKeys.join(", ")}`
|
|
236
|
-
);
|
|
237
|
-
if (config.onError) {
|
|
238
|
-
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
239
|
-
if (res) throw new HttpError(res);
|
|
240
|
-
}
|
|
241
|
-
throw error;
|
|
242
|
-
}
|
|
243
|
-
if (config.whitelist === "ignore")
|
|
244
|
-
for (const key of diff) delete params[key];
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
for (const key in config.rules) {
|
|
248
|
-
const rule = config.rules[key];
|
|
249
|
-
if (!rule) continue;
|
|
250
|
-
let value = params[key];
|
|
251
|
-
if (rule.default) {
|
|
252
|
-
if (type === "cookie" || type === "session")
|
|
253
|
-
logger.warn("Cookie and Session not support default rule.");
|
|
254
|
-
else if (typeof value === "undefined" && rule.default) {
|
|
255
|
-
value = typeof rule.default === "function" ? rule.default(this.request) : rule.default;
|
|
256
|
-
params[key] = value;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
if (rule.required) {
|
|
260
|
-
if (typeof value === "undefined" || value === null) {
|
|
261
|
-
const error = Error(`[${type}] ${baseKey}${key} is required.`);
|
|
262
|
-
if (config.onError) {
|
|
263
|
-
const res = config.onError(
|
|
264
|
-
`${type}.rule.required`,
|
|
265
|
-
`${baseKey}${key}`,
|
|
266
|
-
value
|
|
267
|
-
);
|
|
268
|
-
if (res) throw new HttpError(res);
|
|
269
|
-
}
|
|
270
|
-
throw error;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (typeof value !== "undefined" && value !== null) {
|
|
274
|
-
if (rule.type)
|
|
275
|
-
if (type === "cookie") logger.warn("Cookie not support type rule");
|
|
276
|
-
else {
|
|
277
|
-
let typed = true;
|
|
278
|
-
switch (rule.type) {
|
|
279
|
-
case "array":
|
|
280
|
-
typed = Array.isArray(value);
|
|
281
|
-
break;
|
|
282
|
-
case "object":
|
|
283
|
-
typed = Object.prototype.toString.call(value) === "[object Object]";
|
|
284
|
-
break;
|
|
285
|
-
default:
|
|
286
|
-
typed = typeof value === rule.type;
|
|
287
|
-
break;
|
|
288
|
-
}
|
|
289
|
-
if (!typed) {
|
|
290
|
-
const error = Error(
|
|
291
|
-
`[${type}] ${baseKey}${key} must be a ${rule.type}.`
|
|
292
|
-
);
|
|
293
|
-
if (config.onError) {
|
|
294
|
-
const res = config.onError(
|
|
295
|
-
`${type}.rule.type`,
|
|
296
|
-
`${baseKey}${key}`,
|
|
297
|
-
value
|
|
298
|
-
);
|
|
299
|
-
if (res) throw new HttpError(res);
|
|
300
|
-
}
|
|
301
|
-
throw error;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
if (rule.regexp && (rule.type === "string" || !rule.type) && !rule.regexp.test(value)) {
|
|
305
|
-
const error = Error(
|
|
306
|
-
`[${type}] ${baseKey}${key} must match ${rule.regexp}.`
|
|
307
|
-
);
|
|
308
|
-
if (config.onError) {
|
|
309
|
-
const res = config.onError(
|
|
310
|
-
`${type}.rule.regexp`,
|
|
311
|
-
`${baseKey}${key}`,
|
|
312
|
-
value
|
|
313
|
-
);
|
|
314
|
-
if (res) throw new HttpError(res);
|
|
315
|
-
}
|
|
316
|
-
throw error;
|
|
317
|
-
}
|
|
318
|
-
if (rule.in && !rule.in.includes(value)) {
|
|
319
|
-
const error = Error(
|
|
320
|
-
`[${type}] ${baseKey}${key} must be in ${rule.in.join(", ")}.`
|
|
321
|
-
);
|
|
322
|
-
if (config.onError) {
|
|
323
|
-
const res = config.onError(
|
|
324
|
-
`${type}.rule.in`,
|
|
325
|
-
`${baseKey}${key}`,
|
|
326
|
-
value
|
|
327
|
-
);
|
|
328
|
-
if (res) throw new HttpError(res);
|
|
329
|
-
}
|
|
330
|
-
throw error;
|
|
331
|
-
}
|
|
332
|
-
if (rule.config) {
|
|
333
|
-
if (type === "cookie") logger.warn("Cookie not support nest rule.");
|
|
334
|
-
else if (Array.isArray(value))
|
|
335
|
-
for (const val of value)
|
|
336
|
-
this.validContent(
|
|
337
|
-
type,
|
|
338
|
-
val,
|
|
339
|
-
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
340
|
-
rule.config,
|
|
341
|
-
logger
|
|
342
|
-
);
|
|
343
|
-
else if (typeof value === "object")
|
|
344
|
-
this.validContent(
|
|
345
|
-
type,
|
|
346
|
-
value,
|
|
347
|
-
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
348
|
-
rule.config,
|
|
349
|
-
logger
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
|
|
357
180
|
// src/index.ts
|
|
358
181
|
var ContentType = {
|
|
359
182
|
plain: "text/plain",
|
|
@@ -402,16 +225,10 @@ var Http = class {
|
|
|
402
225
|
cookie;
|
|
403
226
|
session;
|
|
404
227
|
config;
|
|
405
|
-
validatorOptions;
|
|
406
228
|
response;
|
|
407
|
-
validator;
|
|
408
229
|
constructor(config) {
|
|
409
230
|
this.name = config?.name || this.type;
|
|
410
231
|
this.config = config?.config || /* @__PURE__ */ Object.create(null);
|
|
411
|
-
if (config?.validator) {
|
|
412
|
-
console.warn("Validator will deprecated in the v3.");
|
|
413
|
-
this.validatorOptions = config.validator;
|
|
414
|
-
}
|
|
415
232
|
}
|
|
416
233
|
async onMount(data, next) {
|
|
417
234
|
data.logger.debug("merge config");
|
|
@@ -439,12 +256,6 @@ var Http = class {
|
|
|
439
256
|
data.logger.debug("prepare cookie & session");
|
|
440
257
|
this.cookie = new Cookie(this.config.cookie || {}, data.logger);
|
|
441
258
|
this.session = this.cookie.session;
|
|
442
|
-
if (this.validatorOptions) {
|
|
443
|
-
data.logger.debug("prepare validator");
|
|
444
|
-
this.validator = new Validator(
|
|
445
|
-
this.validatorOptions
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
259
|
await next();
|
|
449
260
|
}
|
|
450
261
|
async onInvoke(data, next) {
|
|
@@ -485,18 +296,6 @@ var Http = class {
|
|
|
485
296
|
data.cookie = this.cookie;
|
|
486
297
|
data.session = this.session;
|
|
487
298
|
try {
|
|
488
|
-
if (this.validator) {
|
|
489
|
-
data.logger.debug("Valid request");
|
|
490
|
-
await this.validator.valid(
|
|
491
|
-
{
|
|
492
|
-
headers: this.headers,
|
|
493
|
-
params: this.params,
|
|
494
|
-
cookie: this.cookie,
|
|
495
|
-
session: this.session
|
|
496
|
-
},
|
|
497
|
-
data.logger
|
|
498
|
-
);
|
|
499
|
-
}
|
|
500
299
|
await next();
|
|
501
300
|
} catch (error) {
|
|
502
301
|
data.response = error;
|
|
@@ -608,6 +407,5 @@ exports.Cookie = Cookie;
|
|
|
608
407
|
exports.Http = Http;
|
|
609
408
|
exports.HttpError = HttpError;
|
|
610
409
|
exports.Session = Session;
|
|
611
|
-
exports.Validator = Validator;
|
|
612
410
|
exports.useHttp = useHttp;
|
|
613
411
|
exports.useHttpFunc = useHttpFunc;
|
package/dist/index.mjs
CHANGED
|
@@ -175,183 +175,6 @@ var Cookie = class {
|
|
|
175
175
|
}
|
|
176
176
|
};
|
|
177
177
|
|
|
178
|
-
// src/validator.ts
|
|
179
|
-
var Validator = class {
|
|
180
|
-
before;
|
|
181
|
-
paramsConfig;
|
|
182
|
-
cookieConfig;
|
|
183
|
-
sessionConfig;
|
|
184
|
-
request;
|
|
185
|
-
constructor(config) {
|
|
186
|
-
this.paramsConfig = config.params;
|
|
187
|
-
this.cookieConfig = config.cookie;
|
|
188
|
-
this.sessionConfig = config.session;
|
|
189
|
-
this.before = config.before;
|
|
190
|
-
}
|
|
191
|
-
async valid(request, logger) {
|
|
192
|
-
if (this.before) {
|
|
193
|
-
const result = await this.before(request);
|
|
194
|
-
if (result) throw new HttpError(result);
|
|
195
|
-
}
|
|
196
|
-
this.request = request;
|
|
197
|
-
if (this.paramsConfig && request.params) {
|
|
198
|
-
logger.debug("Valid Params");
|
|
199
|
-
this.validContent("params", request.params, "", this.paramsConfig, logger);
|
|
200
|
-
}
|
|
201
|
-
if (this.cookieConfig && request.cookie) {
|
|
202
|
-
logger.debug("Valid Cookie");
|
|
203
|
-
if (request.cookie == null) throw Error("Not found Cookie");
|
|
204
|
-
this.validContent(
|
|
205
|
-
"cookie",
|
|
206
|
-
request.cookie.content,
|
|
207
|
-
"",
|
|
208
|
-
this.cookieConfig,
|
|
209
|
-
logger
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
if (this.sessionConfig && request.session) {
|
|
213
|
-
logger.debug("Valid Session");
|
|
214
|
-
if (request.session == null) throw Error("Not found Session");
|
|
215
|
-
this.validContent(
|
|
216
|
-
"session",
|
|
217
|
-
request.session.content,
|
|
218
|
-
"",
|
|
219
|
-
this.sessionConfig,
|
|
220
|
-
logger
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
validContent(type, params, baseKey, config, logger) {
|
|
225
|
-
if (config.whitelist) {
|
|
226
|
-
const paramsKeys = Object.keys(params);
|
|
227
|
-
const rulesKeys = Object.keys(config.rules);
|
|
228
|
-
const diff = paramsKeys.filter((k) => !rulesKeys.includes(k));
|
|
229
|
-
if (diff.length > 0) {
|
|
230
|
-
if (config.whitelist === "error") {
|
|
231
|
-
const diffKeys = diff.map((k) => `${baseKey}${k}`);
|
|
232
|
-
const error = Error(
|
|
233
|
-
`[${type}] Not permitted keys: ${diffKeys.join(", ")}`
|
|
234
|
-
);
|
|
235
|
-
if (config.onError) {
|
|
236
|
-
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
237
|
-
if (res) throw new HttpError(res);
|
|
238
|
-
}
|
|
239
|
-
throw error;
|
|
240
|
-
}
|
|
241
|
-
if (config.whitelist === "ignore")
|
|
242
|
-
for (const key of diff) delete params[key];
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
for (const key in config.rules) {
|
|
246
|
-
const rule = config.rules[key];
|
|
247
|
-
if (!rule) continue;
|
|
248
|
-
let value = params[key];
|
|
249
|
-
if (rule.default) {
|
|
250
|
-
if (type === "cookie" || type === "session")
|
|
251
|
-
logger.warn("Cookie and Session not support default rule.");
|
|
252
|
-
else if (typeof value === "undefined" && rule.default) {
|
|
253
|
-
value = typeof rule.default === "function" ? rule.default(this.request) : rule.default;
|
|
254
|
-
params[key] = value;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
if (rule.required) {
|
|
258
|
-
if (typeof value === "undefined" || value === null) {
|
|
259
|
-
const error = Error(`[${type}] ${baseKey}${key} is required.`);
|
|
260
|
-
if (config.onError) {
|
|
261
|
-
const res = config.onError(
|
|
262
|
-
`${type}.rule.required`,
|
|
263
|
-
`${baseKey}${key}`,
|
|
264
|
-
value
|
|
265
|
-
);
|
|
266
|
-
if (res) throw new HttpError(res);
|
|
267
|
-
}
|
|
268
|
-
throw error;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
if (typeof value !== "undefined" && value !== null) {
|
|
272
|
-
if (rule.type)
|
|
273
|
-
if (type === "cookie") logger.warn("Cookie not support type rule");
|
|
274
|
-
else {
|
|
275
|
-
let typed = true;
|
|
276
|
-
switch (rule.type) {
|
|
277
|
-
case "array":
|
|
278
|
-
typed = Array.isArray(value);
|
|
279
|
-
break;
|
|
280
|
-
case "object":
|
|
281
|
-
typed = Object.prototype.toString.call(value) === "[object Object]";
|
|
282
|
-
break;
|
|
283
|
-
default:
|
|
284
|
-
typed = typeof value === rule.type;
|
|
285
|
-
break;
|
|
286
|
-
}
|
|
287
|
-
if (!typed) {
|
|
288
|
-
const error = Error(
|
|
289
|
-
`[${type}] ${baseKey}${key} must be a ${rule.type}.`
|
|
290
|
-
);
|
|
291
|
-
if (config.onError) {
|
|
292
|
-
const res = config.onError(
|
|
293
|
-
`${type}.rule.type`,
|
|
294
|
-
`${baseKey}${key}`,
|
|
295
|
-
value
|
|
296
|
-
);
|
|
297
|
-
if (res) throw new HttpError(res);
|
|
298
|
-
}
|
|
299
|
-
throw error;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
if (rule.regexp && (rule.type === "string" || !rule.type) && !rule.regexp.test(value)) {
|
|
303
|
-
const error = Error(
|
|
304
|
-
`[${type}] ${baseKey}${key} must match ${rule.regexp}.`
|
|
305
|
-
);
|
|
306
|
-
if (config.onError) {
|
|
307
|
-
const res = config.onError(
|
|
308
|
-
`${type}.rule.regexp`,
|
|
309
|
-
`${baseKey}${key}`,
|
|
310
|
-
value
|
|
311
|
-
);
|
|
312
|
-
if (res) throw new HttpError(res);
|
|
313
|
-
}
|
|
314
|
-
throw error;
|
|
315
|
-
}
|
|
316
|
-
if (rule.in && !rule.in.includes(value)) {
|
|
317
|
-
const error = Error(
|
|
318
|
-
`[${type}] ${baseKey}${key} must be in ${rule.in.join(", ")}.`
|
|
319
|
-
);
|
|
320
|
-
if (config.onError) {
|
|
321
|
-
const res = config.onError(
|
|
322
|
-
`${type}.rule.in`,
|
|
323
|
-
`${baseKey}${key}`,
|
|
324
|
-
value
|
|
325
|
-
);
|
|
326
|
-
if (res) throw new HttpError(res);
|
|
327
|
-
}
|
|
328
|
-
throw error;
|
|
329
|
-
}
|
|
330
|
-
if (rule.config) {
|
|
331
|
-
if (type === "cookie") logger.warn("Cookie not support nest rule.");
|
|
332
|
-
else if (Array.isArray(value))
|
|
333
|
-
for (const val of value)
|
|
334
|
-
this.validContent(
|
|
335
|
-
type,
|
|
336
|
-
val,
|
|
337
|
-
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
338
|
-
rule.config,
|
|
339
|
-
logger
|
|
340
|
-
);
|
|
341
|
-
else if (typeof value === "object")
|
|
342
|
-
this.validContent(
|
|
343
|
-
type,
|
|
344
|
-
value,
|
|
345
|
-
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
346
|
-
rule.config,
|
|
347
|
-
logger
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
};
|
|
354
|
-
|
|
355
178
|
// src/index.ts
|
|
356
179
|
var ContentType = {
|
|
357
180
|
plain: "text/plain",
|
|
@@ -400,16 +223,10 @@ var Http = class {
|
|
|
400
223
|
cookie;
|
|
401
224
|
session;
|
|
402
225
|
config;
|
|
403
|
-
validatorOptions;
|
|
404
226
|
response;
|
|
405
|
-
validator;
|
|
406
227
|
constructor(config) {
|
|
407
228
|
this.name = config?.name || this.type;
|
|
408
229
|
this.config = config?.config || /* @__PURE__ */ Object.create(null);
|
|
409
|
-
if (config?.validator) {
|
|
410
|
-
console.warn("Validator will deprecated in the v3.");
|
|
411
|
-
this.validatorOptions = config.validator;
|
|
412
|
-
}
|
|
413
230
|
}
|
|
414
231
|
async onMount(data, next) {
|
|
415
232
|
data.logger.debug("merge config");
|
|
@@ -437,12 +254,6 @@ var Http = class {
|
|
|
437
254
|
data.logger.debug("prepare cookie & session");
|
|
438
255
|
this.cookie = new Cookie(this.config.cookie || {}, data.logger);
|
|
439
256
|
this.session = this.cookie.session;
|
|
440
|
-
if (this.validatorOptions) {
|
|
441
|
-
data.logger.debug("prepare validator");
|
|
442
|
-
this.validator = new Validator(
|
|
443
|
-
this.validatorOptions
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
257
|
await next();
|
|
447
258
|
}
|
|
448
259
|
async onInvoke(data, next) {
|
|
@@ -483,18 +294,6 @@ var Http = class {
|
|
|
483
294
|
data.cookie = this.cookie;
|
|
484
295
|
data.session = this.session;
|
|
485
296
|
try {
|
|
486
|
-
if (this.validator) {
|
|
487
|
-
data.logger.debug("Valid request");
|
|
488
|
-
await this.validator.valid(
|
|
489
|
-
{
|
|
490
|
-
headers: this.headers,
|
|
491
|
-
params: this.params,
|
|
492
|
-
cookie: this.cookie,
|
|
493
|
-
session: this.session
|
|
494
|
-
},
|
|
495
|
-
data.logger
|
|
496
|
-
);
|
|
497
|
-
}
|
|
498
297
|
await next();
|
|
499
298
|
} catch (error) {
|
|
500
299
|
data.response = error;
|
|
@@ -601,4 +400,4 @@ function useHttpFunc(handler, config) {
|
|
|
601
400
|
return func;
|
|
602
401
|
}
|
|
603
402
|
|
|
604
|
-
export { ContentType, Cookie, Http, HttpError, Session,
|
|
403
|
+
export { ContentType, Cookie, Http, HttpError, Session, useHttp, useHttpFunc };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/http",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
"dist"
|
|
35
35
|
],
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@faasjs/func": "
|
|
38
|
-
"@faasjs/logger": "
|
|
37
|
+
"@faasjs/func": "4.0.0",
|
|
38
|
+
"@faasjs/logger": "4.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@faasjs/func": "
|
|
42
|
-
"@faasjs/logger": "
|
|
41
|
+
"@faasjs/func": "4.0.0",
|
|
42
|
+
"@faasjs/logger": "4.0.0"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=22.0.0",
|