1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-21 13:11:20 +00:00

Compare commits

..

1 Commits

Author SHA1 Message Date
sabir_hassan
591e1ebc09 fix: callback type checking fixes #304 2021-05-27 21:59:40 +05:00
29 changed files with 2544 additions and 3186 deletions

View File

@@ -6,7 +6,7 @@ GREEN="\033[1;32m"
# temporary file which holds the message). # temporary file which holds the message).
commit_message=$(cat "$1") commit_message=$(cat "$1")
if (echo "$commit_message" | grep -Eq "^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9 \-]+\))?!?: .+$") then if (echo "$commit_message" | grep -Eq "^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z \-]+\))?!?: .+$") then
echo "${GREEN} ✔ Commit message meets Conventional Commit standards" echo "${GREEN} ✔ Commit message meets Conventional Commit standards"
exit 0 exit 0
fi fi

View File

@@ -1,7 +0,0 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10

4648
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -38,38 +38,31 @@
}, },
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/jest": "^26.0.23", "@types/jest": "^26.0.22",
"@types/mime": "^2.0.3",
"@types/tough-cookie": "^4.0.0",
"cp": "^0.2.0", "cp": "^0.2.0",
"dotenv": "^10.0.0", "dotenv": "^8.2.0",
"jest": "^27.0.4", "jest": "^26.6.3",
"jest-extended": "^0.11.5", "jest-extended": "^0.11.5",
"mime": "^2.5.2",
"path": "^0.12.7", "path": "^0.12.7",
"process": "^0.11.10",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"semantic-release": "^17.4.3", "semantic-release": "^17.4.2",
"terser-webpack-plugin": "^5.1.3", "terser-webpack-plugin": "^4.2.3",
"ts-jest": "^27.0.3", "ts-jest": "^25.5.1",
"ts-loader": "^9.2.2", "ts-loader": "^9.1.2",
"tslint": "^6.1.3", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0", "tslint-config-prettier": "^1.18.0",
"typedoc": "^0.20.36", "typedoc": "^0.20.35",
"typedoc-neo-theme": "^1.1.1", "typedoc-neo-theme": "^1.1.0",
"typedoc-plugin-external-module-name": "^4.0.6", "typedoc-plugin-external-module-name": "^4.0.6",
"typescript": "^4.3.2", "typescript": "^3.9.9",
"webpack": "^5.38.1", "webpack": "^5.33.2",
"webpack-cli": "^4.7.2" "webpack-cli": "^4.7.0"
}, },
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"@sasjs/utils": "^2.20.1", "@sasjs/utils": "^2.10.2",
"axios": "^0.21.1", "axios": "^0.21.1",
"axios-cookiejar-support": "^1.0.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"https": "^1.0.0", "https": "^1.0.0"
"tough-cookie": "^4.0.0",
"url": "^0.11.0"
} }
} }

View File

@@ -6,7 +6,7 @@ When developing on `@sasjs/adapter`, it's good practice to run the test suite ag
You can use the provided `update:adapter` NPM script for this. You can use the provided `update:adapter` NPM script for this.
```bash ```
npm run update:adapter npm run update:adapter
``` ```
@@ -37,7 +37,7 @@ To be able to run the `deploy` script, two environment variables need to be set:
So you can run the script like so: So you can run the script like so:
```bash ```
SSH_ACCOUNT=me@my-sas-server.com DEPLOY_PATH=/var/www/html/my-folder/sasjs-tests npm run deploy SSH_ACCOUNT=me@my-sas-server.com DEPLOY_PATH=/var/www/html/my-folder/sasjs-tests npm run deploy
``` ```
@@ -49,7 +49,8 @@ The below services need to be created on your SAS server, at the location specif
### SAS 9 ### SAS 9
```sas ```
filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas"; filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
%inc mc; %inc mc;
filename ft15f001 temp; filename ft15f001 temp;
@@ -71,24 +72,11 @@ parmcards4;
%webout(CLOSE) %webout(CLOSE)
;;;; ;;;;
%mm_createwebservice(path=/Public/app/common,name=sendArr) %mm_createwebservice(path=/Public/app/common,name=sendArr)
parmcards4;
let he who hath understanding, reckon the number of the beast
;;;;
%mm_createwebservice(path=/Public/app/common,name=makeErr)
parmcards4;
%webout(OPEN)
data _null_;
file _webout;
put ' the discovery channel ';
run;
%webout(CLOSE)
;;;;
%mm_createwebservice(path=/Public/app/common,name=invalidJSON)
``` ```
### SAS Viya ### SAS Viya
```sas ```
filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas"; filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
%inc mc; %inc mc;
filename ft15f001 temp; filename ft15f001 temp;
@@ -127,15 +115,6 @@ If you can trust yourself when all men doubt you,
But make allowance for their doubting too; But make allowance for their doubting too;
;;;; ;;;;
%mp_createwebservice(path=/Public/app/common,name=makeErr) %mp_createwebservice(path=/Public/app/common,name=makeErr)
parmcards4;
%webout(OPEN)
data _null_;
file _webout;
put ' the discovery channel ';
run;
%webout(CLOSE)
;;;;
%mp_createwebservice(path=/Public/app/common,name=invalidJSON)
``` ```
You should now be able to access the tests in your browser at the deployed path on your server. You should now be able to access the tests in your browser at the deployed path on your server.

View File

