From ccb8599f002434556c722e185cc4aca748f2b4c2 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Fri, 28 Jul 2023 11:55:52 +0300 Subject: [PATCH] docs(request-client): added comments --- src/SASjs.ts | 1 + src/request/RequestClient.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/SASjs.ts b/src/SASjs.ts index 3e15a9d..c7ed3c0 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -854,6 +854,7 @@ export default class SASjs { * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { maxPollCount: 24 * 60 * 60, pollInterval: 1000 }. More information available at src/api/viya/pollJobState.ts. * @param printPid - a boolean that indicates whether the function should print (PID) of the started job. * @param variables - an object that represents macro variables. + * @param verboseMode - boolean to enable verbose mode (log every HTTP response). */ public async startComputeJob( sasJob: string, diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index b364781..6d45a9e 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -393,13 +393,24 @@ export class RequestClient implements HttpClient { }) } + /** + * Adds colors to the string. + * @param str - string to be prettified. + * @returns - prettified string + */ private prettifyString = (str: any) => inspect(str, { colors: true }) + /** + * Formats HTTP request/response body. + * @param body - HTTP request/response body. + * @returns - formatted string. + */ private parseInterceptedBody = (body: any) => { if (!body) return '' let parsedBody + // Tries to parse body into JSON object. if (typeof body === 'string') { try { parsedBody = JSON.parse(body) @@ -412,6 +423,7 @@ export class RequestClient implements HttpClient { const bodyLines = this.prettifyString(parsedBody).split('\n') + // Leaves first 50 lines if (bodyLines.length > 51) { bodyLines.splice(50) bodyLines.push('...') @@ -426,6 +438,8 @@ export class RequestClient implements HttpClient { const { _header: reqHeaders, res } = request const { rawHeaders } = res + // Converts an array of strings into a single string with the following format: + // : const resHeaders = rawHeaders.reduce( (acc: string, value: string, i: number) => { if (i % 2 === 0) { @@ -441,6 +455,7 @@ export class RequestClient implements HttpClient { const parsedResBody = this.parseInterceptedBody(resData) + // HTTP response summary. process.logger?.info(`HTTP Request (first 50 lines): ${reqHeaders}${this.parseInterceptedBody(reqData)} @@ -453,6 +468,11 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''} return response } + /** + * Turns on verbose mode to log every HTTP response. + * @param successCallBack - function that should be triggered on every HTTP response with the status 2**. + * @param errorCallBack - function that should be triggered on every HTTP response with the status different from 2**. + */ public enableVerboseMode = ( successCallBack = this.defaultInterceptionCallBack, errorCallBack = this.defaultInterceptionCallBack @@ -463,6 +483,9 @@ ${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''} ) } + /** + * Turns off verbose mode to log every HTTP response. + */ public disableVerboseMode = () => { if (this.httpInterceptor) { this.httpClient.interceptors.response.eject(this.httpInterceptor)