From ea4b30d6ef3ea4b5561fa0a402c65db08477e8fe Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Mon, 31 Jul 2023 16:34:09 +0300 Subject: [PATCH] feat(request-client): made verbose mode easier to configure --- README.md | 8 ++++++-- src/SASjs.ts | 25 +++++++++++++++++++++++-- src/minified/sas9/SASjs.ts | 3 ++- src/request/RequestClient.ts | 5 ++++- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b08ef9c..8997f5d 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,11 @@ The `request()` method also has optional parameters such as a config object and The response object will contain returned tables and columns. Table names are always lowercase, and column names uppercase. -The adapter will also cache the logs (if debug enabled) and even the work tables. For performance, it is best to keep debug mode off. +The adapter will also cache the logs (if debug enabled) and even the work tables. For performance, it is best to keep debug mode off. + +### Verbose Mode + +Enabling debug will also enable a verbose mode that logs a summary of every HTTP response. Verbose mode can be disabled by calling `disableVerboseMode` method or enabled by `enableVerboseMode` method. Verbose mode can also be enabled/disabled by `startComputeJob` method. ### Session Manager @@ -272,7 +276,7 @@ Configuration on the client side involves passing an object on startup, which ca * `appLoc` - this is the folder (eg in metadata or SAS Drive) under which the SAS services are created. * `serverType` - either `SAS9`, `SASVIYA` or `SASJS`. The `SASJS` server type is for use with [sasjs/server](https://github.com/sasjs/server). * `serverUrl` - the location (including http protocol and port) of the SAS Server. Can be omitted, eg if serving directly from the SAS Web Server, or in streaming mode. -* `debug` - if `true` then SAS Logs and extra debug information is returned. +* `debug` - if `true` then SAS Logs and extra debug information is returned. Setting debug to true will also enable a verbose mode that will log every HTTP response summary. * `loginMechanism` - either `Default` or `Redirected`. See [SAS Logon](#sas-logon) section. * `useComputeApi` - Only relevant when the serverType is `SASVIYA`. If `true` the [Compute API](#using-the-compute-api) is used. If `false` the [JES API](#using-the-jes-api) is used. If `null` or `undefined` the [Web](#using-jes-web-app) approach is used. * `contextName` - Compute context on which the requests will be called. If missing or not provided, defaults to `Job Execution Compute context`. diff --git a/src/SASjs.ts b/src/SASjs.ts index c7ed3c0..6dc67b1 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -31,6 +31,7 @@ import { } from './job-execution' import { ErrorResponse } from './types/errors' import { LoginOptions, LoginResult } from './types/Login' +import { AxiosResponse } from 'axios' interface ExecuteScriptParams { linesOfCode: string[] @@ -880,7 +881,7 @@ export default class SASjs { } if (verboseMode) this.requestClient?.enableVerboseMode() - else this.requestClient?.disableVerboseMode() + else if (verboseMode === false) this.requestClient?.disableVerboseMode() return this.sasViyaApiClient?.executeComputeJob( sasJob, @@ -975,7 +976,8 @@ export default class SASjs { this.requestClient = new RequestClientClass( this.sasjsConfig.serverUrl, this.sasjsConfig.httpsAgentOptions, - this.sasjsConfig.requestHistoryLimit + this.sasjsConfig.requestHistoryLimit, + this.sasjsConfig.debug // enable verbose mode if debug is true ) } else { this.requestClient.setConfig( @@ -1139,4 +1141,23 @@ export default class SASjs { ) } } + + /** + * Enables verbose mode that will log a summary of 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?: (response: AxiosResponse) => AxiosResponse, + errorCallBack?: (response: AxiosResponse) => AxiosResponse + ) { + this.requestClient?.enableVerboseMode(successCallBack, errorCallBack) + } + + /** + * Turns off verbose mode to log every HTTP response. + */ + public disableVerboseMode() { + this.requestClient?.disableVerboseMode() + } } diff --git a/src/minified/sas9/SASjs.ts b/src/minified/sas9/SASjs.ts index 5832cb6..91d9a95 100644 --- a/src/minified/sas9/SASjs.ts +++ b/src/minified/sas9/SASjs.ts @@ -233,7 +233,8 @@ export default class SASjs { this.requestClient = new RequestClient( this.sasjsConfig.serverUrl, this.sasjsConfig.httpsAgentOptions, - this.sasjsConfig.requestHistoryLimit + this.sasjsConfig.requestHistoryLimit, + this.sasjsConfig.debug // enable verbose mode if debug is true ) } else { this.requestClient.setConfig( diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 6d45a9e..181cbb0 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -69,11 +69,14 @@ export class RequestClient implements HttpClient { constructor( protected baseUrl: string, httpsAgentOptions?: https.AgentOptions, - requestsLimit?: number + requestsLimit?: number, + verboseMode?: boolean ) { this.createHttpClient(baseUrl, httpsAgentOptions) if (requestsLimit) this.requestsLimit = requestsLimit + + if (verboseMode) this.enableVerboseMode() } public setConfig(baseUrl: string, httpsAgentOptions?: https.AgentOptions) {