@faasjs/http 0.0.2-beta.276 → 0.0.2-beta.291
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.es.js +18 -19
- package/lib/index.js +18 -19
- package/lib/validator.d.ts +2 -1
- package/package.json +2 -2
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)
|
|
@@ -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();
|
|
@@ -382,7 +381,7 @@ class Http {
|
|
|
382
381
|
this.body = data.event.body;
|
|
383
382
|
this.params = Object.create(null);
|
|
384
383
|
this.response = { headers: Object.create(null) };
|
|
385
|
-
if (data.event.body)
|
|
384
|
+
if (data.event.body) {
|
|
386
385
|
if (data.event.headers && data.event.headers['content-type'] && data.event.headers['content-type'].includes('application/json')) {
|
|
387
386
|
this.logger.debug('[onInvoke] Parse params from json body');
|
|
388
387
|
this.params = JSON.parse(data.event.body);
|
|
@@ -391,16 +390,19 @@ class Http {
|
|
|
391
390
|
this.logger.debug('[onInvoke] Parse params from raw body');
|
|
392
391
|
this.params = data.event.body;
|
|
393
392
|
}
|
|
393
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
394
|
+
}
|
|
394
395
|
else if (data.event.queryString) {
|
|
395
396
|
this.logger.debug('[onInvoke] Parse params from queryString');
|
|
396
397
|
this.params = data.event.queryString;
|
|
398
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
397
399
|
}
|
|
398
|
-
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
399
|
-
this.logger.debug('[onInvoke] Parse cookie');
|
|
400
400
|
this.cookie.invoke(this.headers.cookie);
|
|
401
|
-
this.
|
|
402
|
-
|
|
403
|
-
|
|
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) {
|
|
404
406
|
this.logger.debug('[onInvoke] Valid request');
|
|
405
407
|
try {
|
|
406
408
|
await this.validator.valid({
|
|
@@ -414,10 +416,7 @@ class Http {
|
|
|
414
416
|
this.logger.error(error);
|
|
415
417
|
data.response = {
|
|
416
418
|
statusCode: error.statusCode || 500,
|
|
417
|
-
headers: {
|
|
418
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
419
|
-
'X-SCF-RequestId': data.context.request_id
|
|
420
|
-
},
|
|
419
|
+
headers: { 'Content-Type': 'application/json; charset=utf-8' },
|
|
421
420
|
body: JSON.stringify({ error: { message: error.message } })
|
|
422
421
|
};
|
|
423
422
|
return;
|
|
@@ -455,7 +454,7 @@ class Http {
|
|
|
455
454
|
// 处理 headers
|
|
456
455
|
this.response.headers = Object.assign({
|
|
457
456
|
'Content-Type': 'application/json; charset=utf-8',
|
|
458
|
-
'
|
|
457
|
+
'Cache-Control': 'no-cache, no-store'
|
|
459
458
|
}, this.cookie.headers(), this.response.headers);
|
|
460
459
|
data.response = this.response;
|
|
461
460
|
if (process.env.FaasMode === 'local') {
|
|
@@ -537,7 +536,7 @@ class Http {
|
|
|
537
536
|
}
|
|
538
537
|
function useHttp(config) {
|
|
539
538
|
const name = (config === null || config === void 0 ? void 0 : config.name) || Name;
|
|
540
|
-
if (process.env.FaasEnv !== 'testing' && globals[name])
|
|
539
|
+
if (process.env.FaasMode === 'mono' || (process.env.FaasEnv !== 'testing' && globals[name]))
|
|
541
540
|
return usePlugin(globals[name]);
|
|
542
541
|
return usePlugin(new Http(config));
|
|
543
542
|
}
|
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)
|
|
@@ -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();
|
|
@@ -386,7 +385,7 @@ class Http {
|
|
|
386
385
|
this.body = data.event.body;
|
|
387
386
|
this.params = Object.create(null);
|
|
388
387
|
this.response = { headers: Object.create(null) };
|
|
389
|
-
if (data.event.body)
|
|
388
|
+
if (data.event.body) {
|
|
390
389
|
if (data.event.headers && data.event.headers['content-type'] && data.event.headers['content-type'].includes('application/json')) {
|
|
391
390
|
this.logger.debug('[onInvoke] Parse params from json body');
|
|
392
391
|
this.params = JSON.parse(data.event.body);
|
|
@@ -395,16 +394,19 @@ class Http {
|
|
|
395
394
|
this.logger.debug('[onInvoke] Parse params from raw body');
|
|
396
395
|
this.params = data.event.body;
|
|
397
396
|
}
|
|
397
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
398
|
+
}
|
|
398
399
|
else if (data.event.queryString) {
|
|
399
400
|
this.logger.debug('[onInvoke] Parse params from queryString');
|
|
400
401
|
this.params = data.event.queryString;
|
|
402
|
+
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
401
403
|
}
|
|
402
|
-
this.logger.debug('[onInvoke] Params: %j', this.params);
|
|
403
|
-
this.logger.debug('[onInvoke] Parse cookie');
|
|
404
404
|
this.cookie.invoke(this.headers.cookie);
|
|
405
|
-
this.
|
|
406
|
-
|
|
407
|
-
|
|
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) {
|
|
408
410
|
this.logger.debug('[onInvoke] Valid request');
|
|
409
411
|
try {
|
|
410
412
|
await this.validator.valid({
|
|
@@ -418,10 +420,7 @@ class Http {
|
|
|
418
420
|
this.logger.error(error);
|
|
419
421
|
data.response = {
|
|
420
422
|
statusCode: error.statusCode || 500,
|
|
421
|
-
headers: {
|
|
422
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
423
|
-
'X-SCF-RequestId': data.context.request_id
|
|
424
|
-
},
|
|
423
|
+
headers: { 'Content-Type': 'application/json; charset=utf-8' },
|
|
425
424
|
body: JSON.stringify({ error: { message: error.message } })
|
|
426
425
|
};
|
|
427
426
|
return;
|
|
@@ -459,7 +458,7 @@ class Http {
|
|
|
459
458
|
// 处理 headers
|
|
460
459
|
this.response.headers = Object.assign({
|
|
461
460
|
'Content-Type': 'application/json; charset=utf-8',
|
|
462
|
-
'
|
|
461
|
+
'Cache-Control': 'no-cache, no-store'
|
|
463
462
|
}, this.cookie.headers(), this.response.headers);
|
|
464
463
|
data.response = this.response;
|
|
465
464
|
if (process.env.FaasMode === 'local') {
|
|
@@ -541,7 +540,7 @@ class Http {
|
|
|
541
540
|
}
|
|
542
541
|
function useHttp(config) {
|
|
543
542
|
const name = (config === null || config === void 0 ? void 0 : config.name) || Name;
|
|
544
|
-
if (process.env.FaasEnv !== 'testing' && globals[name])
|
|
543
|
+
if (process.env.FaasMode === 'mono' || (process.env.FaasEnv !== 'testing' && globals[name]))
|
|
545
544
|
return func.usePlugin(globals[name]);
|
|
546
545
|
return func.usePlugin(new Http(config));
|
|
547
546
|
}
|
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.291",
|
|
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": "5321c45ee65f2c5989ec39ad89d76f9f95bf205e"
|
|
30
30
|
}
|