@new10com/axios-logger
Advanced tools
Comparing version 0.2.11 to 0.2.12
{ | ||
"name": "@new10com/axios-logger", | ||
"version": "0.2.11", | ||
"version": "0.2.12", | ||
"description": "Library that should be used as axios request/response interceptor for logging in a pretty format request/response details", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -50,2 +50,70 @@ "use strict"; | ||
}); | ||
}); | ||
describe(`Axios Logger Request Test Suite`, () => { | ||
it(`Test simple get request`, () => { | ||
let message = ''; | ||
const loggerMock = (msg, ..._args) => { | ||
message = typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2); | ||
logger_1.logger.info(message); | ||
}; | ||
const log4jsLogger = (0, log4js_1.getLogger)('axios'); | ||
log4jsLogger.info = loggerMock; | ||
const axiosLogger = axios_logger_1.AxiosLogger.from(log4jsLogger); | ||
const url = 'https://doodle.com'; | ||
const method = 'GET'; | ||
const headers = { 'Content-Type': 'application/json' }; | ||
const params = { firstName: 'John', lastName: 'Wick' }; | ||
const axiosRequestConfig = { | ||
url, | ||
method, | ||
baseURL: url, | ||
headers, | ||
params, | ||
}; | ||
axiosLogger.logRequest(axiosRequestConfig); | ||
expect(message).toMatchInlineSnapshot(` | ||
" | ||
┌────── Request ────────────────────────────────────────────────────────────────────────────────────────────── | ||
URL: https://doodle.com?firstName=John&lastName=Wick | ||
Method: @GET | ||
Headers: | ||
┌ | ||
├ Content-Type: \\"application/json\\" | ||
└ | ||
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────" | ||
`); | ||
}); | ||
it(`Test simple delete request`, () => { | ||
let message = ''; | ||
const loggerMock = (msg, ..._args) => { | ||
message = typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2); | ||
logger_1.logger.info(message); | ||
}; | ||
const log4jsLogger = (0, log4js_1.getLogger)('axios'); | ||
log4jsLogger.info = loggerMock; | ||
const axiosLogger = axios_logger_1.AxiosLogger.from(log4jsLogger); | ||
const url = 'https://doodle.com'; | ||
const method = 'DELETE'; | ||
const headers = { 'Content-Type': 'application/json' }; | ||
const params = { firstName: 'John', lastName: 'Wick' }; | ||
const axiosRequestConfig = { | ||
url, | ||
method, | ||
baseURL: url, | ||
headers, | ||
params, | ||
}; | ||
axiosLogger.logRequest(axiosRequestConfig); | ||
expect(message).toMatchInlineSnapshot(` | ||
" | ||
┌────── Request ────────────────────────────────────────────────────────────────────────────────────────────── | ||
URL: https://doodle.com?firstName=John&lastName=Wick | ||
Method: @DELETE | ||
Headers: | ||
┌ | ||
├ Content-Type: \\"application/json\\" | ||
└ | ||
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────" | ||
`); | ||
}); | ||
it(`Test simple post request`, () => { | ||
@@ -102,2 +170,99 @@ let message = ''; | ||
}); | ||
it(`Test simple update request`, () => { | ||
let message = ''; | ||
const loggerMock = (msg, ..._args) => { | ||
message = typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2); | ||
logger_1.logger.info(message); | ||
}; | ||
const log4jsLogger = (0, log4js_1.getLogger)('axios'); | ||
log4jsLogger.info = loggerMock; | ||
const axiosLogger = axios_logger_1.AxiosLogger.from(log4jsLogger); | ||
const url = 'https://doodle.com'; | ||
const method = 'PATCH'; | ||
const headers = { 'Content-Type': 'application/json' }; | ||
const params = { firstName: 'John', lastName: 'Wick' }; | ||
const body = { | ||
city: 'Amsterdam', | ||
console: 'PS4', | ||
score: 100, | ||
hobbies: ['games', 'programming', 'tv shows'], | ||
}; | ||
const axiosRequestConfig = { | ||
url, | ||
method, | ||
baseURL: url, | ||
headers, | ||
params, | ||
data: body, | ||
}; | ||
axiosLogger.logRequest(axiosRequestConfig); | ||
expect(message).toMatchInlineSnapshot(` | ||
" | ||
┌────── Request ────────────────────────────────────────────────────────────────────────────────────────────── | ||
URL: https://doodle.com?firstName=John&lastName=Wick | ||
Method: @PATCH | ||
Headers: | ||
┌ | ||
├ Content-Type: \\"application/json\\" | ||
└ | ||
Body: | ||
{ | ||
\\"city\\": \\"Amsterdam\\", | ||
\\"console\\": \\"PS4\\", | ||
\\"score\\": 100, | ||
\\"hobbies\\": [ | ||
\\"games\\", | ||
\\"programming\\", | ||
\\"tv shows\\" | ||
] | ||
} | ||
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────" | ||
`); | ||
}); | ||
it(`Test that body does not get logged when it's more then allowed`, () => { | ||
let message = ''; | ||
const loggerMock = (msg, ..._args) => { | ||
message = typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2); | ||
logger_1.logger.info(message); | ||
}; | ||
const log4jsLogger = (0, log4js_1.getLogger)('axios'); | ||
log4jsLogger.info = loggerMock; | ||
const axiosLogger = axios_logger_1.AxiosLogger.from(log4jsLogger, { | ||
request: { maxLogContentLength: 10 }, | ||
}); | ||
const url = 'https://doodle.com'; | ||
const method = 'PATCH'; | ||
const body = { | ||
city: 'Amsterdam', | ||
console: 'PS4', | ||
score: 100, | ||
hobbies: ['games', 'programming', 'tv shows'], | ||
}; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'Content-Length': JSON.stringify(body).length, | ||
}; | ||
const params = { firstName: 'John', lastName: 'Wick' }; | ||
const axiosRequestConfig = { | ||
url, | ||
method, | ||
baseURL: url, | ||
headers, | ||
params, | ||
data: body, | ||
}; | ||
axiosLogger.logRequest(axiosRequestConfig); | ||
expect(message).toMatchInlineSnapshot(` | ||
" | ||
┌────── Request ────────────────────────────────────────────────────────────────────────────────────────────── | ||
URL: https://doodle.com?firstName=John&lastName=Wick | ||
Method: @PATCH | ||
Headers: | ||
┌ Content-Type: \\"application/json\\" | ||
└ Content-Length: \\"93\\" | ||
Body: | ||
Body is too long to be displayed. Length: 93 bytes. Max length: 10 bytes. | ||
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────" | ||
`); | ||
}); | ||
}); | ||
@@ -151,2 +316,49 @@ describe(`Axios Logger Response Test Suite`, () => { | ||
}); | ||
it(`Test that body does not get logged when it's more then allowed`, () => { | ||
let message = ''; | ||
const loggerMock = (msg, ..._args) => { | ||
message = typeof msg === 'string' ? msg : JSON.stringify(msg, null, 2); | ||
logger_1.logger.info(message); | ||
}; | ||
const log4jsLogger = (0, log4js_1.getLogger)('axios'); | ||
log4jsLogger.info = loggerMock; | ||
const axiosLogger = axios_logger_1.AxiosLogger.from(log4jsLogger, { | ||
response: { maxLogContentLength: 10 }, | ||
}); | ||
const url = 'https://doodle.com'; | ||
const method = 'GET'; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
'Content-Length': '100', | ||
}; | ||
const axiosRequestConfig = { | ||
url, | ||
method, | ||
baseURL: url, | ||
headers, | ||
}; | ||
const response = { success: true, status: 'DONE' }; | ||
const axiosResponse = { | ||
data: response, | ||
status: 200, | ||
statusText: 'SUCCESS', | ||
headers, | ||
config: axiosRequestConfig, | ||
request: axiosRequestConfig, | ||
}; | ||
axiosLogger.logResponse(axiosResponse); | ||
expect(message).toMatchInlineSnapshot(` | ||
" | ||
┌────── Response ────────────────────────────────────────────────────────────────────────────────────────────── | ||
URL: https://doodle.com | ||
Method: @GET | ||
Status: 200 SUCCESS | ||
Headers | ||
┌ Content-Type: \\"application/json\\" | ||
└ Content-Length: \\"100\\" | ||
Body: | ||
Body is too long to be displayed. Length: 100 bytes. Max length: 10 bytes. | ||
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────" | ||
`); | ||
}); | ||
it(`Test Response without body`, () => { | ||
@@ -153,0 +365,0 @@ let message = ''; |
@@ -17,5 +17,6 @@ export interface Headers { | ||
title(detailName: string): string; | ||
prettyFormatBody({ body, maxLogContentLength, }: { | ||
prettyFormatBody({ body, maxLogContentLength, contentLength, }: { | ||
body: string | object; | ||
maxLogContentLength?: number; | ||
contentLength?: number; | ||
}): string; | ||
@@ -22,0 +23,0 @@ indent(): string; |
@@ -5,3 +5,2 @@ "use strict"; | ||
const constants_1 = require("../constants/constants"); | ||
const buffer_1 = require("buffer"); | ||
class Formatter { | ||
@@ -17,3 +16,3 @@ constructor(config) { | ||
} | ||
prettyFormatBody({ body, maxLogContentLength, }) { | ||
prettyFormatBody({ body, maxLogContentLength, contentLength, }) { | ||
let bodyAsString = ''; | ||
@@ -43,7 +42,6 @@ const indent = this.indent(); | ||
const lastCurlyBracket = bodyAsString.lastIndexOf('}'); | ||
if (maxLogContentLength !== undefined) { | ||
const bodyLengthInBytes = buffer_1.Buffer.byteLength(bodyAsString); | ||
if (bodyLengthInBytes > maxLogContentLength) { | ||
return `Body is too long to be displayed. Length: ${bodyLengthInBytes} bytes. Max length: ${maxLogContentLength} bytes.`; | ||
} | ||
if (contentLength !== undefined && | ||
maxLogContentLength !== undefined && | ||
contentLength > maxLogContentLength) { | ||
return `Body is too long to be displayed. Length: ${contentLength} bytes. Max length: ${maxLogContentLength} bytes.`; | ||
} | ||
@@ -50,0 +48,0 @@ return lastCurlyBracket > 0 |
@@ -35,3 +35,7 @@ "use strict"; | ||
const body = 'Hello how are you?'.repeat(100); | ||
expect(formatter.prettyFormatBody({ body, maxLogContentLength: 10 })).toEqual(`Body is too long to be displayed. Length: 1802 bytes. Max length: 10 bytes.`); | ||
expect(formatter.prettyFormatBody({ | ||
body, | ||
maxLogContentLength: 10, | ||
contentLength: body.length, | ||
})).toEqual(`Body is too long to be displayed. Length: 1800 bytes. Max length: 10 bytes.`); | ||
}); | ||
@@ -38,0 +42,0 @@ }); |
@@ -21,3 +21,4 @@ import type { IConfig } from '../config/axios-logger-config'; | ||
private parseBodyDetails; | ||
private static getContentLength; | ||
} | ||
export {}; |
@@ -103,3 +103,3 @@ "use strict"; | ||
parseRequest(request) { | ||
var _a, _b, _c; | ||
var _a, _b, _c, _d; | ||
let requestDetailsArr; | ||
@@ -116,5 +116,8 @@ const startOfRequest = separator_1.Separator.startingLine('Request'); | ||
if (request.data !== undefined && ((_c = this.config.request) === null || _c === void 0 ? void 0 : _c.shouldLogBody)) { | ||
const contentLength = Parser.getContentLength(request.headers); | ||
const { bodyTitle, body } = this.parseBodyDetails({ | ||
axiosRequestConfig: request, | ||
obfuscationConfig: this.config.obfuscation, | ||
maxLogContentLength: (_d = this.config.request) === null || _d === void 0 ? void 0 : _d.maxLogContentLength, | ||
contentLength, | ||
}); | ||
@@ -126,3 +129,3 @@ requestDetailsArr = [...requestDetailsArr, bodyTitle, body]; | ||
parseResponse(resp) { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c, _d, _e; | ||
const startOfRequest = separator_1.Separator.startingLine('Response'); | ||
@@ -150,5 +153,8 @@ const url = `${this.formatter.title('URL')}: ${this.parseUrl(resp.config)}`; | ||
((_c = this.config.response) === null || _c === void 0 ? void 0 : _c.shouldLogBody)) { | ||
const contentLength = Parser.getContentLength(resp.headers); | ||
const { bodyTitle, body } = this.parseBodyDetails({ | ||
axiosRequestConfig: resp, | ||
obfuscationConfig: this.config.obfuscation, | ||
maxLogContentLength: (_d = this.config.response) === null || _d === void 0 ? void 0 : _d.maxLogContentLength, | ||
contentLength, | ||
}); | ||
@@ -162,3 +168,3 @@ return [ | ||
} | ||
else if ((_d = this.config.response) === null || _d === void 0 ? void 0 : _d.shouldLogBody) { | ||
else if ((_e = this.config.response) === null || _e === void 0 ? void 0 : _e.shouldLogBody) { | ||
const bodyTitle = `${this.formatter.title('Body')}:`; | ||
@@ -222,3 +228,3 @@ return [ | ||
} | ||
parseBodyDetails({ axiosRequestConfig, obfuscationConfig, }) { | ||
parseBodyDetails({ axiosRequestConfig, obfuscationConfig, maxLogContentLength, contentLength, }) { | ||
const bodyTitle = `${this.formatter.title('Body')}:`; | ||
@@ -231,7 +237,16 @@ const parsedBody = Parser.prepareBodyForFormatting({ | ||
body: parsedBody, | ||
maxLogContentLength, | ||
contentLength, | ||
}); | ||
return { bodyTitle, body: prettyFormattedBody }; | ||
} | ||
static getContentLength(headers) { | ||
const foundKey = Object.keys(headers).find((key) => key.toLowerCase() === 'Content-Length'.toLowerCase()); | ||
if (foundKey === undefined) { | ||
return undefined; | ||
} | ||
return Number(headers[foundKey]); | ||
} | ||
} | ||
exports.Parser = Parser; | ||
//# sourceMappingURL=parser.js.map |
{ | ||
"name": "@new10com/axios-logger", | ||
"version": "0.2.11", | ||
"version": "0.2.12", | ||
"description": "Library that should be used as axios request/response interceptor for logging in a pretty format request/response details", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
201928
2816