Skip to content

Commit

Permalink
Added logs for api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed Nov 7, 2019
1 parent 8501cb4 commit 80bad94
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 45 deletions.
34 changes: 34 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ class API {
};
}

// Logging and setters and getters

set shouldCreateLog(bool) {
this._shouldCreateLog = bool;
}

get shouldCreateLog() {
return this._shouldCreateLog;
}

set shouldLogRequestData(bool) {
this._shouldLogRequestData = bool;
}

get shouldLogRequestData() {
return typeof this._shouldLogRequestData !== 'undefined' ? this._shouldLogRequestData : true;
}

set shouldLogRequestHeaders(bool) {
this._shouldLogRequestHeaders = bool;
}

get shouldLogRequestHeaders() {
return typeof this._shouldLogRequestHeaders !== 'undefined' ? this._shouldLogRequestHeaders : true;
}

set shouldLogResponseBody(bool) {
this._shouldLogResponseBody = bool;
}

get shouldLogResponseBody() {
return typeof this._shouldLogResponseBody !== 'undefined' ? this._shouldLogResponseBody : true;
}

// Request setters and getters

set data(data) {
Expand Down
53 changes: 53 additions & 0 deletions lib/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
const { struct } = require('superstruct');
const { ApiSession } = require('@janiscommerce/api-session');

const Log = require('@janiscommerce/log');

const Fetcher = require('./fetcher');
const { omitRecursive } = require('./utils');

const API = require('./api');
const APIError = require('./error');
Expand All @@ -19,6 +22,7 @@ class Dispatcher {
this.headers = request.headers || {};
this.cookies = request.cookies || {};
this.authenticationData = request.authenticationData || {};
this._executionStarted = process.hrtime();
}

/**
Expand Down Expand Up @@ -61,6 +65,51 @@ class Dispatcher {
throw new APIError('authenticationData must be an Object', APIError.codes.INVALID_AUTHENTICATION_DATA);
}

_saveLog() {

// Set default in case of shouldCreateLog is not defined
if(typeof this.shouldCreateLog === 'undefined')
this.shouldCreateLog = this.endpoint !== 'get';

if(!this._isObject(this.session) || typeof this.session.clientCode !== 'string' || !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: {

...this.api.shouldLogRequestHeaders ?
{ headers: omitRecursive(this.headers, ['janis-api-key', 'janis-api-secret']) } : {},

...this.api.shouldLogRequestData ?
{ data: this.api.excludeFieldsLogRequestData ? omitRecursive(this.data, this.api.excludeFieldsLogRequestData) : this.data } : {}
},
response: {

code: response.code,
headers: response.headers,

...this.api.shouldLogResponseBody ?
{ body: this.api.excludeFieldsLogResponseBody ? omitRecursive(response.body, this.api.excludeFieldsLogResponseBody) : response.body } : {}
},
executionTime
};

Log.add(this.session.clientCode, {
entity: 'api',
entityId,
type: 'api-request',
log
});
}

get fetcher() {

if(!this._fetcher)
Expand All @@ -83,6 +132,10 @@ class Dispatcher {

await this.process();

this._executionFinished = process.hrtime(this._executionStarted);

this._saveLog();

return this.response();
}

Expand Down
18 changes: 18 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const omit = require('lodash.omit');

const omitRecursive = (object, exclude) => {

object = { ...object }; // Avoid original object modification

Object.entries(object).forEach(([key, value]) => {
object[key] = typeof value === 'object' && !Array.isArray(value) ? omitRecursive(value, exclude) : value;
});

return omit(object, exclude);
};

module.exports = {
omitRecursive
};
Loading

0 comments on commit 80bad94

Please sign in to comment.