@faasjs/http 0.0.2-beta.273 → 0.0.2-beta.290

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 CHANGED
@@ -42,6 +42,7 @@ export declare class Http<TParams extends Record<string, any> = any, TCookie ext
42
42
  headers: {
43
43
  [key: string]: string;
44
44
  };
45
+ body: any;
45
46
  params: TParams;
46
47
  cookie: Cookie<TCookie, TSession>;
47
48
  session: Session<TSession, TCookie>;
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 = new Logger('Http.Validator');
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 params');
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 cookie');
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}] Unpermitted keys: ${diffKeys.join(', ')}`);
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();
@@ -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.logger.debug('[onInvoke] Cookie: %j', this.cookie.content);
401
- this.logger.debug('[onInvoke] Session: %j', this.session.content);
402
- if (this.validator && data.event.httpMethod) {
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;
@@ -454,7 +454,7 @@ class Http {
454
454
  // 处理 headers
455
455
  this.response.headers = Object.assign({
456
456
  'Content-Type': 'application/json; charset=utf-8',
457
- 'X-SCF-RequestId': data.context.request_id
457
+ 'Cache-Control': 'no-cache, no-store'
458
458
  }, this.cookie.headers(), this.response.headers);
459
459
  data.response = this.response;
460
460
  if (process.env.FaasMode === 'local') {
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 = new logger.Logger('Http.Validator');
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 params');
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 cookie');
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}] Unpermitted keys: ${diffKeys.join(', ')}`);
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();
@@ -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.logger.debug('[onInvoke] Cookie: %j', this.cookie.content);
405
- this.logger.debug('[onInvoke] Session: %j', this.session.content);
406
- if (this.validator && data.event.httpMethod) {
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;
@@ -458,7 +458,7 @@ class Http {
458
458
  // 处理 headers
459
459
  this.response.headers = Object.assign({
460
460
  'Content-Type': 'application/json; charset=utf-8',
461
- 'X-SCF-RequestId': data.context.request_id
461
+ 'Cache-Control': 'no-cache, no-store'
462
462
  }, this.cookie.headers(), this.response.headers);
463
463
  data.response = this.response;
464
464
  if (process.env.FaasMode === 'local') {
@@ -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.273",
3
+ "version": "0.0.2-beta.290",
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": "f7dc9d18202a0194a4cf2350b6fb97d1ca64c90e"
29
+ "gitHead": "e2e31c34a721d45a2c98dcec493521127cf6f4d2"
30
30
  }