From 936e4f8c0a11413102401d86f3323ad5bc3fd2ab Mon Sep 17 00:00:00 2001 From: Mihajlo Date: Tue, 26 Apr 2022 16:18:36 +0200 Subject: [PATCH 1/3] fix: sasjs server type - request and job execution auth fix --- src/SASjs.ts | 4 +-- src/SASjsApiClient.ts | 6 ++-- src/job-execution/WebJobExecutor.ts | 48 ++++++++++++++++++----------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/SASjs.ts b/src/SASjs.ts index ac83240..f554947 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -916,8 +916,8 @@ export default class SASjs { return await this.sasJSApiClient?.deploy(dataJson, appLoc, authConfig) } - public async executeJobSASjs(query: ExecutionQuery) { - return await this.sasJSApiClient?.executeJob(query) + public async executeJobSASjs(query: ExecutionQuery, authConfig?: AuthConfig) { + return await this.sasJSApiClient?.executeJob(query, authConfig) } /** diff --git a/src/SASjsApiClient.ts b/src/SASjsApiClient.ts index b152f5d..af17911 100644 --- a/src/SASjsApiClient.ts +++ b/src/SASjsApiClient.ts @@ -43,7 +43,9 @@ export class SASjsApiClient { return Promise.resolve(result) } - public async executeJob(query: ExecutionQuery) { + public async executeJob(query: ExecutionQuery, authConfig?: AuthConfig) { + const access_token = authConfig ? authConfig.access_token : undefined + const { result } = await this.requestClient.post<{ status: string message: string @@ -51,7 +53,7 @@ export class SASjsApiClient { logPath?: string error?: {} _webout?: string - }>('SASjsApi/stp/execute', query, undefined) + }>('SASjsApi/stp/execute', query, access_token) if (Object.keys(result).includes('_webout')) { result._webout = parseWeboutResponse(result._webout!) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index f3c3031..187bc9c 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -21,6 +21,7 @@ import { } from '../utils' import { BaseJobExecutor } from './JobExecutor' import { parseWeboutResponse } from '../utils/parseWeboutResponse' +import { Server } from 'https' export interface WaitingRequstPromise { promise: Promise | null @@ -220,25 +221,36 @@ export class WebJobExecutor extends BaseJobExecutor { } if (e instanceof LoginRequiredError) { - this.appendWaitingRequest(() => { - return this.execute( - sasJob, - data, - config, - loginRequiredCallback, - authConfig, - extraResponseAttributes - ).then( - (res: any) => { - resolve(res) - }, - (err: any) => { - reject(err) - } - ) - }) + switch (this.serverType) { + case ServerType.Sasjs: + reject( + new ErrorResponse( + 'Request is not authenticated. Make sure .env file exists with valid credentials.', + e + ) + ) + break + default: + this.appendWaitingRequest(() => { + return this.execute( + sasJob, + data, + config, + loginRequiredCallback, + authConfig, + extraResponseAttributes + ).then( + (res: any) => { + resolve(res) + }, + (err: any) => { + reject(err) + } + ) + }) - await loginCallback() + await loginCallback() + } } else { reject(new ErrorResponse(e?.message, e)) } From fc47222830ed96c5ffff72725e734b38974dfbb7 Mon Sep 17 00:00:00 2001 From: Mihajlo Date: Tue, 26 Apr 2022 17:37:13 +0200 Subject: [PATCH 2/3] fix: web request method - login callback handling --- src/job-execution/WebJobExecutor.ts | 58 ++++++++++++++--------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index 187bc9c..62e2431 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -47,7 +47,7 @@ export class WebJobExecutor extends BaseJobExecutor { authConfig?: AuthConfig, extraResponseAttributes: ExtraResponseAttributes[] = [] ) { - const loginCallback = loginRequiredCallback || (() => Promise.resolve()) + const loginCallback = loginRequiredCallback const program = isRelativePath(sasJob) ? config.appLoc ? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '') @@ -80,7 +80,7 @@ export class WebJobExecutor extends BaseJobExecutor { ) }) - await loginCallback() + if (loginCallback) await loginCallback() } else { reject(new ErrorResponse(e?.message, e)) } @@ -221,36 +221,34 @@ export class WebJobExecutor extends BaseJobExecutor { } if (e instanceof LoginRequiredError) { - switch (this.serverType) { - case ServerType.Sasjs: - reject( - new ErrorResponse( - 'Request is not authenticated. Make sure .env file exists with valid credentials.', - e - ) + if (!loginRequiredCallback) { + reject( + new ErrorResponse( + 'Request is not authenticated. Make sure .env file exists with valid credentials.', + e ) - break - default: - this.appendWaitingRequest(() => { - return this.execute( - sasJob, - data, - config, - loginRequiredCallback, - authConfig, - extraResponseAttributes - ).then( - (res: any) => { - resolve(res) - }, - (err: any) => { - reject(err) - } - ) - }) - - await loginCallback() + ) } + + this.appendWaitingRequest(() => { + return this.execute( + sasJob, + data, + config, + loginRequiredCallback, + authConfig, + extraResponseAttributes + ).then( + (res: any) => { + resolve(res) + }, + (err: any) => { + reject(err) + } + ) + }) + + if (loginCallback) await loginCallback() } else { reject(new ErrorResponse(e?.message, e)) } From fdc3e1cce8948de2a5bd464084f6984c1d3ae5e2 Mon Sep 17 00:00:00 2001 From: Mihajlo Date: Tue, 26 Apr 2022 17:41:35 +0200 Subject: [PATCH 3/3] style: lint --- src/job-execution/WebJobExecutor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index 62e2431..a4bdbe8 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -229,7 +229,7 @@ export class WebJobExecutor extends BaseJobExecutor { ) ) } - + this.appendWaitingRequest(() => { return this.execute( sasJob,