@janiscommerce/api
Advanced tools
Comparing version 6.1.0 to 6.2.0
@@ -9,2 +9,9 @@ # Changelog | ||
## [6.2.0] - 2020-12-01 | ||
### Added | ||
- Added `userId` for logs | ||
### Changed | ||
- Created `LogHelper` to improve package organization | ||
## [6.1.0] - 2020-11-25 | ||
@@ -11,0 +18,0 @@ ### Added |
@@ -6,8 +6,4 @@ 'use strict'; | ||
const Log = require('@janiscommerce/log'); | ||
const { v4: uuid } = require('uuid'); | ||
const Fetcher = require('./fetcher'); | ||
const { omitRecursive, isObject } = require('./utils'); | ||
const { isObject } = require('./utils'); | ||
@@ -17,2 +13,4 @@ const API = require('./api'); | ||
const LogHelper = require('./helpers/log'); | ||
module.exports = class Dispatcher { | ||
@@ -29,3 +27,3 @@ | ||
this.authenticationData = request.authenticationData || {}; | ||
this._executionStarted = process.hrtime(); | ||
this.executionStarted = process.hrtime(); | ||
} | ||
@@ -63,51 +61,2 @@ | ||
_shouldCreateLog() { | ||
// Set default in case of shouldCreateLog is not defined | ||
if(typeof this.api.shouldCreateLog === 'undefined') | ||
this.api.shouldCreateLog = this.method !== 'get'; | ||
return isObject(this.api.session) && typeof this.api.session.clientCode === 'string' && this.api.shouldCreateLog; | ||
} | ||
_saveLog() { | ||
if(!this._shouldCreateLog()) | ||
return; | ||
const executionTime = this._executionFinished[1] / 1000000; | ||
const response = this.response(); | ||
const entityId = this.endpoint.split('/')[0]; | ||
const log = { | ||
api: { | ||
endpoint: this.endpoint, | ||
httpMethod: this.method | ||
}, | ||
request: {}, | ||
response: { | ||
code: response.code, | ||
headers: response.headers | ||
}, | ||
executionTime | ||
}; | ||
if(this.api.shouldLogRequestHeaders) | ||
log.request.headers = omitRecursive(this.headers, ['janis-api-key', 'janis-api-secret']); | ||
if(this.api.shouldLogRequestData) | ||
log.request.data = omitRecursive(this.data, this.api.excludeFieldsLogRequestData); | ||
if(this.api.shouldLogResponseBody) | ||
log.response.body = omitRecursive(response.body, this.api.excludeFieldsLogResponseBody); | ||
return Log.add(this.api.session.clientCode, { | ||
id: this.api.logId, | ||
entity: 'api', | ||
entityId, | ||
type: 'api-request', | ||
log | ||
}); | ||
} | ||
get fetcher() { | ||
@@ -135,5 +84,5 @@ | ||
this._executionFinished = process.hrtime(this._executionStarted); | ||
this.api.executionTime = process.hrtime(this.executionStarted)[1] / 1000000; | ||
await this._saveLog(); | ||
await LogHelper.save(this.api); | ||
@@ -149,2 +98,3 @@ return this.response(); | ||
this.api.endpoint = this.endpoint; | ||
this.api.httpMethod = this.method; | ||
this.api.data = this.data; | ||
@@ -154,7 +104,7 @@ this.api.pathParameters = this.fetcher.pathParameters; | ||
this.api.cookies = this.cookies; | ||
this.api.logId = uuid(); | ||
this.api.logId = LogHelper.generateId(); | ||
} catch(err) { | ||
this.api = new API(); // para poder setear error correctamente | ||
this.api = new API(); // para poder setear error y code correctamente | ||
@@ -232,2 +182,5 @@ /** | ||
if(!this.api.response.code) | ||
this.api.setCode(200); | ||
} catch(err) { | ||
@@ -256,8 +209,4 @@ | ||
response() { | ||
if(!this.api.response.code) | ||
this.api.setCode(200); | ||
return this.api.response; | ||
} | ||
}; |
{ | ||
"name": "@janiscommerce/api", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "A package for managing API from any origin", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26962
11
429