@faasjs/http 0.0.2-beta.278 → 0.0.2-beta.292

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 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();
@@ -403,7 +402,7 @@ class Http {
403
402
  this.logger.debug('[onInvoke] Cookie: %j', this.cookie.content);
404
403
  this.logger.debug('[onInvoke] Session: %j', this.session.content);
405
404
  }
406
- if (this.validator && data.event.httpMethod) {
405
+ if (this.validator) {
407
406
  this.logger.debug('[onInvoke] Valid request');
408
407
  try {
409
408
  await this.validator.valid({
@@ -417,10 +416,7 @@ class Http {
417
416
  this.logger.error(error);
418
417
  data.response = {
419
418
  statusCode: error.statusCode || 500,
420
- headers: {
421
- 'Content-Type': 'application/json; charset=utf-8',
422
- 'X-SCF-RequestId': data.context.request_id
423
- },
419
+ headers: { 'Content-Type': 'application/json; charset=utf-8' },
424
420
  body: JSON.stringify({ error: { message: error.message } })
425
421
  };
426
422
  return;
@@ -458,7 +454,7 @@ class Http {
458
454
  // 处理 headers
459
455
  this.response.headers = Object.assign({
460
456
  'Content-Type': 'application/json; charset=utf-8',
461
- 'X-SCF-RequestId': data.context.request_id
457
+ 'Cache-Control': 'no-cache, no-store'
462
458
  }, this.cookie.headers(), this.response.headers);
463
459
  data.response = this.response;
464
460
  if (process.env.FaasMode === 'local') {
@@ -540,7 +536,7 @@ class Http {
540
536
  }
541
537
  function useHttp(config) {
542
538
  const name = (config === null || config === void 0 ? void 0 : config.name) || Name;
543
- if (process.env.FaasEnv !== 'testing' && globals[name])
539
+ if (process.env.FaasMode !== 'mono' && process.env.FaasEnv !== 'testing' && globals[name])
544
540
  return usePlugin(globals[name]);
545
541
  return usePlugin(new Http(config));
546
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 = 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();
@@ -407,7 +406,7 @@ class Http {
407
406
  this.logger.debug('[onInvoke] Cookie: %j', this.cookie.content);
408
407
  this.logger.debug('[onInvoke] Session: %j', this.session.content);
409
408
  }
410
- if (this.validator && data.event.httpMethod) {
409
+ if (this.validator) {
411
410
  this.logger.debug('[onInvoke] Valid request');
412
411
  try {
413
412
  await this.validator.valid({
@@ -421,10 +420,7 @@ class Http {
421
420
  this.logger.error(error);
422
421
  data.response = {
423
422
  statusCode: error.statusCode || 500,
424
- headers: {
425
- 'Content-Type': 'application/json; charset=utf-8',
426
- 'X-SCF-RequestId': data.context.request_id
427
- },
423
+ headers: { 'Content-Type': 'application/json; charset=utf-8' },
428
424
  body: JSON.stringify({ error: { message: error.message } })
429
425
  };
430
426
  return;
@@ -462,7 +458,7 @@ class Http {
462
458
  // 处理 headers
463
459
  this.response.headers = Object.assign({
464
460
  'Content-Type': 'application/json; charset=utf-8',
465
- 'X-SCF-RequestId': data.context.request_id
461
+ 'Cache-Control': 'no-cache, no-store'
466
462
  }, this.cookie.headers(), this.response.headers);
467
463
  data.response = this.response;
468
464
  if (process.env.FaasMode === 'local') {
@@ -544,7 +540,7 @@ class Http {
544
540
  }
545
541
  function useHttp(config) {
546
542
  const name = (config === null || config === void 0 ? void 0 : config.name) || Name;
547
- if (process.env.FaasEnv !== 'testing' && globals[name])
543
+ if (process.env.FaasMode !== 'mono' && process.env.FaasEnv !== 'testing' && globals[name])
548
544
  return func.usePlugin(globals[name]);
549
545
  return func.usePlugin(new Http(config));
550
546
  }
@@ -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.278",
3
+ "version": "0.0.2-beta.292",
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": "bd5d012da331ff072eae91c0c006a44248bcc9e2"
29
+ "gitHead": "07a254eef51ef6bd85f9cdead9407b2c8dc85abc"
30
30
  }