@@ -2005,15 +2005,12 @@
}, },
"@sasjs/adapter": { "@sasjs/adapter": {
"version": "file:../build/sasjs-adapter-5.0.0.tgz", "version": "file:../build/sasjs-adapter-5.0.0.tgz",
"integrity": "sha512-nP9O64IslMipxSKAG8PV/X2fRr+0E4/RqwD8jXP2bqZ/QraiKZG0bQPC5hSKqEp7bho8+XpZ4HaXW3Vr9kEZ8Q==", "integrity": "sha512-DxoQbdJqzqOTIuT7qwSfAbmNTWdpOx5zGkiMuZBSwoi9lSsRNoARiWnJq5Vl6h4RXJlc/FVdBFt35RZm4Mc0ZQ==",
"requires": { "requires": {
"@sasjs/utils": "^2.14.0", "@sasjs/utils": "^2.10.2",
"axios": "^0.21.1", "axios": "^0.21.1",
"axios-cookiejar-support": "^1.0.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"https": "^1.0.0", "https": "^1.0.0"
"tough-cookie": "^4.0.0",
"url": "^0.11.0"
}, },
"dependencies": { "dependencies": {
"form-data": { "form-data": {
@@ -2025,21 +2022,6 @@
"combined-stream": "^1.0.8", "combined-stream": "^1.0.8",
"mime-types": "^2.1.12" "mime-types": "^2.1.12"
} }
},
"tough-cookie": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.0.0.tgz",
"integrity": "sha512-tHdtEpQCMrc1YLrMaqXXcj6AxhYi/xgit6mZu1+EDWUn+qhUf8wMQoFIy9NXuq23zAwtcB0t/MjACGR18pcRbg==",
"requires": {
"psl": "^1.1.33",
"punycode": "^2.1.1",
"universalify": "^0.1.2"
}
},
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
} }
} }
}, },
@@ -2064,15 +2046,14 @@
} }
}, },
"@sasjs/utils": { "@sasjs/utils": {
"version": "2.15.5", "version": "2.12.1",
"resolved": "https://registry.npmjs.org/@sasjs/utils/-/utils-2.15.5.tgz", "resolved": "https://registry.npmjs.org/@sasjs/utils/-/utils-2.12.1.tgz",
"integrity": "sha512-5HSWX5fy8D0Zy+Le+LgeRZG4vb5quLqhNiHw3dl0MS2hpsWACSRKia060jZk9LNHayKwBuusjlz5Ba0SyyaiEQ==", "integrity": "sha512-6gZS5zW0J70P7XaVuEczyfHVaVa8Ks/aWr4PIlpJcxWD0enJtCEmos2DdnezdSoNvODkPq/8rzMPqko5jaXK1Q==",
"requires": { "requires": {
"@types/prompts": "^2.0.11", "@types/prompts": "^2.0.11",
"chalk": "^4.1.1", "chalk": "^4.1.1",
"cli-table": "^0.3.6", "cli-table": "^0.3.6",
"consola": "^2.15.0", "consola": "^2.15.0",
"fs-extra": "^10.0.0",
"prompts": "^2.4.1", "prompts": "^2.4.1",
"valid-url": "^1.0.9" "valid-url": "^1.0.9"
}, },
@@ -2107,16 +2088,6 @@
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="
}, },
"fs-extra": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.0.tgz",
"integrity": "sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==",
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^6.0.1",
"universalify": "^2.0.0"
}
},
"has-flag": { "has-flag": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
@@ -2431,9 +2402,9 @@
"integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==" "integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA=="
}, },
"@types/node": { "@types/node": {
"version": "14.14.41", "version": "14.14.25",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.41.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.25.tgz",
"integrity": "sha512-dueRKfaJL4RTtSa7bWeTK1M+VH+Gns73oCgzvYfHZywRCoPSd8EkXBL0mZ9unPTveBn+D9phZBaxuzpwjWkW0g==" "integrity": "sha512-EPpXLOVqDvisVxtlbvzfyqSsFeQxltFbluZNRndIb8tr9KiBnYNLzrc1N3pyKUCww2RNrfHDViqDWWE1LCJQtQ=="
}, },
"@types/normalize-package-data": { "@types/normalize-package-data": {
"version": "2.4.0", "version": "2.4.0",
@@ -2451,9 +2422,9 @@
"integrity": "sha512-6gOkRe7OIioWAXfnO/2lFiv+SJichKVSys1mSsgyrYHSEjk8Ctv4tSR/Odvnu+HWlH2C8j53dahU03XmQdd5fA==" "integrity": "sha512-6gOkRe7OIioWAXfnO/2lFiv+SJichKVSys1mSsgyrYHSEjk8Ctv4tSR/Odvnu+HWlH2C8j53dahU03XmQdd5fA=="
}, },
"@types/prompts": { "@types/prompts": {
"version": "2.0.12", "version": "2.0.11",
"resolved": "https://registry.npmjs.org/@types/prompts/-/prompts-2.0.12.tgz", "resolved": "https://registry.npmjs.org/@types/prompts/-/prompts-2.0.11.tgz",
"integrity": "sha512-Hr6osqfNg3IcQT3pJDXCsSnb0KnldY/hXeJCKJriwbZLnedN9n1e8kcZwLc25GIWULDb6h5aEyOBbf33XpZBXQ==", "integrity": "sha512-dcF5L3rU9VfpLEJIV++FEyhGhuIpJllNEwllVuJ5g8eoVqjf048tW9+spivIwjzgPbtaGAl7mIZW3cmhDAq2UQ==",
"requires": { "requires": {
"@types/node": "*" "@types/node": "*"
} }
@@ -3496,22 +3467,6 @@
"follow-redirects": "^1.10.0" "follow-redirects": "^1.10.0"
} }
}, },
"axios-cookiejar-support": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/axios-cookiejar-support/-/axios-cookiejar-support-1.0.1.tgz",
"integrity": "sha512-IZJxnAJ99XxiLqNeMOqrPbfR7fRyIfaoSLdPUf4AMQEGkH8URs0ghJK/xtqBsD+KsSr3pKl4DEQjCn834pHMig==",
"requires": {
"is-redirect": "^1.0.0",
"pify": "^5.0.0"
},
"dependencies": {
"pify": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/pify/-/pify-5.0.0.tgz",
"integrity": "sha512-eW/gHNMlxdSP6dmG6uJip6FXN0EQBwm2clYYd8Wul42Cwu/DK8HEftzsapcNdYe2MfLiIwZqsDk2RDEsTE79hA=="
}
}
},
"axobject-query": { "axobject-query": {
"version": "2.2.0", "version": "2.2.0",
"resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz",
@@ -8602,11 +8557,6 @@
"resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.0.tgz", "resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.0.tgz",
"integrity": "sha1-DFLlS8yjkbssSUsh6GJtczbG45c=" "integrity": "sha1-DFLlS8yjkbssSUsh6GJtczbG45c="
}, },
"is-redirect": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-redirect/-/is-redirect-1.0.0.tgz",
"integrity": "sha1-HQPd7VO9jbDzDCbk+V02/HyH3CQ="
},
"is-regex": { "is-regex": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.0.tgz", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.0.tgz",

View File

@@ -7,7 +7,7 @@
"@sasjs/adapter": "file:../build/sasjs-adapter-5.0.0.tgz", "@sasjs/adapter": "file:../build/sasjs-adapter-5.0.0.tgz",
"@sasjs/test-framework": "^1.4.0", "@sasjs/test-framework": "^1.4.0",
"@types/jest": "^26.0.20", "@types/jest": "^26.0.20",
"@types/node": "^14.14.41", "@types/node": "^14.14.25",
"@types/react": "^17.0.1", "@types/react": "^17.0.1",
"@types/react-dom": "^17.0.0", "@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.1.7", "@types/react-router-dom": "^5.1.7",

View File

@@ -13,19 +13,14 @@ const App = (): ReactElement<{}> => {
useEffect(() => { useEffect(() => {
if (adapter) { if (adapter) {
const testSuites = [ setTestSuites([
basicTests(adapter, config.userName, config.password), basicTests(adapter, config.userName, config.password),
sendArrTests(adapter), sendArrTests(adapter),
sendObjTests(adapter), sendObjTests(adapter),
specialCaseTests(adapter), specialCaseTests(adapter),
sasjsRequestTests(adapter) sasjsRequestTests(adapter),
] computeTests(adapter)
])
if (adapter.getSasjsConfig().serverType === 'SASVIYA') {
testSuites.push(computeTests(adapter))
}
setTestSuites(testSuites)
} }
}, [adapter, config]) }, [adapter, config])

View File

@@ -145,29 +145,6 @@ export const basicTests = (
sasjsConfig.debug === false sasjsConfig.debug === false
) )
} }
},
{
title: 'Request with extra attributes on JES approach',
description:
'Should complete successful request with extra attributes present in response',
test: async () => {
const config = {
useComputeApi: false
}
return await adapter.request(
'common/sendArr',
stringData,
config,
undefined,
undefined,
['file', 'data']
)
},
assertion: (response: any) => {
const responseKeys: any = Object.keys(response)
return responseKeys.includes('file') && responseKeys.includes('data')
}
} }
] ]
}) })

View File

