@faasjs/http 0.0.2-beta.269 → 0.0.2-beta.289
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/index.d.ts +1 -0
- package/lib/index.es.js +25 -25
- package/lib/index.js +25 -25
- package/lib/validator.d.ts +2 -1
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
package/lib/index.es.js
CHANGED
|
@@ -147,15 +147,14 @@ class Cookie {
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
class Validator {
|
|
150
|
-
constructor(config) {
|
|
150
|
+
constructor(config, logger) {
|
|
151
151
|
this.paramsConfig = config.params;
|
|
152
152
|
this.cookieConfig = config.cookie;
|
|
153
153
|
this.sessionConfig = config.session;
|
|
154
154
|
this.before = config.before;
|
|
155
|
-
this.logger =
|
|
155
|
+
this.logger = logger;
|
|
156
156
|
}
|
|
157
157
|
async valid(request) {
|
|
158
|
-
this.logger.debug('Begin');
|
|
159
158
|
if (this.before) {
|
|
160
159
|
const result = await this.before(request);
|
|
161
160
|
if (result)
|
|
@@ -163,11 +162,11 @@ class Validator {
|
|
|
163
162
|
}
|
|
164
163
|
this.request = request;
|
|
165
164
|
if (this.paramsConfig && request.params) {
|
|
166
|
-
this.logger.debug('Valid
|
|
165
|
+
this.logger.debug('Valid Params');
|
|
167
166
|
this.validContent('params', request.params, '', this.paramsConfig);
|
|
168
167
|
}
|
|
169
168
|
if (this.cookieConfig && request.cookie) {
|
|
170
|
-
this.logger.debug('Valid
|
|
169
|
+
this.logger.debug('Valid Cookie');
|
|
171
170
|
if (request.cookie == null)
|
|
172
171
|
throw Error('Not found Cookie');
|
|
173
172
|
this.validContent('cookie', request.cookie.content, '', this.cookieConfig);
|
|
@@ -187,7 +186,7 @@ class Validator {
|
|
|
187
186
|
if (diff.length > 0)
|
|
188
187
|
if (config.whitelist === 'error') {
|
|
189
188
|
const diffKeys = diff.map(k => `${baseKey}${k}`);
|
|
190
|
-
const error = Error(`[${type}]
|
|
189
|
+
const error = Error(`[${type}] Not permitted keys: ${diffKeys.join(', ')}`);
|
|
191
190
|
if (config.onError) {
|
|
192
191
|
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
193
192
|
if (res)
|
|
@@ -339,22 +338,22 @@ class Http {
|
|
|
339
338
|
var _a;
|
|
340
339
|
await next();
|
|
341
340
|
this.logger.debug('组装网关配置');
|
|
342
|
-
this.logger.debug('%
|
|
341
|
+
this.logger.debug('%j', data);
|
|
343
342
|
const config = data.config.plugins ?
|
|
344
343
|
deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) :
|
|
345
344
|
{ config: this.config };
|
|
346
345
|
// 根据文件及文件夹名生成路径
|
|
347
346
|
if (!config.config.path) {
|
|
348
|
-
config.config.path = '
|
|
349
|
-
if (config.config.path === '
|
|
350
|
-
config.config.path = '
|
|
347
|
+
config.config.path = '/' + ((_a = data.name) === null || _a === void 0 ? void 0 : _a.replace(/_/g, '/').replace(/\/index$/, ''));
|
|
348
|
+
if (config.config.path === '/index')
|
|
349
|
+
config.config.path = '/';
|
|
351
350
|
if (config.config.ignorePathPrefix) {
|
|
352
|
-
config.config.path = config.config.path.replace(new RegExp('
|
|
353
|
-
if (config.config.path === '
|
|
354
|
-
config.config.path = '
|
|
351
|
+
config.config.path = config.config.path.replace(new RegExp('^' + config.config.ignorePathPrefix), '');
|
|
352
|
+
if (config.config.path === '')
|
|
353
|
+
config.config.path = '/';
|
|
355
354
|
}
|
|
356
355
|
}
|
|
357
|
-
this.logger.debug('组装完成 %
|
|
356
|
+
this.logger.debug('组装完成 %j', config);
|
|
358
357
|
// 引用服务商部署插件
|
|
359
358
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
360
359
|
const Provider = require(config.provider.type).Provider;
|
|
@@ -371,7 +370,7 @@ class Http {
|
|
|
371
370
|
this.session = this.cookie.session;
|
|
372
371
|
if (this.validatorOptions) {
|
|
373
372
|
this.logger.debug('[onMount] prepare validator');
|
|
374
|
-
this.validator = new Validator(this.validatorOptions);
|
|
373
|
+
this.validator = new Validator(this.validatorOptions, this.logger);
|
|
375
374
|
}
|
|
376
375
|
globals[this.name] = this;
|
|
377
376
|
await next();
|
|
@@ -379,9 +378,10 @@ class Http {
|
|
|
379
378
|
async onInvoke(data, next) {
|
|
380
379
|
var _a, _b;
|
|
381
380
|
this.headers = data.event.headers || Object.create(null);
|
|
381
|
+
this.body = data.event.body;
|
|
382
382
|
this.params = Object.create(null);
|
|
383
383
|
this.response = { headers: Object.create(null) };
|
|
384
|
-
if (data.event.body)
|
|
384
|
+
if (data.event.body) {
|
|
385
385
|
if (data.event.headers && data.event.headers['content-type'] && data.event.headers['content-type'].includes('application/json')) {
|
|
386
386
|
this.logger.debug('[onInvoke] Parse params from json body');
|
|
387
387
|
this.params = JSON.parse(data.event.body);
|
|
@@ -390,16 +390,19 @@ class Http {
|
|
|
390
390
|
this.logger.debug('[onInvoke] Parse params from raw body');
|
|
391
391
|
this.params = data.event.body;
|
|
392
392
|
}
|
|
393
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
394
|
+
}
|
|
393
395
|
else if (data.event.queryString) {
|
|
394
396
|
this.logger.debug('[onInvoke] Parse params from queryString');
|
|
395
397
|
this.params = data.event.queryString;
|
|
398
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
396
399
|
}
|
|
397
|
-
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
398
|
-
this.logger.debug('[onInvoke] Parse cookie');
|
|
399
400
|
this.cookie.invoke(this.headers.cookie);
|
|
400
|
-
this.
|
|
401
|
-
|
|
402
|
-
|
|
401
|
+
if (this.headers.cookie) {
|
|
402
|
+
this.logger.debug('[onInvoke] Cookie: %j', this.cookie.content);
|
|
403
|
+
this.logger.debug('[onInvoke] Session: %j', this.session.content);
|
|
404
|
+
}
|
|
405
|
+
if (this.validator) {
|
|
403
406
|
this.logger.debug('[onInvoke] Valid request');
|
|
404
407
|
try {
|
|
405
408
|
await this.validator.valid({
|
|
@@ -413,10 +416,7 @@ class Http {
|
|
|
413
416
|
this.logger.error(error);
|
|
414
417
|
data.response = {
|
|
415
418
|
statusCode: error.statusCode || 500,
|
|
416
|
-
headers: {
|
|
417
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
418
|
-
'X-SCF-RequestId': data.context.request_id
|
|
419
|
-
},
|
|
419
|
+
headers: { 'Content-Type': 'application/json; charset=utf-8' },
|
|
420
420
|
body: JSON.stringify({ error: { message: error.message } })
|
|
421
421
|
};
|
|
422
422
|
return;
|
package/lib/index.js
CHANGED
|
@@ -151,15 +151,14 @@ class Cookie {
|
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
class Validator {
|
|
154
|
-
constructor(config) {
|
|
154
|
+
constructor(config, logger) {
|
|
155
155
|
this.paramsConfig = config.params;
|
|
156
156
|
this.cookieConfig = config.cookie;
|
|
157
157
|
this.sessionConfig = config.session;
|
|
158
158
|
this.before = config.before;
|
|
159
|
-
this.logger =
|
|
159
|
+
this.logger = logger;
|
|
160
160
|
}
|
|
161
161
|
async valid(request) {
|
|
162
|
-
this.logger.debug('Begin');
|
|
163
162
|
if (this.before) {
|
|
164
163
|
const result = await this.before(request);
|
|
165
164
|
if (result)
|
|
@@ -167,11 +166,11 @@ class Validator {
|
|
|
167
166
|
}
|
|
168
167
|
this.request = request;
|
|
169
168
|
if (this.paramsConfig && request.params) {
|
|
170
|
-
this.logger.debug('Valid
|
|
169
|
+
this.logger.debug('Valid Params');
|
|
171
170
|
this.validContent('params', request.params, '', this.paramsConfig);
|
|
172
171
|
}
|
|
173
172
|
if (this.cookieConfig && request.cookie) {
|
|
174
|
-
this.logger.debug('Valid
|
|
173
|
+
this.logger.debug('Valid Cookie');
|
|
175
174
|
if (request.cookie == null)
|
|
176
175
|
throw Error('Not found Cookie');
|
|
177
176
|
this.validContent('cookie', request.cookie.content, '', this.cookieConfig);
|
|
@@ -191,7 +190,7 @@ class Validator {
|
|
|
191
190
|
if (diff.length > 0)
|
|
192
191
|
if (config.whitelist === 'error') {
|
|
193
192
|
const diffKeys = diff.map(k => `${baseKey}${k}`);
|
|
194
|
-
const error = Error(`[${type}]
|
|
193
|
+
const error = Error(`[${type}] Not permitted keys: ${diffKeys.join(', ')}`);
|
|
195
194
|
if (config.onError) {
|
|
196
195
|
const res = config.onError(`${type}.whitelist`, baseKey, diffKeys);
|
|
197
196
|
if (res)
|
|
@@ -343,22 +342,22 @@ class Http {
|
|
|
343
342
|
var _a;
|
|
344
343
|
await next();
|
|
345
344
|
this.logger.debug('组装网关配置');
|
|
346
|
-
this.logger.debug('%
|
|
345
|
+
this.logger.debug('%j', data);
|
|
347
346
|
const config = data.config.plugins ?
|
|
348
347
|
deep_merge.deepMerge(data.config.plugins[this.name || this.type], { config: this.config }) :
|
|
349
348
|
{ config: this.config };
|
|
350
349
|
// 根据文件及文件夹名生成路径
|
|
351
350
|
if (!config.config.path) {
|
|
352
|
-
config.config.path = '
|
|
353
|
-
if (config.config.path === '
|
|
354
|
-
config.config.path = '
|
|
351
|
+
config.config.path = '/' + ((_a = data.name) === null || _a === void 0 ? void 0 : _a.replace(/_/g, '/').replace(/\/index$/, ''));
|
|
352
|
+
if (config.config.path === '/index')
|
|
353
|
+
config.config.path = '/';
|
|
355
354
|
if (config.config.ignorePathPrefix) {
|
|
356
|
-
config.config.path = config.config.path.replace(new RegExp('
|
|
357
|
-
if (config.config.path === '
|
|
358
|
-
config.config.path = '
|
|
355
|
+
config.config.path = config.config.path.replace(new RegExp('^' + config.config.ignorePathPrefix), '');
|
|
356
|
+
if (config.config.path === '')
|
|
357
|
+
config.config.path = '/';
|
|
359
358
|
}
|
|
360
359
|
}
|
|
361
|
-
this.logger.debug('组装完成 %
|
|
360
|
+
this.logger.debug('组装完成 %j', config);
|
|
362
361
|
// 引用服务商部署插件
|
|
363
362
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
364
363
|
const Provider = require(config.provider.type).Provider;
|
|
@@ -375,7 +374,7 @@ class Http {
|
|
|
375
374
|
this.session = this.cookie.session;
|
|
376
375
|
if (this.validatorOptions) {
|
|
377
376
|
this.logger.debug('[onMount] prepare validator');
|
|
378
|
-
this.validator = new Validator(this.validatorOptions);
|
|
377
|
+
this.validator = new Validator(this.validatorOptions, this.logger);
|
|
379
378
|
}
|
|
380
379
|
globals[this.name] = this;
|
|
381
380
|
await next();
|
|
@@ -383,9 +382,10 @@ class Http {
|
|
|
383
382
|
async onInvoke(data, next) {
|
|
384
383
|
var _a, _b;
|
|
385
384
|
this.headers = data.event.headers || Object.create(null);
|
|
385
|
+
this.body = data.event.body;
|
|
386
386
|
this.params = Object.create(null);
|
|
387
387
|
this.response = { headers: Object.create(null) };
|
|
388
|
-
if (data.event.body)
|
|
388
|
+
if (data.event.body) {
|
|
389
389
|
if (data.event.headers && data.event.headers['content-type'] && data.event.headers['content-type'].includes('application/json')) {
|
|
390
390
|
this.logger.debug('[onInvoke] Parse params from json body');
|
|
391
391
|
this.params = JSON.parse(data.event.body);
|
|
@@ -394,16 +394,19 @@ class Http {
|
|
|
394
394
|
this.logger.debug('[onInvoke] Parse params from raw body');
|
|
395
395
|
this.params = data.event.body;
|
|
396
396
|
}
|
|
397
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
398
|
+
}
|
|
397
399
|
else if (data.event.queryString) {
|
|
398
400
|
this.logger.debug('[onInvoke] Parse params from queryString');
|
|
399
401
|
this.params = data.event.queryString;
|
|
402
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
400
403
|
}
|
|
401
|
-
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
402
|
-
this.logger.debug('[onInvoke] Parse cookie');
|
|
403
404
|
this.cookie.invoke(this.headers.cookie);
|
|
404
|
-
this.
|
|
405
|
-
|
|
406
|
-
|
|
405
|
+
if (this.headers.cookie) {
|
|
406
|
+
this.logger.debug('[onInvoke] Cookie: %j', this.cookie.content);
|
|
407
|
+
this.logger.debug('[onInvoke] Session: %j', this.session.content);
|
|
408
|
+
}
|
|
409
|
+
if (this.validator) {
|
|
407
410
|
this.logger.debug('[onInvoke] Valid request');
|
|
408
411
|
try {
|
|
409
412
|
await this.validator.valid({
|
|
@@ -417,10 +420,7 @@ class Http {
|
|
|
417
420
|
this.logger.error(error);
|
|
418
421
|
data.response = {
|
|
419
422
|
statusCode: error.statusCode || 500,
|
|
420
|
-
headers: {
|
|
421
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
422
|
-
'X-SCF-RequestId': data.context.request_id
|
|
423
|
-
},
|
|
423
|
+
headers: { 'Content-Type': 'application/json; charset=utf-8' },
|
|
424
424
|
body: JSON.stringify({ error: { message: error.message } })
|
|
425
425
|
};
|
|
426
426
|
return;
|
package/lib/validator.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Cookie } from './cookie';
|
|
2
2
|
import { Session } from './session';
|
|
3
|
+
import { Logger } from '@faasjs/logger';
|
|
3
4
|
export declare type ValidatorRuleOptions = {
|
|
4
5
|
type?: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
5
6
|
required?: boolean;
|
|
@@ -47,7 +48,7 @@ export declare class Validator<TParams extends Record<string, any> = any, TCooki
|
|
|
47
48
|
cookie?: ValidatorOptions<TCookie>;
|
|
48
49
|
session?: ValidatorOptions<TSession>;
|
|
49
50
|
before?: BeforeOption<TParams, TCookie, TSession>;
|
|
50
|
-
});
|
|
51
|
+
}, logger: Logger);
|
|
51
52
|
valid(request: Request<TParams, TCookie, TSession>): Promise<void>;
|
|
52
53
|
validContent(type: string, params: {
|
|
53
54
|
[key: string]: any;
|
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.289",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.es.js",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"rollup-plugin-typescript2": "*",
|
|
27
27
|
"typescript": "*"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "dd1885081fb34c8497bafa5e03325d4e4bdd47ad"
|
|
30
30
|
}
|