mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 01:14:36 +00:00
Compare commits
144 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a0d62d8f3 | ||
|
|
8f4d1c7aea | ||
|
|
5a2ee88cbc | ||
|
|
b23f199334 | ||
|
|
ed5dabee9f | ||
|
|
0c88c5a522 | ||
|
|
640e7015c8 | ||
|
|
2fd306f435 | ||
|
|
e3f779dbd1 | ||
|
|
1064f11663 | ||
|
|
46abc54cb0 | ||
|
|
2c808a937a | ||
|
|
52cf9a420f | ||
|
|
2d29be45f5 | ||
|
|
a44222c3ba | ||
|
|
efc82101c1 | ||
|
|
09ce2fb6be | ||
|
|
a383388e54 | ||
|
|
362078b12c | ||
|
|
9d0c3410a5 | ||
|
|
dfb9c28f3a | ||
|
|
8d155283dd | ||
|
|
d991ead86a | ||
|
|
33a202fa1c | ||
|
|
ff5463a84c | ||
|
|
aa7c3ae4a9 | ||
|
|
2e66bfde4b | ||
|
|
16e21adb20 | ||
|
|
01c5682c3d | ||
|
|
cfc8ff2837 | ||
|
|
edf25b471a | ||
|
|
bb894e6107 | ||
|
|
6b3a0cdb13 | ||
|
|
8c98a26160 | ||
|
|
bcd9310f26 | ||
|
|
57e9b67207 | ||
|
|
7bf53858f0 | ||
|
|
02780d0bcd | ||
|
|
6356aed06b | ||
|
|
69fd7b2cb5 | ||
|
|
5d1eed1494 | ||
|
|
e2e2824f37 | ||
|
|
d461135980 | ||
|
|
65fbae7610 | ||
|
|
761428502a | ||
|
|
6eb2ceaf53 | ||
|
|
66813b9824 | ||
|
|
140d8e4eac | ||
|
|
0d730e0576 | ||
|
|
ca18fcecf0 | ||
|
|
009069169f | ||
|
|
6d166efd11 | ||
|
|
1b117a67aa | ||
|
|
9037160362 | ||
|
|
505d85c256 | ||
|
|
71a3fe04a0 | ||
|
|
79bb27524c | ||
|
|
9651b7adb4 | ||
|
|
59e5bec731 | ||
|
|
182e66216f | ||
|
|
2408fd091e | ||
|
|
0e38a24664 | ||
|
|
aa643d1782 | ||
|
|
cdc91e9cda | ||
|
|
3f0590e0fe | ||
|
|
5efb294ff2 | ||
|
|
011e2d83dc | ||
|
|
e36b511530 | ||
|
|
b6a2a85d1d | ||
|
|
f1cceeb5e6 | ||
|
|
6fee2548fd | ||
|
|
91005066cf | ||
|
|
e1f17ef47d | ||
|
|
8a40071c35 | ||
|
|
430957eb3d | ||
|
|
25874be679 | ||
|
|
ed8440434f | ||
|
|
0f9884c1b6 | ||
|
|
d126a05347 | ||
|
|
3e26bbbbba | ||
|
|
982cc8f7a0 | ||
|
|
d1770698e0 | ||
|
|
b78e8617c4 | ||
|
|
3ce9ca0986 | ||
|
|
04d17c3680 | ||
|
|
d26e15f91c | ||
|
|
83c46091b3 | ||
|
|
d640d7c040 | ||
|
|
c934eb2b08 | ||
|
|
24dd5e32ad | ||
|
|
a23103b2c3 | ||
|
|
35aa4235e4 | ||
|
|
e9be1cf99a | ||
|
|
c7b0821081 | ||
|
|
4a4618dd32 | ||
|
|
d223e83c60 | ||
|
|
d1f1a20126 | ||
|
|
4b89e3762f | ||
|
|
bc110288de | ||
|
|
e94e16b52c | ||
|
|
76aacee016 | ||
|
|
1a3bd5d1f5 | ||
|
|
3f6e89d716 | ||
|
|
361ec84638 | ||
|
|
35cc1e4f62 | ||
|
|
64a976e888 | ||
|
|
7e2cb8491f | ||
|
|
2cdab7522d | ||
|
|
a07eabc408 | ||
|
|
7279c23fe2 | ||
|
|
80707d77d9 | ||
|
|
d5920c5885 | ||
|
|
6a3a6b4485 | ||
|
|
2b1df0c61a | ||
|
|
216725f306 | ||
|
|
3183f89a62 | ||
|
|
f5cc16c3bd | ||
|
|
e78dc76e56 | ||
|
|
bfdb5ef0a6 | ||
|
|
35353d3fce | ||
|
|
7a02c8ad34 | ||
|
|
331d9b0010 | ||
|
|
ef5686cce7 | ||
|
|
fa87111f4a | ||
|
|
94967b0f6c | ||
|
|
3f796b300d | ||
|
|
a07c16fb52 | ||
|
|
fd6905ea9f | ||
|
|
08f58b5f4f | ||
|
|
bd8012fe3e | ||
|
|
fa531b34fd | ||
|
|
354443c98b | ||
|
|
ee30ab195f | ||
|
|
02c1712d22 | ||
|
|
37def7a956 | ||
|
|
653e3d05e0 | ||
|
|
d8467c24b1 | ||
|
|
fc9056c1ac | ||
|
|
9b1d295b82 | ||
|
|
e2ea3f4ddc | ||
|
|
99d0b01a24 | ||
|
|
131c672020 | ||
|
|
338f2fb2dd | ||
|
|
4552a9a856 |
2
.env.example
Normal file
2
.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
SERVER_URL=https://server.com
|
||||
DEFAULT_COMPUTE_CONTEXT=SAS Job Execution compute context
|
||||
9
.github/reviewer-lottery.yml
vendored
Normal file
9
.github/reviewer-lottery.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
groups:
|
||||
- name: SASjs Devs # name of the group
|
||||
reviewers: 1 # how many reviewers do you want to assign?
|
||||
usernames: # github usernames of the reviewers
|
||||
- krishna-acondy
|
||||
- YuryShkoda
|
||||
- saadjutt01
|
||||
- medjedovicm
|
||||
- allanbowe
|
||||
13
.github/workflows/assign-reviewer.yml
vendored
Normal file
13
.github/workflows/assign-reviewer.yml
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
name: 'Assign Reviewer'
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: uesteibar/reviewer-lottery@v1
|
||||
with:
|
||||
repo-token: ${{ secrets.GH_TOKEN }}
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,6 @@
|
||||
node_modules
|
||||
build
|
||||
|
||||
.env
|
||||
|
||||
/coverage
|
||||
@@ -14,4 +14,5 @@ What code changes have been made to achieve the intent.
|
||||
|
||||
- [ ] Code is formatted correctly (`npm run lint:fix`).
|
||||
- [ ] All unit tests are passing (`npm test`).
|
||||
- [ ] All `sasjs-cli` unit tests are passing (`npm test`).
|
||||
- [ ] All `sasjs-tests` are passing (instructions available [here](https://github.com/sasjs/adapter/blob/master/sasjs-tests/README.md)).
|
||||
|
||||
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-762.reflection-214.fileuploader.html
Normal file
231
docs/classes/reflection-762.reflection-214.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-762.reflection-214.sas9apiclient.html
Normal file
312
docs/classes/reflection-762.reflection-214.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1430
docs/classes/reflection-762.reflection-214.sasjs.html
Normal file
1430
docs/classes/reflection-762.reflection-214.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1395
docs/classes/reflection-762.reflection-214.sasviyaapiclient.html
Normal file
1395
docs/classes/reflection-762.reflection-214.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
271
docs/classes/reflection-762.reflection-214.sessionmanager.html
Normal file
271
docs/classes/reflection-762.reflection-214.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-787.reflection-214.fileuploader.html
Normal file
231
docs/classes/reflection-787.reflection-214.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-787.reflection-214.sas9apiclient.html
Normal file
312
docs/classes/reflection-787.reflection-214.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1590
docs/classes/reflection-787.reflection-214.sasjs.html
Normal file
1590
docs/classes/reflection-787.reflection-214.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1435
docs/classes/reflection-787.reflection-214.sasviyaapiclient.html
Normal file
1435
docs/classes/reflection-787.reflection-214.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
323
docs/classes/reflection-787.reflection-214.sessionmanager.html
Normal file
323
docs/classes/reflection-787.reflection-214.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-790.reflection-214.fileuploader.html
Normal file
231
docs/classes/reflection-790.reflection-214.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-790.reflection-214.sas9apiclient.html
Normal file
312
docs/classes/reflection-790.reflection-214.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1641
docs/classes/reflection-790.reflection-214.sasjs.html
Normal file
1641
docs/classes/reflection-790.reflection-214.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1444
docs/classes/reflection-790.reflection-214.sasviyaapiclient.html
Normal file
1444
docs/classes/reflection-790.reflection-214.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
323
docs/classes/reflection-790.reflection-214.sessionmanager.html
Normal file
323
docs/classes/reflection-790.reflection-214.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-804.reflection-219.fileuploader.html
Normal file
231
docs/classes/reflection-804.reflection-219.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-804.reflection-219.sas9apiclient.html
Normal file
312
docs/classes/reflection-804.reflection-219.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1670
docs/classes/reflection-804.reflection-219.sasjs.html
Normal file
1670
docs/classes/reflection-804.reflection-219.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1471
docs/classes/reflection-804.reflection-219.sasviyaapiclient.html
Normal file
1471
docs/classes/reflection-804.reflection-219.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
323
docs/classes/reflection-804.reflection-219.sessionmanager.html
Normal file
323
docs/classes/reflection-804.reflection-219.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-831.reflection-220.fileuploader.html
Normal file
231
docs/classes/reflection-831.reflection-220.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-831.reflection-220.sas9apiclient.html
Normal file
312
docs/classes/reflection-831.reflection-220.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1725
docs/classes/reflection-831.reflection-220.sasjs.html
Normal file
1725
docs/classes/reflection-831.reflection-220.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1567
docs/classes/reflection-831.reflection-220.sasviyaapiclient.html
Normal file
1567
docs/classes/reflection-831.reflection-220.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
323
docs/classes/reflection-831.reflection-220.sessionmanager.html
Normal file
323
docs/classes/reflection-831.reflection-220.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
231
docs/classes/reflection-837.reflection-220.fileuploader.html
Normal file
231
docs/classes/reflection-837.reflection-220.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/reflection-837.reflection-220.sas9apiclient.html
Normal file
312
docs/classes/reflection-837.reflection-220.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1782
docs/classes/reflection-837.reflection-220.sasjs.html
Normal file
1782
docs/classes/reflection-837.reflection-220.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1567
docs/classes/reflection-837.reflection-220.sasviyaapiclient.html
Normal file
1567
docs/classes/reflection-837.reflection-220.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
323
docs/classes/reflection-837.reflection-220.sessionmanager.html
Normal file
323
docs/classes/reflection-837.reflection-220.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
622
docs/classes/root.contextmanager.html
Normal file
622
docs/classes/root.contextmanager.html
Normal file
File diff suppressed because one or more lines are too long
231
docs/classes/root.fileuploader.html
Normal file
231
docs/classes/root.fileuploader.html
Normal file
File diff suppressed because one or more lines are too long
312
docs/classes/root.sas9apiclient.html
Normal file
312
docs/classes/root.sas9apiclient.html
Normal file
File diff suppressed because one or more lines are too long
1818
docs/classes/root.sasjs.html
Normal file
1818
docs/classes/root.sasjs.html
Normal file
File diff suppressed because one or more lines are too long
1609
docs/classes/root.sasviyaapiclient.html
Normal file
1609
docs/classes/root.sasviyaapiclient.html
Normal file
File diff suppressed because one or more lines are too long
360
docs/classes/root.sessionmanager.html
Normal file
360
docs/classes/root.sessionmanager.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
215
docs/interfaces/types.polloptions.html
Normal file
215
docs/interfaces/types.polloptions.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
197
docs/interfaces/types.sessionvariable.html
Normal file
197
docs/interfaces/types.sessionvariable.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-762.html
Normal file
106
docs/modules/reflection-762.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-762.reflection-214.html
Normal file
128
docs/modules/reflection-762.reflection-214.html
Normal file
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-787.html
Normal file
106
docs/modules/reflection-787.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-787.reflection-214.html
Normal file
128
docs/modules/reflection-787.reflection-214.html
Normal file
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-790.html
Normal file
106
docs/modules/reflection-790.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-790.reflection-214.html
Normal file
128
docs/modules/reflection-790.reflection-214.html
Normal file
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-804.html
Normal file
106
docs/modules/reflection-804.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-804.reflection-219.html
Normal file
128
docs/modules/reflection-804.reflection-219.html
Normal file
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-831.html
Normal file
106
docs/modules/reflection-831.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-831.reflection-220.html
Normal file
128
docs/modules/reflection-831.reflection-220.html
Normal file
File diff suppressed because one or more lines are too long
106
docs/modules/reflection-837.html
Normal file
106
docs/modules/reflection-837.html
Normal file
File diff suppressed because one or more lines are too long
128
docs/modules/reflection-837.reflection-220.html
Normal file
128
docs/modules/reflection-837.reflection-220.html
Normal file
File diff suppressed because one or more lines are too long
129
docs/modules/root.html
Normal file
129
docs/modules/root.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -30,8 +30,8 @@
|
||||
$('#chart-container').append('<canvas id="myChart" style="display: none;"></canvas>')
|
||||
// make a request to a SAS service
|
||||
var type = $("#cars")[0].options[$("#cars")[0].selectedIndex].value;
|
||||
// request data from an endpoint under your appLoc
|
||||
sasJs.request("/common/getdata", {
|
||||
// request data from an endpoint under your appLoc (missing opening slash implies relative path)
|
||||
sasJs.request("common/getdata", {
|
||||
// send data as an array of objects - each object is one row
|
||||
fromjs: [{ type: type }]
|
||||
}).then((response) => {
|
||||
|
||||
5586
package-lock.json
generated
5586
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@@ -2,12 +2,12 @@
|
||||
"name": "@sasjs/adapter",
|
||||
"description": "JavaScript adapter for SAS",
|
||||
"scripts": {
|
||||
"build": "rimraf build && webpack",
|
||||
"build": "rimraf build && rimraf node && mkdir node && cp -r src/* node && webpack && rimraf build/src && rimraf node",
|
||||
"package:lib": "npm run build && cp ./package.json build && cd build && npm version \"5.0.0\" && npm pack",
|
||||
"publish:lib": "npm run build && cd build && npm publish",
|
||||
"lint:fix": "npx prettier --write 'src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}'",
|
||||
"lint": "npx prettier --check 'src/**/*.{ts,tsx,js,jsx,html,css,sass,less,json,yml,md,graphql}'",
|
||||
"test": "jest",
|
||||
"test": "jest --coverage",
|
||||
"prepublishOnly": "cp -r ./build/* . && rm -rf ./build",
|
||||
"postpublish": "git clean -fd",
|
||||
"semantic-release": "semantic-release",
|
||||
@@ -37,15 +37,16 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/isomorphic-fetch": "0.0.35",
|
||||
"@types/jest": "^26.0.14",
|
||||
"@types/jest": "^26.0.15",
|
||||
"cp": "^0.2.0",
|
||||
"dotenv": "^8.2.0",
|
||||
"jest": "^25.5.4",
|
||||
"path": "^0.12.7",
|
||||
"rimraf": "^3.0.2",
|
||||
"semantic-release": "^17.1.2",
|
||||
"terser-webpack-plugin": "^4.2.2",
|
||||
"semantic-release": "^17.3.0",
|
||||
"terser-webpack-plugin": "^4.2.3",
|
||||
"ts-jest": "^25.5.1",
|
||||
"ts-loader": "^8.0.4",
|
||||
"ts-loader": "^8.0.11",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0",
|
||||
"typedoc": "^0.17.8",
|
||||
@@ -53,10 +54,11 @@
|
||||
"typedoc-plugin-external-module-name": "^4.0.3",
|
||||
"typescript": "^3.9.7",
|
||||
"webpack": "^4.44.2",
|
||||
"webpack-cli": "^3.3.12"
|
||||
"webpack-cli": "^4.2.0"
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@sasjs/utils": "^1.5.0",
|
||||
"es6-promise": "^4.2.8",
|
||||
"form-data": "^3.0.0",
|
||||
"isomorphic-fetch": "^2.2.1"
|
||||
|
||||
6
sasjs-tests/package-lock.json
generated
6
sasjs-tests/package-lock.json
generated
@@ -1357,9 +1357,9 @@
|
||||
"integrity": "sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw=="
|
||||
},
|
||||
"@sasjs/adapter": {
|
||||
"version": "1.12.0",
|
||||
"resolved": "https://registry.npmjs.org/@sasjs/adapter/-/adapter-1.12.0.tgz",
|
||||
"integrity": "sha512-0uGQH9ynomWzdBaEujEtcR38q6V7LCgG0mrb1Wellv6cC/IHD3j6WfeZZAgtiMPeOSJjbCDBOlVnzC2TlBqJFw==",
|
||||
"version": "1.18.3",
|
||||
"resolved": "https://registry.npmjs.org/@sasjs/adapter/-/adapter-1.18.3.tgz",
|
||||
"integrity": "sha512-wzDFJRyt2dXFeQP+JzqRGunYUbujrAbU/Jc4IWg5URsCkGAzwsNl/4G0xJVbqOTy1MvoZ431rfCnvhkUlg7D3Q==",
|
||||
"requires": {
|
||||
"es6-promise": "^4.2.8",
|
||||
"form-data": "^3.0.0",
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"homepage": ".",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@sasjs/adapter": "^1.12.0",
|
||||
"@sasjs/adapter": "^1.18.2",
|
||||
"@sasjs/test-framework": "^1.4.0",
|
||||
"@testing-library/jest-dom": "^4.2.4",
|
||||
"@testing-library/react": "^9.5.0",
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import React from "react";
|
||||
import { render } from "@testing-library/react";
|
||||
import App from "./App";
|
||||
|
||||
test("renders learn react link", () => {
|
||||
const { getByText } = render(<App />);
|
||||
const linkElement = getByText(/learn react/i);
|
||||
expect(linkElement).toBeInTheDocument();
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import { sendArrTests, sendObjTests } from "./testSuites/RequestData";
|
||||
import { specialCaseTests } from "./testSuites/SpecialCases";
|
||||
import { sasjsRequestTests } from "./testSuites/SasjsRequests";
|
||||
import "@sasjs/test-framework/dist/index.css";
|
||||
import { computeTests } from "./testSuites/Compute";
|
||||
|
||||
const App = (): ReactElement<{}> => {
|
||||
const { adapter, config } = useContext(AppContext);
|
||||
@@ -17,7 +18,8 @@ const App = (): ReactElement<{}> => {
|
||||
sendArrTests(adapter),
|
||||
sendObjTests(adapter),
|
||||
specialCaseTests(adapter),
|
||||
sasjsRequestTests(adapter)
|
||||
sasjsRequestTests(adapter),
|
||||
computeTests(adapter)
|
||||
]);
|
||||
}
|
||||
}, [adapter, config]);
|
||||
|
||||
@@ -3,12 +3,12 @@ import { TestSuite } from "@sasjs/test-framework";
|
||||
|
||||
const defaultConfig: SASjsConfig = {
|
||||
serverUrl: window.location.origin,
|
||||
pathSAS9: "/SASStoredProcess/do",
|
||||
pathSASViya: "/SASJobExecution",
|
||||
appLoc: "/Public/seedapp",
|
||||
pathSAS9: '/SASStoredProcess/do',
|
||||
pathSASViya: '/SASJobExecution',
|
||||
appLoc: '/Public/seedapp',
|
||||
serverType: ServerType.SASViya,
|
||||
debug: true,
|
||||
contextName: "SAS Job Execution compute context",
|
||||
debug: false,
|
||||
contextName: 'SAS Job Execution compute context',
|
||||
useComputeApi: false
|
||||
};
|
||||
|
||||
@@ -37,6 +37,17 @@ export const basicTests = (
|
||||
assertion: (response: any) =>
|
||||
response && response.isLoggedIn && response.userName === userName
|
||||
},
|
||||
{
|
||||
title: "Multiple Log in attempts",
|
||||
description: "Should fail on first attempt and should log the user in on second attempt",
|
||||
test: async () => {
|
||||
await adapter.logOut()
|
||||
await adapter.logIn('invalid', 'invalid')
|
||||
return adapter.logIn(userName, password)
|
||||
},
|
||||
assertion: (response: any) =>
|
||||
response && response.isLoggedIn && response.userName === userName
|
||||
},
|
||||
{
|
||||
title: "Default config",
|
||||
description:
|
||||
@@ -46,6 +57,7 @@ export const basicTests = (
|
||||
},
|
||||
assertion: (sasjsInstance: SASjs) => {
|
||||
const sasjsConfig = sasjsInstance.getSasjsConfig();
|
||||
|
||||
return (
|
||||
sasjsConfig.serverUrl === defaultConfig.serverUrl &&
|
||||
sasjsConfig.pathSAS9 === defaultConfig.pathSAS9 &&
|
||||
|
||||
95
sasjs-tests/src/testSuites/Compute.ts
Normal file
95
sasjs-tests/src/testSuites/Compute.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import SASjs from "@sasjs/adapter";
|
||||
import { TestSuite } from "@sasjs/test-framework";
|
||||
|
||||
export const computeTests = (adapter: SASjs): TestSuite => ({
|
||||
name: "Compute",
|
||||
tests: [
|
||||
{
|
||||
title: "Start Compute Job - not waiting for result",
|
||||
description: "Should start a compute job and return the session",
|
||||
test: () => {
|
||||
const data: any = { table1: [{ col1: "first col value" }] };
|
||||
return adapter.startComputeJob("/Public/app/common/sendArr", data);
|
||||
},
|
||||
assertion: (res: any) => {
|
||||
const expectedProperties = ["id", "applicationName", "attributes"]
|
||||
return validate(expectedProperties, res);
|
||||
}
|
||||
},
|
||||
{
|
||||
title: "Start Compute Job - waiting for result",
|
||||
description: "Should start a compute job and return the job",
|
||||
test: () => {
|
||||
const data: any = { table1: [{ col1: "first col value" }] };
|
||||
return adapter.startComputeJob("/Public/app/common/sendArr", data, {}, "", true);
|
||||
},
|
||||
assertion: (res: any) => {
|
||||
const expectedProperties = ["id", "state", "creationTimeStamp", "jobConditionCode"]
|
||||
return validate(expectedProperties, res.result);
|
||||
}
|
||||
},
|
||||
{
|
||||
title: "Execute Script Viya - complete job",
|
||||
description: "Should execute sas file and return log",
|
||||
test: () => {
|
||||
const fileLines = [
|
||||
`data;`,
|
||||
`do x=1 to 100;`,
|
||||
`output;`,
|
||||
`end;`,
|
||||
`run;`
|
||||
]
|
||||
|
||||
return adapter.executeScriptSASViya(
|
||||
'sasCode.sas',
|
||||
fileLines,
|
||||
'SAS Studio compute context',
|
||||
undefined,
|
||||
true
|
||||
)
|
||||
},
|
||||
assertion: (res: any) => {
|
||||
const expectedLogContent = `1 data;\\n2 do x=1 to 100;\\n3 output;\\n4 end;\\n5 run;\\n\\n`
|
||||
|
||||
return validateLog(expectedLogContent, res.log);
|
||||
}
|
||||
},
|
||||
{
|
||||
title: "Execute Script Viya - failed job",
|
||||
description: "Should execute sas file and return log",
|
||||
test: () => {
|
||||
const fileLines = [
|
||||
`%abort;`
|
||||
]
|
||||
|
||||
return adapter.executeScriptSASViya(
|
||||
'sasCode.sas',
|
||||
fileLines,
|
||||
'SAS Studio compute context',
|
||||
undefined,
|
||||
true
|
||||
).catch((err: any) => err )
|
||||
},
|
||||
assertion: (res: any) => {
|
||||
const expectedLogContent = `1 %abort;\\nERROR: The %ABORT statement is not valid in open code.\\n`
|
||||
|
||||
return validateLog(expectedLogContent, res.log);
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const validateLog = (text: string, log: string): boolean => {
|
||||
const isValid = JSON.stringify(log).includes(text)
|
||||
|
||||
return isValid
|
||||
}
|
||||
|
||||
const validate = (expectedProperties: string[], data: any): boolean => {
|
||||
const actualProperties = Object.keys(data);
|
||||
|
||||
const isValid = expectedProperties.every(
|
||||
(property) => actualProperties.includes(property)
|
||||
);
|
||||
return isValid
|
||||
}
|
||||
@@ -88,7 +88,7 @@ export const sendArrTests = (adapter: SASjs): TestSuite => ({
|
||||
return adapter.request("common/sendArr", data).catch((e) => e);
|
||||
},
|
||||
assertion: (error: any) => {
|
||||
return !!error && !!error.body && !!error.body.message;
|
||||
return !!error && !!error.error && !!error.error.message;
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -185,7 +185,7 @@ export const sendObjTests = (adapter: SASjs): TestSuite => ({
|
||||
};
|
||||
return adapter.request("common/sendObj", invalidData).catch((e) => e);
|
||||
},
|
||||
assertion: (error: any) => !!error && !!error.body && !!error.body.message
|
||||
assertion: (error: any) => !!error && !!error.error && !!error.error.message
|
||||
},
|
||||
{
|
||||
title: "Single string value",
|
||||
@@ -219,7 +219,7 @@ export const sendObjTests = (adapter: SASjs): TestSuite => ({
|
||||
.catch((e) => e);
|
||||
},
|
||||
assertion: (error: any) => {
|
||||
return !!error && !!error.body && !!error.body.message;
|
||||
return !!error && !!error.error && !!error.error.message;
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -23,22 +23,24 @@ export const sasjsRequestTests = (adapter: SASjs): TestSuite => ({
|
||||
},
|
||||
{
|
||||
title: "Make error and capture log",
|
||||
description: "Should make an error and capture log",
|
||||
description: "Should make an error and capture log, in the same time it is testing if debug override is working",
|
||||
test: async () => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
adapter
|
||||
.request("common/makeErr", data)
|
||||
.request("common/makeErr", data, {debug: true})
|
||||
.then((res) => {
|
||||
//no action here, this request must throw error
|
||||
})
|
||||
.catch((err) => {
|
||||
let sasRequests = adapter.getSasRequests();
|
||||
let makeErrRequest =
|
||||
let makeErrRequest: any =
|
||||
sasRequests.find((req) =>
|
||||
req.serviceLink.includes("makeErr")
|
||||
) || null;
|
||||
|
||||
resolve(!!makeErrRequest);
|
||||
if (!makeErrRequest) resolve(false)
|
||||
|
||||
resolve(!!(makeErrRequest.logFile && makeErrRequest.logFile.length > 0));
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
538
src/ContextManager.ts
Normal file
538
src/ContextManager.ts
Normal file
@@ -0,0 +1,538 @@
|
||||
import {
|
||||
Context,
|
||||
CsrfToken,
|
||||
EditContextInput,
|
||||
ContextAllAttributes
|
||||
} from './types'
|
||||
import { makeRequest, isUrl } from './utils'
|
||||
import { SASViyaApiClient } from './SASViyaApiClient'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
|
||||
export class ContextManager {
|
||||
private defaultComputeContexts = [
|
||||
'CAS Formats service compute context',
|
||||
'Data Mining compute context',
|
||||
'Import 9 service compute context',
|
||||
'SAS Job Execution compute context',
|
||||
'SAS Model Manager compute context',
|
||||
'SAS Studio compute context',
|
||||
'SAS Visual Forecasting compute context'
|
||||
]
|
||||
private defaultLauncherContexts = [
|
||||
'CAS Formats service launcher context',
|
||||
'Data Mining launcher context',
|
||||
'Import 9 service launcher context',
|
||||
'Job Flow Execution launcher context',
|
||||
'SAS Job Execution launcher context',
|
||||
'SAS Model Manager launcher context',
|
||||
'SAS Studio launcher context',
|
||||
'SAS Visual Forecasting launcher context'
|
||||
]
|
||||
|
||||
private csrfToken: CsrfToken | null = null
|
||||
|
||||
get getDefaultComputeContexts() {
|
||||
return this.defaultComputeContexts
|
||||
}
|
||||
get getDefaultLauncherContexts() {
|
||||
return this.defaultLauncherContexts
|
||||
}
|
||||
|
||||
constructor(
|
||||
private serverUrl: string,
|
||||
private setCsrfToken: (csrfToken: CsrfToken) => void
|
||||
) {
|
||||
if (serverUrl) isUrl(serverUrl)
|
||||
}
|
||||
|
||||
public async getComputeContexts(accessToken?: string) {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: contexts } = await this.request<{ items: Context[] }>(
|
||||
`${this.serverUrl}/compute/contexts?limit=10000`,
|
||||
{ headers }
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while getting compute contexts. ')
|
||||
})
|
||||
|
||||
const contextsList = contexts && contexts.items ? contexts.items : []
|
||||
|
||||
return contextsList.map((context: any) => ({
|
||||
createdBy: context.createdBy,
|
||||
id: context.id,
|
||||
name: context.name,
|
||||
version: context.version,
|
||||
attributes: {}
|
||||
}))
|
||||
}
|
||||
|
||||
public async getLauncherContexts(accessToken?: string) {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: contexts } = await this.request<{ items: Context[] }>(
|
||||
`${this.serverUrl}/launcher/contexts?limit=10000`,
|
||||
{ headers }
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while getting launcher contexts. ')
|
||||
})
|
||||
|
||||
const contextsList = contexts && contexts.items ? contexts.items : []
|
||||
|
||||
return contextsList.map((context: any) => ({
|
||||
createdBy: context.createdBy,
|
||||
id: context.id,
|
||||
name: context.name,
|
||||
version: context.version,
|
||||
attributes: {}
|
||||
}))
|
||||
}
|
||||
|
||||
public async createComputeContext(
|
||||
contextName: string,
|
||||
launchContextName: string,
|
||||
sharedAccountId: string,
|
||||
autoExecLines: string[],
|
||||
accessToken?: string,
|
||||
authorizedUsers?: string[]
|
||||
) {
|
||||
this.validateContextName(contextName)
|
||||
|
||||
this.isDefaultContext(
|
||||
contextName,
|
||||
this.defaultComputeContexts,
|
||||
`Compute context '${contextName}' already exists.`
|
||||
)
|
||||
|
||||
const existingComputeContexts = await this.getComputeContexts(accessToken)
|
||||
|
||||
if (
|
||||
existingComputeContexts.find((context) => context.name === contextName)
|
||||
) {
|
||||
throw new Error(`Compute context '${contextName}' already exists.`)
|
||||
}
|
||||
|
||||
if (launchContextName) {
|
||||
if (!this.defaultLauncherContexts.includes(launchContextName)) {
|
||||
const launcherContexts = await this.getLauncherContexts(accessToken)
|
||||
|
||||
if (
|
||||
!launcherContexts.find(
|
||||
(context) => context.name === launchContextName
|
||||
)
|
||||
) {
|
||||
const description = `The launcher context for ${launchContextName}`
|
||||
const launchType = 'direct'
|
||||
|
||||
const newLauncherContext = await this.createLauncherContext(
|
||||
launchContextName,
|
||||
description,
|
||||
launchType,
|
||||
accessToken
|
||||
).catch((err) => {
|
||||
throw new Error(`Error while creating launcher context. ${err}`)
|
||||
})
|
||||
|
||||
if (newLauncherContext && newLauncherContext.name) {
|
||||
launchContextName = newLauncherContext.name
|
||||
} else {
|
||||
throw new Error('Error while creating launcher context.')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
let attributes = { reuseServerProcesses: true } as object
|
||||
|
||||
if (sharedAccountId)
|
||||
attributes = { ...attributes, runServerAs: sharedAccountId }
|
||||
|
||||
const requestBody: any = {
|
||||
name: contextName,
|
||||
launchContext: {
|
||||
contextName: launchContextName || ''
|
||||
},
|
||||
attributes
|
||||
}
|
||||
|
||||
if (authorizedUsers && authorizedUsers.length) {
|
||||
requestBody['authorizedUsers'] = authorizedUsers
|
||||
} else {
|
||||
requestBody['authorizeAllAuthenticatedUsers'] = true
|
||||
}
|
||||
|
||||
if (autoExecLines) {
|
||||
requestBody.environment = { autoExecLines }
|
||||
}
|
||||
|
||||
const createContextRequest: RequestInit = {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(requestBody)
|
||||
}
|
||||
|
||||
const { result: context } = await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts`,
|
||||
createContextRequest
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while creating compute context. ')
|
||||
})
|
||||
|
||||
return context
|
||||
}
|
||||
|
||||
public async createLauncherContext(
|
||||
contextName: string,
|
||||
description: string,
|
||||
launchType = 'direct',
|
||||
accessToken?: string
|
||||
) {
|
||||
if (!contextName) {
|
||||
throw new Error('Context name is required.')
|
||||
}
|
||||
|
||||
this.isDefaultContext(
|
||||
contextName,
|
||||
this.defaultLauncherContexts,
|
||||
`Launcher context '${contextName}' already exists.`
|
||||
)
|
||||
|
||||
const existingLauncherContexts = await this.getLauncherContexts(accessToken)
|
||||
|
||||
if (
|
||||
existingLauncherContexts.find((context) => context.name === contextName)
|
||||
) {
|
||||
throw new Error(`Launcher context '${contextName}' already exists.`)
|
||||
}
|
||||
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const requestBody: any = {
|
||||
name: contextName,
|
||||
description: description,
|
||||
launchType
|
||||
}
|
||||
|
||||
const createContextRequest: RequestInit = {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(requestBody)
|
||||
}
|
||||
|
||||
const { result: context } = await this.request<Context>(
|
||||
`${this.serverUrl}/launcher/contexts`,
|
||||
createContextRequest
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while creating launcher context. ')
|
||||
})
|
||||
|
||||
return context
|
||||
}
|
||||
|
||||
public async editComputeContext(
|
||||
contextName: string,
|
||||
editedContext: EditContextInput,
|
||||
accessToken?: string
|
||||
) {
|
||||
this.validateContextName(contextName)
|
||||
|
||||
this.isDefaultContext(
|
||||
contextName,
|
||||
this.defaultComputeContexts,
|
||||
'Editing default SAS compute contexts is not allowed.',
|
||||
true
|
||||
)
|
||||
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
let originalContext
|
||||
|
||||
originalContext = await this.getComputeContextByName(
|
||||
contextName,
|
||||
accessToken
|
||||
)
|
||||
|
||||
// Try to find context by id, when context name has been changed.
|
||||
if (!originalContext) {
|
||||
originalContext = await this.getComputeContextById(
|
||||
editedContext.id!,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
const { result: context, etag } = await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts/${originalContext.id}`,
|
||||
{
|
||||
headers
|
||||
}
|
||||
).catch((err) => {
|
||||
if (err && err.status === 404) {
|
||||
throw new Error(
|
||||
`The context '${contextName}' was not found on this server.`
|
||||
)
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
|
||||
// An If-Match header with the value of the last ETag for the context
|
||||
// is required to be able to update it
|
||||
// https://developer.sas.com/apis/rest/Compute/#update-a-context-definition
|
||||
headers['If-Match'] = etag
|
||||
|
||||
const updateContextRequest: RequestInit = {
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
...context,
|
||||
...editedContext,
|
||||
attributes: { ...context.attributes, ...editedContext.attributes }
|
||||
})
|
||||
}
|
||||
|
||||
return await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts/${context.id}`,
|
||||
updateContextRequest
|
||||
)
|
||||
}
|
||||
|
||||
public async getComputeContextByName(
|
||||
contextName: string,
|
||||
accessToken?: string
|
||||
): Promise<Context> {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: contexts } = await this.request<{ items: Context[] }>(
|
||||
`${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`,
|
||||
{ headers }
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while getting compute context by name. ')
|
||||
})
|
||||
|
||||
if (!contexts || !(contexts.items && contexts.items.length)) {
|
||||
throw new Error(
|
||||
`The context '${contextName}' was not found at '${this.serverUrl}'.`
|
||||
)
|
||||
}
|
||||
|
||||
return contexts.items[0]
|
||||
}
|
||||
|
||||
public async getComputeContextById(
|
||||
contextId: string,
|
||||
accessToken?: string
|
||||
): Promise<ContextAllAttributes> {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: context } = await this.request<ContextAllAttributes>(
|
||||
`${this.serverUrl}/compute/contexts/${contextId}`,
|
||||
{ headers }
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while getting compute context by id. ')
|
||||
})
|
||||
|
||||
return context
|
||||
}
|
||||
|
||||
public async getExecutableContexts(
|
||||
executeScript: Function,
|
||||
accessToken?: string
|
||||
) {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: contexts } = await this.request<{ items: Context[] }>(
|
||||
`${this.serverUrl}/compute/contexts?limit=10000`,
|
||||
{ headers }
|
||||
).catch((err) => {
|
||||
throw prefixMessage(err, 'Error while fetching compute contexts.')
|
||||
})
|
||||
|
||||
const contextsList = contexts.items || []
|
||||
const executableContexts: any[] = []
|
||||
|
||||
const promises = contextsList.map((context: any) => {
|
||||
const linesOfCode = ['%put &=sysuserid;']
|
||||
|
||||
return () =>
|
||||
executeScript(
|
||||
`test-${context.name}`,
|
||||
linesOfCode,
|
||||
context.name,
|
||||
accessToken,
|
||||
null,
|
||||
false,
|
||||
true,
|
||||
true
|
||||
).catch((err: any) => err)
|
||||
})
|
||||
|
||||
let results: any[] = []
|
||||
|
||||
for (const promise of promises) results.push(await promise())
|
||||
|
||||
results.forEach((result: any, index: number) => {
|
||||
if (result && result.log) {
|
||||
try {
|
||||
const resultParsed = result.log
|
||||
let sysUserId = ''
|
||||
|
||||
const sysUserIdLog = resultParsed
|
||||
.split('\n')
|
||||
.find((line: string) => line.startsWith('SYSUSERID='))
|
||||
|
||||
if (sysUserIdLog) {
|
||||
sysUserId = sysUserIdLog.replace('SYSUSERID=', '')
|
||||
|
||||
executableContexts.push({
|
||||
createdBy: contextsList[index].createdBy,
|
||||
id: contextsList[index].id,
|
||||
name: contextsList[index].name,
|
||||
version: contextsList[index].version,
|
||||
attributes: {
|
||||
sysUserId
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return executableContexts
|
||||
}
|
||||
|
||||
public async deleteComputeContext(contextName: string, accessToken?: string) {
|
||||
this.validateContextName(contextName)
|
||||
|
||||
this.isDefaultContext(
|
||||
contextName,
|
||||
this.defaultComputeContexts,
|
||||
'Deleting default SAS compute contexts is not allowed.',
|
||||
true
|
||||
)
|
||||
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const context = await this.getComputeContextByName(contextName, accessToken)
|
||||
|
||||
const deleteContextRequest: RequestInit = {
|
||||
method: 'DELETE',
|
||||
headers
|
||||
}
|
||||
|
||||
return await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts/${context.id}`,
|
||||
deleteContextRequest
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: implement editLauncherContext method
|
||||
|
||||
// TODO: implement deleteLauncherContext method
|
||||
|
||||
private async request<T>(
|
||||
url: string,
|
||||
options: RequestInit,
|
||||
contentType: 'text' | 'json' = 'json'
|
||||
) {
|
||||
if (this.csrfToken) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
[this.csrfToken.headerName]: this.csrfToken.value
|
||||
}
|
||||
}
|
||||
|
||||
return await makeRequest<T>(
|
||||
url,
|
||||
options,
|
||||
(token) => {
|
||||
this.csrfToken = token
|
||||
this.setCsrfToken(token)
|
||||
},
|
||||
contentType
|
||||
).catch((err) => {
|
||||
throw prefixMessage(
|
||||
err,
|
||||
'Error while making request in Context Manager. '
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
private validateContextName(name: string) {
|
||||
if (!name) throw new Error('Context name is required.')
|
||||
}
|
||||
|
||||
public isDefaultContext(
|
||||
context: string,
|
||||
defaultContexts: string[] = this.defaultComputeContexts,
|
||||
errorMessage = '',
|
||||
listDefaults = false
|
||||
) {
|
||||
if (defaultContexts.includes(context)) {
|
||||
throw new Error(
|
||||
`${errorMessage}${
|
||||
listDefaults
|
||||
? '\nDefault contexts:' +
|
||||
defaultContexts.map((context, i) => `\n${i + 1}. ${context}`)
|
||||
: ''
|
||||
}`
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { isLogInRequired, needsRetry, isUrl } from './utils'
|
||||
import { CsrfToken } from './types/CsrfToken'
|
||||
import { UploadFile } from './types/UploadFile'
|
||||
import { ErrorResponse } from './types'
|
||||
|
||||
const requestRetryLimit = 5
|
||||
|
||||
@@ -18,29 +19,31 @@ export class FileUploader {
|
||||
private retryCount = 0
|
||||
|
||||
public uploadFile(sasJob: string, files: UploadFile[], params: any) {
|
||||
if (files?.length < 1)
|
||||
throw new Error('At least one file must be provided.')
|
||||
|
||||
let paramsString = ''
|
||||
|
||||
for (let param in params) {
|
||||
if (params.hasOwnProperty(param)) {
|
||||
paramsString += `&${param}=${params[param]}`
|
||||
}
|
||||
}
|
||||
|
||||
const program = this.appLoc
|
||||
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
: sasJob
|
||||
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
||||
'_program=' + program
|
||||
}${paramsString}`
|
||||
|
||||
const headers = {
|
||||
'cache-control': 'no-cache'
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (files?.length < 1)
|
||||
reject(new ErrorResponse('At least one file must be provided.'))
|
||||
if (!sasJob || sasJob === '')
|
||||
reject(new ErrorResponse('sasJob must be provided.'))
|
||||
|
||||
let paramsString = ''
|
||||
|
||||
for (let param in params) {
|
||||
if (params.hasOwnProperty(param)) {
|
||||
paramsString += `&${param}=${params[param]}`
|
||||
}
|
||||
}
|
||||
|
||||
const program = this.appLoc
|
||||
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
: sasJob
|
||||
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
||||
'_program=' + program
|
||||
}${paramsString}`
|
||||
|
||||
const headers = {
|
||||
'cache-control': 'no-cache'
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
|
||||
for (let file of files) {
|
||||
@@ -76,7 +79,7 @@ export class FileUploader {
|
||||
})
|
||||
.then((responseText) => {
|
||||
if (isLogInRequired(responseText))
|
||||
reject('You must be logged in to upload a file')
|
||||
reject(new ErrorResponse('You must be logged in to upload a file.'))
|
||||
|
||||
if (needsRetry(responseText)) {
|
||||
if (this.retryCount < requestRetryLimit) {
|
||||
@@ -95,10 +98,18 @@ export class FileUploader {
|
||||
try {
|
||||
resolve(JSON.parse(responseText))
|
||||
} catch (e) {
|
||||
reject(e)
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Error while parsing json from upload response.',
|
||||
e
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
reject(new ErrorResponse('Upload request failed.', err))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,14 @@ import {
|
||||
Folder,
|
||||
CsrfToken,
|
||||
EditContextInput,
|
||||
ErrorResponse,
|
||||
JobDefinition
|
||||
JobDefinition,
|
||||
PollOptions
|
||||
} from './types'
|
||||
import { formatDataForRequest } from './utils/formatDataForRequest'
|
||||
import { SessionManager } from './SessionManager'
|
||||
import { ContextManager } from './ContextManager'
|
||||
import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
||||
import { Logger, LogLevel } from '@sasjs/utils/logger'
|
||||
|
||||
/**
|
||||
* A client for interfacing with the SAS Viya REST API.
|
||||
@@ -38,14 +41,26 @@ export class SASViyaApiClient {
|
||||
|
||||
private csrfToken: CsrfToken | null = null
|
||||
private fileUploadCsrfToken: CsrfToken | null = null
|
||||
private _debug = false
|
||||
private sessionManager = new SessionManager(
|
||||
this.serverUrl,
|
||||
this.contextName,
|
||||
this.setCsrfToken
|
||||
)
|
||||
private isForceDeploy: boolean = false
|
||||
private contextManager = new ContextManager(this.serverUrl, this.setCsrfToken)
|
||||
private folderMap = new Map<string, Job[]>()
|
||||
|
||||
public get debug() {
|
||||
return this._debug
|
||||
}
|
||||
|
||||
public set debug(value: boolean) {
|
||||
this._debug = value
|
||||
if (this.sessionManager) {
|
||||
this.sessionManager.debug = value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of jobs in the currently set root folder.
|
||||
*/
|
||||
@@ -85,29 +100,23 @@ export class SASViyaApiClient {
|
||||
* Returns all available compute contexts on this server.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async getAllContexts(accessToken?: string) {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
public async getComputeContexts(accessToken?: string) {
|
||||
return await this.contextManager.getComputeContexts(accessToken)
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
/**
|
||||
* Returns default(system) compute contexts.
|
||||
*/
|
||||
public getDefaultComputeContexts() {
|
||||
return this.contextManager.getDefaultComputeContexts
|
||||
}
|
||||
|
||||
const { result: contexts } = await this.request<{ items: Context[] }>(
|
||||
`${this.serverUrl}/compute/contexts?limit=10000`,
|
||||
{ headers }
|
||||
)
|
||||
|
||||
const contextsList = contexts && contexts.items ? contexts.items : []
|
||||
|
||||
return contextsList.map((context: any) => ({
|
||||
createdBy: context.createdBy,
|
||||
id: context.id,
|
||||
name: context.name,
|
||||
version: context.version,
|
||||
attributes: {}
|
||||
}))
|
||||
/**
|
||||
* Returns all available launcher contexts on this server.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async getLauncherContexts(accessToken?: string) {
|
||||
return await this.contextManager.getLauncherContexts(accessToken)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,67 +124,12 @@ export class SASViyaApiClient {
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async getExecutableContexts(accessToken?: string) {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
const bindedExecuteScript = this.executeScript.bind(this)
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: contexts } = await this.request<{ items: Context[] }>(
|
||||
`${this.serverUrl}/compute/contexts?limit=10000`,
|
||||
{ headers }
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
const contextsList = contexts.items || []
|
||||
const executableContexts: any[] = []
|
||||
|
||||
const promises = contextsList.map((context: any) => {
|
||||
const linesOfCode = ['%put &=sysuserid;']
|
||||
|
||||
return this.executeScript(
|
||||
`test-${context.name}`,
|
||||
linesOfCode,
|
||||
context.name,
|
||||
accessToken,
|
||||
false,
|
||||
null,
|
||||
true
|
||||
).catch(() => null)
|
||||
})
|
||||
|
||||
const results = await Promise.all(promises)
|
||||
|
||||
results.forEach((result: any, index: number) => {
|
||||
if (result) {
|
||||
let sysUserId = ''
|
||||
|
||||
if (result.log) {
|
||||
const sysUserIdLog = result.log
|
||||
.split('\n')
|
||||
.find((line: string) => line.startsWith('SYSUSERID='))
|
||||
|
||||
if (sysUserIdLog) {
|
||||
sysUserId = sysUserIdLog.replace('SYSUSERID=', '')
|
||||
}
|
||||
}
|
||||
|
||||
executableContexts.push({
|
||||
createdBy: contextsList[index].createdBy,
|
||||
id: contextsList[index].id,
|
||||
name: contextsList[index].name,
|
||||
version: contextsList[index].version,
|
||||
attributes: {
|
||||
sysUserId
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return executableContexts
|
||||
return await this.contextManager.getExecutableContexts(
|
||||
bindedExecuteScript,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -228,7 +182,7 @@ export class SASViyaApiClient {
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
* @param authorizedUsers - an optional list of authorized user IDs.
|
||||
*/
|
||||
public async createContext(
|
||||
public async createComputeContext(
|
||||
contextName: string,
|
||||
launchContextName: string,
|
||||
sharedAccountId: string,
|
||||
@@ -236,59 +190,35 @@ export class SASViyaApiClient {
|
||||
accessToken?: string,
|
||||
authorizedUsers?: string[]
|
||||
) {
|
||||
if (!contextName) {
|
||||
throw new Error('Context name is required.')
|
||||
}
|
||||
|
||||
if (!launchContextName) {
|
||||
throw new Error('Launch context name is required.')
|
||||
}
|
||||
|
||||
if (!sharedAccountId) {
|
||||
throw new Error('Shared account ID is required.')
|
||||
}
|
||||
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const requestBody: any = {
|
||||
name: contextName,
|
||||
launchContext: {
|
||||
contextName: launchContextName
|
||||
},
|
||||
attributes: {
|
||||
reuseServerProcesses: true,
|
||||
runServerAs: sharedAccountId
|
||||
}
|
||||
}
|
||||
|
||||
if (authorizedUsers && authorizedUsers.length) {
|
||||
requestBody['authorizedUsers'] = authorizedUsers
|
||||
} else {
|
||||
requestBody['authorizeAllAuthenticatedUsers'] = true
|
||||
}
|
||||
|
||||
if (autoExecLines) {
|
||||
requestBody.environment = { autoExecLines }
|
||||
}
|
||||
|
||||
const createContextRequest: RequestInit = {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(requestBody)
|
||||
}
|
||||
|
||||
const { result: context } = await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts`,
|
||||
createContextRequest
|
||||
return await this.contextManager.createComputeContext(
|
||||
contextName,
|
||||
launchContextName,
|
||||
sharedAccountId,
|
||||
autoExecLines,
|
||||
accessToken,
|
||||
authorizedUsers
|
||||
)
|
||||
}
|
||||
|
||||
return context
|
||||
/**
|
||||
* Creates a launcher context on the given server.
|
||||
* @param contextName - the name of the context to be created.
|
||||
* @param description - the description of the context to be created.
|
||||
* @param launchType - launch type of the context to be created.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async createLauncherContext(
|
||||
contextName: string,
|
||||
description: string,
|
||||
launchType = 'direct',
|
||||
accessToken?: string
|
||||
) {
|
||||
return await this.contextManager.createLauncherContext(
|
||||
contextName,
|
||||
description,
|
||||
launchType,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -297,73 +227,15 @@ export class SASViyaApiClient {
|
||||
* @param editedContext - an object with the properties to be updated.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async editContext(
|
||||
public async editComputeContext(
|
||||
contextName: string,
|
||||
editedContext: EditContextInput,
|
||||
accessToken?: string
|
||||
) {
|
||||
if (!contextName) {
|
||||
throw new Error('Invalid context name.')
|
||||
}
|
||||
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
let originalContext
|
||||
|
||||
originalContext = await this.getComputeContextByName(
|
||||
return await this.contextManager.editComputeContext(
|
||||
contextName,
|
||||
editedContext,
|
||||
accessToken
|
||||
).catch((_) => {})
|
||||
|
||||
// Try to find context by id, when context name has been changed.
|
||||
if (!originalContext) {
|
||||
originalContext = await this.getComputeContextById(
|
||||
editedContext.id!,
|
||||
accessToken
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
const { result: context, etag } = await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts/${originalContext.id}`,
|
||||
{
|
||||
headers
|
||||
}
|
||||
).catch((err) => {
|
||||
if (err && err.status === 404) {
|
||||
throw new Error(
|
||||
`The context '${contextName}' was not found on this server.`
|
||||
)
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
|
||||
// An If-Match header with the value of the last ETag for the context
|
||||
// is required to be able to update it
|
||||
// https://developer.sas.com/apis/rest/Compute/#update-a-context-definition
|
||||
headers['If-Match'] = etag
|
||||
|
||||
const updateContextRequest: RequestInit = {
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: JSON.stringify({
|
||||
...context,
|
||||
...editedContext,
|
||||
attributes: { ...context.attributes, ...editedContext.attributes }
|
||||
})
|
||||
}
|
||||
|
||||
return await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts/${context.id}`,
|
||||
updateContextRequest
|
||||
)
|
||||
}
|
||||
|
||||
@@ -372,29 +244,10 @@ export class SASViyaApiClient {
|
||||
* @param contextName - the name of the context to be deleted.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async deleteContext(contextName: string, accessToken?: string) {
|
||||
if (!contextName) {
|
||||
throw new Error('Invalid context name.')
|
||||
}
|
||||
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const context = await this.getComputeContextByName(contextName, accessToken)
|
||||
|
||||
const deleteContextRequest: RequestInit = {
|
||||
method: 'DELETE',
|
||||
headers
|
||||
}
|
||||
|
||||
return await this.request<Context>(
|
||||
`${this.serverUrl}/compute/contexts/${context.id}`,
|
||||
deleteContextRequest
|
||||
public async deleteComputeContext(contextName: string, accessToken?: string) {
|
||||
return await this.contextManager.deleteComputeContext(
|
||||
contextName,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
@@ -404,23 +257,25 @@ export class SASViyaApiClient {
|
||||
* @param linesOfCode - an array of code lines to execute.
|
||||
* @param contextName - the context to execute the code in.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
* @param sessionId - optional session ID to reuse.
|
||||
* @param silent - optional flag to disable logging.
|
||||
* @param data - execution data.
|
||||
* @param debug - when set to true, the log will be returned.
|
||||
* @param expectWebout - when set to true, the automatic _webout fileref will be checked for content, and that content returned. This fileref is used when the Job contains a SASjs web request (as opposed to executing arbitrary SAS code).
|
||||
* @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 printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||
*/
|
||||
public async executeScript(
|
||||
jobPath: string,
|
||||
linesOfCode: string[],
|
||||
contextName: string,
|
||||
accessToken?: string,
|
||||
silent = false,
|
||||
data = null,
|
||||
debug = false,
|
||||
expectWebout = false
|
||||
debug: boolean = false,
|
||||
expectWebout = false,
|
||||
waitForResult = true,
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false
|
||||
): Promise<any> {
|
||||
silent = !debug
|
||||
try {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
@@ -431,9 +286,36 @@ export class SASViyaApiClient {
|
||||
}
|
||||
|
||||
let executionSessionId: string
|
||||
const session = await this.sessionManager.getSession(accessToken)
|
||||
const session = await this.sessionManager
|
||||
.getSession(accessToken)
|
||||
.catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
executionSessionId = session!.id
|
||||
|
||||
if (printPid) {
|
||||
const { result: jobIdVariable } = await this.sessionManager.getVariable(
|
||||
executionSessionId,
|
||||
'SYSJOBID',
|
||||
accessToken
|
||||
)
|
||||
|
||||
if (jobIdVariable && jobIdVariable.value) {
|
||||
const relativeJobPath = this.rootFolderName
|
||||
? jobPath.split(this.rootFolderName).join('').replace(/^\//, '')
|
||||
: jobPath
|
||||
|
||||
const logger = new Logger(debug ? LogLevel.Debug : LogLevel.Info)
|
||||
|
||||
logger.info(
|
||||
`Triggered '${relativeJobPath}' with PID ${
|
||||
jobIdVariable.value
|
||||
} at ${timestampToYYYYMMDDHHMMSS()}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const jobArguments: { [key: string]: any } = {
|
||||
_contextName: contextName,
|
||||
_OMITJSONLISTING: true,
|
||||
@@ -470,7 +352,9 @@ export class SASViyaApiClient {
|
||||
|
||||
if (data) {
|
||||
if (JSON.stringify(data).includes(';')) {
|
||||
files = await this.uploadTables(data, accessToken)
|
||||
files = await this.uploadTables(data, accessToken).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
jobVariables['_webin_file_count'] = files.length
|
||||
|
||||
@@ -501,9 +385,15 @@ export class SASViyaApiClient {
|
||||
const { result: postedJob, etag } = await this.request<Job>(
|
||||
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs`,
|
||||
postJobRequest
|
||||
)
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
if (!silent) {
|
||||
if (!waitForResult) {
|
||||
return session
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
console.log(`Job has been submitted for '${fileName}'.`)
|
||||
console.log(
|
||||
`You can monitor the job progress at '${this.serverUrl}${
|
||||
@@ -516,13 +406,15 @@ export class SASViyaApiClient {
|
||||
postedJob,
|
||||
etag,
|
||||
accessToken,
|
||||
silent
|
||||
pollOptions
|
||||
)
|
||||
|
||||
const { result: currentJob } = await this.request<Job>(
|
||||
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`,
|
||||
{ headers }
|
||||
)
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
let jobResult
|
||||
let log
|
||||
@@ -535,19 +427,25 @@ export class SASViyaApiClient {
|
||||
{
|
||||
headers
|
||||
}
|
||||
).then((res: any) =>
|
||||
res.result.items.map((i: any) => i.line).join('\n')
|
||||
)
|
||||
.then((res: any) =>
|
||||
res.result.items.map((i: any) => i.line).join('\n')
|
||||
)
|
||||
.catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
if (jobStatus === 'failed' || jobStatus === 'error') {
|
||||
return Promise.reject({ error: currentJob.error, log })
|
||||
return Promise.reject({ job: currentJob, log })
|
||||
}
|
||||
|
||||
let resultLink
|
||||
|
||||
if (expectWebout) {
|
||||
resultLink = `/compute/sessions/${executionSessionId}/filerefs/_webout/content`
|
||||
} else {
|
||||
return { job: currentJob, log }
|
||||
}
|
||||
|
||||
if (resultLink) {
|
||||
@@ -563,16 +461,18 @@ export class SASViyaApiClient {
|
||||
{
|
||||
headers
|
||||
}
|
||||
).then((res: any) =>
|
||||
res.result.items.map((i: any) => i.line).join('\n')
|
||||
)
|
||||
|
||||
return Promise.reject(
|
||||
new ErrorResponse('Job execution failed', {
|
||||
status: 500,
|
||||
body: log
|
||||
.then((res: any) =>
|
||||
res.result.items.map((i: any) => i.line).join('\n')
|
||||
)
|
||||
.catch((err) => {
|
||||
throw err
|
||||
})
|
||||
)
|
||||
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
log: log
|
||||
})
|
||||
}
|
||||
}
|
||||
return {
|
||||
@@ -581,7 +481,11 @@ export class SASViyaApiClient {
|
||||
})
|
||||
}
|
||||
|
||||
await this.sessionManager.clearSession(executionSessionId, accessToken)
|
||||
await this.sessionManager
|
||||
.clearSession(executionSessionId, accessToken)
|
||||
.catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
return { result: jobResult?.result, log }
|
||||
} catch (e) {
|
||||
@@ -591,9 +495,10 @@ export class SASViyaApiClient {
|
||||
linesOfCode,
|
||||
contextName,
|
||||
accessToken,
|
||||
silent,
|
||||
data,
|
||||
debug
|
||||
debug,
|
||||
false,
|
||||
true
|
||||
)
|
||||
} else {
|
||||
throw e
|
||||
@@ -625,8 +530,6 @@ export class SASViyaApiClient {
|
||||
if (!parentFolderUri && parentFolderPath) {
|
||||
parentFolderUri = await this.getFolderUri(parentFolderPath, accessToken)
|
||||
if (!parentFolderUri) {
|
||||
if (isForced) this.isForceDeploy = true
|
||||
|
||||
console.log(
|
||||
`Parent folder at path '${parentFolderPath}' is not present.`
|
||||
)
|
||||
@@ -652,37 +555,16 @@ export class SASViyaApiClient {
|
||||
`Parent folder '${newFolderName}' has been successfully created.`
|
||||
)
|
||||
parentFolderUri = `/folders/folders/${parentFolder.id}`
|
||||
} else if (isForced && accessToken && !this.isForceDeploy) {
|
||||
this.isForceDeploy = true
|
||||
} else if (isForced && accessToken) {
|
||||
const folderPath = parentFolderPath + '/' + folderName
|
||||
const folderUri = await this.getFolderUri(folderPath, accessToken)
|
||||
|
||||
await this.deleteFolder(parentFolderPath, accessToken)
|
||||
|
||||
const newParentFolderPath = parentFolderPath.substring(
|
||||
0,
|
||||
parentFolderPath.lastIndexOf('/')
|
||||
)
|
||||
const newFolderName = `${parentFolderPath.split('/').pop()}`
|
||||
|
||||
if (newParentFolderPath === '') {
|
||||
throw new Error(`Root folder has to be present on the server.`)
|
||||
if (folderUri) {
|
||||
await this.deleteFolder(
|
||||
parentFolderPath + '/' + folderName,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'`
|
||||
)
|
||||
|
||||
const parentFolder = await this.createFolder(
|
||||
newFolderName,
|
||||
newParentFolderPath,
|
||||
undefined,
|
||||
accessToken
|
||||
)
|
||||
|
||||
console.log(
|
||||
`Parent folder '${newFolderName}' has been successfully created.`
|
||||
)
|
||||
|
||||
parentFolderUri = `/folders/folders/${parentFolder.id}`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -929,13 +811,21 @@ export class SASViyaApiClient {
|
||||
* @param debug - sets the _debug flag in the job arguments.
|
||||
* @param data - any data to be passed in as input to the job.
|
||||
* @param accessToken - an optional access token for an authorized user.
|
||||
* @param waitForResult - a boolean indicating if the function should wait for a result.
|
||||
* @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 printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||
*/
|
||||
public async executeComputeJob(
|
||||
sasJob: string,
|
||||
contextName: string,
|
||||
debug: boolean,
|
||||
debug?: boolean,
|
||||
data?: any,
|
||||
accessToken?: string
|
||||
accessToken?: string,
|
||||
waitForResult = true,
|
||||
expectWebout = false,
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false
|
||||
) {
|
||||
if (isRelativePath(sasJob) && !this.rootFolderName) {
|
||||
throw new Error(
|
||||
@@ -943,23 +833,20 @@ export class SASViyaApiClient {
|
||||
)
|
||||
}
|
||||
|
||||
if (isRelativePath(sasJob)) {
|
||||
const folderName = sasJob.split('/')[0]
|
||||
await this.populateFolderMap(
|
||||
`${this.rootFolderName}/${folderName}`,
|
||||
accessToken
|
||||
)
|
||||
const folderPathParts = sasJob.split('/')
|
||||
const jobName = folderPathParts.pop()
|
||||
const folderPath = folderPathParts.join('/')
|
||||
const fullFolderPath = isRelativePath(sasJob)
|
||||
? `${this.rootFolderName}/${folderPath}`
|
||||
: folderPath
|
||||
|
||||
if (!this.folderMap.get(`${this.rootFolderName}/${folderName}`)) {
|
||||
throw new Error(
|
||||
`The folder '${folderName}' was not found at '${this.serverUrl}/${this.rootFolderName}'`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
const folderPathParts = sasJob.split('/')
|
||||
folderPathParts.pop()
|
||||
const folderPath = folderPathParts.join('/')
|
||||
await this.populateFolderMap(folderPath, accessToken)
|
||||
await this.populateFolderMap(fullFolderPath, accessToken)
|
||||
|
||||
const jobFolder = this.folderMap.get(fullFolderPath)
|
||||
if (!jobFolder) {
|
||||
throw new Error(
|
||||
`The folder '${fullFolderPath}' was not found on '${this.serverUrl}'`
|
||||
)
|
||||
}
|
||||
|
||||
const headers: any = { 'Content-Type': 'application/json' }
|
||||
@@ -967,21 +854,7 @@ export class SASViyaApiClient {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
let jobToExecute
|
||||
if (isRelativePath(sasJob)) {
|
||||
const folderName = sasJob.split('/')[0]
|
||||
const jobName = sasJob.split('/')[1]
|
||||
const jobFolder = this.folderMap.get(
|
||||
`${this.rootFolderName}/${folderName}`
|
||||
)
|
||||
jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
||||
} else {
|
||||
const folderPathParts = sasJob.split('/')
|
||||
const jobName = folderPathParts.pop()
|
||||
const folderPath = folderPathParts.join('/')
|
||||
const jobFolder = this.folderMap.get(folderPath)
|
||||
jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
||||
}
|
||||
const jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
||||
|
||||
if (!jobToExecute) {
|
||||
throw new Error(`Job was not found.`)
|
||||
@@ -1009,16 +882,20 @@ export class SASViyaApiClient {
|
||||
jobToExecute.code = code
|
||||
}
|
||||
|
||||
if (!code) code = ''
|
||||
|
||||
const linesToExecute = code.replace(/\r\n/g, '\n').split('\n')
|
||||
return await this.executeScript(
|
||||
sasJob,
|
||||
linesToExecute,
|
||||
contextName,
|
||||
accessToken,
|
||||
true,
|
||||
data,
|
||||
debug,
|
||||
true
|
||||
expectWebout,
|
||||
waitForResult,
|
||||
pollOptions,
|
||||
printPid
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1043,54 +920,30 @@ export class SASViyaApiClient {
|
||||
)
|
||||
}
|
||||
|
||||
if (isRelativePath(sasJob)) {
|
||||
const folderName = sasJob.split('/')[0]
|
||||
await this.populateFolderMap(
|
||||
`${this.rootFolderName}/${folderName}`,
|
||||
accessToken
|
||||
)
|
||||
const folderPathParts = sasJob.split('/')
|
||||
const jobName = folderPathParts.pop()
|
||||
const folderPath = folderPathParts.join('/')
|
||||
const fullFolderPath = isRelativePath(sasJob)
|
||||
? `${this.rootFolderName}/${folderPath}`
|
||||
: folderPath
|
||||
await this.populateFolderMap(fullFolderPath, accessToken)
|
||||
|
||||
if (!this.folderMap.get(`${this.rootFolderName}/${folderName}`)) {
|
||||
throw new Error(
|
||||
`The folder '${folderName}' was not found at '${this.serverUrl}/${this.rootFolderName}'.`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
const folderPathParts = sasJob.split('/')
|
||||
folderPathParts.pop()
|
||||
const folderPath = folderPathParts.join('/')
|
||||
await this.populateFolderMap(folderPath, accessToken)
|
||||
if (!this.folderMap.get(folderPath)) {
|
||||
throw new Error(
|
||||
`The folder '${folderPath}' was not found at '${this.serverUrl}'.`
|
||||
)
|
||||
}
|
||||
const jobFolder = this.folderMap.get(fullFolderPath)
|
||||
if (!jobFolder) {
|
||||
throw new Error(
|
||||
`The folder '${fullFolderPath}' was not found on '${this.serverUrl}'.`
|
||||
)
|
||||
}
|
||||
|
||||
const jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
||||
|
||||
let files: any[] = []
|
||||
if (data && Object.keys(data).length) {
|
||||
files = await this.uploadTables(data, accessToken)
|
||||
}
|
||||
|
||||
let jobToExecute: Job | undefined
|
||||
let jobName: string | undefined
|
||||
let jobPath: string | undefined
|
||||
if (isRelativePath(sasJob)) {
|
||||
const folderName = sasJob.split('/')[0]
|
||||
jobName = sasJob.split('/')[1]
|
||||
jobPath = `${this.rootFolderName}/${folderName}`
|
||||
const jobFolder = this.folderMap.get(jobPath)
|
||||
jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
||||
} else {
|
||||
const folderPathParts = sasJob.split('/')
|
||||
jobName = folderPathParts.pop()
|
||||
jobPath = folderPathParts.join('/')
|
||||
const jobFolder = this.folderMap.get(jobPath)
|
||||
jobToExecute = jobFolder?.find((item) => item.name === jobName)
|
||||
}
|
||||
|
||||
if (!jobToExecute) {
|
||||
throw new Error(`The job ${sasJob} was not found.`)
|
||||
throw new Error(`Job was not found.`)
|
||||
}
|
||||
const jobDefinitionLink = jobToExecute?.links.find(
|
||||
(l) => l.rel === 'getResource'
|
||||
@@ -1113,7 +966,7 @@ export class SASViyaApiClient {
|
||||
|
||||
const jobArguments: { [key: string]: any } = {
|
||||
_contextName: contextName,
|
||||
_program: `${jobPath}/${jobName}`,
|
||||
_program: `${fullFolderPath}/${jobName}`,
|
||||
_webin_file_count: files.length,
|
||||
_OMITJSONLISTING: true,
|
||||
_OMITJSONLOG: true,
|
||||
@@ -1149,12 +1002,7 @@ export class SASViyaApiClient {
|
||||
`${this.serverUrl}/jobExecution/jobs?_action=wait`,
|
||||
postJobRequest
|
||||
)
|
||||
const jobStatus = await this.pollJobState(
|
||||
postedJob,
|
||||
etag,
|
||||
accessToken,
|
||||
true
|
||||
)
|
||||
const jobStatus = await this.pollJobState(postedJob, etag, accessToken)
|
||||
const { result: currentJob } = await this.request<Job>(
|
||||
`${this.serverUrl}/jobExecution/jobs/${postedJob.id}`,
|
||||
{ headers }
|
||||
@@ -1208,7 +1056,7 @@ export class SASViyaApiClient {
|
||||
throw new Error(`The path ${path} does not exist on ${this.serverUrl}`)
|
||||
}
|
||||
const { result: members } = await this.request<{ items: any[] }>(
|
||||
`${this.serverUrl}/folders/folders/${folder.id}/members`,
|
||||
`${this.serverUrl}/folders/folders/${folder.id}/members?limit=${folder.memberCount}`,
|
||||
requestInfo
|
||||
)
|
||||
|
||||
@@ -1216,14 +1064,21 @@ export class SASViyaApiClient {
|
||||
this.folderMap.set(path, itemsAtRoot)
|
||||
}
|
||||
|
||||
// REFACTOR: set default value for 'pollOptions' attribute
|
||||
private async pollJobState(
|
||||
postedJob: any,
|
||||
etag: string | null,
|
||||
accessToken?: string,
|
||||
silent = false
|
||||
pollOptions?: PollOptions
|
||||
) {
|
||||
const MAX_POLL_COUNT = 1000
|
||||
const POLL_INTERVAL = 100
|
||||
let POLL_INTERVAL = 100
|
||||
let MAX_POLL_COUNT = 1000
|
||||
|
||||
if (pollOptions) {
|
||||
POLL_INTERVAL = pollOptions.POLL_INTERVAL || POLL_INTERVAL
|
||||
MAX_POLL_COUNT = pollOptions.MAX_POLL_COUNT || MAX_POLL_COUNT
|
||||
}
|
||||
|
||||
let postedJobState = ''
|
||||
let pollCount = 0
|
||||
const headers: any = {
|
||||
@@ -1252,6 +1107,8 @@ export class SASViyaApiClient {
|
||||
}
|
||||
|
||||
return new Promise(async (resolve, _) => {
|
||||
let printedState = ''
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
if (
|
||||
postedJobState === 'running' ||
|
||||
@@ -1259,9 +1116,6 @@ export class SASViyaApiClient {
|
||||
postedJobState === 'pending'
|
||||
) {
|
||||
if (stateLink) {
|
||||
if (!silent) {
|
||||
console.log('Polling job status... \n')
|
||||
}
|
||||
const { result: jobState } = await this.request<string>(
|
||||
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
|
||||
{
|
||||
@@ -1271,10 +1125,16 @@ export class SASViyaApiClient {
|
||||
)
|
||||
|
||||
postedJobState = jobState.trim()
|
||||
if (!silent) {
|
||||
console.log(`Current state: ${postedJobState}\n`)
|
||||
|
||||
if (this.debug && printedState !== postedJobState) {
|
||||
console.log('Polling job status...')
|
||||
console.log(`Current job state: ${postedJobState}`)
|
||||
|
||||
printedState = postedJobState
|
||||
}
|
||||
|
||||
pollCount++
|
||||
|
||||
if (pollCount >= MAX_POLL_COUNT) {
|
||||
resolve(postedJobState)
|
||||
}
|
||||
@@ -1287,49 +1147,6 @@ export class SASViyaApiClient {
|
||||
})
|
||||
}
|
||||
|
||||
private async waitForSession(
|
||||
session: Session,
|
||||
etag: string | null,
|
||||
accessToken?: string,
|
||||
silent = false
|
||||
) {
|
||||
let sessionState = session.state
|
||||
let pollCount = 0
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json',
|
||||
'If-None-Match': etag
|
||||
}
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
const stateLink = session.links.find((l: any) => l.rel === 'state')
|
||||
return new Promise(async (resolve, _) => {
|
||||
if (sessionState === 'pending') {
|
||||
if (stateLink) {
|
||||
if (!silent) {
|
||||
console.log('Polling session status... \n')
|
||||
}
|
||||
const { result: state } = await this.request<string>(
|
||||
`${this.serverUrl}${stateLink.href}?wait=30`,
|
||||
{
|
||||
headers
|
||||
},
|
||||
'text'
|
||||
)
|
||||
|
||||
sessionState = state.trim()
|
||||
if (!silent) {
|
||||
console.log(`Current state: ${sessionState}\n`)
|
||||
}
|
||||
pollCount++
|
||||
resolve(sessionState)
|
||||
}
|
||||
} else {
|
||||
resolve(sessionState)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private async uploadTables(data: any, accessToken?: string) {
|
||||
const uploadedFiles = []
|
||||
const headers: any = {
|
||||
@@ -1416,26 +1233,10 @@ export class SASViyaApiClient {
|
||||
contextName: string,
|
||||
accessToken?: string
|
||||
): Promise<Context> {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: contexts } = await this.request<{ items: Context[] }>(
|
||||
`${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`,
|
||||
{ headers }
|
||||
return await this.contextManager.getComputeContextByName(
|
||||
contextName,
|
||||
accessToken
|
||||
)
|
||||
|
||||
if (!contexts || !(contexts.items && contexts.items.length)) {
|
||||
throw new Error(
|
||||
`The context '${contextName}' was not found at '${this.serverUrl}'.`
|
||||
)
|
||||
}
|
||||
|
||||
return contexts.items[0]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1447,22 +1248,10 @@ export class SASViyaApiClient {
|
||||
contextId: string,
|
||||
accessToken?: string
|
||||
): Promise<ContextAllAttributes> {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
|
||||
const { result: context } = await this.request<ContextAllAttributes>(
|
||||
`${this.serverUrl}/compute/contexts/${contextId}`,
|
||||
{ headers }
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
return context
|
||||
return await this.contextManager.getComputeContextById(
|
||||
contextId,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1508,6 +1297,16 @@ export class SASViyaApiClient {
|
||||
`${this.serverUrl}${url}`,
|
||||
requestInfo
|
||||
).catch((err) => {
|
||||
if (err.code && err.code === 'ENOTFOUND') {
|
||||
const notFoundError = {
|
||||
body: JSON.stringify({
|
||||
message: `Folder '${sourceFolder.split('/').pop()}' was not found.`
|
||||
})
|
||||
}
|
||||
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
|
||||
|
||||
348
src/SASjs.ts
348
src/SASjs.ts
@@ -32,7 +32,8 @@ import {
|
||||
CsrfToken,
|
||||
UploadFile,
|
||||
EditContextInput,
|
||||
ErrorResponse
|
||||
ErrorResponse,
|
||||
PollOptions
|
||||
} from './types'
|
||||
import { SASViyaApiClient } from './SASViyaApiClient'
|
||||
import { SAS9ApiClient } from './SAS9ApiClient'
|
||||
@@ -44,7 +45,7 @@ const defaultConfig: SASjsConfig = {
|
||||
pathSASViya: '/SASJobExecution',
|
||||
appLoc: '/Public/seedapp',
|
||||
serverType: ServerType.SASViya,
|
||||
debug: true,
|
||||
debug: false,
|
||||
contextName: 'SAS Job Execution compute context',
|
||||
useComputeApi: false
|
||||
}
|
||||
@@ -95,12 +96,39 @@ export default class SASjs {
|
||||
)
|
||||
}
|
||||
|
||||
public async getAllContexts(accessToken: string) {
|
||||
this.isMethodSupported('getAllContexts', ServerType.SASViya)
|
||||
/**
|
||||
* Gets compute contexts.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async getComputeContexts(accessToken: string) {
|
||||
this.isMethodSupported('getComputeContexts', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient!.getAllContexts(accessToken)
|
||||
return await this.sasViyaApiClient!.getComputeContexts(accessToken)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets launcher contexts.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async getLauncherContexts(accessToken: string) {
|
||||
this.isMethodSupported('getLauncherContexts', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient!.getLauncherContexts(accessToken)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets default(system) launcher contexts.
|
||||
*/
|
||||
public getDefaultComputeContexts() {
|
||||
this.isMethodSupported('getDefaultComputeContexts', ServerType.SASViya)
|
||||
|
||||
return this.sasViyaApiClient!.getDefaultComputeContexts()
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets executable compute contexts.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async getExecutableContexts(accessToken: string) {
|
||||
this.isMethodSupported('getExecutableContexts', ServerType.SASViya)
|
||||
|
||||
@@ -116,7 +144,7 @@ export default class SASjs {
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
* @param authorizedUsers - an optional list of authorized user IDs.
|
||||
*/
|
||||
public async createContext(
|
||||
public async createComputeContext(
|
||||
contextName: string,
|
||||
launchContextName: string,
|
||||
sharedAccountId: string,
|
||||
@@ -124,9 +152,9 @@ export default class SASjs {
|
||||
accessToken: string,
|
||||
authorizedUsers?: string[]
|
||||
) {
|
||||
this.isMethodSupported('createContext', ServerType.SASViya)
|
||||
this.isMethodSupported('createComputeContext', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient!.createContext(
|
||||
return await this.sasViyaApiClient!.createComputeContext(
|
||||
contextName,
|
||||
launchContextName,
|
||||
sharedAccountId,
|
||||
@@ -136,20 +164,43 @@ export default class SASjs {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a launcher context on the given server.
|
||||
* @param contextName - the name of the context to be created.
|
||||
* @param description - the description of the context to be created.
|
||||
* @param launchType - launch type of the context to be created.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async createLauncherContext(
|
||||
contextName: string,
|
||||
description: string,
|
||||
launchType: string,
|
||||
accessToken: string
|
||||
) {
|
||||
this.isMethodSupported('createLauncherContext', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient!.createLauncherContext(
|
||||
contextName,
|
||||
description,
|
||||
launchType,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a compute context on the given server.
|
||||
* @param contextName - the original name of the context to be deleted.
|
||||
* @param editedContext - an object with the properties to be updated.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async editContext(
|
||||
public async editComputeContext(
|
||||
contextName: string,
|
||||
editedContext: EditContextInput,
|
||||
accessToken?: string
|
||||
) {
|
||||
this.isMethodSupported('editContext', ServerType.SASViya)
|
||||
this.isMethodSupported('editComputeContext', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient!.editContext(
|
||||
return await this.sasViyaApiClient!.editComputeContext(
|
||||
contextName,
|
||||
editedContext,
|
||||
accessToken
|
||||
@@ -161,10 +212,13 @@ export default class SASjs {
|
||||
* @param contextName - the name of the context to be deleted.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public async deleteContext(contextName: string, accessToken?: string) {
|
||||
this.isMethodSupported('deleteContext', ServerType.SASViya)
|
||||
public async deleteComputeContext(contextName: string, accessToken?: string) {
|
||||
this.isMethodSupported('deleteComputeContext', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient!.deleteContext(contextName, accessToken)
|
||||
return await this.sasViyaApiClient!.deleteComputeContext(
|
||||
contextName,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,13 +259,20 @@ export default class SASjs {
|
||||
return await this.sasViyaApiClient!.createSession(contextName, accessToken)
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the sas code against given sas server
|
||||
* @param fileName - name of the file to run. It will be converted to path to the file being submitted for execution.
|
||||
* @param linesOfCode - lines of sas code from the file to run.
|
||||
* @param contextName - context name on which code will be run on the server.
|
||||
* @param accessToken - (optional) the access token for authorizing the request.
|
||||
* @param debug - (optional) if true, global debug config will be overriden
|
||||
*/
|
||||
public async executeScriptSASViya(
|
||||
fileName: string,
|
||||
linesOfCode: string[],
|
||||
contextName: string,
|
||||
accessToken?: string,
|
||||
sessionId = '',
|
||||
silent = false
|
||||
debug?: boolean
|
||||
) {
|
||||
this.isMethodSupported('executeScriptSASViya', ServerType.SASViya)
|
||||
|
||||
@@ -220,9 +281,8 @@ export default class SASjs {
|
||||
linesOfCode,
|
||||
contextName,
|
||||
accessToken,
|
||||
silent,
|
||||
null,
|
||||
this.sasjsConfig.debug
|
||||
debug ? debug : this.sasjsConfig.debug
|
||||
)
|
||||
}
|
||||
|
||||
@@ -243,8 +303,6 @@ export default class SASjs {
|
||||
sasApiClient?: SASViyaApiClient,
|
||||
isForced?: boolean
|
||||
) {
|
||||
this.isMethodSupported('createFolder', ServerType.SASViya)
|
||||
|
||||
if (sasApiClient)
|
||||
return await sasApiClient.createFolder(
|
||||
folderName,
|
||||
@@ -261,6 +319,40 @@ export default class SASjs {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* For performance (and in case of accidental error) the `deleteFolder` function does not actually delete the folder (and all its content and subfolder content). Instead the folder is simply moved to the recycle bin. Deletion time will be added to the folder name.
|
||||
* @param folderPath - the full path (eg `/Public/example/deleteThis`) of the folder to be deleted.
|
||||
* @param accessToken - an access token for authorizing the request.
|
||||
*/
|
||||
public async deleteFolder(folderPath: string, accessToken: string) {
|
||||
this.isMethodSupported('deleteFolder', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient?.deleteFolder(folderPath, accessToken)
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves folder to a new location. The folder may be renamed at the same time.
|
||||
* @param sourceFolder - the full path (eg `/Public/example/myFolder`) or URI of the source folder to be moved. Providing URI instead of path will save one extra request.
|
||||
* @param targetParentFolder - the full path or URI of the _parent_ folder to which the `sourceFolder` will be moved (eg `/Public/newDestination`). To move a folder, a user has to have write permissions in targetParentFolder. Providing URI instead of path will save one extra request.
|
||||
* @param targetFolderName - the name of the "moved" folder. If left blank, the original folder name will be used (eg `myFolder` in `/Public/newDestination/myFolder` for the example above). Optional field.
|
||||
* @param accessToken - an access token for authorizing the request.
|
||||
*/
|
||||
public async moveFolder(
|
||||
sourceFolder: string,
|
||||
targetParentFolder: string,
|
||||
targetFolderName: string,
|
||||
accessToken: string
|
||||
) {
|
||||
this.isMethodSupported('moveFolder', ServerType.SASViya)
|
||||
|
||||
return await this.sasViyaApiClient?.moveFolder(
|
||||
sourceFolder,
|
||||
targetParentFolder,
|
||||
targetFolderName,
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
|
||||
public async createJobDefinition(
|
||||
jobName: string,
|
||||
code: string,
|
||||
@@ -378,6 +470,32 @@ export default class SASjs {
|
||||
*/
|
||||
public setDebugState(value: boolean) {
|
||||
this.sasjsConfig.debug = value
|
||||
if (this.sasViyaApiClient) {
|
||||
this.sasViyaApiClient.debug = value
|
||||
}
|
||||
}
|
||||
|
||||
private async getLoginForm(response: any) {
|
||||
const pattern: RegExp = /<form.+action="(.*Logon[^"]*).*>/
|
||||
const matches = pattern.exec(response)
|
||||
const formInputs: any = {}
|
||||
|
||||
if (matches && matches.length) {
|
||||
this.setLoginUrl(matches)
|
||||
const inputs = response.match(/<input.*"hidden"[^>]*>/g)
|
||||
|
||||
if (inputs) {
|
||||
inputs.forEach((inputStr: string) => {
|
||||
const valueMatch = inputStr.match(/name="([^"]*)"\svalue="([^"]*)/)
|
||||
|
||||
if (valueMatch && valueMatch.length) {
|
||||
formInputs[valueMatch[1]] = valueMatch[2]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return Object.keys(formInputs).length ? formInputs : null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -388,10 +506,16 @@ export default class SASjs {
|
||||
const loginResponse = await fetch(this.loginUrl.replace('.do', ''))
|
||||
const responseText = await loginResponse.text()
|
||||
const isLoggedIn = /<button.+onClick.+logout/gm.test(responseText)
|
||||
let loginForm: any = null
|
||||
|
||||
if (!isLoggedIn) {
|
||||
loginForm = await this.getLoginForm(responseText)
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
isLoggedIn,
|
||||
userName: this.userName
|
||||
userName: this.userName,
|
||||
loginForm
|
||||
})
|
||||
}
|
||||
|
||||
@@ -409,7 +533,7 @@ export default class SASjs {
|
||||
|
||||
this.userName = loginParams.username
|
||||
|
||||
const { isLoggedIn } = await this.checkSession()
|
||||
const { isLoggedIn, loginForm } = await this.checkSession()
|
||||
if (isLoggedIn) {
|
||||
this.resendWaitingRequests()
|
||||
|
||||
@@ -419,15 +543,13 @@ export default class SASjs {
|
||||
})
|
||||
}
|
||||
|
||||
const loginForm = await this.getLoginForm()
|
||||
|
||||
for (const key in loginForm) {
|
||||
loginParams[key] = loginForm[key]
|
||||
}
|
||||
const loginParamsStr = serialize(loginParams)
|
||||
|
||||
return fetch(this.loginUrl, {
|
||||
method: 'post',
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
referrerPolicy: 'same-origin',
|
||||
body: loginParamsStr,
|
||||
@@ -603,6 +725,7 @@ export default class SASjs {
|
||||
this.sasjsConfig.contextName,
|
||||
this.setCsrfTokenApi
|
||||
)
|
||||
sasApiClient.debug = this.sasjsConfig.debug
|
||||
} else if (this.sasjsConfig.serverType === ServerType.SAS9) {
|
||||
sasApiClient = new SAS9ApiClient(serverUrl)
|
||||
}
|
||||
@@ -624,10 +747,7 @@ export default class SASjs {
|
||||
)
|
||||
}
|
||||
|
||||
const members =
|
||||
serviceJson.members[0].name === 'services'
|
||||
? serviceJson.members[0].members
|
||||
: serviceJson.members
|
||||
const members = serviceJson.members
|
||||
|
||||
await this.createFoldersAndServices(
|
||||
appLoc,
|
||||
@@ -638,6 +758,57 @@ export default class SASjs {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Kicks off execution of the given job via the compute API.
|
||||
* @returns an object representing the compute session created for the given job.
|
||||
* @param sasJob - the path to the SAS program (ultimately resolves to
|
||||
* the SAS `_program` parameter to run a Job Definition or SAS 9 Stored
|
||||
* Process). Is prepended at runtime with the value of `appLoc`.
|
||||
* @param data - a JSON object containing one or more tables to be sent to
|
||||
* SAS. Can be `null` if no inputs required.
|
||||
* @param config - provide any changes to the config here, for instance to
|
||||
* enable/disable `debug`. Any change provided will override the global config,
|
||||
* for that particular function call.
|
||||
* @param accessToken - a valid access token that is authorised to execute compute jobs.
|
||||
* The access token is not required when the user is authenticated via the browser.
|
||||
* @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 printPid - a boolean that indicates whether the function should print (PID) of the started job.
|
||||
*/
|
||||
public async startComputeJob(
|
||||
sasJob: string,
|
||||
data: any,
|
||||
config: any = {},
|
||||
accessToken?: string,
|
||||
waitForResult?: boolean,
|
||||
pollOptions?: PollOptions,
|
||||
printPid = false
|
||||
) {
|
||||
config = {
|
||||
...this.sasjsConfig,
|
||||
...config
|
||||
}
|
||||
|
||||
this.isMethodSupported('startComputeJob', ServerType.SASViya)
|
||||
if (!config.contextName) {
|
||||
throw new Error(
|
||||
'Context name is undefined. Please set a `contextName` in your SASjs or override config.'
|
||||
)
|
||||
}
|
||||
|
||||
return this.sasViyaApiClient?.executeComputeJob(
|
||||
sasJob,
|
||||
config.contextName,
|
||||
config.debug,
|
||||
data,
|
||||
accessToken,
|
||||
!!waitForResult,
|
||||
false,
|
||||
pollOptions,
|
||||
printPid
|
||||
)
|
||||
}
|
||||
|
||||
private async executeJobViaComputeApi(
|
||||
sasJob: string,
|
||||
data: any,
|
||||
@@ -657,13 +828,17 @@ export default class SASjs {
|
||||
|
||||
sasjsWaitingRequest.requestPromise.promise = new Promise(
|
||||
async (resolve, reject) => {
|
||||
const waitForResult = true
|
||||
const expectWebout = true
|
||||
this.sasViyaApiClient
|
||||
?.executeComputeJob(
|
||||
sasJob,
|
||||
config.contextName,
|
||||
config.debug,
|
||||
data,
|
||||
accessToken
|
||||
accessToken,
|
||||
waitForResult,
|
||||
expectWebout
|
||||
)
|
||||
.then((response) => {
|
||||
if (!config.debug) {
|
||||
@@ -701,11 +876,23 @@ export default class SASjs {
|
||||
} else {
|
||||
this.retryCountComputeApi = 0
|
||||
reject(
|
||||
new ErrorResponse('Compute API retry requests limit reached')
|
||||
new ErrorResponse('Compute API retry requests limit reached.')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (response?.log) {
|
||||
this.appendSasjsRequest(response.log, sasJob, null)
|
||||
}
|
||||
|
||||
if (error.toString().includes('Job was not found')) {
|
||||
reject(
|
||||
new ErrorResponse('Service not found on the server.', {
|
||||
sasJob: sasJob
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
if (error && error.status === 401) {
|
||||
if (loginRequiredCallback) loginRequiredCallback(true)
|
||||
sasjsWaitingRequest.requestPromise.resolve = resolve
|
||||
@@ -713,10 +900,8 @@ export default class SASjs {
|
||||
sasjsWaitingRequest.config = config
|
||||
this.sasjsWaitingRequests.push(sasjsWaitingRequest)
|
||||
} else {
|
||||
reject(new ErrorResponse('Job execution failed', error))
|
||||
reject(new ErrorResponse('Job execution failed.', error))
|
||||
}
|
||||
|
||||
this.appendSasjsRequest(response.log, sasJob, null)
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -779,8 +964,8 @@ export default class SASjs {
|
||||
|
||||
return responseJson
|
||||
})
|
||||
.catch(async (e) => {
|
||||
if (needsRetry(JSON.stringify(e))) {
|
||||
.catch(async (response) => {
|
||||
if (needsRetry(JSON.stringify(response))) {
|
||||
if (this.retryCountJeseApi < requestRetryLimit) {
|
||||
let retryResponse = await this.executeJobViaJesApi(
|
||||
sasJob,
|
||||
@@ -796,12 +981,24 @@ export default class SASjs {
|
||||
} else {
|
||||
this.retryCountJeseApi = 0
|
||||
reject(
|
||||
new ErrorResponse('Jes API retry requests limit reached')
|
||||
new ErrorResponse('Jes API retry requests limit reached.')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
reject(new ErrorResponse('Job execution failed', e))
|
||||
if (response?.log) {
|
||||
this.appendSasjsRequest(response.log, sasJob, null)
|
||||
}
|
||||
|
||||
if (response.toString().includes('Job was not found')) {
|
||||
reject(
|
||||
new ErrorResponse('Service not found on the server.', {
|
||||
sasJob: sasJob
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
reject(new ErrorResponse('Job execution failed.', response))
|
||||
})
|
||||
)
|
||||
}
|
||||
@@ -985,7 +1182,7 @@ export default class SASjs {
|
||||
} else {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB execution failed',
|
||||
'Job WEB execution failed.',
|
||||
this.parseSAS9ErrorResponse(responseText)
|
||||
)
|
||||
)
|
||||
@@ -1003,7 +1200,7 @@ export default class SASjs {
|
||||
} catch (e) {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB debug response parsing failed',
|
||||
'Job WEB debug response parsing failed.',
|
||||
{ response: resText, exception: e }
|
||||
)
|
||||
)
|
||||
@@ -1012,7 +1209,7 @@ export default class SASjs {
|
||||
(err: any) => {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB debug response parsing failed',
|
||||
'Job WEB debug response parsing failed.',
|
||||
err
|
||||
)
|
||||
)
|
||||
@@ -1021,19 +1218,34 @@ export default class SASjs {
|
||||
} catch (e) {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Job WEB debug response parsing failed',
|
||||
'Job WEB debug response parsing failed.',
|
||||
{ response: responseText, exception: e }
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
this.updateUsername(responseText)
|
||||
if (
|
||||
responseText.includes(
|
||||
'The requested URL /SASStoredProcess/do/ was not found on this server.'
|
||||
) ||
|
||||
responseText.includes('Stored process not found')
|
||||
) {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Service not found on the server.',
|
||||
{ service: sasJob },
|
||||
responseText
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const parsedJson = JSON.parse(responseText)
|
||||
resolve(parsedJson)
|
||||
} catch (e) {
|
||||
reject(
|
||||
new ErrorResponse('Job WEB response parsing failed', {
|
||||
new ErrorResponse('Job WEB response parsing failed.', {
|
||||
response: responseText,
|
||||
exception: e
|
||||
})
|
||||
@@ -1044,7 +1256,7 @@ export default class SASjs {
|
||||
}
|
||||
})
|
||||
.catch((e: Error) => {
|
||||
reject(new ErrorResponse('Job WEB request failed', e))
|
||||
reject(new ErrorResponse('Job WEB request failed.', e))
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -1185,10 +1397,20 @@ export default class SASjs {
|
||||
}
|
||||
}
|
||||
|
||||
private fetchLogFileContent(logLink: string) {
|
||||
/**
|
||||
* Fetches content of the log file
|
||||
* @param logLink - url of the log file.
|
||||
* @param accessToken - an access token for an authorized user.
|
||||
*/
|
||||
public fetchLogFileContent(logLink: string, accessToken?: string) {
|
||||
const headers: any = { 'Content-Type': 'application/json' }
|
||||
|
||||
if (accessToken) headers.Authorization = 'Bearer ' + accessToken
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(logLink, {
|
||||
method: 'GET'
|
||||
method: 'GET',
|
||||
headers
|
||||
})
|
||||
.then((response: any) => response.text())
|
||||
.then((response: any) => resolve(response))
|
||||
@@ -1286,11 +1508,15 @@ export default class SASjs {
|
||||
this.sasjsConfig.serverUrl === undefined ||
|
||||
this.sasjsConfig.serverUrl === ''
|
||||
) {
|
||||
let url = `${location.protocol}//${location.hostname}`
|
||||
if (location.port) {
|
||||
url = `${url}:${location.port}`
|
||||
if (typeof location !== 'undefined') {
|
||||
let url = `${location.protocol}//${location.hostname}`
|
||||
|
||||
if (location.port) url = `${url}:${location.port}`
|
||||
|
||||
this.sasjsConfig.serverUrl = url
|
||||
} else {
|
||||
this.sasjsConfig.serverUrl = ''
|
||||
}
|
||||
this.sasjsConfig.serverUrl = url
|
||||
}
|
||||
|
||||
if (this.sasjsConfig.serverUrl.slice(-1) === '/') {
|
||||
@@ -1320,6 +1546,8 @@ export default class SASjs {
|
||||
this.sasjsConfig.contextName,
|
||||
this.setCsrfTokenApi
|
||||
)
|
||||
|
||||
this.sasViyaApiClient.debug = this.sasjsConfig.debug
|
||||
}
|
||||
if (this.sasjsConfig.serverType === ServerType.SAS9) {
|
||||
if (this.sas9ApiClient)
|
||||
@@ -1353,26 +1581,6 @@ export default class SASjs {
|
||||
}
|
||||
}
|
||||
|
||||
private async getLoginForm() {
|
||||
const pattern: RegExp = /<form.+action="(.*Logon[^"]*).*>/
|
||||
const response = await fetch(this.loginUrl).then((r) => r.text())
|
||||
const matches = pattern.exec(response)
|
||||
const formInputs: any = {}
|
||||
if (matches && matches.length) {
|
||||
this.setLoginUrl(matches)
|
||||
const inputs = response.match(/<input.*"hidden"[^>]*>/g)
|
||||
if (inputs) {
|
||||
inputs.forEach((inputStr: string) => {
|
||||
const valueMatch = inputStr.match(/name="([^"]*)"\svalue="([^"]*)/)
|
||||
if (valueMatch && valueMatch.length) {
|
||||
formInputs[valueMatch[1]] = valueMatch[2]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
return Object.keys(formInputs).length ? formInputs : null
|
||||
}
|
||||
|
||||
private async createFoldersAndServices(
|
||||
parentFolder: string,
|
||||
membersJson: any[],
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { Session, Context, CsrfToken } from './types'
|
||||
import { Session, Context, CsrfToken, SessionVariable } from './types'
|
||||
import { asyncForEach, makeRequest, isUrl } from './utils'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
|
||||
const MAX_SESSION_COUNT = 1
|
||||
const RETRY_LIMIT: number = 3
|
||||
let RETRY_COUNT: number = 0
|
||||
const INTERNAL_SAS_ERROR = {
|
||||
status: 304,
|
||||
message: 'Not Modified'
|
||||
}
|
||||
|
||||
export class SessionManager {
|
||||
constructor(
|
||||
@@ -15,22 +22,38 @@ export class SessionManager {
|
||||
private sessions: Session[] = []
|
||||
private currentContext: Context | null = null
|
||||
private csrfToken: CsrfToken | null = null
|
||||
private _debug: boolean = false
|
||||
private printedSessionState = {
|
||||
printed: false,
|
||||
state: ''
|
||||
}
|
||||
|
||||
public get debug() {
|
||||
return this._debug
|
||||
}
|
||||
|
||||
public set debug(value: boolean) {
|
||||
this._debug = value
|
||||
}
|
||||
|
||||
async getSession(accessToken?: string) {
|
||||
await this.createSessions(accessToken)
|
||||
this.createAndWaitForSession(accessToken)
|
||||
await this.createAndWaitForSession(accessToken)
|
||||
const session = this.sessions.pop()
|
||||
const secondsSinceSessionCreation =
|
||||
(new Date().getTime() - new Date(session!.creationTimeStamp).getTime()) /
|
||||
1000
|
||||
|
||||
if (
|
||||
!session!.attributes ||
|
||||
secondsSinceSessionCreation >= session!.attributes.sessionInactiveTimeout
|
||||
) {
|
||||
await this.createSessions(accessToken)
|
||||
const freshSession = this.sessions.pop()
|
||||
|
||||
return freshSession
|
||||
}
|
||||
|
||||
return session
|
||||
}
|
||||
|
||||
@@ -39,22 +62,37 @@ export class SessionManager {
|
||||
method: 'DELETE',
|
||||
headers: this.getHeaders(accessToken)
|
||||
}
|
||||
|
||||
return await this.request<Session>(
|
||||
`${this.serverUrl}/compute/sessions/${id}`,
|
||||
deleteSessionRequest
|
||||
).then(() => {
|
||||
this.sessions = this.sessions.filter((s) => s.id !== id)
|
||||
})
|
||||
)
|
||||
.then(() => {
|
||||
this.sessions = this.sessions.filter((s) => s.id !== id)
|
||||
})
|
||||
.catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
private async createSessions(accessToken?: string) {
|
||||
if (!this.sessions.length) {
|
||||
if (!this.currentContext) {
|
||||
await this.setCurrentContext(accessToken)
|
||||
await this.setCurrentContext(accessToken).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
await asyncForEach(new Array(MAX_SESSION_COUNT), async () => {
|
||||
const createdSession = await this.createAndWaitForSession(accessToken)
|
||||
const createdSession = await this.createAndWaitForSession(
|
||||
accessToken
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
this.sessions.push(createdSession)
|
||||
}).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -64,13 +102,18 @@ export class SessionManager {
|
||||
method: 'POST',
|
||||
headers: this.getHeaders(accessToken)
|
||||
}
|
||||
|
||||
const { result: createdSession, etag } = await this.request<Session>(
|
||||
`${this.serverUrl}/compute/contexts/${this.currentContext!.id}/sessions`,
|
||||
createSessionRequest
|
||||
)
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
await this.waitForSession(createdSession, etag, accessToken)
|
||||
|
||||
this.sessions.push(createdSession)
|
||||
|
||||
return createdSession
|
||||
}
|
||||
|
||||
@@ -80,6 +123,8 @@ export class SessionManager {
|
||||
items: Context[]
|
||||
}>(`${this.serverUrl}/compute/contexts?limit=10000`, {
|
||||
headers: this.getHeaders(accessToken)
|
||||
}).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
const contextsList =
|
||||
@@ -98,6 +143,8 @@ export class SessionManager {
|
||||
}
|
||||
|
||||
this.currentContext = currentContext
|
||||
|
||||
Promise.resolve()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +152,7 @@ export class SessionManager {
|
||||
const headers: any = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
if (accessToken) {
|
||||
headers.Authorization = `Bearer ${accessToken}`
|
||||
}
|
||||
@@ -115,8 +163,7 @@ export class SessionManager {
|
||||
private async waitForSession(
|
||||
session: Session,
|
||||
etag: string | null,
|
||||
accessToken?: string,
|
||||
silent = false
|
||||
accessToken?: string
|
||||
) {
|
||||
let sessionState = session.state
|
||||
const headers: any = {
|
||||
@@ -124,24 +171,50 @@ export class SessionManager {
|
||||
'If-None-Match': etag
|
||||
}
|
||||
const stateLink = session.links.find((l: any) => l.rel === 'state')
|
||||
|
||||
return new Promise(async (resolve, _) => {
|
||||
if (sessionState === 'pending') {
|
||||
if (
|
||||
sessionState === 'pending' ||
|
||||
sessionState === 'running' ||
|
||||
sessionState === ''
|
||||
) {
|
||||
if (stateLink) {
|
||||
if (!silent) {
|
||||
console.log('Polling session status... \n') // ?
|
||||
if (this.debug && !this.printedSessionState.printed) {
|
||||
console.log('Polling session status...')
|
||||
|
||||
this.printedSessionState.printed = true
|
||||
}
|
||||
const { result: state } = await this.request<string>(
|
||||
|
||||
const { result: state } = await this.requestSessionStatus<string>(
|
||||
`${this.serverUrl}${stateLink.href}?wait=30`,
|
||||
{
|
||||
headers
|
||||
},
|
||||
'text'
|
||||
)
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
|
||||
sessionState = state.trim()
|
||||
if (!silent) {
|
||||
console.log(`Current state is '${sessionState}'\n`)
|
||||
|
||||
if (this.debug && this.printedSessionState.state !== sessionState) {
|
||||
console.log(`Current session state is '${sessionState}'`)
|
||||
|
||||
this.printedSessionState.state = sessionState
|
||||
this.printedSessionState.printed = false
|
||||
}
|
||||
|
||||
// There is an internal error present in SAS Viya 3.5
|
||||
// Retry to wait for a session status in such case of SAS internal error
|
||||
if (
|
||||
sessionState === INTERNAL_SAS_ERROR.message &&
|
||||
RETRY_COUNT < RETRY_LIMIT
|
||||
) {
|
||||
RETRY_COUNT++
|
||||
|
||||
resolve(this.waitForSession(session, etag, accessToken))
|
||||
}
|
||||
|
||||
resolve(sessionState)
|
||||
}
|
||||
} else {
|
||||
@@ -161,6 +234,7 @@ export class SessionManager {
|
||||
[this.csrfToken.headerName]: this.csrfToken.value
|
||||
}
|
||||
}
|
||||
|
||||
return await makeRequest<T>(
|
||||
url,
|
||||
options,
|
||||
@@ -169,6 +243,53 @@ export class SessionManager {
|
||||
this.setCsrfToken(token)
|
||||
},
|
||||
contentType
|
||||
)
|
||||
).catch((err) => {
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
private async requestSessionStatus<T>(
|
||||
url: string,
|
||||
options: RequestInit,
|
||||
contentType: 'text' | 'json' = 'json'
|
||||
) {
|
||||
if (this.csrfToken) {
|
||||
options.headers = {
|
||||
...options.headers,
|
||||
[this.csrfToken.headerName]: this.csrfToken.value
|
||||
}
|
||||
}
|
||||
|
||||
return await makeRequest<T>(
|
||||
url,
|
||||
options,
|
||||
(token) => {
|
||||
this.csrfToken = token
|
||||
this.setCsrfToken(token)
|
||||
},
|
||||
contentType
|
||||
).catch((err) => {
|
||||
if (err.status === INTERNAL_SAS_ERROR.status)
|
||||
return { result: INTERNAL_SAS_ERROR.message }
|
||||
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
async getVariable(sessionId: string, variable: string, accessToken?: string) {
|
||||
const getSessionVariable = {
|
||||
method: 'GET',
|
||||
headers: this.getHeaders(accessToken)
|
||||
}
|
||||
|
||||
return await this.request<SessionVariable>(
|
||||
`${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`,
|
||||
getSessionVariable
|
||||
).catch((err) => {
|
||||
throw prefixMessage(
|
||||
err,
|
||||
`Error while fetching session variable '${variable}'.`
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
585
src/test/ContextManager.spec.ts
Normal file
585
src/test/ContextManager.spec.ts
Normal file
@@ -0,0 +1,585 @@
|
||||
import { ContextManager } from '../ContextManager'
|
||||
|
||||
describe('ContextManager', () => {
|
||||
let originalFetch: any
|
||||
let fetchCallNumber = 0
|
||||
|
||||
const fakeGlobalFetch = (fakeResponses: object[]) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() => {
|
||||
const fakeResponse = fakeResponses[fetchCallNumber]
|
||||
|
||||
if (
|
||||
fetchCallNumber !== fakeResponses.length &&
|
||||
fakeResponses.length > 1
|
||||
) {
|
||||
if (fetchCallNumber + 1 === fakeResponses.length) fetchCallNumber = 0
|
||||
else fetchCallNumber += 1
|
||||
} else {
|
||||
fetchCallNumber = 0
|
||||
}
|
||||
|
||||
return Promise.resolve({
|
||||
ok: true,
|
||||
headers: { get: () => '' },
|
||||
json: () => Promise.resolve(fakeResponse)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const contextManager = new ContextManager(
|
||||
process.env.SERVER_URL as string,
|
||||
() => {}
|
||||
)
|
||||
|
||||
const defaultComputeContexts = contextManager.getDefaultComputeContexts
|
||||
const defaultLauncherContexts = contextManager.getDefaultLauncherContexts
|
||||
|
||||
const getRandomDefaultComputeContext = () =>
|
||||
defaultComputeContexts[
|
||||
Math.floor(Math.random() * defaultComputeContexts.length)
|
||||
]
|
||||
const getRandomDefaultLauncherContext = () =>
|
||||
defaultLauncherContexts[
|
||||
Math.floor(Math.random() * defaultLauncherContexts.length)
|
||||
]
|
||||
|
||||
beforeAll(() => {
|
||||
originalFetch = (global as any).fetch
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
;(global as any).fetch = originalFetch
|
||||
})
|
||||
|
||||
describe('getComputeContexts', () => {
|
||||
it('should fetch compute contexts', async () => {
|
||||
const sampleComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Fake Compute Context',
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponse = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
|
||||
await expect(contextManager.getComputeContexts()).resolves.toEqual([
|
||||
sampleComputeContext
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('getLauncherContexts', () => {
|
||||
it('should fetch launcher contexts', async () => {
|
||||
const sampleComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Fake Launcher Context',
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponse = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
|
||||
await expect(contextManager.getLauncherContexts()).resolves.toEqual([
|
||||
sampleComputeContext
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('createComputeContext', () => {
|
||||
it('should throw an error if context name was not provided', async () => {
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
'',
|
||||
'Test Launcher Context',
|
||||
'fakeAccountId',
|
||||
[]
|
||||
)
|
||||
).rejects.toEqual(new Error('Context name is required.'))
|
||||
})
|
||||
|
||||
it('should throw an error when attempt to create context with reserved name', async () => {
|
||||
const contextName = getRandomDefaultComputeContext()
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
contextName,
|
||||
'Test Launcher Context',
|
||||
'fakeAccountId',
|
||||
[]
|
||||
)
|
||||
).rejects.toEqual(
|
||||
new Error(`Compute context '${contextName}' already exists.`)
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw an error if context already exists', async () => {
|
||||
const contextName = 'Existing Compute Context'
|
||||
|
||||
const sampleComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: contextName,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponse = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
contextName,
|
||||
'Test Launcher Context',
|
||||
'fakeAccountId',
|
||||
[]
|
||||
)
|
||||
).rejects.toEqual(
|
||||
new Error(`Compute context '${contextName}' already exists.`)
|
||||
)
|
||||
})
|
||||
|
||||
it('should create compute context without launcher context', async () => {
|
||||
const contextName = 'New Compute Context'
|
||||
|
||||
const sampleExistingComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Existing Compute Context',
|
||||
attributes: {}
|
||||
}
|
||||
const sampleNewComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: contextName,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponseExistingComputeContexts = {
|
||||
items: [sampleExistingComputeContext]
|
||||
}
|
||||
const sampleResponseCreatedComputeContext = {
|
||||
items: [sampleNewComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingComputeContexts,
|
||||
sampleResponseCreatedComputeContext
|
||||
])
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
contextName,
|
||||
'',
|
||||
'fakeAccountId',
|
||||
[]
|
||||
)
|
||||
).resolves.toEqual({
|
||||
items: [
|
||||
{
|
||||
attributes: {},
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
name: contextName,
|
||||
version: 2
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
|
||||
it('should create compute context with default launcher context', async () => {
|
||||
const contextName = 'New Compute Context'
|
||||
|
||||
const sampleExistingComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Existing Compute Context',
|
||||
attributes: {}
|
||||
}
|
||||
const sampleNewComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: contextName,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponseExistingComputeContexts = {
|
||||
items: [sampleExistingComputeContext]
|
||||
}
|
||||
const sampleResponseCreatedComputeContext = {
|
||||
items: [sampleNewComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingComputeContexts,
|
||||
sampleResponseCreatedComputeContext
|
||||
])
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
contextName,
|
||||
getRandomDefaultLauncherContext(),
|
||||
'fakeAccountId',
|
||||
[]
|
||||
)
|
||||
).resolves.toEqual({
|
||||
items: [
|
||||
{
|
||||
attributes: {},
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
name: contextName,
|
||||
version: 2
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
|
||||
it('should create compute context with not existing launcher context', async () => {
|
||||
const computeContextName = 'New Compute Context'
|
||||
const launcherContextName = 'New Launcher Context'
|
||||
|
||||
const sampleExistingComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Existing Compute Context',
|
||||
attributes: {}
|
||||
}
|
||||
const sampleNewComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: computeContextName,
|
||||
attributes: {}
|
||||
}
|
||||
const sampleNewLauncherContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: launcherContextName,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponseExistingComputeContexts = {
|
||||
items: [sampleExistingComputeContext]
|
||||
}
|
||||
const sampleResponseCreatedLauncherContext = {
|
||||
items: [sampleNewLauncherContext]
|
||||
}
|
||||
const sampleResponseCreatedComputeContext = {
|
||||
items: [sampleNewComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingComputeContexts,
|
||||
sampleResponseCreatedLauncherContext,
|
||||
sampleResponseCreatedComputeContext
|
||||
])
|
||||
|
||||
await expect(
|
||||
contextManager.createComputeContext(
|
||||
computeContextName,
|
||||
launcherContextName,
|
||||
'fakeAccountId',
|
||||
[]
|
||||
)
|
||||
).resolves.toEqual({
|
||||
items: [
|
||||
{
|
||||
attributes: {},
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
name: computeContextName,
|
||||
version: 2
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('createLauncherContext', () => {
|
||||
it('should throw an error if context name was not provided', async () => {
|
||||
await expect(
|
||||
contextManager.createLauncherContext('', 'Test Description')
|
||||
).rejects.toEqual(new Error('Context name is required.'))
|
||||
})
|
||||
|
||||
it('should throw an error when attempt to create context with reserved name', async () => {
|
||||
const contextName = getRandomDefaultLauncherContext()
|
||||
|
||||
await expect(
|
||||
contextManager.createLauncherContext(contextName, 'Test Description')
|
||||
).rejects.toEqual(
|
||||
new Error(`Launcher context '${contextName}' already exists.`)
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw an error if context already exists', async () => {
|
||||
const contextName = 'Existing Launcher Context'
|
||||
|
||||
const sampleLauncherContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: contextName,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponse = {
|
||||
items: [sampleLauncherContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
|
||||
await expect(
|
||||
contextManager.createLauncherContext(contextName, 'Test Description')
|
||||
).rejects.toEqual(
|
||||
new Error(`Launcher context '${contextName}' already exists.`)
|
||||
)
|
||||
})
|
||||
|
||||
it('should create launcher context', async () => {
|
||||
const contextName = 'New Launcher Context'
|
||||
|
||||
const sampleExistingLauncherContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Existing Launcher Context',
|
||||
attributes: {}
|
||||
}
|
||||
const sampleNewLauncherContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: contextName,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponseExistingLauncherContext = {
|
||||
items: [sampleExistingLauncherContext]
|
||||
}
|
||||
const sampleResponseCreatedLauncherContext = {
|
||||
items: [sampleNewLauncherContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseExistingLauncherContext,
|
||||
sampleResponseCreatedLauncherContext
|
||||
])
|
||||
|
||||
await expect(
|
||||
contextManager.createLauncherContext(contextName, 'Test Description')
|
||||
).resolves.toEqual({
|
||||
items: [
|
||||
{
|
||||
attributes: {},
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
name: contextName,
|
||||
version: 2
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('editComputeContext', () => {
|
||||
const editedContext = {
|
||||
name: 'updated name',
|
||||
description: 'updated description',
|
||||
id: 'someId'
|
||||
}
|
||||
|
||||
it('should throw an error if context name was not provided', async () => {
|
||||
await expect(
|
||||
contextManager.editComputeContext('', editedContext)
|
||||
).rejects.toEqual(new Error('Context name is required.'))
|
||||
})
|
||||
|
||||
it('should throw an error when attempt to edit context with reserved name', async () => {
|
||||
const contextName = getRandomDefaultComputeContext()
|
||||
|
||||
let editError: Error = { name: '', message: '' }
|
||||
|
||||
try {
|
||||
contextManager.isDefaultContext(
|
||||
contextName,
|
||||
defaultComputeContexts,
|
||||
'Editing default SAS compute contexts is not allowed.',
|
||||
true
|
||||
)
|
||||
} catch (error) {
|
||||
editError = error
|
||||
}
|
||||
|
||||
await expect(
|
||||
contextManager.editComputeContext(contextName, editedContext)
|
||||
).rejects.toEqual(editError)
|
||||
})
|
||||
|
||||
it('should edit context if founded by name', async () => {
|
||||
const sampleComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: editedContext.name,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponseGetComputeContextByName = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponseGetComputeContextByName])
|
||||
|
||||
const expectedResponse = {
|
||||
etag: '',
|
||||
result: sampleResponseGetComputeContextByName
|
||||
}
|
||||
|
||||
await expect(
|
||||
contextManager.editComputeContext(editedContext.name, editedContext)
|
||||
).resolves.toEqual(expectedResponse)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getExecutableContexts', () => {
|
||||
it('should return executable contexts', async () => {
|
||||
const sampleComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Executable Compute Context',
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponse = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
|
||||
const user = 'testUser'
|
||||
|
||||
const fakedExecuteScript = async () => {
|
||||
return Promise.resolve({ log: `SYSUSERID=${user}` })
|
||||
}
|
||||
|
||||
const expectedResponse = [
|
||||
{
|
||||
...sampleComputeContext,
|
||||
attributes: { sysUserId: user }
|
||||
}
|
||||
]
|
||||
|
||||
await expect(
|
||||
contextManager.getExecutableContexts(fakedExecuteScript)
|
||||
).resolves.toEqual(expectedResponse)
|
||||
})
|
||||
|
||||
it('should not return executable contexts', async () => {
|
||||
const sampleComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: 'Not Executable Compute Context',
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponse = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([sampleResponse])
|
||||
|
||||
const fakedExecuteScript = async () => {
|
||||
return Promise.resolve({ log: '' })
|
||||
}
|
||||
|
||||
await expect(
|
||||
contextManager.getExecutableContexts(fakedExecuteScript)
|
||||
).resolves.toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('deleteComputeContext', () => {
|
||||
it('should throw an error if context name was not provided', async () => {
|
||||
await expect(contextManager.deleteComputeContext('')).rejects.toEqual(
|
||||
new Error('Context name is required.')
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw an error when attempt to delete context with reserved name', async () => {
|
||||
const contextName = getRandomDefaultComputeContext()
|
||||
|
||||
let deleteError: Error = { name: '', message: '' }
|
||||
|
||||
try {
|
||||
contextManager.isDefaultContext(
|
||||
contextName,
|
||||
defaultComputeContexts,
|
||||
'Deleting default SAS compute contexts is not allowed.',
|
||||
true
|
||||
)
|
||||
} catch (error) {
|
||||
deleteError = error
|
||||
}
|
||||
|
||||
await expect(
|
||||
contextManager.deleteComputeContext(contextName)
|
||||
).rejects.toEqual(deleteError)
|
||||
})
|
||||
|
||||
it('should delete context', async () => {
|
||||
const contextName = 'Compute Context To Delete'
|
||||
|
||||
const sampleComputeContext = {
|
||||
createdBy: 'fake creator',
|
||||
id: 'fakeId',
|
||||
version: 2,
|
||||
name: contextName,
|
||||
attributes: {}
|
||||
}
|
||||
|
||||
const sampleResponseGetComputeContextByName = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
const sampleResponseDeletedContext = {
|
||||
items: [sampleComputeContext]
|
||||
}
|
||||
|
||||
fakeGlobalFetch([
|
||||
sampleResponseGetComputeContextByName,
|
||||
sampleResponseDeletedContext
|
||||
])
|
||||
|
||||
const expectedResponse = {
|
||||
etag: '',
|
||||
result: sampleResponseDeletedContext
|
||||
}
|
||||
|
||||
await expect(
|
||||
contextManager.deleteComputeContext(contextName)
|
||||
).resolves.toEqual(expectedResponse)
|
||||
})
|
||||
})
|
||||
})
|
||||
137
src/test/FileUploader.spec.ts
Normal file
137
src/test/FileUploader.spec.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { FileUploader } from '../FileUploader'
|
||||
import { UploadFile } from '../types'
|
||||
|
||||
const sampleResponse = `{
|
||||
"SYSUSERID": "cas",
|
||||
"_DEBUG":" ",
|
||||
"SYS_JES_JOB_URI": "/jobExecution/jobs/000-000-000-000",
|
||||
"_PROGRAM" : "/Public/app/editors/loadfile",
|
||||
"SYSCC" : "0",
|
||||
"SYSJOBID" : "117382",
|
||||
"SYSWARNINGTEXT" : ""
|
||||
}`
|
||||
|
||||
const prepareFilesAndParams = () => {
|
||||
const files: UploadFile[] = [
|
||||
{
|
||||
file: new File([''], 'testfile'),
|
||||
fileName: 'testfile'
|
||||
}
|
||||
]
|
||||
const params = { table: 'libtable' }
|
||||
|
||||
return { files, params }
|
||||
}
|
||||
|
||||
describe('FileUploader', () => {
|
||||
let originalFetch: any
|
||||
const fileUploader = new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
beforeAll(() => {
|
||||
originalFetch = (global as any).fetch
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve(sampleResponse)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
;(global as any).fetch = originalFetch
|
||||
})
|
||||
|
||||
it('should upload successfully', async (done) => {
|
||||
const sasJob = 'test/upload'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
||||
expect(JSON.stringify(res)).toEqual(
|
||||
JSON.stringify(JSON.parse(sampleResponse))
|
||||
)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should an error when no files are provided', async (done) => {
|
||||
const sasJob = 'test/upload'
|
||||
const files: UploadFile[] = []
|
||||
const params = { table: 'libtable' }
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('At least one file must be provided.')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should throw an error when no sasJob is provided', async (done) => {
|
||||
const sasJob = ''
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('sasJob must be provided.')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should throw an error when login is required', async (done) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve('<form action="Logon">')
|
||||
})
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
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 (done) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve('{invalid: "json"')
|
||||
})
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual(
|
||||
'Error while parsing json from upload response.'
|
||||
)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should throw an error when the server request fails', async (done) => {
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.reject('{message: "Server error"}')
|
||||
})
|
||||
)
|
||||
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('Upload request failed.')
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
53
src/test/SessionManager.spec.ts
Normal file
53
src/test/SessionManager.spec.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { SessionManager } from '../SessionManager'
|
||||
import * as dotenv from 'dotenv'
|
||||
|
||||
describe('SessionManager', () => {
|
||||
dotenv.config()
|
||||
|
||||
let originalFetch: any
|
||||
|
||||
const sessionManager = new SessionManager(
|
||||
process.env.SERVER_URL as string,
|
||||
process.env.DEFAULT_COMPUTE_CONTEXT as string,
|
||||
() => {}
|
||||
)
|
||||
|
||||
beforeAll(() => {
|
||||
originalFetch = (global as any).fetch
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
;(global as any).fetch = originalFetch
|
||||
})
|
||||
|
||||
describe('getVariable', () => {
|
||||
it('should fetch session variable', async () => {
|
||||
const sampleResponse = {
|
||||
ok: true,
|
||||
links: [],
|
||||
name: 'SYSJOBID',
|
||||
scope: 'GLOBAL',
|
||||
value: '25218',
|
||||
version: 1
|
||||
}
|
||||
|
||||
;(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
ok: true,
|
||||
headers: { get: () => '' },
|
||||
json: () => Promise.resolve(sampleResponse)
|
||||
})
|
||||
)
|
||||
|
||||
const expectedResponse = { etag: '', result: sampleResponse }
|
||||
|
||||
await expect(
|
||||
sessionManager.getVariable(
|
||||
'fakeSessionId',
|
||||
'SYSJOBID',
|
||||
'fakeAccessToken'
|
||||
)
|
||||
).resolves.toEqual(expectedResponse)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseGeneratedCode } from './index'
|
||||
import { parseGeneratedCode } from '../../utils/index'
|
||||
|
||||
it('should parse generated code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseSourceCode } from './index'
|
||||
import { parseSourceCode } from '../../utils/index'
|
||||
|
||||
it('should parse SAS9 source code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
@@ -26,6 +26,9 @@ export interface ContextAllAttributes {
|
||||
createdBy: string
|
||||
creationTimeStamp: string
|
||||
launchType: string
|
||||
environment: {
|
||||
autoExecLines: [string]
|
||||
}
|
||||
launchContext: {
|
||||
contextName: string
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
export class ErrorResponse {
|
||||
body: ErrorBody
|
||||
error: ErrorBody
|
||||
|
||||
constructor(message: string, details?: any) {
|
||||
let detailsString = ''
|
||||
let raw
|
||||
constructor(message: string, details?: any, raw?: any) {
|
||||
let detailsString = details
|
||||
|
||||
try {
|
||||
detailsString = JSON.stringify(details)
|
||||
} catch {
|
||||
raw = details
|
||||
if (typeof details !== 'object') {
|
||||
try {
|
||||
detailsString = JSON.parse(details)
|
||||
} catch {
|
||||
raw = details
|
||||
detailsString = ''
|
||||
}
|
||||
}
|
||||
|
||||
this.body = {
|
||||
this.error = {
|
||||
message,
|
||||
details: detailsString,
|
||||
raw
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user