1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

Compare commits

...

72 Commits

Author SHA1 Message Date
Krishna Acondy
9651b7adb4 Merge pull request #161 from sasjs/fix-build-process
fix(*): export type declarations for node version of adapter
2020-11-25 21:06:20 +00:00
Krishna Acondy
59e5bec731 chore(*): fix PR template 2020-11-25 20:29:43 +00:00
Krishna Acondy
182e66216f fix(*): export type declarations for node version of adapter 2020-11-25 20:23:59 +00:00
Krishna Acondy
2408fd091e Merge pull request #155 from sasjs/dependabot/npm_and_yarn/semantic-release-17.3.0
chore(deps-dev): bump semantic-release from 17.2.3 to 17.3.0
2020-11-25 11:23:15 +00:00
dependabot-preview[bot]
0e38a24664 chore(deps-dev): bump semantic-release from 17.2.3 to 17.3.0
Bumps [semantic-release](https://github.com/semantic-release/semantic-release) from 17.2.3 to 17.3.0.
- [Release notes](https://github.com/semantic-release/semantic-release/releases)
- [Commits](https://github.com/semantic-release/semantic-release/compare/v17.2.3...v17.3.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-25 11:20:35 +00:00
Krishna Acondy
aa643d1782 Merge pull request #159 from sasjs/dependabot/npm_and_yarn/highlight.js-10.4.0
chore(deps): [security] bump highlight.js from 10.1.1 to 10.4.0
2020-11-25 11:18:17 +00:00
dependabot-preview[bot]
cdc91e9cda chore(deps): [security] bump highlight.js from 10.1.1 to 10.4.0
Bumps [highlight.js](https://github.com/highlightjs/highlight.js) from 10.1.1 to 10.4.0. **This update includes a security fix.**
- [Release notes](https://github.com/highlightjs/highlight.js/releases)
- [Changelog](https://github.com/highlightjs/highlight.js/blob/master/CHANGES.md)
- [Commits](https://github.com/highlightjs/highlight.js/compare/10.1.1...10.4.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-25 11:16:14 +00:00
Krishna Acondy
3f0590e0fe Merge pull request #158 from sasjs/issue157
Issue157
2020-11-25 11:14:39 +00:00
Mihajlo Medjedovic
5efb294ff2 fix: tests updated 2020-11-25 12:03:07 +01:00
Mihajlo Medjedovic
011e2d83dc fix: computeApi debug override 2020-11-24 15:19:19 +01:00
Mihajlo Medjedovic
e36b511530 test: make error and capture log, fixed 2020-11-24 13:02:17 +01:00
Mihajlo Medjedovic
b6a2a85d1d fix: compute api log not appended to sasjs requests 2020-11-24 11:59:17 +01:00
Krishna Acondy
f1cceeb5e6 Merge pull request #156 from sasjs/fix
fix(job execute): details for failing job
2020-11-24 07:46:32 +00:00
Krishna Acondy
6fee2548fd fix(job-failure): return original job object when a job has failed 2020-11-24 07:44:11 +00:00
Saad Jutt
91005066cf fix(job execute): details for failing job 2020-11-24 04:16:18 +05:00
Krishna Acondy
e1f17ef47d Merge pull request #127 from sasjs/dependabot/npm_and_yarn/terser-webpack-plugin-4.2.3
chore(deps-dev): bump terser-webpack-plugin from 4.2.2 to 4.2.3
2020-11-20 09:07:14 +00:00
dependabot-preview[bot]
8a40071c35 chore(deps-dev): bump terser-webpack-plugin from 4.2.2 to 4.2.3
Bumps [terser-webpack-plugin](https://github.com/webpack-contrib/terser-webpack-plugin) from 4.2.2 to 4.2.3.
- [Release notes](https://github.com/webpack-contrib/terser-webpack-plugin/releases)
- [Changelog](https://github.com/webpack-contrib/terser-webpack-plugin/blob/master/CHANGELOG.md)
- [Commits](https://github.com/webpack-contrib/terser-webpack-plugin/compare/v4.2.2...v4.2.3)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-20 09:01:25 +00:00
Krishna Acondy
430957eb3d Merge pull request #132 from sasjs/dependabot/npm_and_yarn/npm-user-validate-1.0.1
chore(deps): [security] bump npm-user-validate from 1.0.0 to 1.0.1
2020-11-20 08:58:57 +00:00
dependabot-preview[bot]
25874be679 chore(deps): [security] bump npm-user-validate from 1.0.0 to 1.0.1
Bumps [npm-user-validate](https://github.com/npm/npm-user-validate) from 1.0.0 to 1.0.1. **This update includes a security fix.**
- [Release notes](https://github.com/npm/npm-user-validate/releases)
- [Commits](https://github.com/npm/npm-user-validate/compare/v1.0.0...v1.0.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-20 08:57:20 +00:00
Krishna Acondy
ed8440434f Merge pull request #135 from sasjs/dependabot/npm_and_yarn/types/jest-26.0.15
chore(deps-dev): bump @types/jest from 26.0.14 to 26.0.15
2020-11-20 08:55:42 +00:00
dependabot-preview[bot]
0f9884c1b6 chore(deps-dev): bump @types/jest from 26.0.14 to 26.0.15
Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 26.0.14 to 26.0.15.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-20 08:46:00 +00:00
Krishna Acondy
d126a05347 Merge pull request #146 from sasjs/dependabot/npm_and_yarn/webpack-cli-4.2.0
chore(deps-dev): bump webpack-cli from 3.3.12 to 4.2.0
2020-11-20 08:43:50 +00:00
dependabot-preview[bot]
3e26bbbbba chore(deps-dev): bump webpack-cli from 3.3.12 to 4.2.0
Bumps [webpack-cli](https://github.com/webpack/webpack-cli) from 3.3.12 to 4.2.0.
- [Release notes](https://github.com/webpack/webpack-cli/releases)
- [Changelog](https://github.com/webpack/webpack-cli/blob/master/CHANGELOG.md)
- [Commits](https://github.com/webpack/webpack-cli/compare/v3.3.12...webpack-cli@4.2.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-20 08:34:49 +00:00
Krishna Acondy
982cc8f7a0 Merge pull request #149 from sasjs/dependabot/npm_and_yarn/ts-loader-8.0.11
chore(deps-dev): bump ts-loader from 8.0.4 to 8.0.11
2020-11-20 08:32:39 +00:00
dependabot-preview[bot]
d1770698e0 chore(deps-dev): bump ts-loader from 8.0.4 to 8.0.11
Bumps [ts-loader](https://github.com/TypeStrong/ts-loader) from 8.0.4 to 8.0.11.
- [Release notes](https://github.com/TypeStrong/ts-loader/releases)
- [Changelog](https://github.com/TypeStrong/ts-loader/blob/master/CHANGELOG.md)
- [Commits](https://github.com/TypeStrong/ts-loader/compare/8.0.4...v8.0.11)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-20 08:29:41 +00:00
Krishna Acondy
b78e8617c4 Merge pull request #151 from sasjs/dependabot/npm_and_yarn/semantic-release-17.2.3
[security] chore(deps-dev): bump semantic-release from 17.1.2 to 17.2.3
2020-11-20 08:27:32 +00:00
dependabot-preview[bot]
3ce9ca0986 chore(deps-dev): bump semantic-release from 17.1.2 to 17.2.3
Bumps [semantic-release](https://github.com/semantic-release/semantic-release) from 17.1.2 to 17.2.3.
- [Release notes](https://github.com/semantic-release/semantic-release/releases)
- [Commits](https://github.com/semantic-release/semantic-release/compare/v17.1.2...v17.2.3)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-11-20 08:19:07 +00:00
Yury Shkoda
04d17c3680 Merge pull request #153 from sasjs/context-issue
fix(context): fixed log parsing
2020-11-19 16:53:48 +03:00
Yury Shkoda
d26e15f91c Merge branch 'master' into context-issue 2020-11-19 16:43:30 +03:00
Yury Shkoda
83c46091b3 fix(context): fixed log parsing 2020-11-19 16:39:47 +03:00
Krishna Acondy
d640d7c040 Merge pull request #152 from sasjs/issue117
fix: viya login issue
2020-11-18 17:16:26 +00:00
Mihajlo Medjedovic
c934eb2b08 test: added test for multiple login attempts 2020-11-18 14:13:13 +01:00
Mihajlo Medjedovic
24dd5e32ad style: lint 2020-11-18 13:10:29 +01:00
Mihajlo Medjedovic
a23103b2c3 fix: viya login issue 2020-11-18 13:09:49 +01:00
Yury Shkoda
35aa4235e4 Merge pull request #148 from sasjs/issue113
feat: service not found error handling
2020-11-16 09:54:03 +03:00
Mihajlo Medjedovic
e9be1cf99a fix: service not found error handling 2020-11-12 16:03:32 +01:00
Mihajlo Medjedovic
c7b0821081 style: lint 2020-11-09 18:24:10 +01:00
Mihajlo Medjedovic
4a4618dd32 feat: service not found error handling for SAS9 2020-11-09 18:19:39 +01:00
Krishna Acondy
d223e83c60 Merge pull request #142 from sasjs/issue138
fix(file-uploader): handle errors during file upload
2020-11-02 14:54:58 +00:00
Krishna Acondy
d1f1a20126 chore(file-uploader): move uploader to describe scope 2020-11-02 09:29:33 +00:00
Krishna Acondy
4b89e3762f chore(file-uploader): remove duplication 2020-11-02 08:54:26 +00:00
Krishna Acondy
bc110288de chore(file-uploader): improve mocking of fetch, add tests for all error scenarios 2020-11-02 08:51:27 +00:00
Krishna Acondy
e94e16b52c chore(*): fix linting errors 2020-11-02 07:55:48 +00:00
Krishna Acondy
76aacee016 Merge branch 'master' into issue138 2020-11-02 07:42:39 +00:00
Mihajlo Medjedovic
1a3bd5d1f5 chore: lint 2020-10-30 16:13:03 +01:00
Mihajlo Medjedovic
3f6e89d716 fix: file uploader error handling and tests 2020-10-30 16:11:50 +01:00
Yury Shkoda
361ec84638 Merge pull request #141 from sasjs/fetch-log-feat
chore(log): made 'fetchLogFileContent' method public
2020-10-30 11:28:07 +03:00
Yury Shkoda
35cc1e4f62 chore(fetchLogFileContent): made accessToken optional 2020-10-30 11:26:43 +03:00
Yury Shkoda
64a976e888 doc: updated docs 2020-10-30 10:36:53 +03:00
Yury Shkoda
7e2cb8491f feat(log): made 'fetchLogFileContent' method public 2020-10-30 10:36:04 +03:00
Krishna Acondy
2cdab7522d Merge pull request #139 from sasjs/location-issue
fix(location): added handle cases when 'location' is not defined
2020-10-29 08:07:11 +00:00
Yury Shkoda
a07eabc408 fix(location): added handle cases when 'location' is not defined 2020-10-29 10:07:30 +03:00
Mihajlo Medjedovic
7279c23fe2 fix: FIleUploader added catch 2020-10-27 14:50:05 +01:00
Mihajlo Medjedovic
80707d77d9 gitfe Merge branches 'errorResponse' and 'master' of github.com:sasjs/adapter 2020-10-27 14:40:41 +01:00
Yury Shkoda
d5920c5885 Merge pull request #134 from sasjs/executeComputeJob
fix(executeComputeJob): added fix for cases when code was not provided
2020-10-21 11:55:43 +03:00
Yury Shkoda
6a3a6b4485 fix(executeComputeJob): added fix for cases when code was not provided 2020-10-21 11:45:21 +03:00
Krishna Acondy
2b1df0c61a Merge pull request #123 from sasjs/sasjs-job
feat(start-compute-job): Add API that returns immediately after job is started
2020-10-16 11:27:02 +01:00
Krishna Acondy
216725f306 chore(doc): update documentation 2020-10-16 11:04:03 +01:00
Krishna Acondy
3183f89a62 chore(*): fix lint warning 2020-10-16 10:58:04 +01:00
Krishna Acondy
f5cc16c3bd chore(create-job): add tests 2020-10-16 10:56:10 +01:00
Krishna Acondy
e78dc76e56 fix(config): set debug to false by default
feat(create-job): add the ability to wait for result
2020-10-16 10:55:56 +01:00
Krishna Acondy
bfdb5ef0a6 chore(*): regenerate documentation 2020-10-16 09:13:48 +01:00
Krishna Acondy
35353d3fce Merge branch 'master' into sasjs-job 2020-10-15 09:11:50 +01:00
Yury Shkoda
7a02c8ad34 Merge pull request #131 from sasjs/issue-124
fix(session): add internal SAS error handler
2020-10-14 14:03:58 +03:00
Yury Shkoda
331d9b0010 fix(session): add internal SAS error handler 2020-10-14 12:53:59 +03:00
Yury Shkoda
ef5686cce7 Merge branch 'master' into sasjs-job 2020-10-12 09:21:00 +03:00
Yury Shkoda
fa87111f4a Merge pull request #126 from sasjs/issue-124
fix(context): fixed 'getExecutableContexts' method
2020-10-07 17:53:31 +03:00
Yury Shkoda
94967b0f6c fix(context): fixed 'getExecutableContexts' method 2020-10-07 17:25:47 +03:00
Mihajlo Medjedovic
3f796b300d fix: ErrorResponse body changed to error 2020-10-07 11:15:00 +02:00
Krishna Acondy
a07c16fb52 chore(start-compute-job): add test 2020-10-06 09:21:58 +01:00
Krishna Acondy
fd6905ea9f feat(start-compute-job): add API that starts a compute job and immediately returns the session 2020-10-06 09:21:15 +01:00
Krishna Acondy
08f58b5f4f fix(debug): only set session manager debug if it is defined 2020-10-06 08:17:02 +01:00
38 changed files with 10114 additions and 1846 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules
build
node

View File

@@ -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

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

View File

@@ -76,7 +76,7 @@
<section class="tsd-index-section ">
<h3>Modules</h3>
<ul class="tsd-index-list">
<li class="tsd-kind-module tsd-is-not-exported"><a href="modules/reflection-762.html" class="tsd-kind-icon"><em>Module</em></a></li>
<li class="tsd-kind-module tsd-is-not-exported"><a href="modules/reflection-790.html" class="tsd-kind-icon"><em>Module</em></a></li>
<li class="tsd-kind-module"><a href="modules/types.html" class="tsd-kind-icon">types</a></li>
<li class="tsd-kind-module"><a href="modules/utils.html" class="tsd-kind-icon">utils</a></li>
</ul>

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

2660
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,7 +2,7 @@
"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",
"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}'",
@@ -37,15 +37,15 @@
"license": "ISC",
"devDependencies": {
"@types/isomorphic-fetch": "0.0.35",
"@types/jest": "^26.0.14",
"@types/jest": "^26.0.15",
"cp": "^0.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,7 +53,7 @@
"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": {

View File

@@ -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",

View File

@@ -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",

View File

@@ -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();
});

View File

@@ -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]);

View File

@@ -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 &&

View File

@@ -0,0 +1,41 @@
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);
}
}
]
});
const validate = (expectedProperties: string[], data: any): boolean => {
const actualProperties = Object.keys(data);
const isValid = expectedProperties.every(
(property) => actualProperties.includes(property)
);
return isValid
}

View File

@@ -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;
}
},
{

View File

@@ -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));
});
});
},

View File

@@ -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))
})
})
}
}

View File

@@ -52,7 +52,9 @@ export class SASViyaApiClient {
public set debug(value: boolean) {
this._debug = value
this.sessionManager.debug = value
if (this.sessionManager) {
this.sessionManager.debug = value
}
}
/**
@@ -145,41 +147,51 @@ export class SASViyaApiClient {
const promises = contextsList.map((context: any) => {
const linesOfCode = ['%put &=sysuserid;']
return this.executeScript(
`test-${context.name}`,
linesOfCode,
context.name,
accessToken,
null,
true
).catch(() => null)
return () =>
this.executeScript(
`test-${context.name}`,
linesOfCode,
context.name,
accessToken,
null,
true,
true
).catch((err) => err)
})
const results = await Promise.all(promises)
let results: any[] = []
for (const promise of promises) results.push(await promise())
results.forEach((result: any, index: number) => {
if (result) {
let sysUserId = ''
if (result && result.error && result.error.details) {
try {
const resultParsed = result.error.details
if (result.log) {
const sysUserIdLog = result.log
.split('\n')
.find((line: string) => line.startsWith('SYSUSERID='))
if (resultParsed && resultParsed.body) {
let sysUserId = ''
if (sysUserIdLog) {
sysUserId = sysUserIdLog.replace('SYSUSERID=', '')
const sysUserIdLog = resultParsed.body
.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
}
executableContexts.push({
createdBy: contextsList[index].createdBy,
id: contextsList[index].id,
name: contextsList[index].name,
version: contextsList[index].version,
attributes: {
sysUserId
}
})
}
})
@@ -327,7 +339,9 @@ export class SASViyaApiClient {
originalContext = await this.getComputeContextByName(
contextName,
accessToken
).catch((_) => {})
).catch((err) => {
throw err
})
// Try to find context by id, when context name has been changed.
if (!originalContext) {
@@ -412,10 +426,10 @@ 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 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
*/
public async executeScript(
jobPath: string,
@@ -423,7 +437,9 @@ export class SASViyaApiClient {
contextName: string,
accessToken?: string,
data = null,
expectWebout = false
debug: boolean = false,
expectWebout = false,
waitForResult = true
): Promise<any> {
try {
const headers: any = {
@@ -435,7 +451,12 @@ 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
const jobArguments: { [key: string]: any } = {
@@ -447,7 +468,7 @@ export class SASViyaApiClient {
_OMITTEXTLOG: true
}
if (this.debug) {
if (debug) {
jobArguments['_OMITTEXTLOG'] = false
jobArguments['_OMITSESSIONRESULTS'] = false
jobArguments['_DEBUG'] = 131
@@ -474,7 +495,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
@@ -505,9 +528,15 @@ export class SASViyaApiClient {
const { result: postedJob, etag } = await this.request<Job>(
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs`,
postJobRequest
)
).catch((err) => {
throw err
})
if (this.debug) {
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}${
@@ -521,32 +550,40 @@ export class SASViyaApiClient {
const { result: currentJob } = await this.request<Job>(
`${this.serverUrl}/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`,
{ headers }
)
).catch((err) => {
throw err
})
let jobResult
let log
const logLink = currentJob.links.find((l) => l.rel === 'log')
if (this.debug && logLink) {
if (debug && logLink) {
log = await this.request<any>(
`${this.serverUrl}${logLink.href}/content?limit=10000`,
{
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 currentJob
}
if (resultLink) {
@@ -562,16 +599,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 {
@@ -580,7 +619,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) {
@@ -590,7 +633,10 @@ export class SASViyaApiClient {
linesOfCode,
contextName,
accessToken,
data
data,
debug,
false,
true
)
} else {
throw e
@@ -903,13 +949,17 @@ 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.
*/
public async executeComputeJob(
sasJob: string,
contextName: string,
debug: boolean,
debug?: boolean,
data?: any,
accessToken?: string
accessToken?: string,
waitForResult = true,
expectWebout = false
) {
if (isRelativePath(sasJob) && !this.rootFolderName) {
throw new Error(
@@ -983,6 +1033,8 @@ export class SASViyaApiClient {
jobToExecute.code = code
}
if (!code) code = ''
const linesToExecute = code.replace(/\r\n/g, '\n').split('\n')
return await this.executeScript(
sasJob,
@@ -990,7 +1042,9 @@ export class SASViyaApiClient {
contextName,
accessToken,
data,
true
debug,
expectWebout,
waitForResult
)
}
@@ -1062,7 +1116,7 @@ export class SASViyaApiClient {
}
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'

View File

@@ -44,7 +44,7 @@ const defaultConfig: SASjsConfig = {
pathSASViya: '/SASJobExecution',
appLoc: '/Public/seedapp',
serverType: ServerType.SASViya,
debug: true,
debug: false,
contextName: 'SAS Job Execution compute context',
useComputeApi: false
}
@@ -411,6 +411,29 @@ export default class SASjs {
}
}
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
}
/**
* Checks whether a session is active, or login is required.
* @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`.
@@ -419,10 +442,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
})
}
@@ -440,7 +469,7 @@ export default class SASjs {
this.userName = loginParams.username
const { isLoggedIn } = await this.checkSession()
const { isLoggedIn, loginForm } = await this.checkSession()
if (isLoggedIn) {
this.resendWaitingRequests()
@@ -450,15 +479,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,
@@ -670,6 +697,51 @@ 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.
*/
public async startComputeJob(
sasJob: string,
data: any,
config: any = {},
accessToken?: string,
waitForResult?: boolean
) {
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
)
}
private async executeJobViaComputeApi(
sasJob: string,
data: any,
@@ -689,13 +761,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) {
@@ -733,11 +809,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
@@ -745,10 +833,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)
})
}
)
@@ -811,8 +897,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,
@@ -828,12 +914,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))
})
)
}
@@ -1017,7 +1115,7 @@ export default class SASjs {
} else {
reject(
new ErrorResponse(
'Job WEB execution failed',
'Job WEB execution failed.',
this.parseSAS9ErrorResponse(responseText)
)
)
@@ -1035,7 +1133,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 }
)
)
@@ -1044,7 +1142,7 @@ export default class SASjs {
(err: any) => {
reject(
new ErrorResponse(
'Job WEB debug response parsing failed',
'Job WEB debug response parsing failed.',
err
)
)
@@ -1053,19 +1151,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
})
@@ -1076,7 +1189,7 @@ export default class SASjs {
}
})
.catch((e: Error) => {
reject(new ErrorResponse('Job WEB request failed', e))
reject(new ErrorResponse('Job WEB request failed.', e))
})
}
)
@@ -1217,10 +1330,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))
@@ -1318,11 +1441,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) === '/') {
@@ -1387,26 +1514,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[],

View File

@@ -2,6 +2,12 @@ import { Session, Context, CsrfToken } from './types'
import { asyncForEach, makeRequest, isUrl } from './utils'
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(
@@ -27,19 +33,22 @@ export class SessionManager {
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
}
@@ -48,22 +57,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
})
}
}
@@ -73,13 +97,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
}
@@ -89,6 +118,8 @@ export class SessionManager {
items: Context[]
}>(`${this.serverUrl}/compute/contexts?limit=10000`, {
headers: this.getHeaders(accessToken)
}).catch((err) => {
throw err
})
const contextsList =
@@ -107,6 +138,8 @@ export class SessionManager {
}
this.currentContext = currentContext
Promise.resolve()
}
}
@@ -114,6 +147,7 @@ export class SessionManager {
const headers: any = {
'Content-Type': 'application/json'
}
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`
}
@@ -132,24 +166,41 @@ 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 (stateLink) {
if (this.debug) {
console.log('Polling session status... \n') // ?
}
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 (this.debug) {
console.log(`Current state is '${sessionState}'\n`)
}
// 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 {
@@ -169,6 +220,7 @@ export class SessionManager {
[this.csrfToken.headerName]: this.csrfToken.value
}
}
return await makeRequest<T>(
url,
options,
@@ -177,6 +229,36 @@ 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
})
}
}

View 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()
})
})
})

View File

@@ -1,4 +1,4 @@
import { parseGeneratedCode } from './index'
import { parseGeneratedCode } from '../../utils/index'
it('should parse generated code', async (done) => {
expect(sampleResponse).toBeTruthy()

View File

@@ -1,4 +1,4 @@
import { parseSourceCode } from './index'
import { parseSourceCode } from '../../utils/index'
it('should parse SAS9 source code', async (done) => {
expect(sampleResponse).toBeTruthy()

View File

@@ -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

View File

@@ -2,7 +2,7 @@ import { CsrfToken } from '../types'
import { needsRetry } from './needsRetry'
let retryCount: number = 0
let retryLimit: number = 5
const retryLimit: number = 5
export async function makeRequest<T>(
url: string,
@@ -18,57 +18,118 @@ export async function makeRequest<T>(
: (res: Response) => res.text()
let etag = null
const result = await fetch(url, request).then(async (response) => {
if (response.redirected && response.url.includes('SASLogon/login')) {
return Promise.reject({ status: 401 })
}
if (!response.ok) {
if (response.status === 403) {
const tokenHeader = response.headers.get('X-CSRF-HEADER')
const result = await fetch(url, request)
.then(async (response) => {
if (response.redirected && response.url.includes('SASLogon/login')) {
return Promise.reject({ status: 401 })
}
if (tokenHeader) {
const token = response.headers.get(tokenHeader)
callback({
headerName: tokenHeader,
value: token || ''
if (!response.ok) {
if (response.status === 403) {
const tokenHeader = response.headers.get('X-CSRF-HEADER')
if (tokenHeader) {
const token = response.headers.get(tokenHeader)
callback({
headerName: tokenHeader,
value: token || ''
})
retryRequest = {
...request,
headers: { ...request.headers, [tokenHeader]: token }
}
return await fetch(url, retryRequest).then((res) => {
etag = res.headers.get('ETag')
return responseTransform(res)
})
} else {
let body: any = await response.text().catch((err) => {
throw err
})
try {
body = JSON.parse(body)
body.message = `Forbidden. Check your permissions and user groups, and also the scopes granted when registering your CLIENT_ID. ${
body.message || ''
}`
body = JSON.stringify(body)
} catch (_) {}
return Promise.reject({ status: response.status, body })
}
} else {
let body: any = await response.text().catch((err) => {
throw err
})
retryRequest = {
...request,
headers: { ...request.headers, [tokenHeader]: token }
if (needsRetry(body)) {
if (retryCount < retryLimit) {
retryCount++
let retryResponse = await makeRequest(
url,
retryRequest || request,
callback,
contentType
).catch((err) => {
throw err
})
retryCount = 0
etag = retryResponse.etag
return retryResponse.result
} else {
retryCount = 0
throw new Error('Request retry limit exceeded')
}
}
return fetch(url, retryRequest).then((res) => {
etag = res.headers.get('ETag')
return responseTransform(res)
})
} else {
let body: any = await response.text()
if (response.status === 401) {
try {
body = JSON.parse(body)
try {
body = JSON.parse(body)
body.message = `Unauthorized request. Check your credentials(client, secret, access token). ${
body.message || ''
}`
body.message = `Forbidden. Check your permissions and user groups, and also the scopes granted when registering your CLIENT_ID. ${
body.message || ''
}`
body = JSON.stringify(body)
} catch (_) {}
body = JSON.stringify(body)
} catch (_) {}
}
return Promise.reject({ status: response.status, body })
}
} else {
let body: any = await response.text()
if (response.status === 204) {
return Promise.resolve()
}
const responseTransformed = await responseTransform(response).catch(
(err) => {
throw err
}
)
let responseText = ''
if (needsRetry(body)) {
if (typeof responseTransformed === 'string') {
responseText = responseTransformed
} else {
responseText = JSON.stringify(responseTransformed)
}
if (needsRetry(responseText)) {
if (retryCount < retryLimit) {
retryCount++
let retryResponse = await makeRequest(
const retryResponse = await makeRequest(
url,
retryRequest || request,
callback,
contentType
)
).catch((err) => {
throw err
})
retryCount = 0
etag = retryResponse.etag
@@ -80,57 +141,14 @@ export async function makeRequest<T>(
}
}
if (response.status === 401) {
try {
body = JSON.parse(body)
etag = response.headers.get('ETag')
body.message = `Unauthorized request. Check your credentials(client, secret, access token). ${
body.message || ''
}`
body = JSON.stringify(body)
} catch (_) {}
}
return Promise.reject({ status: response.status, body })
return responseTransformed
}
} else {
if (response.status === 204) {
return Promise.resolve()
}
const responseTransformed = await responseTransform(response)
let responseText = ''
if (typeof responseTransformed === 'string') {
responseText = responseTransformed
} else {
responseText = JSON.stringify(responseTransformed)
}
if (needsRetry(responseText)) {
if (retryCount < retryLimit) {
retryCount++
const retryResponse = await makeRequest(
url,
retryRequest || request,
callback,
contentType
)
retryCount = 0
etag = retryResponse.etag
return retryResponse.result
} else {
retryCount = 0
throw new Error('Request retry limit exceeded')
}
}
etag = response.headers.get('ETag')
return responseTransformed
}
})
})
.catch((err) => {
throw err
})
return { result, etag }
}

View File

@@ -47,6 +47,7 @@ const browserConfig = {
const nodeConfig = {
...browserConfig,
target: 'node',
entry: './node/index.ts',
output: {
...browserConfig.output,
path: path.resolve(__dirname, 'build', 'node')