@faasjs/http 0.0.2-beta.399 → 0.0.2-beta.401
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/dist/index.d.ts +9 -38
- package/dist/index.js +37 -34
- package/dist/index.mjs +37 -34
- package/package.json +5 -4
package/dist/index.d.ts
CHANGED
|
@@ -121,17 +121,11 @@ declare class Validator<TParams extends Record<string, any> = any, TCookie exten
|
|
|
121
121
|
cookieConfig?: ValidatorOptions<TCookie>;
|
|
122
122
|
sessionConfig?: ValidatorOptions<TSession>;
|
|
123
123
|
private request;
|
|
124
|
-
|
|
125
|
-
|
|
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>;
|
|
124
|
+
constructor(config: ValidatorConfig<TParams, TCookie, TSession>);
|
|
125
|
+
valid(request: Request<TParams, TCookie, TSession>, logger: Logger): Promise<void>;
|
|
132
126
|
validContent(type: string, params: {
|
|
133
127
|
[key: string]: any;
|
|
134
|
-
}, baseKey: string, config: ValidatorOptions): void;
|
|
128
|
+
}, baseKey: string, config: ValidatorOptions, logger: Logger): void;
|
|
135
129
|
}
|
|
136
130
|
|
|
137
131
|
declare const ContentType: {
|
|
@@ -142,8 +136,10 @@ declare type HttpConfig<TParams extends Record<string, any> = any, TCookie exten
|
|
|
142
136
|
name?: string;
|
|
143
137
|
config?: {
|
|
144
138
|
[key: string]: any;
|
|
139
|
+
/** POST as default */
|
|
145
140
|
method?: 'BEGIN' | 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PUT' | 'OPTIONS' | 'TRACE' | 'PATCH' | 'ANY';
|
|
146
141
|
timeout?: number;
|
|
142
|
+
/** file relative path as default */
|
|
147
143
|
path?: string;
|
|
148
144
|
ignorePathPrefix?: string;
|
|
149
145
|
functionName?: string;
|
|
@@ -181,54 +177,29 @@ declare class Http<TParams extends Record<string, any> = any, TCookie extends Re
|
|
|
181
177
|
private readonly validatorOptions?;
|
|
182
178
|
private response?;
|
|
183
179
|
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
180
|
constructor(config?: HttpConfig<TParams, TCookie, TSession>);
|
|
210
181
|
onDeploy(data: DeployData, next: Next): Promise<void>;
|
|
211
182
|
onMount(data: MountData, next: Next): Promise<void>;
|
|
212
183
|
onInvoke(data: InvokeData, next: Next): Promise<void>;
|
|
213
184
|
/**
|
|
214
|
-
*
|
|
185
|
+
* set header
|
|
215
186
|
* @param key {string} key
|
|
216
187
|
* @param value {*} value
|
|
217
188
|
*/
|
|
218
189
|
setHeader(key: string, value: string): Http<TParams, TCookie, TSession>;
|
|
219
190
|
/**
|
|
220
|
-
*
|
|
191
|
+
* set Content-Type
|
|
221
192
|
* @param type {string} 类型
|
|
222
193
|
* @param charset {string} 编码
|
|
223
194
|
*/
|
|
224
195
|
setContentType(type: string, charset?: string): Http<TParams, TCookie, TSession>;
|
|
225
196
|
/**
|
|
226
|
-
*
|
|
197
|
+
* set status code
|
|
227
198
|
* @param code {number} 状态码
|
|
228
199
|
*/
|
|
229
200
|
setStatusCode(code: number): Http<TParams, TCookie, TSession>;
|
|
230
201
|
/**
|
|
231
|
-
*
|
|
202
|
+
* set body
|
|
232
203
|
* @param body {*} 内容
|
|
233
204
|
*/
|
|
234
205
|
setBody(body: string): Http<TParams, TCookie, TSession>;
|
package/dist/index.js
CHANGED
|
@@ -216,14 +216,13 @@ var Cookie = class {
|
|
|
216
216
|
|
|
217
217
|
// src/validator.ts
|
|
218
218
|
var Validator = class {
|
|
219
|
-
constructor(config
|
|
219
|
+
constructor(config) {
|
|
220
220
|
this.paramsConfig = config.params;
|
|
221
221
|
this.cookieConfig = config.cookie;
|
|
222
222
|
this.sessionConfig = config.session;
|
|
223
223
|
this.before = config.before;
|
|
224
|
-
this.logger = logger;
|
|
225
224
|
}
|
|
226
|
-
async valid(request) {
|
|
225
|
+
async valid(request, logger) {
|
|
227
226
|
if (this.before) {
|
|
228
227
|
const result = await this.before(request);
|
|
229
228
|
if (result)
|
|
@@ -231,33 +230,35 @@ var Validator = class {
|
|
|
231
230
|
}
|
|
232
231
|
this.request = request;
|
|
233
232
|
if (this.paramsConfig && request.params) {
|
|
234
|
-
|
|
235
|
-
this.validContent("params", request.params, "", this.paramsConfig);
|
|
233
|
+
logger.debug("Valid Params");
|
|
234
|
+
this.validContent("params", request.params, "", this.paramsConfig, logger);
|
|
236
235
|
}
|
|
237
236
|
if (this.cookieConfig && request.cookie) {
|
|
238
|
-
|
|
237
|
+
logger.debug("Valid Cookie");
|
|
239
238
|
if (request.cookie == null)
|
|
240
239
|
throw Error("Not found Cookie");
|
|
241
240
|
this.validContent(
|
|
242
241
|
"cookie",
|
|
243
242
|
request.cookie.content,
|
|
244
243
|
"",
|
|
245
|
-
this.cookieConfig
|
|
244
|
+
this.cookieConfig,
|
|
245
|
+
logger
|
|
246
246
|
);
|
|
247
247
|
}
|
|
248
248
|
if (this.sessionConfig && request.session) {
|
|
249
|
-
|
|
249
|
+
logger.debug("Valid Session");
|
|
250
250
|
if (request.session == null)
|
|
251
251
|
throw Error("Not found Session");
|
|
252
252
|
this.validContent(
|
|
253
253
|
"session",
|
|
254
254
|
request.session.content,
|
|
255
255
|
"",
|
|
256
|
-
this.sessionConfig
|
|
256
|
+
this.sessionConfig,
|
|
257
|
+
logger
|
|
257
258
|
);
|
|
258
259
|
}
|
|
259
260
|
}
|
|
260
|
-
validContent(type, params, baseKey, config) {
|
|
261
|
+
validContent(type, params, baseKey, config, logger) {
|
|
261
262
|
if (config.whitelist) {
|
|
262
263
|
const paramsKeys = Object.keys(params);
|
|
263
264
|
const rulesKeys = Object.keys(config.rules);
|
|
@@ -286,7 +287,7 @@ var Validator = class {
|
|
|
286
287
|
let value = params[key];
|
|
287
288
|
if (rule.default) {
|
|
288
289
|
if (type === "cookie" || type === "session")
|
|
289
|
-
|
|
290
|
+
logger.warn("Cookie and Session not support default rule.");
|
|
290
291
|
else if (typeof value === "undefined" && rule.default) {
|
|
291
292
|
value = typeof rule.default === "function" ? rule.default(this.request) : rule.default;
|
|
292
293
|
params[key] = value;
|
|
@@ -310,7 +311,7 @@ var Validator = class {
|
|
|
310
311
|
if (typeof value !== "undefined" && value !== null) {
|
|
311
312
|
if (rule.type)
|
|
312
313
|
if (type === "cookie")
|
|
313
|
-
|
|
314
|
+
logger.warn("Cookie not support type rule");
|
|
314
315
|
else {
|
|
315
316
|
let typed = true;
|
|
316
317
|
switch (rule.type) {
|
|
@@ -372,21 +373,23 @@ var Validator = class {
|
|
|
372
373
|
}
|
|
373
374
|
if (rule.config) {
|
|
374
375
|
if (type === "cookie")
|
|
375
|
-
|
|
376
|
+
logger.warn("Cookie not support nest rule.");
|
|
376
377
|
else if (Array.isArray(value))
|
|
377
378
|
for (const val of value)
|
|
378
379
|
this.validContent(
|
|
379
380
|
type,
|
|
380
381
|
val,
|
|
381
382
|
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
382
|
-
rule.config
|
|
383
|
+
rule.config,
|
|
384
|
+
logger
|
|
383
385
|
);
|
|
384
386
|
else if (typeof value === "object")
|
|
385
387
|
this.validContent(
|
|
386
388
|
type,
|
|
387
389
|
value,
|
|
388
390
|
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
389
|
-
rule.config
|
|
391
|
+
rule.config,
|
|
392
|
+
logger
|
|
390
393
|
);
|
|
391
394
|
}
|
|
392
395
|
}
|
|
@@ -427,7 +430,6 @@ var Http = class {
|
|
|
427
430
|
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
428
431
|
if (config == null ? void 0 : config.validator)
|
|
429
432
|
this.validatorOptions = config.validator;
|
|
430
|
-
this.logger = new import_logger.Logger(this.name);
|
|
431
433
|
this.headers = /* @__PURE__ */ Object.create(null);
|
|
432
434
|
this.cookie = new Cookie(this.config.cookie || {});
|
|
433
435
|
this.session = this.cookie.session;
|
|
@@ -436,8 +438,9 @@ var Http = class {
|
|
|
436
438
|
var _a;
|
|
437
439
|
data.dependencies["@faasjs/http"] = "*";
|
|
438
440
|
await next();
|
|
439
|
-
|
|
440
|
-
|
|
441
|
+
const logger = new import_logger.Logger(this.name);
|
|
442
|
+
logger.debug("Generate api gateway's config");
|
|
443
|
+
logger.debug("%j", data);
|
|
441
444
|
const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
|
|
442
445
|
if (!config.config.path) {
|
|
443
446
|
config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
|
|
@@ -449,21 +452,21 @@ var Http = class {
|
|
|
449
452
|
config.config.path = "/";
|
|
450
453
|
}
|
|
451
454
|
}
|
|
452
|
-
|
|
455
|
+
logger.debug("Api gateway's config: %j", config);
|
|
453
456
|
const Provider = require(config.provider.type).Provider;
|
|
454
457
|
const provider = new Provider(config.provider.config);
|
|
455
458
|
await provider.deploy(this.type, data, config);
|
|
456
459
|
}
|
|
457
460
|
async onMount(data, next) {
|
|
458
|
-
|
|
461
|
+
data.logger.debug("[onMount] merge config");
|
|
459
462
|
if (data.config.plugins && data.config.plugins[this.name || this.type])
|
|
460
463
|
this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
461
|
-
|
|
464
|
+
data.logger.debug("[onMount] prepare cookie & session");
|
|
462
465
|
this.cookie = new Cookie(this.config.cookie || {});
|
|
463
466
|
this.session = this.cookie.session;
|
|
464
467
|
if (this.validatorOptions) {
|
|
465
|
-
|
|
466
|
-
this.validator = new Validator(this.validatorOptions
|
|
468
|
+
data.logger.debug("[onMount] prepare validator");
|
|
469
|
+
this.validator = new Validator(this.validatorOptions);
|
|
467
470
|
}
|
|
468
471
|
await next();
|
|
469
472
|
}
|
|
@@ -475,32 +478,32 @@ var Http = class {
|
|
|
475
478
|
this.response = { headers: /* @__PURE__ */ Object.create(null) };
|
|
476
479
|
if (data.event.body) {
|
|
477
480
|
if (data.event.headers && data.event.headers["content-type"] && data.event.headers["content-type"].includes("application/json")) {
|
|
478
|
-
|
|
481
|
+
data.logger.debug("[onInvoke] Parse params from json body");
|
|
479
482
|
this.params = JSON.parse(data.event.body);
|
|
480
483
|
} else {
|
|
481
|
-
|
|
484
|
+
data.logger.debug("[onInvoke] Parse params from raw body");
|
|
482
485
|
this.params = data.event.body;
|
|
483
486
|
}
|
|
484
|
-
|
|
487
|
+
data.logger.debug("[onInvoke] Params: %j", this.params);
|
|
485
488
|
} else if (data.event.queryString) {
|
|
486
|
-
|
|
489
|
+
data.logger.debug("[onInvoke] Parse params from queryString");
|
|
487
490
|
this.params = data.event.queryString;
|
|
488
|
-
|
|
491
|
+
data.logger.debug("[onInvoke] Params: %j", this.params);
|
|
489
492
|
}
|
|
490
493
|
this.cookie.invoke(this.headers.cookie);
|
|
491
494
|
if (this.headers.cookie) {
|
|
492
|
-
|
|
493
|
-
|
|
495
|
+
data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
|
|
496
|
+
data.logger.debug("[onInvoke] Session: %j", this.session.content);
|
|
494
497
|
}
|
|
495
498
|
try {
|
|
496
499
|
if (this.validator) {
|
|
497
|
-
|
|
500
|
+
data.logger.debug("[onInvoke] Valid request");
|
|
498
501
|
await this.validator.valid({
|
|
499
502
|
headers: this.headers,
|
|
500
503
|
params: this.params,
|
|
501
504
|
cookie: this.cookie,
|
|
502
505
|
session: this.session
|
|
503
|
-
});
|
|
506
|
+
}, data.logger);
|
|
504
507
|
}
|
|
505
508
|
await next();
|
|
506
509
|
} catch (error) {
|
|
@@ -509,7 +512,7 @@ var Http = class {
|
|
|
509
512
|
this.session.update();
|
|
510
513
|
if (data.response)
|
|
511
514
|
if (data.response instanceof Error || ((_a = data.response.constructor) == null ? void 0 : _a.name) === "Error") {
|
|
512
|
-
|
|
515
|
+
data.logger.error(data.response);
|
|
513
516
|
this.response.body = JSON.stringify({ error: { message: data.response.message } });
|
|
514
517
|
try {
|
|
515
518
|
this.response.statusCode = data.response.statusCode || 500;
|
|
@@ -530,7 +533,7 @@ var Http = class {
|
|
|
530
533
|
const originBody = data.response.body;
|
|
531
534
|
data.response.originBody = originBody;
|
|
532
535
|
if (process.env.FaasMode === "local") {
|
|
533
|
-
|
|
536
|
+
data.logger.debug("[onInvoke] Response: %j", data.response);
|
|
534
537
|
return;
|
|
535
538
|
}
|
|
536
539
|
if (data.response.isBase64Encoded || typeof data.response.body !== "string" || !((_b = data.response.headers["Content-Type"]) == null ? void 0 : _b.includes("json")) || data.response.body.length < 100)
|
package/dist/index.mjs
CHANGED
|
@@ -202,14 +202,13 @@ var Cookie = class {
|
|
|
202
202
|
|
|
203
203
|
// src/validator.ts
|
|
204
204
|
var Validator = class {
|
|
205
|
-
constructor(config
|
|
205
|
+
constructor(config) {
|
|
206
206
|
this.paramsConfig = config.params;
|
|
207
207
|
this.cookieConfig = config.cookie;
|
|
208
208
|
this.sessionConfig = config.session;
|
|
209
209
|
this.before = config.before;
|
|
210
|
-
this.logger = logger;
|
|
211
210
|
}
|
|
212
|
-
async valid(request) {
|
|
211
|
+
async valid(request, logger) {
|
|
213
212
|
if (this.before) {
|
|
214
213
|
const result = await this.before(request);
|
|
215
214
|
if (result)
|
|
@@ -217,33 +216,35 @@ var Validator = class {
|
|
|
217
216
|
}
|
|
218
217
|
this.request = request;
|
|
219
218
|
if (this.paramsConfig && request.params) {
|
|
220
|
-
|
|
221
|
-
this.validContent("params", request.params, "", this.paramsConfig);
|
|
219
|
+
logger.debug("Valid Params");
|
|
220
|
+
this.validContent("params", request.params, "", this.paramsConfig, logger);
|
|
222
221
|
}
|
|
223
222
|
if (this.cookieConfig && request.cookie) {
|
|
224
|
-
|
|
223
|
+
logger.debug("Valid Cookie");
|
|
225
224
|
if (request.cookie == null)
|
|
226
225
|
throw Error("Not found Cookie");
|
|
227
226
|
this.validContent(
|
|
228
227
|
"cookie",
|
|
229
228
|
request.cookie.content,
|
|
230
229
|
"",
|
|
231
|
-
this.cookieConfig
|
|
230
|
+
this.cookieConfig,
|
|
231
|
+
logger
|
|
232
232
|
);
|
|
233
233
|
}
|
|
234
234
|
if (this.sessionConfig && request.session) {
|
|
235
|
-
|
|
235
|
+
logger.debug("Valid Session");
|
|
236
236
|
if (request.session == null)
|
|
237
237
|
throw Error("Not found Session");
|
|
238
238
|
this.validContent(
|
|
239
239
|
"session",
|
|
240
240
|
request.session.content,
|
|
241
241
|
"",
|
|
242
|
-
this.sessionConfig
|
|
242
|
+
this.sessionConfig,
|
|
243
|
+
logger
|
|
243
244
|
);
|
|
244
245
|
}
|
|
245
246
|
}
|
|
246
|
-
validContent(type, params, baseKey, config) {
|
|
247
|
+
validContent(type, params, baseKey, config, logger) {
|
|
247
248
|
if (config.whitelist) {
|
|
248
249
|
const paramsKeys = Object.keys(params);
|
|
249
250
|
const rulesKeys = Object.keys(config.rules);
|
|
@@ -272,7 +273,7 @@ var Validator = class {
|
|
|
272
273
|
let value = params[key];
|
|
273
274
|
if (rule.default) {
|
|
274
275
|
if (type === "cookie" || type === "session")
|
|
275
|
-
|
|
276
|
+
logger.warn("Cookie and Session not support default rule.");
|
|
276
277
|
else if (typeof value === "undefined" && rule.default) {
|
|
277
278
|
value = typeof rule.default === "function" ? rule.default(this.request) : rule.default;
|
|
278
279
|
params[key] = value;
|
|
@@ -296,7 +297,7 @@ var Validator = class {
|
|
|
296
297
|
if (typeof value !== "undefined" && value !== null) {
|
|
297
298
|
if (rule.type)
|
|
298
299
|
if (type === "cookie")
|
|
299
|
-
|
|
300
|
+
logger.warn("Cookie not support type rule");
|
|
300
301
|
else {
|
|
301
302
|
let typed = true;
|
|
302
303
|
switch (rule.type) {
|
|
@@ -358,21 +359,23 @@ var Validator = class {
|
|
|
358
359
|
}
|
|
359
360
|
if (rule.config) {
|
|
360
361
|
if (type === "cookie")
|
|
361
|
-
|
|
362
|
+
logger.warn("Cookie not support nest rule.");
|
|
362
363
|
else if (Array.isArray(value))
|
|
363
364
|
for (const val of value)
|
|
364
365
|
this.validContent(
|
|
365
366
|
type,
|
|
366
367
|
val,
|
|
367
368
|
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
368
|
-
rule.config
|
|
369
|
+
rule.config,
|
|
370
|
+
logger
|
|
369
371
|
);
|
|
370
372
|
else if (typeof value === "object")
|
|
371
373
|
this.validContent(
|
|
372
374
|
type,
|
|
373
375
|
value,
|
|
374
376
|
baseKey ? `${baseKey}.${key}.` : `${key}.`,
|
|
375
|
-
rule.config
|
|
377
|
+
rule.config,
|
|
378
|
+
logger
|
|
376
379
|
);
|
|
377
380
|
}
|
|
378
381
|
}
|
|
@@ -417,7 +420,6 @@ var Http = class {
|
|
|
417
420
|
this.config = (config == null ? void 0 : config.config) || /* @__PURE__ */ Object.create(null);
|
|
418
421
|
if (config == null ? void 0 : config.validator)
|
|
419
422
|
this.validatorOptions = config.validator;
|
|
420
|
-
this.logger = new Logger(this.name);
|
|
421
423
|
this.headers = /* @__PURE__ */ Object.create(null);
|
|
422
424
|
this.cookie = new Cookie(this.config.cookie || {});
|
|
423
425
|
this.session = this.cookie.session;
|
|
@@ -426,8 +428,9 @@ var Http = class {
|
|
|
426
428
|
var _a;
|
|
427
429
|
data.dependencies["@faasjs/http"] = "*";
|
|
428
430
|
await next();
|
|
429
|
-
this.
|
|
430
|
-
|
|
431
|
+
const logger = new Logger(this.name);
|
|
432
|
+
logger.debug("Generate api gateway's config");
|
|
433
|
+
logger.debug("%j", data);
|
|
431
434
|
const config = data.config.plugins ? deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) : { config: this.config };
|
|
432
435
|
if (!config.config.path) {
|
|
433
436
|
config.config.path = "/" + ((_a = data.name) == null ? void 0 : _a.replace(/_/g, "/").replace(/\/index$/, ""));
|
|
@@ -439,21 +442,21 @@ var Http = class {
|
|
|
439
442
|
config.config.path = "/";
|
|
440
443
|
}
|
|
441
444
|
}
|
|
442
|
-
|
|
445
|
+
logger.debug("Api gateway's config: %j", config);
|
|
443
446
|
const Provider = __require(config.provider.type).Provider;
|
|
444
447
|
const provider = new Provider(config.provider.config);
|
|
445
448
|
await provider.deploy(this.type, data, config);
|
|
446
449
|
}
|
|
447
450
|
async onMount(data, next) {
|
|
448
|
-
|
|
451
|
+
data.logger.debug("[onMount] merge config");
|
|
449
452
|
if (data.config.plugins && data.config.plugins[this.name || this.type])
|
|
450
453
|
this.config = deepMerge(this.config, data.config.plugins[this.name || this.type].config);
|
|
451
|
-
|
|
454
|
+
data.logger.debug("[onMount] prepare cookie & session");
|
|
452
455
|
this.cookie = new Cookie(this.config.cookie || {});
|
|
453
456
|
this.session = this.cookie.session;
|
|
454
457
|
if (this.validatorOptions) {
|
|
455
|
-
|
|
456
|
-
this.validator = new Validator(this.validatorOptions
|
|
458
|
+
data.logger.debug("[onMount] prepare validator");
|
|
459
|
+
this.validator = new Validator(this.validatorOptions);
|
|
457
460
|
}
|
|
458
461
|
await next();
|
|
459
462
|
}
|
|
@@ -465,32 +468,32 @@ var Http = class {
|
|
|
465
468
|
this.response = { headers: /* @__PURE__ */ Object.create(null) };
|
|
466
469
|
if (data.event.body) {
|
|
467
470
|
if (data.event.headers && data.event.headers["content-type"] && data.event.headers["content-type"].includes("application/json")) {
|
|
468
|
-
|
|
471
|
+
data.logger.debug("[onInvoke] Parse params from json body");
|
|
469
472
|
this.params = JSON.parse(data.event.body);
|
|
470
473
|
} else {
|
|
471
|
-
|
|
474
|
+
data.logger.debug("[onInvoke] Parse params from raw body");
|
|
472
475
|
this.params = data.event.body;
|
|
473
476
|
}
|
|
474
|
-
|
|
477
|
+
data.logger.debug("[onInvoke] Params: %j", this.params);
|
|
475
478
|
} else if (data.event.queryString) {
|
|
476
|
-
|
|
479
|
+
data.logger.debug("[onInvoke] Parse params from queryString");
|
|
477
480
|
this.params = data.event.queryString;
|
|
478
|
-
|
|
481
|
+
data.logger.debug("[onInvoke] Params: %j", this.params);
|
|
479
482
|
}
|
|
480
483
|
this.cookie.invoke(this.headers.cookie);
|
|
481
484
|
if (this.headers.cookie) {
|
|
482
|
-
|
|
483
|
-
|
|
485
|
+
data.logger.debug("[onInvoke] Cookie: %j", this.cookie.content);
|
|
486
|
+
data.logger.debug("[onInvoke] Session: %j", this.session.content);
|
|
484
487
|
}
|
|
485
488
|
try {
|
|
486
489
|
if (this.validator) {
|
|
487
|
-
|
|
490
|
+
data.logger.debug("[onInvoke] Valid request");
|
|
488
491
|
await this.validator.valid({
|
|
489
492
|
headers: this.headers,
|
|
490
493
|
params: this.params,
|
|
491
494
|
cookie: this.cookie,
|
|
492
495
|
session: this.session
|
|
493
|
-
});
|
|
496
|
+
}, data.logger);
|
|
494
497
|
}
|
|
495
498
|
await next();
|
|
496
499
|
} catch (error) {
|
|
@@ -499,7 +502,7 @@ var Http = class {
|
|
|
499
502
|
this.session.update();
|
|
500
503
|
if (data.response)
|
|
501
504
|
if (data.response instanceof Error || ((_a = data.response.constructor) == null ? void 0 : _a.name) === "Error") {
|
|
502
|
-
|
|
505
|
+
data.logger.error(data.response);
|
|
503
506
|
this.response.body = JSON.stringify({ error: { message: data.response.message } });
|
|
504
507
|
try {
|
|
505
508
|
this.response.statusCode = data.response.statusCode || 500;
|
|
@@ -520,7 +523,7 @@ var Http = class {
|
|
|
520
523
|
const originBody = data.response.body;
|
|
521
524
|
data.response.originBody = originBody;
|
|
522
525
|
if (process.env.FaasMode === "local") {
|
|
523
|
-
|
|
526
|
+
data.logger.debug("[onInvoke] Response: %j", data.response);
|
|
524
527
|
return;
|
|
525
528
|
}
|
|
526
529
|
if (data.response.isBase64Encoded || typeof data.response.body !== "string" || !((_b = data.response.headers["Content-Type"]) == null ? void 0 : _b.includes("json")) || data.response.body.length < 100)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/http",
|
|
3
|
-
"version": "0.0.2-beta.
|
|
3
|
+
"version": "0.0.2-beta.401",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -22,14 +22,15 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@faasjs/func": "^0.0.2-beta.
|
|
26
|
-
"@faasjs/logger": "^0.0.2-beta.
|
|
25
|
+
"@faasjs/func": "^0.0.2-beta.401",
|
|
26
|
+
"@faasjs/logger": "^0.0.2-beta.401"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"tsup": "*",
|
|
30
30
|
"typescript": "*"
|
|
31
31
|
},
|
|
32
32
|
"engines": {
|
|
33
|
-
"npm": ">=8.0.0"
|
|
33
|
+
"npm": ">=8.0.0",
|
|
34
|
+
"node": ">=16.0.0"
|
|
34
35
|
}
|
|
35
36
|
}
|