@@ -176,59 +176,11 @@ export const sendObjTests = (adapter: SASjs): TestSuite => ({
name: 'sendObj', name: 'sendObj',
tests: [ tests: [
{ {
title: 'Table name starts with numeric', title: 'Invalid column name',
description: 'Should throw an error', description: 'Should throw an error',
test: async () => { test: async () => {
const invalidData: any = { const invalidData: any = {
'1InvalidTable': [{ col1: 42 }] '1 invalid table': [{ col1: 42 }]
}
return adapter.request('common/sendObj', invalidData).catch((e) => e)
},
assertion: (error: any) =>
!!error && !!error.error && !!error.error.message
},
{
title: 'Table name contains a space',
description: 'Should throw an error',
test: async () => {
const invalidData: any = {
'an invalidTable': [{ col1: 42 }]
}
return adapter.request('common/sendObj', invalidData).catch((e) => e)
},
assertion: (error: any) =>
!!error && !!error.error && !!error.error.message
},
{
title: 'Table name contains a special character',
description: 'Should throw an error',
test: async () => {
const invalidData: any = {
'anInvalidTable#': [{ col1: 42 }]
}
return adapter.request('common/sendObj', invalidData).catch((e) => e)
},
assertion: (error: any) =>
!!error && !!error.error && !!error.error.message
},
{
title: 'Table name exceeds max length of 32 characters',
description: 'Should throw an error',
test: async () => {
const invalidData: any = {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: [{ col1: 42 }]
}
return adapter.request('common/sendObj', invalidData).catch((e) => e)
},
assertion: (error: any) =>
!!error && !!error.error && !!error.error.message
},
{
title: "Invalid data object's structure",
description: 'Should throw an error',
test: async () => {
const invalidData: any = {
inData: [[{ data: 'value' }]]
} }
return adapter.request('common/sendObj', invalidData).catch((e) => e) return adapter.request('common/sendObj', invalidData).catch((e) => e)
}, },

View File

@@ -1,6 +1,4 @@
import { generateTimestamp } from '@sasjs/utils/time' import axios, { AxiosInstance } from 'axios'
import * as NodeFormData from 'form-data'
import { Sas9RequestClient } from './request/Sas9RequestClient'
import { isUrl } from './utils' import { isUrl } from './utils'
/** /**
@@ -8,11 +6,11 @@ import { isUrl } from './utils'
* *
*/ */
export class SAS9ApiClient { export class SAS9ApiClient {
private requestClient: Sas9RequestClient private httpClient: AxiosInstance
constructor(private serverUrl: string, private jobsPath: string) { constructor(private serverUrl: string) {
if (serverUrl) isUrl(serverUrl) if (serverUrl) isUrl(serverUrl)
this.requestClient = new Sas9RequestClient(serverUrl, false) this.httpClient = axios.create({ baseURL: this.serverUrl })
} }
/** /**
@@ -35,61 +33,27 @@ export class SAS9ApiClient {
/** /**
* Executes code on a SAS9 server. * Executes code on a SAS9 server.
* @param linesOfCode - an array of code lines to execute. * @param linesOfCode - an array of code lines to execute.
* @param userName - the user name to log into the current SAS server. * @param serverName - the server to execute the code on.
* @param password - the password to log into the current SAS server. * @param repositoryName - the repository to execute the code in.
*/ */
public async executeScript( public async executeScript(
linesOfCode: string[], linesOfCode: string[],
userName: string, serverName: string,
password: string repositoryName: string
) { ) {
await this.requestClient.login(userName, password, this.jobsPath) const requestPayload = linesOfCode.join('\n')
// This piece of code forces a webout to prevent Stored Process Errors. const executeScriptResponse = await this.httpClient.put(
const forceOutputCode = [ `/sas/servers/${serverName}/cmd?repositoryName=${repositoryName}`,
'data _null_;', `command=${requestPayload}`,
'file _webout;', {
`put 'Executed sasjs run';`, headers: {
'run;' Accept: 'application/json'
] },
const formData = generateFileUploadForm( responseType: 'text'
[...linesOfCode, ...forceOutputCode].join('\n') }
) )
const codeInjectorPath = `/User Folders/${userName}/My Folder/sasjs/runner` return executeScriptResponse.data
const contentType =
'multipart/form-data; boundary=' + formData.getBoundary()
const contentLength = formData.getLengthSync()
const headers = {
'cache-control': 'no-cache',
Accept: '*/*',
'Content-Type': contentType,
'Content-Length': contentLength,
Connection: 'keep-alive'
}
const storedProcessUrl = `${this.jobsPath}/?${
'_program=' + codeInjectorPath + '&_debug=log'
}`
const response = await this.requestClient.post(
storedProcessUrl,
formData,
undefined,
contentType,
headers
)
return response.result as string
} }
} }
const generateFileUploadForm = (data: any): NodeFormData => {
const formData = new NodeFormData()
const filename = `sasjs-execute-sas9-${generateTimestamp('')}.sas`
formData.append(filename, data, {
filename,
contentType: 'text/plain'
})
return formData
}

View File

@@ -12,7 +12,6 @@ import {
Context, Context,
ContextAllAttributes, ContextAllAttributes,
Folder, Folder,
File,
EditContextInput, EditContextInput,
JobDefinition, JobDefinition,
PollOptions PollOptions
@@ -29,9 +28,8 @@ import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
import { Logger, LogLevel } from '@sasjs/utils/logger' import { Logger, LogLevel } from '@sasjs/utils/logger'
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired' import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
import { RequestClient } from './request/RequestClient' import { RequestClient } from './request/RequestClient'
import { SasAuthResponse, MacroVar } from '@sasjs/utils/types' import { SasAuthResponse } from '@sasjs/utils/types'
import { prefixMessage } from '@sasjs/utils/error' import { prefixMessage } from '@sasjs/utils/error'
import * as mime from 'mime'
/** /**
* A client for interfacing with the SAS Viya REST API. * A client for interfacing with the SAS Viya REST API.
@@ -273,7 +271,6 @@ export class SASViyaApiClient {
* @param waitForResult - when set to true, function will return the session * @param waitForResult - when set to true, function will return the session
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job. * @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
* @param variables - an object that represents macro variables.
*/ */
public async executeScript( public async executeScript(
jobPath: string, jobPath: string,
@@ -285,8 +282,7 @@ export class SASViyaApiClient {
expectWebout = false, expectWebout = false,
waitForResult = true, waitForResult = true,
pollOptions?: PollOptions, pollOptions?: PollOptions,
printPid = false, printPid = false
variables?: MacroVar
): Promise<any> { ): Promise<any> {
try { try {
const headers: any = { const headers: any = {
@@ -360,8 +356,6 @@ export class SASViyaApiClient {
: jobPath : jobPath
} }
if (variables) jobVariables = { ...jobVariables, ...variables }
let files: any[] = [] let files: any[] = []
if (data) { if (data) {
@@ -538,53 +532,6 @@ export class SASViyaApiClient {
.then((res) => res.result) .then((res) => res.result)
} }
/**
* Creates a file. Path to or URI of the parent folder is required.
* @param fileName - the name of the new file.
* @param contentBuffer - the content of the new file in Buffer.
* @param parentFolderPath - the full path to the parent folder. If not
* provided, the parentFolderUri must be provided.
* @param parentFolderUri - the URI (eg /folders/folders/UUID) of the parent
* folder. If not provided, the parentFolderPath must be provided.
* @param accessToken - an access token for authorizing the request.
*/
public async createFile(
fileName: string,
contentBuffer: Buffer,
parentFolderPath?: string,
parentFolderUri?: string,
accessToken?: string
): Promise<File> {
if (!parentFolderPath && !parentFolderUri) {
throw new Error('Path or URI of the parent folder is required.')
}
if (!parentFolderUri && parentFolderPath) {
parentFolderUri = await this.getFolderUri(parentFolderPath, accessToken)
}
const headers = {
Accept: 'application/vnd.sas.file+json',
'Content-Disposition': `filename="${fileName}";`
}
const formData = new NodeFormData()
formData.append('file', contentBuffer, fileName)
const mimeType =
mime.getType(fileName.match(/\.[0-9a-z]+$/i)?.[0] || '') ?? 'text/plain'
return (
await this.requestClient.post<File>(
`/files/files?parentFolderUri=${parentFolderUri}&typeDefName=file#rawUpload`,
formData,
accessToken,
'multipart/form-data; boundary=' + (formData as any)._boundary,
headers
)
).result
}
/** /**
* Creates a folder. Path to or URI of the parent folder is required. * Creates a folder. Path to or URI of the parent folder is required.
* @param folderName - the name of the new folder. * @param folderName - the name of the new folder.
@@ -772,11 +719,13 @@ export class SASViyaApiClient {
let formData let formData
if (typeof FormData === 'undefined') { if (typeof FormData === 'undefined') {
formData = new NodeFormData() formData = new NodeFormData()
formData.append('grant_type', 'authorization_code')
formData.append('code', authCode)
} else { } else {
formData = new FormData() formData = new FormData()
formData.append('grant_type', 'authorization_code')
formData.append('code', authCode)
} }
formData.append('grant_type', 'authorization_code')
formData.append('code', authCode)
const authResponse = await this.requestClient const authResponse = await this.requestClient
.post( .post(
@@ -865,7 +814,6 @@ export class SASViyaApiClient {
* @param expectWebout - a boolean indicating whether to expect a _webout response. * @param expectWebout - a boolean indicating whether to expect a _webout response.
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job. * @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
* @param variables - an object that represents macro variables.
*/ */
public async executeComputeJob( public async executeComputeJob(
sasJob: string, sasJob: string,
@@ -876,8 +824,7 @@ export class SASViyaApiClient {
waitForResult = true, waitForResult = true,
expectWebout = false, expectWebout = false,
pollOptions?: PollOptions, pollOptions?: PollOptions,
printPid = false, printPid = false
variables?: MacroVar
) { ) {
if (isRelativePath(sasJob) && !this.rootFolderName) { if (isRelativePath(sasJob) && !this.rootFolderName) {
throw new Error( throw new Error(
@@ -956,8 +903,7 @@ export class SASViyaApiClient {
expectWebout, expectWebout,
waitForResult, waitForResult,
pollOptions, pollOptions,
printPid, printPid
variables
) )
} }

View File

@@ -4,17 +4,15 @@ import { SASViyaApiClient } from './SASViyaApiClient'
import { SAS9ApiClient } from './SAS9ApiClient' import { SAS9ApiClient } from './SAS9ApiClient'
import { FileUploader } from './FileUploader' import { FileUploader } from './FileUploader'
import { AuthManager } from './auth' import { AuthManager } from './auth'
import { ServerType, MacroVar } from '@sasjs/utils/types' import { ServerType } from '@sasjs/utils/types'
import { RequestClient } from './request/RequestClient' import { RequestClient } from './request/RequestClient'
import { import {
JobExecutor, JobExecutor,
WebJobExecutor, WebJobExecutor,
ComputeJobExecutor, ComputeJobExecutor,
JesJobExecutor, JesJobExecutor
Sas9JobExecutor
} from './job-execution' } from './job-execution'
import { ErrorResponse } from './types/errors' import { ErrorResponse } from './types/errors'
import { ExtraResponseAttributes } from '@sasjs/utils/types'
const defaultConfig: SASjsConfig = { const defaultConfig: SASjsConfig = {
serverUrl: '', serverUrl: '',
@@ -43,7 +41,6 @@ export default class SASjs {
private webJobExecutor: JobExecutor | null = null private webJobExecutor: JobExecutor | null = null
private computeJobExecutor: JobExecutor | null = null private computeJobExecutor: JobExecutor | null = null
private jesJobExecutor: JobExecutor | null = null private jesJobExecutor: JobExecutor | null = null
private sas9JobExecutor: JobExecutor | null = null
constructor(config?: any) { constructor(config?: any) {
this.sasjsConfig = { this.sasjsConfig = {
@@ -60,15 +57,15 @@ export default class SASjs {
public async executeScriptSAS9( public async executeScriptSAS9(
linesOfCode: string[], linesOfCode: string[],
userName: string, serverName: string,
password: string repositoryName: string
) { ) {
this.isMethodSupported('executeScriptSAS9', ServerType.Sas9) this.isMethodSupported('executeScriptSAS9', ServerType.Sas9)
return await this.sas9ApiClient?.executeScript( return await this.sas9ApiClient?.executeScript(
linesOfCode, linesOfCode,
userName, serverName,
password repositoryName
) )
} }
@@ -268,7 +265,7 @@ export default class SASjs {
} }
/** /**
* Creates a folder in the logical SAS folder tree * Creates a folder at SAS file system.
* @param folderName - name of the folder to be created. * @param folderName - name of the folder to be created.
* @param parentFolderPath - the full path (eg `/Public/example/myFolder`) of the parent folder. * @param parentFolderPath - the full path (eg `/Public/example/myFolder`) of the parent folder.
* @param parentFolderUri - the URI of the parent folder. * @param parentFolderUri - the URI of the parent folder.
@@ -300,40 +297,6 @@ export default class SASjs {
) )
} }
/**
* Creates a file in the logical SAS folder tree
* @param fileName - name of the file to be created.
* @param content - content of the file to be created.
* @param parentFolderPath - the full path (eg `/Public/example/myFolder`) of the parent folder.
* @param parentFolderUri - the URI of the parent folder.
* @param accessToken - the access token to authorizing the request.
* @param sasApiClient - a client for interfacing with SAS API.
*/
public async createFile(
fileName: string,
content: Buffer,
parentFolderPath: string,
parentFolderUri?: string,
accessToken?: string,
sasApiClient?: SASViyaApiClient
) {
if (sasApiClient)
return await sasApiClient.createFile(
fileName,
content,
parentFolderPath,
parentFolderUri,
accessToken
)
return await this.sasViyaApiClient!.createFile(
fileName,
content,
parentFolderPath,
parentFolderUri,
accessToken
)
}
/** /**
* Fetches a folder from the SAS file system. * Fetches a folder from the SAS file system.
* @param folderPath - path of the folder to be fetched. * @param folderPath - path of the folder to be fetched.
@@ -575,26 +538,22 @@ export default class SASjs {
* `await request(sasJobPath, data, config, () => setIsLoggedIn(false))` * `await request(sasJobPath, data, config, () => setIsLoggedIn(false))`
* If you are not passing in any data and configuration, it will look like so: * If you are not passing in any data and configuration, it will look like so:
* `await request(sasJobPath, {}, {}, () => setIsLoggedIn(false))` * `await request(sasJobPath, {}, {}, () => setIsLoggedIn(false))`
* @param extraResponseAttributes - a array of predefined values that are used
* to provide extra attributes (same names as those values) to be added in response
* Supported values are declared in ExtraResponseAttributes type.
*/ */
public async request( public async request(
sasJob: string, sasJob: string,
data: { [key: string]: any } | null, data: { [key: string]: any },
config: { [key: string]: any } = {}, config: { [key: string]: any } = {},
loginRequiredCallback?: () => any, loginRequiredCallback?: () => any,
accessToken?: string, accessToken?: string
extraResponseAttributes: ExtraResponseAttributes[] = []
) { ) {
config = { config = {
...this.sasjsConfig, ...this.sasjsConfig,
...config ...config
} }
if (
const validationResult = this.validateInput(data) typeof loginRequiredCallback === 'function' ||
typeof loginRequiredCallback === 'undefined'
if (validationResult.status) { ) {
if (config.serverType === ServerType.SasViya && config.contextName) { if (config.serverType === ServerType.SasViya && config.contextName) {
if (config.useComputeApi) { if (config.useComputeApi) {
return await this.computeJobExecutor!.execute( return await this.computeJobExecutor!.execute(
@@ -610,91 +569,23 @@ export default class SASjs {
data, data,
config, config,
loginRequiredCallback, loginRequiredCallback,
accessToken, accessToken
extraResponseAttributes
) )
} }
} else if (
config.serverType === ServerType.Sas9 &&
config.username &&
config.password
) {
return await this.sas9JobExecutor!.execute(sasJob, data, config)
} else { } else {
return await this.webJobExecutor!.execute( return await this.webJobExecutor!.execute(
sasJob, sasJob,
data, data,
config, config,
loginRequiredCallback, loginRequiredCallback
accessToken,
extraResponseAttributes
) )
} }
} else { } else {
return Promise.reject(new ErrorResponse(validationResult.msg)) return Promise.reject(
} new ErrorResponse(
} `Invalid loginRequiredCallback parameter was provided. Expected Callback function but found ${typeof loginRequiredCallback}`
)
/** )
* This function validates the input data structure and table naming convention
*
* @param data A json object that contains one or more tables, it can also be null
* @returns An object which contains two attributes: 1) status: boolean, 2) msg: string
*/
private validateInput(data: { [key: string]: any } | null): {
status: boolean
msg: string
} {
if (data === null) return { status: true, msg: '' }
for (const key in data) {
if (!key.match(/^[a-zA-Z_]/)) {
return {
status: false,
msg: 'First letter of table should be alphabet or underscore.'
}
}
if (!key.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
return { status: false, msg: 'Table name should be alphanumeric.' }
}
if (key.length > 32) {
return {
status: false,
msg: 'Maximum length for table name could be 32 characters.'
}
}
if (this.getType(data[key]) !== 'Array') {
return {
status: false,
msg: 'Parameter data contains invalid table structure.'
}
}
for (let i = 0; i < data[key].length; i++) {
if (this.getType(data[key][i]) !== 'object') {
return {
status: false,
msg: `Table ${key} contains invalid structure.`
}
}
}
}
return { status: true, msg: '' }
}
/**
* this function returns the type of variable
*
* @param data it could be anything, like string, array, object etc.
* @returns a string which tells the type of input parameter
*/
private getType(data: any): string {
if (Array.isArray(data)) {
return 'Array'
} else {
return typeof data
} }
} }
@@ -735,7 +626,7 @@ export default class SASjs {
) )
sasApiClient.debug = this.sasjsConfig.debug sasApiClient.debug = this.sasjsConfig.debug
} else if (this.sasjsConfig.serverType === ServerType.Sas9) { } else if (this.sasjsConfig.serverType === ServerType.Sas9) {
sasApiClient = new SAS9ApiClient(serverUrl, this.jobsPath) sasApiClient = new SAS9ApiClient(serverUrl)
} }
} else { } else {
let sasClientConfig: any = null let sasClientConfig: any = null
@@ -782,7 +673,6 @@ export default class SASjs {
* @param waitForResult - a boolean that indicates whether the function needs to wait for execution to complete. * @param waitForResult - a boolean that indicates whether the function needs to wait for execution to complete.
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job. * @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
* @param variables - an object that represents macro variables.
*/ */
public async startComputeJob( public async startComputeJob(
sasJob: string, sasJob: string,
@@ -791,8 +681,7 @@ export default class SASjs {
accessToken?: string, accessToken?: string,
waitForResult?: boolean, waitForResult?: boolean,
pollOptions?: PollOptions, pollOptions?: PollOptions,
printPid = false, printPid = false
variables?: MacroVar
) { ) {
config = { config = {
...this.sasjsConfig, ...this.sasjsConfig,
@@ -815,8 +704,7 @@ export default class SASjs {
!!waitForResult, !!waitForResult,
false, false,
pollOptions, pollOptions,
printPid, printPid
variables
) )
} }
@@ -927,11 +815,7 @@ export default class SASjs {
if (this.sasjsConfig.serverType === ServerType.Sas9) { if (this.sasjsConfig.serverType === ServerType.Sas9) {
if (this.sas9ApiClient) if (this.sas9ApiClient)
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl) this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl)
else else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl)
this.sas9ApiClient = new SAS9ApiClient(
this.sasjsConfig.serverUrl,
this.jobsPath
)
} }
this.fileUploader = new FileUploader( this.fileUploader = new FileUploader(
@@ -949,12 +833,6 @@ export default class SASjs {
this.sasViyaApiClient! this.sasViyaApiClient!
) )
this.sas9JobExecutor = new Sas9JobExecutor(
this.sasjsConfig.serverUrl,
this.sasjsConfig.serverType!,
this.jobsPath
)
this.computeJobExecutor = new ComputeJobExecutor( this.computeJobExecutor = new ComputeJobExecutor(
this.sasjsConfig.serverUrl, this.sasjsConfig.serverUrl,
this.sasViyaApiClient! this.sasViyaApiClient!
@@ -985,16 +863,6 @@ export default class SASjs {
isForced isForced
) )
break break
case 'file':
await this.createFile(
member.name,
member.code,
parentFolder,
undefined,
accessToken,
sasApiClient
)
break
case 'service': case 'service':
await this.createJobDefinition( await this.createJobDefinition(
member.name, member.name,

View File

@@ -35,7 +35,6 @@ export class AuthManager {
this.userName = loginParams.username this.userName = loginParams.username
const { isLoggedIn, loginForm } = await this.checkSession() const { isLoggedIn, loginForm } = await this.checkSession()
if (isLoggedIn) { if (isLoggedIn) {
await this.loginCallback() await this.loginCallback()
@@ -45,35 +44,6 @@ export class AuthManager {
} }
} }
let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
let loggedIn = isLogInSuccess(loginResponse)
if (!loggedIn) {
if (isCredentialsVerifyError(loginResponse)) {
const newLoginForm = await this.getLoginForm(loginResponse)
loginResponse = await this.sendLoginRequest(newLoginForm, loginParams)
}
const currentSession = await this.checkSession()
loggedIn = currentSession.isLoggedIn
}
if (loggedIn) {
this.loginCallback()
}
return {
isLoggedIn: !!loggedIn,
userName: this.userName
}
}
private async sendLoginRequest(
loginForm: { [key: string]: any },
loginParams: { [key: string]: any }
) {
for (const key in loginForm) { for (const key in loginForm) {
loginParams[key] = loginForm[key] loginParams[key] = loginForm[key]
} }
@@ -90,7 +60,21 @@ export class AuthManager {
} }
) )
return loginResponse let loggedIn = isLogInSuccess(loginResponse)
if (!loggedIn) {
const currentSession = await this.checkSession()
loggedIn = currentSession.isLoggedIn
}
if (loggedIn) {
this.loginCallback()
}
return {
isLoggedIn: !!loggedIn,
userName: this.userName
}
} }
/** /**
@@ -184,10 +168,5 @@ export class AuthManager {
} }
} }
const isCredentialsVerifyError = (response: string): boolean =>
/An error occurred while the system was verifying your credentials. Please enter your credentials again./gm.test(
response
)
const isLogInSuccess = (response: string): boolean => const isLogInSuccess = (response: string): boolean =>
/You have signed in/gm.test(response) /You have signed in/gm.test(response)

View File

@@ -57,7 +57,7 @@ describe('AuthManager', () => {
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?') expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
}) })
it('should call the auth callback and return when already logged in', async () => { it('should call the auth callback and return when already logged in', async (done) => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -77,9 +77,10 @@ describe('AuthManager', () => {
expect(loginResponse.isLoggedIn).toBeTruthy() expect(loginResponse.isLoggedIn).toBeTruthy()
expect(loginResponse.userName).toEqual(userName) expect(loginResponse.userName).toEqual(userName)
expect(authCallback).toHaveBeenCalledTimes(1) expect(authCallback).toHaveBeenCalledTimes(1)
done()
}) })
it('should post a login request to the server if not logged in', async () => { it('should post a login request to the server if not logged in', async (done) => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -120,9 +121,10 @@ describe('AuthManager', () => {
} }
) )
expect(authCallback).toHaveBeenCalledTimes(1) expect(authCallback).toHaveBeenCalledTimes(1)
done()
}) })
it('should parse and submit the authorisation form when necessary', async () => { it('should parse and submit the authorisation form when necessary', async (done) => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -158,9 +160,10 @@ describe('AuthManager', () => {
expect(requestClient.authorize).toHaveBeenCalledWith( expect(requestClient.authorize).toHaveBeenCalledWith(
mockLoginAuthoriseRequiredResponse mockLoginAuthoriseRequiredResponse
) )
done()
}) })
it('should check and return session information if logged in', async () => { it('should check and return session information if logged in', async (done) => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -186,5 +189,7 @@ describe('AuthManager', () => {
} }
} }
) )
done()
}) })
}) })

View File

@@ -5,7 +5,6 @@ import {
JobExecutionError, JobExecutionError,
LoginRequiredError LoginRequiredError
} from '../types/errors' } from '../types/errors'
import { ExtraResponseAttributes } from '@sasjs/utils/types'
import { BaseJobExecutor } from './JobExecutor' import { BaseJobExecutor } from './JobExecutor'
export class JesJobExecutor extends BaseJobExecutor { export class JesJobExecutor extends BaseJobExecutor {
@@ -18,8 +17,7 @@ export class JesJobExecutor extends BaseJobExecutor {
data: any, data: any,
config: any, config: any,
loginRequiredCallback?: any, loginRequiredCallback?: any,
accessToken?: string, accessToken?: string
extraResponseAttributes: ExtraResponseAttributes[] = []
) { ) {
const loginCallback = loginRequiredCallback || (() => Promise.resolve()) const loginCallback = loginRequiredCallback || (() => Promise.resolve())
@@ -32,26 +30,10 @@ export class JesJobExecutor extends BaseJobExecutor {
data, data,
accessToken accessToken
) )
.then((response: any) => { .then((response) => {
this.appendRequest(response, sasJob, config.debug) this.appendRequest(response, sasJob, config.debug)
let responseObject = {} resolve(response)
if (extraResponseAttributes && extraResponseAttributes.length > 0) {
const extraAttributes = extraResponseAttributes.reduce(
(map: any, obj: any) => ((map[obj] = response[obj]), map),
{}
)
responseObject = {
result: response.result,
...extraAttributes
}
} else {
responseObject = response.result
}
resolve(responseObject)
}) })
.catch(async (e: Error) => { .catch(async (e: Error) => {
if (e instanceof JobExecutionError) { if (e instanceof JobExecutionError) {
@@ -68,9 +50,7 @@ export class JesJobExecutor extends BaseJobExecutor {
sasJob, sasJob,
data, data,
config, config,
loginRequiredCallback, loginRequiredCallback
accessToken,
extraResponseAttributes
).then( ).then(
(res: any) => { (res: any) => {
resolve(res) resolve(res)

View File

@@ -1,6 +1,5 @@
import { ServerType } from '@sasjs/utils/types' import { ServerType } from '@sasjs/utils/types'
import { SASjsRequest } from '../types' import { SASjsRequest } from '../types'
import { ExtraResponseAttributes } from '@sasjs/utils/types'
import { asyncForEach, parseGeneratedCode, parseSourceCode } from '../utils' import { asyncForEach, parseGeneratedCode, parseSourceCode } from '../utils'
export type ExecuteFunction = () => Promise<any> export type ExecuteFunction = () => Promise<any>
@@ -11,8 +10,7 @@ export interface JobExecutor {
data: any, data: any,
config: any, config: any,
loginRequiredCallback?: any, loginRequiredCallback?: any,
accessToken?: string, accessToken?: string
extraResponseAttributes?: ExtraResponseAttributes[]
) => Promise<any> ) => Promise<any>
resendWaitingRequests: () => Promise<void> resendWaitingRequests: () => Promise<void>
getRequests: () => SASjsRequest[] getRequests: () => SASjsRequest[]
@@ -30,8 +28,7 @@ export abstract class BaseJobExecutor implements JobExecutor {
data: any, data: any,
config: any, config: any,
loginRequiredCallback?: any, loginRequiredCallback?: any,
accessToken?: string | undefined, accessToken?: string | undefined
extraResponseAttributes?: ExtraResponseAttributes[]
): Promise<any> ): Promise<any>
resendWaitingRequests = async () => { resendWaitingRequests = async () => {
@@ -62,14 +59,14 @@ export abstract class BaseJobExecutor implements JobExecutor {
let sasWork = null let sasWork = null
if (debug) { if (debug) {
if (response?.log) { if (response?.result && response?.log) {
sourceCode = parseSourceCode(response.log) sourceCode = parseSourceCode(response.log)
generatedCode = parseGeneratedCode(response.log) generatedCode = parseGeneratedCode(response.log)
if (response?.result) { if (response.log) {
sasWork = response.result.WORK
} else {
sasWork = response.log sasWork = response.log
} else {
sasWork = response.result.WORK
} }
} else if (response?.result) { } else if (response?.result) {
sourceCode = parseSourceCode(response.result) sourceCode = parseSourceCode(response.result)

View File

@@ -1,110 +0,0 @@
import { ServerType } from '@sasjs/utils/types'
import * as NodeFormData from 'form-data'
import { ErrorResponse } from '../types/errors'
import { convertToCSV, isRelativePath } from '../utils'
import { BaseJobExecutor } from './JobExecutor'
import { Sas9RequestClient } from '../request/Sas9RequestClient'
/**
* Job executor for SAS9 servers for use in Node.js environments.
* Initiates login with the provided username and password from the config
* The cookies are stored in the request client and used in subsequent
* job execution requests.
*/
export class Sas9JobExecutor extends BaseJobExecutor {
private requestClient: Sas9RequestClient
constructor(
serverUrl: string,
serverType: ServerType,
private jobsPath: string
) {
super(serverUrl, serverType)
this.requestClient = new Sas9RequestClient(serverUrl, false)
}
async execute(sasJob: string, data: any, config: any) {
const program = isRelativePath(sasJob)
? config.appLoc
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
: sasJob
: sasJob
let apiUrl = `${config.serverUrl}${this.jobsPath}?${'_program=' + program}`
apiUrl = `${apiUrl}${
config.username && config.password
? '&_username=' + config.username + '&_password=' + config.password
: ''
}`
let requestParams = {
...this.getRequestParams(config)
}
let formData = new NodeFormData()
if (data) {
try {
formData = generateFileUploadForm(formData, data)
} catch (e) {
return Promise.reject(new ErrorResponse(e?.message, e))
}
}
for (const key in requestParams) {
if (requestParams.hasOwnProperty(key)) {
formData.append(key, requestParams[key])
}
}
await this.requestClient.login(
config.username,
config.password,
this.jobsPath
)
const contentType =
data && Object.keys(data).length
? 'multipart/form-data; boundary=' + (formData as any)._boundary
: 'text/plain'
return await this.requestClient!.post(
apiUrl,
formData,
undefined,
contentType,
{
Accept: '*/*',
Connection: 'Keep-Alive'
}
)
}
private getRequestParams(config: any): any {
const requestParams: any = {}
if (config.debug) {
requestParams['_debug'] = 131
}
return requestParams
}
}
const generateFileUploadForm = (
formData: NodeFormData,
data: any
): NodeFormData => {
for (const tableName in data) {
const name = tableName
const csv = convertToCSV(data[tableName])
if (csv === 'ERROR: LARGE STRING LENGTH') {
throw new Error(
'The max length of a string value in SASjs is 32765 characters.'
)
}
formData.append(name, csv, {
filename: `${name}.csv`,
contentType: 'application/csv'
})
}
return formData
}

View File

@@ -1,5 +1,4 @@
export * from './ComputeJobExecutor' export * from './ComputeJobExecutor'
export * from './JesJobExecutor' export * from './JesJobExecutor'
export * from './JobExecutor' export * from './JobExecutor'
export * from './Sas9JobExecutor'
export * from './WebJobExecutor' export * from './WebJobExecutor'

View File

@@ -10,7 +10,6 @@ import {
} from '../types/errors' } from '../types/errors'
import { parseWeboutResponse } from '../utils/parseWeboutResponse' import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { prefixMessage } from '@sasjs/utils/error' import { prefixMessage } from '@sasjs/utils/error'
import { SAS9AuthError } from '../types/errors/SAS9AuthError'
export interface HttpClient { export interface HttpClient {
get<T>( get<T>(
@@ -45,11 +44,11 @@ export interface HttpClient {
} }
export class RequestClient implements HttpClient { export class RequestClient implements HttpClient {
protected csrfToken: CsrfToken = { headerName: '', value: '' } private csrfToken: CsrfToken = { headerName: '', value: '' }
protected fileUploadCsrfToken: CsrfToken | undefined private fileUploadCsrfToken: CsrfToken | undefined
protected httpClient: AxiosInstance private httpClient: AxiosInstance
constructor(protected baseUrl: string, allowInsecure = false) { constructor(private baseUrl: string, allowInsecure = false) {
const https = require('https') const https = require('https')
if (allowInsecure && https.Agent) { if (allowInsecure && https.Agent) {
this.httpClient = axios.create({ this.httpClient = axios.create({
@@ -291,7 +290,7 @@ export class RequestClient implements HttpClient {
}) })
} }
protected getHeaders = ( private getHeaders = (
accessToken: string | undefined, accessToken: string | undefined,
contentType: string contentType: string
) => { ) => {
@@ -316,7 +315,7 @@ export class RequestClient implements HttpClient {
return headers return headers
} }
protected parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => { private parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => {
const token = this.parseCsrfToken(response) const token = this.parseCsrfToken(response)
if (token) { if (token) {
@@ -324,7 +323,7 @@ export class RequestClient implements HttpClient {
} }
} }
protected parseAndSetCsrfToken = (response: AxiosResponse) => { private parseAndSetCsrfToken = (response: AxiosResponse) => {
const token = this.parseCsrfToken(response) const token = this.parseCsrfToken(response)
if (token) { if (token) {
@@ -348,7 +347,7 @@ export class RequestClient implements HttpClient {
} }
} }
protected handleError = async ( private handleError = async (
e: any, e: any,
callback: any, callback: any,
debug: boolean = false debug: boolean = false
@@ -406,7 +405,7 @@ export class RequestClient implements HttpClient {
throw e throw e
} }
protected parseResponse<T>(response: AxiosResponse<any>) { private parseResponse<T>(response: AxiosResponse<any>) {
const etag = response?.headers ? response.headers['etag'] : '' const etag = response?.headers ? response.headers['etag'] : ''
let parsedResponse let parsedResponse
let includeSAS9Log: boolean = false let includeSAS9Log: boolean = false
@@ -440,7 +439,7 @@ export class RequestClient implements HttpClient {
} }
} }
export const throwIfError = (response: AxiosResponse) => { const throwIfError = (response: AxiosResponse) => {
if (response.status === 401) { if (response.status === 401) {
throw new LoginRequiredError() throw new LoginRequiredError()
} }
@@ -471,10 +470,6 @@ export const throwIfError = (response: AxiosResponse) => {
throw new AuthorizeError(response.data.message, authorizeRequestUrl) throw new AuthorizeError(response.data.message, authorizeRequestUrl)
} }
if (response.config?.url?.includes('sasAuthError')) {
throw new SAS9AuthError()
}
const error = parseError(response.data as string) const error = parseError(response.data as string)
if (error) { if (error) {

View File

@@ -1,121 +0,0 @@
import { AxiosRequestConfig } from 'axios'
import axiosCookieJarSupport from 'axios-cookiejar-support'
import * as tough from 'tough-cookie'
import { prefixMessage } from '@sasjs/utils/error'
import { RequestClient, throwIfError } from './RequestClient'
/**
* Specific request client for SAS9 in Node.js environments.
* Handles redirects and cookie management.
*/
export class Sas9RequestClient extends RequestClient {
constructor(baseUrl: string, allowInsecure = false) {
super(baseUrl, allowInsecure)
this.httpClient.defaults.maxRedirects = 0
this.httpClient.defaults.validateStatus = (status) =>
status >= 200 && status < 303
if (axiosCookieJarSupport) {
axiosCookieJarSupport(this.httpClient)
this.httpClient.defaults.jar = new tough.CookieJar()
}
}
public async login(username: string, password: string, jobsPath: string) {
const codeInjectorPath = `/User Folders/${username}/My Folder/sasjs/runner`
if (this.httpClient.defaults.jar) {
;(this.httpClient.defaults.jar as tough.CookieJar).removeAllCookies()
await this.get(
`${jobsPath}?_program=${codeInjectorPath}&_username=${username}&_password=${password}`,
undefined,
'text/plain'
)
}
}
public async get<T>(
url: string,
accessToken: string | undefined,
contentType: string = 'application/json',
overrideHeaders: { [key: string]: string | number } = {},
debug: boolean = false
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
...overrideHeaders
}
const requestConfig: AxiosRequestConfig = {
headers,
responseType: contentType === 'text/plain' ? 'text' : 'json',
withCredentials: true
}
if (contentType === 'text/plain') {
requestConfig.transformResponse = undefined
}
return this.httpClient
.get<T>(url, requestConfig)
.then((response) => {
if (response.status === 302) {
return this.get(
response.headers['location'],
accessToken,
contentType
)
}
throwIfError(response)
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(
e,
() =>
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
(err) => {
throw prefixMessage(
err,
'Error while executing handle error callback. '
)
}
),
debug
).catch((err) => {
throw prefixMessage(err, 'Error while handling error. ')
})
})
}
public post<T>(
url: string,
data: any,
accessToken: string | undefined,
contentType = 'application/json',
overrideHeaders: { [key: string]: string | number } = {}
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
...overrideHeaders
}
return this.httpClient
.post<T>(url, data, { headers, withCredentials: true })
.then(async (response) => {
if (response.status === 302) {
return await this.get(
response.headers['location'],
undefined,
contentType,
overrideHeaders
)
}
throwIfError(response)
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(e, () =>
this.post<T>(url, data, accessToken, contentType, overrideHeaders)
)
})
}
}

View File

@@ -1,7 +1,3 @@
/**
* @jest-environment jsdom
*/
import { FileUploader } from '../FileUploader' import { FileUploader } from '../FileUploader'
import { UploadFile } from '../types' import { UploadFile } from '../types'
import { RequestClient } from '../request/RequestClient' import { RequestClient } from '../request/RequestClient'
@@ -39,40 +35,41 @@ describe('FileUploader', () => {
new RequestClient('https://sample.server.com') new RequestClient('https://sample.server.com')
) )
it('should upload successfully', async () => { it('should upload successfully', async (done) => {
const sasJob = 'test/upload' const sasJob = 'test/upload'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.resolve({ data: sampleResponse }) Promise.resolve({ data: sampleResponse })
) )
const res = await fileUploader.uploadFile(sasJob, files, params) fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
expect(res).toEqual(JSON.parse(sampleResponse))
expect(res).toEqual(JSON.parse(sampleResponse)) done()
})
}) })
it('should an error when no files are provided', async () => { it('should an error when no files are provided', async (done) => {
const sasJob = 'test/upload' const sasJob = 'test/upload'
const files: UploadFile[] = [] const files: UploadFile[] = []
const params = { table: 'libtable' } const params = { table: 'libtable' }
const err = await fileUploader fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
.uploadFile(sasJob, files, params) expect(err.error.message).toEqual('At least one file must be provided.')
.catch((err: any) => err) done()
expect(err.error.message).toEqual('At least one file must be provided.') })
}) })
it('should throw an error when no sasJob is provided', async () => { it('should throw an error when no sasJob is provided', async (done) => {
const sasJob = '' const sasJob = ''
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
const err = await fileUploader fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
.uploadFile(sasJob, files, params) expect(err.error.message).toEqual('sasJob must be provided.')
.catch((err: any) => err) done()
expect(err.error.message).toEqual('sasJob must be provided.') })
}) })
it('should throw an error when login is required', async () => { it('should throw an error when login is required', async (done) => {
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.resolve({ data: '<form action="Logon">' }) Promise.resolve({ data: '<form action="Logon">' })
) )
@@ -80,13 +77,15 @@ describe('FileUploader', () => {
const sasJob = 'test' const sasJob = 'test'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
const err = await fileUploader fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
.uploadFile(sasJob, files, params) expect(err.error.message).toEqual(
.catch((err: any) => err) 'You must be logged in to upload a file.'
expect(err.error.message).toEqual('You must be logged in to upload a file.') )
done()
})
}) })
it('should throw an error when invalid JSON is returned by the server', async () => { it('should throw an error when invalid JSON is returned by the server', async (done) => {
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.resolve({ data: '{invalid: "json"' }) Promise.resolve({ data: '{invalid: "json"' })
) )
@@ -94,13 +93,13 @@ describe('FileUploader', () => {
const sasJob = 'test' const sasJob = 'test'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
const err = await fileUploader fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
.uploadFile(sasJob, files, params) expect(err.error.message).toEqual('File upload request failed.')
.catch((err: any) => err) done()
expect(err.error.message).toEqual('File upload request failed.') })
}) })
it('should throw an error when the server request fails', async () => { it('should throw an error when the server request fails', async (done) => {
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.reject({ data: '{message: "Server error"}' }) Promise.reject({ data: '{message: "Server error"}' })
) )
@@ -108,9 +107,10 @@ describe('FileUploader', () => {
const sasJob = 'test' const sasJob = 'test'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
const err = await fileUploader fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
.uploadFile(sasJob, files, params) expect(err.error.message).toEqual('File upload request failed.')
.catch((err: any) => err)
expect(err.error.message).toEqual('File upload request failed.') done()
})
}) })
}) })

View File

@@ -14,7 +14,7 @@ describe('FolderOperations', () => {
beforeEach(() => {}) beforeEach(() => {})
it('should move and rename folder', async () => { it('should move and rename folder', async (done) => {
mockFetchResponse(false) mockFetchResponse(false)
let res: any = await sasViyaApiClient.moveFolder( let res: any = await sasViyaApiClient.moveFolder(
@@ -26,9 +26,11 @@ describe('FolderOperations', () => {
expect(res.folder.name).toEqual('newName') expect(res.folder.name).toEqual('newName')
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder') expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
done()
}) })
it('should move and keep the name of folder', async () => { it('should move and keep the name of folder', async (done) => {
mockFetchResponse(true) mockFetchResponse(true)
let res: any = await sasViyaApiClient.moveFolder( let res: any = await sasViyaApiClient.moveFolder(
@@ -40,9 +42,11 @@ describe('FolderOperations', () => {
expect(res.folder.name).toEqual('oldName') expect(res.folder.name).toEqual('oldName')
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder') expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
done()
}) })
it('should only rename folder', async () => { it('should only rename folder', async (done) => {
mockFetchResponse(false) mockFetchResponse(false)
let res: any = await sasViyaApiClient.moveFolder( let res: any = await sasViyaApiClient.moveFolder(
@@ -54,6 +58,8 @@ describe('FolderOperations', () => {
expect(res.folder.name).toEqual('newName') expect(res.folder.name).toEqual('newName')
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder') expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
done()
}) })
}) })

View File

@@ -1,6 +1,6 @@
import { parseGeneratedCode } from '../../utils/index' import { parseGeneratedCode } from '../../utils/index'
it('should parse generated code', () => { it('should parse generated code', async (done) => {
expect(sampleResponse).toBeTruthy() expect(sampleResponse).toBeTruthy()
const parsedGeneratedCode = parseGeneratedCode(sampleResponse) const parsedGeneratedCode = parseGeneratedCode(sampleResponse)
@@ -15,6 +15,8 @@ it('should parse generated code', () => {
expect(generatedCodeLines[2].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy() expect(generatedCodeLines[2].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
expect(generatedCodeLines[3].startsWith('MPRINT(MM_WEBRIGHT)')).toBeTruthy() expect(generatedCodeLines[3].startsWith('MPRINT(MM_WEBRIGHT)')).toBeTruthy()
expect(generatedCodeLines[4].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy() expect(generatedCodeLines[4].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
done()
}) })
/* tslint:disable */ /* tslint:disable */

View File

@@ -1,6 +1,6 @@
import { parseSourceCode } from '../../utils/index' import { parseSourceCode } from '../../utils/index'
it('should parse SAS9 source code', async () => { it('should parse SAS9 source code', async (done) => {
expect(sampleResponse).toBeTruthy() expect(sampleResponse).toBeTruthy()
const parsedSourceCode = parseSourceCode(sampleResponse) const parsedSourceCode = parseSourceCode(sampleResponse)
@@ -15,6 +15,8 @@ it('should parse SAS9 source code', async () => {
expect(sourceCodeLines[2].startsWith('8')).toBeTruthy() expect(sourceCodeLines[2].startsWith('8')).toBeTruthy()
expect(sourceCodeLines[3].startsWith('9')).toBeTruthy() expect(sourceCodeLines[3].startsWith('9')).toBeTruthy()
expect(sourceCodeLines[4].startsWith('10')).toBeTruthy() expect(sourceCodeLines[4].startsWith('10')).toBeTruthy()
done()
}) })
/* tslint:disable */ /* tslint:disable */

View File

@@ -1,8 +0,0 @@
import { Link } from './Link'
export interface File {
id: string
name: string
parentUri: string
links: Link[]
}

View File

@@ -1,9 +0,0 @@
export class SAS9AuthError extends Error {
constructor() {
super(
'The credentials you provided cannot be authenticated. Please provide a valid set of credentials.'
)
this.name = 'AuthorizeError'
Object.setPrototypeOf(this, SAS9AuthError.prototype)
}
}

View File

@@ -1,7 +1,6 @@
export * from './Context' export * from './Context'
export * from './CsrfToken' export * from './CsrfToken'
export * from './Folder' export * from './Folder'
export * from './File'
export * from './Job' export * from './Job'
export * from './JobDefinition' export * from './JobDefinition'
export * from './JobResult' export * from './JobResult'

View File

@@ -2,30 +2,20 @@ const path = require('path')
const webpack = require('webpack') const webpack = require('webpack')
const terserPlugin = require('terser-webpack-plugin') const terserPlugin = require('terser-webpack-plugin')
const defaultPlugins = [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/),
new webpack.SourceMapDevToolPlugin({
filename: null,
exclude: [/node_modules/],
test: /\.ts($|\?)/i
})
]
const optimization = {
minimize: true,
minimizer: [
new terserPlugin({
parallel: true,
terserOptions: {}
})
]
}
const browserConfig = { const browserConfig = {
entry: './src/index.ts', entry: './src/index.ts',
devtool: 'inline-source-map', devtool: 'inline-source-map',
mode: 'production', mode: 'production',
optimization: optimization, optimization: {
minimizer: [
new terserPlugin({
cache: true,
parallel: true,
sourceMap: true,
terserOptions: {}
})
]
},
module: { module: {
rules: [ rules: [
{ {
@@ -46,26 +36,17 @@ const browserConfig = {
library: 'SASjs' library: 'SASjs'
}, },
plugins: [ plugins: [
...defaultPlugins, new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/),
new webpack.ProvidePlugin({ new webpack.SourceMapDevToolPlugin({
process: 'process/browser' filename: null,
exclude: [/node_modules/],
test: /\.ts($|\?)/i
}) })
] ]
} }
const browserConfigWithoutProcessPlugin = {
entry: browserConfig.entry,
devtool: browserConfig.devtool,
mode: browserConfig.mode,
optimization: optimization,
module: browserConfig.module,
resolve: browserConfig.resolve,
output: browserConfig.output,
plugins: defaultPlugins
}
const nodeConfig = { const nodeConfig = {
...browserConfigWithoutProcessPlugin, ...browserConfig,
target: 'node', target: 'node',
entry: './node/index.ts', entry: './node/index.ts',
output: { output: {