@sasjs/adapter 4.19.4 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ContextManager.js +290 -0
- package/DISCLOSURE +45 -0
- package/README.md +1 -1
- package/SAS9ApiClient.d.ts +1 -1
- package/SAS9ApiClient.js +90 -0
- package/SASViyaApiClient.js +918 -0
- package/SASjs.js +794 -0
- package/SASjsApiClient.d.ts +1 -0
- package/SASjsApiClient.js +132 -0
- package/SessionManager.js +353 -0
- package/api/viya/executeOnComputeApi.js +198 -0
- package/api/viya/getFileStream.js +32 -0
- package/api/viya/pollJobState.js +247 -0
- package/api/viya/saveLog.js +45 -0
- package/api/viya/uploadTables.js +45 -0
- package/api/viya/writeStream.js +25 -0
- package/auth/AuthManager.js +305 -0
- package/auth/getAccessTokenForSasjs.js +43 -0
- package/auth/getAccessTokenForViya.js +54 -0
- package/auth/getTokenRequestErrorPrefix.js +70 -0
- package/auth/getTokens.js +110 -0
- package/auth/index.js +20 -0
- package/auth/isAuthorizeFormRequired.js +7 -0
- package/auth/isLoginRequired.js +9 -0
- package/auth/loginHeader.js +87 -0
- package/auth/openWebPage.js +31 -0
- package/auth/refreshTokensForSasjs.js +42 -0
- package/auth/refreshTokensForViya.js +53 -0
- package/auth/verifySas9Login.js +32 -0
- package/auth/verifySasViyaLogin.js +44 -0
- package/dist/minified_sas9.js +10892 -0
- package/dist/sasjs-adapter.js +19569 -0
- package/file/generateFileUploadForm.js +43 -0
- package/file/generateTableUploadForm.js +38 -0
- package/index.js +9 -80376
- package/job-execution/ComputeJobExecutor.js +55 -0
- package/job-execution/FileUploader.js +114 -0
- package/job-execution/JesJobExecutor.js +55 -0
- package/job-execution/JobExecutor.js +40 -0
- package/job-execution/Sas9JobExecutor.d.ts +1 -1
- package/job-execution/Sas9JobExecutor.js +115 -0
- package/job-execution/SasjsJobExecutor.js +100 -0
- package/job-execution/WebJobExecutor.js +254 -0
- package/job-execution/index.js +23 -0
- package/minified/sas9/SASjs.js +198 -0
- package/minified/sas9/WebJobExecutor.js +105 -0
- package/minified/sas9/index.js +22 -0
- package/node/index.d.ts +2 -9
- package/node/index.js +1 -30570
- package/package.json +40 -14
- package/request/RequestClient.d.ts +1 -1
- package/request/RequestClient.js +717 -0
- package/request/Sas9RequestClient.d.ts +1 -1
- package/request/Sas9RequestClient.js +121 -0
- package/request/SasjsRequestClient.js +77 -0
- package/types/Context.js +2 -0
- package/types/CsrfToken.js +2 -0
- package/types/ExecuteScript.js +2 -0
- package/types/File.js +2 -0
- package/types/FileResource.js +2 -0
- package/types/Folder.js +2 -0
- package/types/Job.js +2 -0
- package/types/JobDefinition.js +2 -0
- package/types/JobResult.js +2 -0
- package/types/Link.js +2 -0
- package/types/LogStatistics.js +2 -0
- package/types/Login.js +2 -0
- package/types/PollOptions.js +2 -0
- package/types/RequestClient.js +2 -0
- package/types/SASjsConfig.d.ts +1 -1
- package/types/SASjsConfig.js +91 -0
- package/types/Session.js +14 -0
- package/types/Tables.js +28 -0
- package/types/UploadFile.js +2 -0
- package/types/WriteStream.d.ts +1 -1
- package/types/WriteStream.js +2 -0
- package/types/errors/ArgumentError.js +12 -0
- package/types/errors/AuthorizeError.js +13 -0
- package/types/errors/CertificateError.js +12 -0
- package/types/errors/ComputeJobExecutionError.js +13 -0
- package/types/errors/ErrorResponse.js +23 -0
- package/types/errors/InternalServerError.js +11 -0
- package/types/errors/InvalidJsonError.js +11 -0
- package/types/errors/InvalidSASjsCsrfError.js +12 -0
- package/types/errors/JobExecutionError.js +14 -0
- package/types/errors/JobStatePollError.js +12 -0
- package/types/errors/JsonParseArrayError.js +11 -0
- package/types/errors/LoginRequiredError.js +14 -0
- package/types/errors/NoSessionStateError.js +14 -0
- package/types/errors/NotFoundError.js +12 -0
- package/types/errors/RootFolderNotFoundError.js +22 -0
- package/types/errors/SAS9AuthError.js +11 -0
- package/types/errors/WeboutResponseError.js +12 -0
- package/types/errors/index.js +31 -0
- package/types/index.js +34 -0
- package/utils/appendExtraResponseAttributes.js +25 -0
- package/utils/asyncForEach.js +20 -0
- package/utils/compareTimestamps.js +11 -0
- package/utils/convertToCsv.js +154 -0
- package/utils/createAxiosInstance.d.ts +1 -1
- package/utils/createAxiosInstance.js +9 -0
- package/utils/delay.js +5 -0
- package/{src/utils/extractWeboutBlob.ts → utils/extractWeboutBlob.js} +19 -20
- package/utils/fetchLogByChunks.js +56 -0
- package/utils/formatDataForRequest.js +39 -0
- package/utils/getFormData.js +10 -0
- package/{node/utils/getUserLanguage.d.ts → utils/getUserLanguage.js} +5 -1
- package/utils/getValidJson.js +27 -0
- package/utils/host.d.ts +24 -0
- package/utils/host.js +36 -0
- package/utils/index.js +41 -0
- package/utils/isIeOrEdge.js +30 -0
- package/utils/isNode.js +7 -0
- package/utils/isRelativePath.js +5 -0
- package/utils/isUri.js +9 -0
- package/utils/isUrl.js +20 -0
- package/utils/loginPrompt/index.js +180 -0
- package/utils/needsRetry.js +14 -0
- package/utils/parseGeneratedCode.js +10 -0
- package/utils/parseSasViyaLog.js +17 -0
- package/utils/parseSourceCode.js +9 -0
- package/utils/parseViyaDebugResponse.js +46 -0
- package/utils/parseViyaLogDebugResponse.js +32 -0
- package/utils/parseWeboutResponse.js +22 -0
- package/utils/sas9/extractUserLongNameSas9.js +30 -0
- package/utils/serialize.js +20 -0
- package/utils/splitChunks.js +13 -0
- package/utils/validateInput.js +83 -0
- package/.env.example +0 -2
- package/.git-hooks/commit-msg +0 -39
- package/.git-hooks/pre-commit +0 -12
- package/.gitattributes +0 -1
- package/.pre-commit-config.yaml +0 -25
- package/.vscode/settings.json +0 -3
- package/SASViyaApiClient.spec.d.ts +0 -1
- package/__mocks__/axios.d.ts +0 -3
- package/api/viya/spec/executeScript.spec.d.ts +0 -1
- package/api/viya/spec/getFileStream.spec.d.ts +0 -1
- package/api/viya/spec/mockResponses.d.ts +0 -10
- package/api/viya/spec/pollJobState.spec.d.ts +0 -1
- package/api/viya/spec/saveLog.spec.d.ts +0 -1
- package/api/viya/spec/uploadTables.spec.d.ts +0 -1
- package/api/viya/spec/writeStream.spec.d.ts +0 -1
- package/auth/spec/AuthManager.spec.d.ts +0 -4
- package/auth/spec/getAccessTokenForSasjs.spec.d.ts +0 -1
- package/auth/spec/getAccessTokenForViya.spec.d.ts +0 -1
- package/auth/spec/getTokenRequestErrorPrefix.spec.d.ts +0 -1
- package/auth/spec/getTokens.spec.d.ts +0 -1
- package/auth/spec/loginHeader.spec.d.ts +0 -4
- package/auth/spec/mockResponses.d.ts +0 -23
- package/auth/spec/openWebPage.spec.d.ts +0 -1
- package/auth/spec/refreshTokensForSasjs.spec.d.ts +0 -1
- package/auth/spec/refreshTokensForViya.spec.d.ts +0 -1
- package/auth/spec/verifySas9Login.spec.d.ts +0 -1
- package/auth/spec/verifySasViyaLogin.spec.d.ts +0 -1
- package/createTSDocs.js +0 -35
- package/cypress.config.js +0 -18
- package/example.html +0 -109
- package/file/spec/generateFileUploadForm.spec.d.ts +0 -1
- package/file/spec/generateTableUploadForm.spec.d.ts +0 -1
- package/index-dev.js +0 -80394
- package/job-execution/spec/ComputeJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/JesJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/Sas9JobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/SasjsJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/WebJobExecutor.debug.spec.d.ts +0 -1
- package/job-execution/spec/WebJobExecutor.spec.d.ts +0 -1
- package/job-execution/spec/executionTasks.spec.d.ts +0 -1
- package/minified_sas9.js +0 -14039
- package/node/ContextManager.d.ts +0 -41
- package/node/SAS9ApiClient.d.ts +0 -30
- package/node/SASViyaApiClient.d.ts +0 -298
- package/node/SASjs.d.ts +0 -387
- package/node/SASjsApiClient.d.ts +0 -65
- package/node/SessionManager.d.ts +0 -115
- package/node/api/viya/executeOnComputeApi.d.ts +0 -21
- package/node/api/viya/getFileStream.d.ts +0 -2
- package/node/api/viya/pollJobState.d.ts +0 -57
- package/node/api/viya/saveLog.d.ts +0 -15
- package/node/api/viya/uploadTables.d.ts +0 -13
- package/node/api/viya/writeStream.d.ts +0 -2
- package/node/auth/AuthManager.d.ts +0 -49
- package/node/auth/getAccessTokenForSasjs.d.ts +0 -11
- package/node/auth/getAccessTokenForViya.d.ts +0 -10
- package/node/auth/getTokenRequestErrorPrefix.d.ts +0 -44
- package/node/auth/getTokens.d.ts +0 -22
- package/node/auth/index.d.ts +0 -4
- package/node/auth/isAuthorizeFormRequired.d.ts +0 -1
- package/node/auth/isLoginRequired.d.ts +0 -1
- package/node/auth/loginHeader.d.ts +0 -17
- package/node/auth/openWebPage.d.ts +0 -6
- package/node/auth/refreshTokensForSasjs.d.ts +0 -10
- package/node/auth/refreshTokensForViya.d.ts +0 -11
- package/node/auth/verifySas9Login.d.ts +0 -3
- package/node/auth/verifySasViyaLogin.d.ts +0 -4
- package/node/file/generateFileUploadForm.d.ts +0 -11
- package/node/file/generateTableUploadForm.d.ts +0 -5
- package/node/job-execution/ComputeJobExecutor.d.ts +0 -8
- package/node/job-execution/FileUploader.d.ts +0 -9
- package/node/job-execution/JesJobExecutor.d.ts +0 -9
- package/node/job-execution/JobExecutor.d.ts +0 -17
- package/node/job-execution/Sas9JobExecutor.d.ts +0 -19
- package/node/job-execution/SasjsJobExecutor.d.ts +0 -9
- package/node/job-execution/WebJobExecutor.d.ts +0 -18
- package/node/job-execution/index.d.ts +0 -7
- package/node/request/RequestClient.d.ts +0 -125
- package/node/request/Sas9RequestClient.d.ts +0 -25
- package/node/request/SasjsRequestClient.d.ts +0 -12
- package/node/types/Context.d.ts +0 -42
- package/node/types/CsrfToken.d.ts +0 -4
- package/node/types/ExecuteScript.d.ts +0 -4
- package/node/types/File.d.ts +0 -7
- package/node/types/FileResource.d.ts +0 -32
- package/node/types/Folder.d.ts +0 -7
- package/node/types/Job.d.ts +0 -14
- package/node/types/JobDefinition.d.ts +0 -3
- package/node/types/JobResult.d.ts +0 -3
- package/node/types/Link.d.ts +0 -7
- package/node/types/LogStatistics.d.ts +0 -4
- package/node/types/Login.d.ts +0 -14
- package/node/types/PollOptions.d.ts +0 -8
- package/node/types/RequestClient.d.ts +0 -46
- package/node/types/SASjsConfig.d.ts +0 -94
- package/node/types/Session.d.ts +0 -30
- package/node/types/Tables.d.ts +0 -7
- package/node/types/UploadFile.d.ts +0 -8
- package/node/types/WriteStream.d.ts +0 -7
- package/node/types/errors/ArgumentError.d.ts +0 -4
- package/node/types/errors/AuthorizeError.d.ts +0 -5
- package/node/types/errors/CertificateError.d.ts +0 -3
- package/node/types/errors/ComputeJobExecutionError.d.ts +0 -6
- package/node/types/errors/ErrorResponse.d.ts +0 -9
- package/node/types/errors/InternalServerError.d.ts +0 -3
- package/node/types/errors/InvalidJsonError.d.ts +0 -3
- package/node/types/errors/InvalidSASjsCsrfError.d.ts +0 -3
- package/node/types/errors/JobExecutionError.d.ts +0 -6
- package/node/types/errors/JobStatePollError.d.ts +0 -4
- package/node/types/errors/JsonParseArrayError.d.ts +0 -3
- package/node/types/errors/LoginRequiredError.d.ts +0 -3
- package/node/types/errors/NoSessionStateError.d.ts +0 -6
- package/node/types/errors/NotFoundError.d.ts +0 -4
- package/node/types/errors/RootFolderNotFoundError.d.ts +0 -3
- package/node/types/errors/SAS9AuthError.d.ts +0 -3
- package/node/types/errors/WeboutResponseError.d.ts +0 -4
- package/node/types/errors/index.d.ts +0 -15
- package/node/types/index.d.ts +0 -18
- package/node/utils/appendExtraResponseAttributes.d.ts +0 -2
- package/node/utils/asyncForEach.d.ts +0 -1
- package/node/utils/compareTimestamps.d.ts +0 -6
- package/node/utils/convertToCsv.d.ts +0 -12
- package/node/utils/createAxiosInstance.d.ts +0 -2
- package/node/utils/delay.d.ts +0 -1
- package/node/utils/extractWeboutBlob.d.ts +0 -13
- package/node/utils/fetchLogByChunks.d.ts +0 -20
- package/node/utils/formatDataForRequest.d.ts +0 -1
- package/node/utils/getFormData.d.ts +0 -2
- package/node/utils/getValidJson.d.ts +0 -5
- package/node/utils/index.d.ts +0 -25
- package/node/utils/isNode.d.ts +0 -1
- package/node/utils/isRelativePath.d.ts +0 -1
- package/node/utils/isUri.d.ts +0 -5
- package/node/utils/isUrl.d.ts +0 -5
- package/node/utils/loginPrompt/index.d.ts +0 -1
- package/node/utils/needsRetry.d.ts +0 -1
- package/node/utils/parseGeneratedCode.d.ts +0 -1
- package/node/utils/parseSasViyaLog.d.ts +0 -5
- package/node/utils/parseSourceCode.d.ts +0 -1
- package/node/utils/parseViyaDebugResponse.d.ts +0 -12
- package/node/utils/parseViyaLogDebugResponse.d.ts +0 -8
- package/node/utils/parseWeboutResponse.d.ts +0 -1
- package/node/utils/sas9/extractUserLongNameSas9.d.ts +0 -6
- package/node/utils/serialize.d.ts +0 -1
- package/node/utils/splitChunks.d.ts +0 -1
- package/node/utils/validateInput.d.ts +0 -14
- package/request/spec/SasjsRequestClient.spec.d.ts +0 -1
- package/screenshots/session-manager-first-request.png +0 -0
- package/screenshots/subsequent-session-request.png +0 -0
- package/spec/SAS9ApiClient.spec.d.ts +0 -1
- package/spec/SASjs.startComputeJob.spec.d.ts +0 -1
- package/spec/SASjsApiClient.spec.d.ts +0 -1
- package/src/ContextManager.ts +0 -454
- package/src/SAS9ApiClient.ts +0 -100
- package/src/SASViyaApiClient.ts +0 -1417
- package/src/SASjs.ts +0 -1268
- package/src/SASjsApiClient.ts +0 -177
- package/src/SessionManager.ts +0 -465
- package/src/__mocks__/axios.ts +0 -7
- package/src/api/viya/executeOnComputeApi.ts +0 -307
- package/src/api/viya/getFileStream.ts +0 -17
- package/src/api/viya/pollJobState.ts +0 -393
- package/src/api/viya/saveLog.ts +0 -55
- package/src/api/viya/spec/mockResponses.ts +0 -75
- package/src/api/viya/uploadTables.ts +0 -41
- package/src/api/viya/writeStream.ts +0 -16
- package/src/auth/AuthManager.ts +0 -387
- package/src/auth/getAccessTokenForSasjs.ts +0 -48
- package/src/auth/getAccessTokenForViya.ts +0 -61
- package/src/auth/getTokenRequestErrorPrefix.ts +0 -88
- package/src/auth/getTokens.ts +0 -127
- package/src/auth/index.ts +0 -4
- package/src/auth/isAuthorizeFormRequired.ts +0 -3
- package/src/auth/isLoginRequired.ts +0 -5
- package/src/auth/loginHeader.ts +0 -97
- package/src/auth/openWebPage.ts +0 -40
- package/src/auth/refreshTokensForSasjs.ts +0 -45
- package/src/auth/refreshTokensForViya.ts +0 -66
- package/src/auth/spec/mockResponses.ts +0 -53
- package/src/auth/verifySas9Login.ts +0 -25
- package/src/auth/verifySasViyaLogin.ts +0 -43
- package/src/file/generateFileUploadForm.ts +0 -48
- package/src/file/generateTableUploadForm.ts +0 -50
- package/src/index.ts +0 -9
- package/src/job-execution/ComputeJobExecutor.ts +0 -76
- package/src/job-execution/FileUploader.ts +0 -152
- package/src/job-execution/JesJobExecutor.ts +0 -78
- package/src/job-execution/JobExecutor.ts +0 -63
- package/src/job-execution/Sas9JobExecutor.ts +0 -137
- package/src/job-execution/SasjsJobExecutor.ts +0 -152
- package/src/job-execution/WebJobExecutor.ts +0 -341
- package/src/job-execution/index.ts +0 -7
- package/src/minified/sas9/SASjs.ts +0 -274
- package/src/minified/sas9/WebJobExecutor.ts +0 -156
- package/src/minified/sas9/index.ts +0 -3
- package/src/request/RequestClient.ts +0 -941
- package/src/request/Sas9RequestClient.ts +0 -132
- package/src/request/SasjsRequestClient.ts +0 -82
- package/src/test/SAS_server_app.ts +0 -47
- package/src/types/Context.ts +0 -39
- package/src/types/CsrfToken.ts +0 -4
- package/src/types/ExecuteScript.ts +0 -4
- package/src/types/File.ts +0 -8
- package/src/types/FileResource.ts +0 -33
- package/src/types/Folder.ts +0 -8
- package/src/types/Job.ts +0 -15
- package/src/types/JobDefinition.ts +0 -3
- package/src/types/JobResult.ts +0 -3
- package/src/types/Link.ts +0 -7
- package/src/types/LogStatistics.ts +0 -4
- package/src/types/Login.ts +0 -15
- package/src/types/PollOptions.ts +0 -9
- package/src/types/Process.d.ts +0 -5
- package/src/types/RequestClient.ts +0 -55
- package/src/types/SASjsConfig.ts +0 -95
- package/src/types/Session.ts +0 -34
- package/src/types/Tables.ts +0 -29
- package/src/types/UploadFile.ts +0 -8
- package/src/types/WriteStream.ts +0 -10
- package/src/types/errors/ArgumentError.ts +0 -7
- package/src/types/errors/AuthorizeError.ts +0 -10
- package/src/types/errors/CertificateError.ts +0 -12
- package/src/types/errors/ComputeJobExecutionError.ts +0 -12
- package/src/types/errors/ErrorResponse.ts +0 -28
- package/src/types/errors/InternalServerError.ts +0 -9
- package/src/types/errors/InvalidJsonError.ts +0 -7
- package/src/types/errors/InvalidSASjsCsrfError.ts +0 -9
- package/src/types/errors/JobExecutionError.ts +0 -11
- package/src/types/errors/JobStatePollError.ts +0 -14
- package/src/types/errors/JsonParseArrayError.ts +0 -7
- package/src/types/errors/LoginRequiredError.ts +0 -11
- package/src/types/errors/NoSessionStateError.ts +0 -15
- package/src/types/errors/NotFoundError.ts +0 -7
- package/src/types/errors/RootFolderNotFoundError.ts +0 -24
- package/src/types/errors/SAS9AuthError.ts +0 -9
- package/src/types/errors/WeboutResponseError.ts +0 -7
- package/src/types/errors/index.ts +0 -15
- package/src/types/index.ts +0 -18
- package/src/types/system/global.d.ts +0 -1
- package/src/utils/appendExtraResponseAttributes.ts +0 -22
- package/src/utils/asyncForEach.ts +0 -5
- package/src/utils/compareTimestamps.ts +0 -9
- package/src/utils/convertToCsv.ts +0 -198
- package/src/utils/createAxiosInstance.ts +0 -7
- package/src/utils/delay.ts +0 -2
- package/src/utils/fetchLogByChunks.ts +0 -63
- package/src/utils/formatDataForRequest.ts +0 -48
- package/src/utils/getFormData.ts +0 -5
- package/src/utils/getUserLanguage.ts +0 -10
- package/src/utils/getValidJson.ts +0 -23
- package/src/utils/index.ts +0 -25
- package/src/utils/isIeOrEdge.ts +0 -34
- package/src/utils/isNode.ts +0 -4
- package/src/utils/isRelativePath.ts +0 -2
- package/src/utils/isUri.ts +0 -5
- package/src/utils/isUrl.ts +0 -17
- package/src/utils/loginPrompt/index.ts +0 -177
- package/src/utils/needsRetry.ts +0 -14
- package/src/utils/parseGeneratedCode.ts +0 -7
- package/src/utils/parseSasViyaLog.ts +0 -12
- package/src/utils/parseSourceCode.ts +0 -6
- package/src/utils/parseViyaDebugResponse.ts +0 -46
- package/src/utils/parseViyaLogDebugResponse.ts +0 -22
- package/src/utils/parseWeboutResponse.ts +0 -20
- package/src/utils/sas9/extractUserLongNameSas9.ts +0 -31
- package/src/utils/serialize.ts +0 -15
- package/src/utils/splitChunks.ts +0 -12
- package/src/utils/validateInput.ts +0 -93
- package/test/ContextManager.spec.d.ts +0 -1
- package/test/FileUploader.spec.d.ts +0 -4
- package/test/FolderOperations.spec.d.ts +0 -1
- package/test/RequestClient.spec.d.ts +0 -1
- package/test/SAS_server_app.d.ts +0 -10
- package/test/SessionManager.spec.d.ts +0 -1
- package/test/getJobsInFolder.spec.d.ts +0 -1
- package/test/utils/formatDataForRequest.spec.d.ts +0 -1
- package/test/utils/getValidJson.spec.d.ts +0 -1
- package/test/utils/isUrl.spec.d.ts +0 -1
- package/test/utils/parseGeneratedCode.spec.d.ts +0 -1
- package/test/utils/parseSourceCode.spec.d.ts +0 -1
- package/types/Tables.spec.d.ts +0 -1
- package/types/errors/RootFolderNotFoundError.spec.d.ts +0 -1
- package/types/errors/spec/SAS9AuthError.spec.d.ts +0 -1
- package/utils/spec/convertToCsv.spec.d.ts +0 -1
- package/utils/spec/extractUserLongNameSas9.spec.d.ts +0 -1
- package/utils/spec/getFormData.spec.d.ts +0 -1
- package/utils/spec/parseSasViyaLog.spec.d.ts +0 -1
- package/utils/spec/parseViyaDebugResponse.spec.d.ts +0 -1
- package/utils/spec/parseViyaLogDebugResponse.spec.d.ts +0 -1
- package/utils/spec/validateInput.spec.d.ts +0 -1
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.throwIfError = exports.RequestClient = void 0;
|
|
13
|
+
const auth_1 = require("../auth");
|
|
14
|
+
const errors_1 = require("../types/errors");
|
|
15
|
+
const parseWeboutResponse_1 = require("../utils/parseWeboutResponse");
|
|
16
|
+
const error_1 = require("@sasjs/utils/error");
|
|
17
|
+
const SAS9AuthError_1 = require("../types/errors/SAS9AuthError");
|
|
18
|
+
const utils_1 = require("../utils");
|
|
19
|
+
const InvalidSASjsCsrfError_1 = require("../types/errors/InvalidSASjsCsrfError");
|
|
20
|
+
const host_1 = require("../utils/host");
|
|
21
|
+
const isNode_1 = require("../utils/isNode");
|
|
22
|
+
class RequestClient {
|
|
23
|
+
constructor(baseUrl, httpsAgentOptions, requestsLimit, verboseMode) {
|
|
24
|
+
this.baseUrl = baseUrl;
|
|
25
|
+
this.requests = [];
|
|
26
|
+
this.requestsLimit = 10;
|
|
27
|
+
this.verboseMode = false;
|
|
28
|
+
this.csrfToken = { headerName: '', value: '' };
|
|
29
|
+
this.isRecoveringFromNetworkError = false;
|
|
30
|
+
/**
|
|
31
|
+
* this method returns all requests, an array of SASjsRequest type
|
|
32
|
+
* @returns SASjsRequest[]
|
|
33
|
+
*/
|
|
34
|
+
this.getRequests = () => this.requests;
|
|
35
|
+
/**
|
|
36
|
+
* this method clears the requests array, i.e set to empty
|
|
37
|
+
*/
|
|
38
|
+
this.clearRequests = () => {
|
|
39
|
+
this.requests = [];
|
|
40
|
+
};
|
|
41
|
+
this.authorize = (response) => __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
let authUrl = null;
|
|
43
|
+
const params = {};
|
|
44
|
+
const responseBody = response.split('<body>')[1].split('</body>')[0];
|
|
45
|
+
const bodyElement = document.createElement('div');
|
|
46
|
+
bodyElement.innerHTML = responseBody;
|
|
47
|
+
const form = bodyElement.querySelector('#application_authorization');
|
|
48
|
+
authUrl = form ? this.baseUrl + form.getAttribute('action') : null;
|
|
49
|
+
const inputs = form === null || form === void 0 ? void 0 : form.querySelectorAll('input');
|
|
50
|
+
for (const input of inputs) {
|
|
51
|
+
if (input.name === 'user_oauth_approval') {
|
|
52
|
+
input.value = 'true';
|
|
53
|
+
}
|
|
54
|
+
params[input.name] = input.value;
|
|
55
|
+
}
|
|
56
|
+
const csrfTokenKey = Object.keys(params).find((k) => k === null || k === void 0 ? void 0 : k.toLowerCase().includes('csrf'));
|
|
57
|
+
if (csrfTokenKey) {
|
|
58
|
+
this.csrfToken.value = params[csrfTokenKey];
|
|
59
|
+
this.csrfToken.headerName = this.csrfToken.headerName || 'x-csrf-token';
|
|
60
|
+
}
|
|
61
|
+
const formData = new FormData();
|
|
62
|
+
for (const key in params) {
|
|
63
|
+
if (params.hasOwnProperty(key)) {
|
|
64
|
+
formData.append(key, params[key]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (!authUrl) {
|
|
68
|
+
throw new Error('Auth Form URL is null or undefined.');
|
|
69
|
+
}
|
|
70
|
+
return yield this.httpClient
|
|
71
|
+
.post(authUrl, formData, {
|
|
72
|
+
responseType: 'text',
|
|
73
|
+
headers: { Accept: '*/*', 'Content-Type': 'text/plain' }
|
|
74
|
+
})
|
|
75
|
+
.then((res) => res.data)
|
|
76
|
+
.catch((error) => {
|
|
77
|
+
const logger = (0, host_1.getLogger)();
|
|
78
|
+
logger.error(error);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* Adds colors to the string.
|
|
83
|
+
* If verboseMode is set to 'bleached', colors should be disabled
|
|
84
|
+
* @param str - string to be prettified.
|
|
85
|
+
* @returns - prettified string
|
|
86
|
+
*/
|
|
87
|
+
this.prettifyString = (str) => (0, host_1.inspectValue)(str, this.verboseMode !== 'bleached');
|
|
88
|
+
/**
|
|
89
|
+
* Formats HTTP request/response body.
|
|
90
|
+
* @param body - HTTP request/response body.
|
|
91
|
+
* @returns - formatted string.
|
|
92
|
+
*/
|
|
93
|
+
this.parseInterceptedBody = (body) => {
|
|
94
|
+
if (!body)
|
|
95
|
+
return '';
|
|
96
|
+
let parsedBody;
|
|
97
|
+
// Tries to parse body into JSON object.
|
|
98
|
+
if (typeof body === 'string') {
|
|
99
|
+
try {
|
|
100
|
+
parsedBody = JSON.parse(body);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
parsedBody = body;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
parsedBody = body;
|
|
108
|
+
}
|
|
109
|
+
const bodyLines = this.prettifyString(parsedBody).split('\n');
|
|
110
|
+
// Leaves first 50 lines
|
|
111
|
+
if (bodyLines.length > 51) {
|
|
112
|
+
bodyLines.splice(50);
|
|
113
|
+
bodyLines.push('...');
|
|
114
|
+
}
|
|
115
|
+
return bodyLines.join('\n');
|
|
116
|
+
};
|
|
117
|
+
this.handleAxiosResponse = (response) => {
|
|
118
|
+
var _a, _b, _c;
|
|
119
|
+
const { status, config, request, data } = response;
|
|
120
|
+
const reqHeaders = (_a = request === null || request === void 0 ? void 0 : request._header) !== null && _a !== void 0 ? _a : 'Not provided\n';
|
|
121
|
+
const rawHeaders = (_c = (_b = request === null || request === void 0 ? void 0 : request.res) === null || _b === void 0 ? void 0 : _b.rawHeaders) !== null && _c !== void 0 ? _c : ['Not provided'];
|
|
122
|
+
const resHeaders = this.formatHeaders(rawHeaders);
|
|
123
|
+
const parsedResBody = this.parseInterceptedBody(data);
|
|
124
|
+
(0, host_1.getLogger)().info(`HTTP Request (first 50 lines):
|
|
125
|
+
${reqHeaders}${this.parseInterceptedBody(config.data)}
|
|
126
|
+
|
|
127
|
+
HTTP Response Code: ${this.prettifyString(status)}
|
|
128
|
+
|
|
129
|
+
HTTP Response (first 50 lines):
|
|
130
|
+
${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
|
|
131
|
+
`);
|
|
132
|
+
return response;
|
|
133
|
+
};
|
|
134
|
+
this.handleAxiosError = (error) => {
|
|
135
|
+
var _a, _b, _c, _d, _e;
|
|
136
|
+
// Message indicating absent value.
|
|
137
|
+
const noValueMessage = 'Not provided';
|
|
138
|
+
const { response, request, config } = error;
|
|
139
|
+
// Fallback request object that can be safely used to form request summary.
|
|
140
|
+
// _header is not present in responses with status 1**
|
|
141
|
+
// rawHeaders are not present in responses with status 1**
|
|
142
|
+
let fallbackRequest = {
|
|
143
|
+
_header: `${noValueMessage}\n`,
|
|
144
|
+
res: { rawHeaders: [noValueMessage] }
|
|
145
|
+
};
|
|
146
|
+
if (request) {
|
|
147
|
+
fallbackRequest = {
|
|
148
|
+
_header: (_c = (_a = request._header) !== null && _a !== void 0 ? _a : (_b = request._currentRequest) === null || _b === void 0 ? void 0 : _b._header) !== null && _c !== void 0 ? _c : noValueMessage,
|
|
149
|
+
res: { rawHeaders: (_e = (_d = request.res) === null || _d === void 0 ? void 0 : _d.rawHeaders) !== null && _e !== void 0 ? _e : [noValueMessage] }
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
// Fallback response object that can be safely used to form response summary.
|
|
153
|
+
let fallbackResponse = response || {
|
|
154
|
+
status: noValueMessage,
|
|
155
|
+
request: fallbackRequest,
|
|
156
|
+
config: config || {
|
|
157
|
+
data: noValueMessage,
|
|
158
|
+
headers: {}
|
|
159
|
+
},
|
|
160
|
+
data: noValueMessage
|
|
161
|
+
};
|
|
162
|
+
const { status, request: req, data: resData } = fallbackResponse;
|
|
163
|
+
const { _header: reqHeaders, res } = req;
|
|
164
|
+
const resHeaders = this.formatHeaders(res.rawHeaders);
|
|
165
|
+
const parsedResBody = this.parseInterceptedBody(resData);
|
|
166
|
+
(0, host_1.getLogger)().info(`HTTP Request (first 50 lines):
|
|
167
|
+
${reqHeaders}${this.parseInterceptedBody(config === null || config === void 0 ? void 0 : config.data)}
|
|
168
|
+
|
|
169
|
+
HTTP Response Code: ${this.prettifyString(status)}
|
|
170
|
+
|
|
171
|
+
HTTP Response (first 50 lines):
|
|
172
|
+
${resHeaders}${parsedResBody ? `\n\n${parsedResBody}` : ''}
|
|
173
|
+
`);
|
|
174
|
+
return error;
|
|
175
|
+
};
|
|
176
|
+
// Converts an array of strings into a single string with the following format:
|
|
177
|
+
// <headerName>: <headerValue>
|
|
178
|
+
this.formatHeaders = (rawHeaders) => {
|
|
179
|
+
return rawHeaders.reduce((acc, value, i) => {
|
|
180
|
+
if (i % 2 === 0) {
|
|
181
|
+
acc += `${i === 0 ? '' : '\n'}${value}`;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
acc += `: ${value}`;
|
|
185
|
+
}
|
|
186
|
+
return acc;
|
|
187
|
+
}, '');
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Sets verbose mode.
|
|
191
|
+
* @param verboseMode - value of the verbose mode, can be true, false or bleached(without extra colors).
|
|
192
|
+
*/
|
|
193
|
+
this.setVerboseMode = (verboseMode) => {
|
|
194
|
+
this.verboseMode = verboseMode;
|
|
195
|
+
if (this.verboseMode)
|
|
196
|
+
this.enableVerboseMode();
|
|
197
|
+
else
|
|
198
|
+
this.disableVerboseMode();
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Turns on verbose mode to log every HTTP response.
|
|
202
|
+
* @param successCallBack - function that should be triggered on every HTTP response with the status 2**.
|
|
203
|
+
* @param errorCallBack - function that should be triggered on every HTTP response with the status different from 2**.
|
|
204
|
+
*/
|
|
205
|
+
this.enableVerboseMode = (successCallBack = this.handleAxiosResponse, errorCallBack = this.handleAxiosError) => {
|
|
206
|
+
this.httpInterceptor = this.httpClient.interceptors.response.use(successCallBack, errorCallBack);
|
|
207
|
+
};
|
|
208
|
+
/**
|
|
209
|
+
* Turns off verbose mode to log every HTTP response.
|
|
210
|
+
*/
|
|
211
|
+
this.disableVerboseMode = () => {
|
|
212
|
+
if (this.httpInterceptor) {
|
|
213
|
+
this.httpClient.interceptors.response.eject(this.httpInterceptor);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
this.getHeaders = (accessToken, contentType) => {
|
|
217
|
+
const headers = {};
|
|
218
|
+
if (contentType !== 'application/x-www-form-urlencoded') {
|
|
219
|
+
headers['Content-Type'] = contentType;
|
|
220
|
+
}
|
|
221
|
+
if (contentType === 'application/json') {
|
|
222
|
+
headers.Accept = 'application/json';
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
headers.Accept = '*/*';
|
|
226
|
+
}
|
|
227
|
+
if (accessToken) {
|
|
228
|
+
headers.Authorization = `Bearer ${accessToken}`;
|
|
229
|
+
}
|
|
230
|
+
if (this.csrfToken.headerName && this.csrfToken.value) {
|
|
231
|
+
headers[this.csrfToken.headerName] = this.csrfToken.value;
|
|
232
|
+
}
|
|
233
|
+
return headers;
|
|
234
|
+
};
|
|
235
|
+
this.parseAndSetFileUploadCsrfToken = (response) => {
|
|
236
|
+
const token = this.parseCsrfToken(response);
|
|
237
|
+
if (token) {
|
|
238
|
+
this.fileUploadCsrfToken = token;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
this.parseAndSetCsrfToken = (response) => {
|
|
242
|
+
const token = this.parseCsrfToken(response);
|
|
243
|
+
if (token) {
|
|
244
|
+
this.csrfToken = token;
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
this.parseCsrfToken = (response) => {
|
|
248
|
+
var _a;
|
|
249
|
+
const tokenHeader = (_a = response.headers['x-csrf-header']) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
250
|
+
if (tokenHeader) {
|
|
251
|
+
const token = response.headers[tokenHeader];
|
|
252
|
+
const csrfToken = {
|
|
253
|
+
headerName: tokenHeader,
|
|
254
|
+
value: token || ''
|
|
255
|
+
};
|
|
256
|
+
return csrfToken;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
this.handleError = (e, callback, debug = false) => __awaiter(this, void 0, void 0, function* () {
|
|
260
|
+
var _a;
|
|
261
|
+
const response = e.response;
|
|
262
|
+
if (e instanceof errors_1.AuthorizeError) {
|
|
263
|
+
const res = yield this.httpClient
|
|
264
|
+
.get(e.confirmUrl, {
|
|
265
|
+
responseType: 'text',
|
|
266
|
+
headers: { 'Content-Type': 'text/plain', Accept: '*/*' }
|
|
267
|
+
})
|
|
268
|
+
.catch((err) => {
|
|
269
|
+
throw (0, error_1.prefixMessage)(err, 'Error while getting error confirmUrl. ');
|
|
270
|
+
});
|
|
271
|
+
if ((0, auth_1.isAuthorizeFormRequired)(res === null || res === void 0 ? void 0 : res.data)) {
|
|
272
|
+
yield this.authorize(res.data).catch((err) => {
|
|
273
|
+
throw (0, error_1.prefixMessage)(err, 'Error while authorizing request. ');
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
return yield callback().catch((err) => {
|
|
277
|
+
throw (0, error_1.prefixMessage)(err, 'Error while executing callback in handleError. ');
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
if (e instanceof errors_1.LoginRequiredError) {
|
|
281
|
+
this.clearCsrfTokens();
|
|
282
|
+
throw e;
|
|
283
|
+
}
|
|
284
|
+
if (e instanceof InvalidSASjsCsrfError_1.InvalidSASjsCsrfError) {
|
|
285
|
+
// Fetching root and creating CSRF cookie
|
|
286
|
+
yield this.httpClient
|
|
287
|
+
.get('/', {
|
|
288
|
+
withXSRFToken: true
|
|
289
|
+
})
|
|
290
|
+
.then((response) => {
|
|
291
|
+
var _a;
|
|
292
|
+
const cookie = (_a = /<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(response.data)) === null || _a === void 0 ? void 0 : _a[1];
|
|
293
|
+
if (cookie)
|
|
294
|
+
document.cookie = cookie;
|
|
295
|
+
})
|
|
296
|
+
.catch((err) => {
|
|
297
|
+
throw (0, error_1.prefixMessage)(err, 'Error while re-fetching CSRF token.');
|
|
298
|
+
});
|
|
299
|
+
return yield callback().catch((err) => {
|
|
300
|
+
throw (0, error_1.prefixMessage)(err, 'Error while executing callback in handleError. ');
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
if ((response === null || response === void 0 ? void 0 : response.status) === 403 || (response === null || response === void 0 ? void 0 : response.status) === 449) {
|
|
304
|
+
this.parseAndSetCsrfToken(response);
|
|
305
|
+
if (this.csrfToken.headerName && this.csrfToken.value) {
|
|
306
|
+
return yield callback().catch((err) => {
|
|
307
|
+
throw (0, error_1.prefixMessage)(err, 'Error while executing callback in handleError. ');
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
throw e;
|
|
311
|
+
}
|
|
312
|
+
else if ((response === null || response === void 0 ? void 0 : response.status) === 404) {
|
|
313
|
+
throw new errors_1.NotFoundError(response.config.url);
|
|
314
|
+
}
|
|
315
|
+
else if ((response === null || response === void 0 ? void 0 : response.status) === 502) {
|
|
316
|
+
if (debug)
|
|
317
|
+
throw new errors_1.InternalServerError();
|
|
318
|
+
else
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (e.isAxiosError && e.code === 'UNABLE_TO_VERIFY_LEAF_SIGNATURE') {
|
|
322
|
+
throw new errors_1.CertificateError(e.message);
|
|
323
|
+
}
|
|
324
|
+
if (e.isAxiosError &&
|
|
325
|
+
!response &&
|
|
326
|
+
e.code === 'ERR_NETWORK' &&
|
|
327
|
+
!this.isRecoveringFromNetworkError) {
|
|
328
|
+
// Opaque ERR_NETWORK usually means the server rejected stale credentials.
|
|
329
|
+
// Wipe in-memory auth state, re-establish session via GET /,
|
|
330
|
+
// then retry the original request.
|
|
331
|
+
this.resetInMemoryAuthState();
|
|
332
|
+
this.isRecoveringFromNetworkError = true;
|
|
333
|
+
try {
|
|
334
|
+
// Re-establish session and CSRF cookie
|
|
335
|
+
const rootResponse = yield this.httpClient
|
|
336
|
+
.get('/', { withXSRFToken: true })
|
|
337
|
+
.catch((err) => err.response);
|
|
338
|
+
if (rootResponse === null || rootResponse === void 0 ? void 0 : rootResponse.data) {
|
|
339
|
+
const cookie = (_a = /<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(rootResponse.data)) === null || _a === void 0 ? void 0 : _a[1];
|
|
340
|
+
if (cookie && typeof document !== 'undefined') {
|
|
341
|
+
document.cookie = cookie;
|
|
342
|
+
}
|
|
343
|
+
this.parseAndSetCsrfToken(rootResponse);
|
|
344
|
+
}
|
|
345
|
+
return yield callback();
|
|
346
|
+
}
|
|
347
|
+
catch (_b) {
|
|
348
|
+
// Session could not be recovered — surface LoginRequiredError
|
|
349
|
+
throw new errors_1.LoginRequiredError();
|
|
350
|
+
}
|
|
351
|
+
finally {
|
|
352
|
+
this.isRecoveringFromNetworkError = false;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (e.message)
|
|
356
|
+
throw e;
|
|
357
|
+
else
|
|
358
|
+
throw (0, error_1.prefixMessage)(e, 'Error while handling error. ');
|
|
359
|
+
});
|
|
360
|
+
this.createHttpClient(baseUrl, httpsAgentOptions);
|
|
361
|
+
if (requestsLimit)
|
|
362
|
+
this.requestsLimit = requestsLimit;
|
|
363
|
+
if (verboseMode) {
|
|
364
|
+
this.setVerboseMode(verboseMode);
|
|
365
|
+
this.enableVerboseMode();
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
setConfig(baseUrl, httpsAgentOptions) {
|
|
369
|
+
this.createHttpClient(baseUrl, httpsAgentOptions);
|
|
370
|
+
}
|
|
371
|
+
saveLocalStorageToken(accessToken, refreshToken) {
|
|
372
|
+
localStorage.setItem('accessToken', accessToken);
|
|
373
|
+
localStorage.setItem('refreshToken', refreshToken);
|
|
374
|
+
}
|
|
375
|
+
getCsrfToken(type = 'general') {
|
|
376
|
+
return type === 'file' ? this.fileUploadCsrfToken : this.csrfToken;
|
|
377
|
+
}
|
|
378
|
+
clearCsrfTokens() {
|
|
379
|
+
this.csrfToken = { headerName: '', value: '' };
|
|
380
|
+
this.fileUploadCsrfToken = { headerName: '', value: '' };
|
|
381
|
+
}
|
|
382
|
+
clearLocalStorageTokens() {
|
|
383
|
+
localStorage.setItem('accessToken', '');
|
|
384
|
+
localStorage.setItem('refreshToken', '');
|
|
385
|
+
}
|
|
386
|
+
resetInMemoryAuthState() {
|
|
387
|
+
this.clearCsrfTokens();
|
|
388
|
+
if (typeof localStorage !== 'undefined') {
|
|
389
|
+
this.clearLocalStorageTokens();
|
|
390
|
+
}
|
|
391
|
+
if (typeof document !== 'undefined') {
|
|
392
|
+
this.clearAllCookies();
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
clearAllCookies() {
|
|
396
|
+
const cookies = document.cookie.split(';');
|
|
397
|
+
for (const cookie of cookies) {
|
|
398
|
+
const name = cookie.split('=')[0].trim();
|
|
399
|
+
if (!name)
|
|
400
|
+
continue;
|
|
401
|
+
document.cookie = `${name}=; Max-Age=0; Path=/;`;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
getBaseUrl() {
|
|
405
|
+
return this.httpClient.defaults.baseURL || '';
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* this method appends the response from sasjs request to requests array
|
|
409
|
+
* @param response - response from sasjs request
|
|
410
|
+
* @param program - name of program
|
|
411
|
+
* @param debug - a boolean that indicates whether debug was enabled or not
|
|
412
|
+
*/
|
|
413
|
+
appendRequest(response, program, debug) {
|
|
414
|
+
let sourceCode = '';
|
|
415
|
+
let generatedCode = '';
|
|
416
|
+
let sasWork = null;
|
|
417
|
+
if (debug) {
|
|
418
|
+
if (response === null || response === void 0 ? void 0 : response.log) {
|
|
419
|
+
sourceCode = (0, utils_1.parseSourceCode)(response.log);
|
|
420
|
+
generatedCode = (0, utils_1.parseGeneratedCode)(response.log);
|
|
421
|
+
if (response === null || response === void 0 ? void 0 : response.result) {
|
|
422
|
+
sasWork = response.result.WORK;
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
sasWork = response.log;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
else if (response === null || response === void 0 ? void 0 : response.result) {
|
|
429
|
+
// We parse only if it's a string, otherwise it would throw error
|
|
430
|
+
if (typeof response.result === 'string') {
|
|
431
|
+
sourceCode = (0, utils_1.parseSourceCode)(response.result);
|
|
432
|
+
generatedCode = (0, utils_1.parseGeneratedCode)(response.result);
|
|
433
|
+
}
|
|
434
|
+
sasWork = response.result.WORK;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
const stringifiedResult = typeof (response === null || response === void 0 ? void 0 : response.result) === 'string'
|
|
438
|
+
? response === null || response === void 0 ? void 0 : response.result
|
|
439
|
+
: JSON.stringify(response === null || response === void 0 ? void 0 : response.result, null, 2);
|
|
440
|
+
this.requests.push({
|
|
441
|
+
logFile: (response === null || response === void 0 ? void 0 : response.log) || stringifiedResult || response,
|
|
442
|
+
serviceLink: program,
|
|
443
|
+
timestamp: new Date(),
|
|
444
|
+
sourceCode,
|
|
445
|
+
generatedCode,
|
|
446
|
+
SASWORK: sasWork
|
|
447
|
+
});
|
|
448
|
+
if (this.requests.length > this.requestsLimit) {
|
|
449
|
+
this.requests.splice(0, 1);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
get(url, accessToken, contentType = 'application/json', overrideHeaders = {}, debug = false) {
|
|
453
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
454
|
+
const headers = Object.assign(Object.assign({}, this.getHeaders(accessToken, contentType)), overrideHeaders);
|
|
455
|
+
const requestConfig = {
|
|
456
|
+
headers,
|
|
457
|
+
responseType: contentType === 'text/plain' ? 'text' : 'json',
|
|
458
|
+
withXSRFToken: true
|
|
459
|
+
};
|
|
460
|
+
if (contentType === 'text/plain') {
|
|
461
|
+
requestConfig.transformResponse = undefined;
|
|
462
|
+
}
|
|
463
|
+
return this.httpClient
|
|
464
|
+
.get(url, requestConfig)
|
|
465
|
+
.then((response) => {
|
|
466
|
+
(0, exports.throwIfError)(response);
|
|
467
|
+
return this.parseResponse(response);
|
|
468
|
+
})
|
|
469
|
+
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
470
|
+
return yield this.handleError(e, () => this.get(url, accessToken, contentType, overrideHeaders).catch((err) => {
|
|
471
|
+
throw (0, error_1.prefixMessage)(err, 'Error while executing handle error callback. ');
|
|
472
|
+
}), debug);
|
|
473
|
+
}));
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* @param contentType Newer version of Axios is more strict so if you don't
|
|
478
|
+
* set the contentType to `form data` while sending a FormData object
|
|
479
|
+
* application/json will be used by default, axios won’t treat it as FormData.
|
|
480
|
+
* Instead, it serializes data as JSON—resulting in a payload like
|
|
481
|
+
* {"sometable":{}} and we lose the multipart/form-data formatting.
|
|
482
|
+
*/
|
|
483
|
+
post(url, data, accessToken, contentType = 'application/json', overrideHeaders = {}, additionalSettings = {}) {
|
|
484
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
485
|
+
const headers = Object.assign(Object.assign({}, this.getHeaders(accessToken, contentType)), overrideHeaders);
|
|
486
|
+
return this.httpClient
|
|
487
|
+
.post(url, data, Object.assign({ headers, withXSRFToken: true }, additionalSettings))
|
|
488
|
+
.then((response) => {
|
|
489
|
+
(0, exports.throwIfError)(response);
|
|
490
|
+
return this.parseResponse(response);
|
|
491
|
+
})
|
|
492
|
+
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
493
|
+
return yield this.handleError(e, () => this.post(url, data, accessToken, contentType, overrideHeaders));
|
|
494
|
+
}));
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
put(url, data, accessToken, overrideHeaders = {}) {
|
|
498
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
499
|
+
const headers = Object.assign(Object.assign({}, this.getHeaders(accessToken, 'application/json')), overrideHeaders);
|
|
500
|
+
return this.httpClient
|
|
501
|
+
.put(url, data, { headers, withXSRFToken: true })
|
|
502
|
+
.then((response) => {
|
|
503
|
+
(0, exports.throwIfError)(response);
|
|
504
|
+
return this.parseResponse(response);
|
|
505
|
+
})
|
|
506
|
+
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
507
|
+
return yield this.handleError(e, () => this.put(url, data, accessToken, overrideHeaders));
|
|
508
|
+
}));
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
delete(url, accessToken) {
|
|
512
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
513
|
+
const headers = this.getHeaders(accessToken, 'application/json');
|
|
514
|
+
return this.httpClient
|
|
515
|
+
.delete(url, { headers, withXSRFToken: true })
|
|
516
|
+
.then((response) => {
|
|
517
|
+
(0, exports.throwIfError)(response);
|
|
518
|
+
return this.parseResponse(response);
|
|
519
|
+
})
|
|
520
|
+
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
521
|
+
return yield this.handleError(e, () => this.delete(url, accessToken));
|
|
522
|
+
}));
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
patch(url, data = {}, accessToken, overrideHeaders = {}) {
|
|
526
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
527
|
+
const headers = Object.assign(Object.assign({}, this.getHeaders(accessToken, 'application/json')), overrideHeaders);
|
|
528
|
+
return this.httpClient
|
|
529
|
+
.patch(url, data, { headers, withXSRFToken: true })
|
|
530
|
+
.then((response) => {
|
|
531
|
+
(0, exports.throwIfError)(response);
|
|
532
|
+
return this.parseResponse(response);
|
|
533
|
+
})
|
|
534
|
+
.catch((e) => __awaiter(this, void 0, void 0, function* () {
|
|
535
|
+
return yield this.handleError(e, () => this.patch(url, data, accessToken));
|
|
536
|
+
}));
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
uploadFile(url, content, accessToken) {
|
|
540
|
+
var _a;
|
|
541
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
542
|
+
const headers = this.getHeaders(accessToken, 'application/json');
|
|
543
|
+
if ((_a = this.fileUploadCsrfToken) === null || _a === void 0 ? void 0 : _a.value) {
|
|
544
|
+
headers[this.fileUploadCsrfToken.headerName] =
|
|
545
|
+
this.fileUploadCsrfToken.value;
|
|
546
|
+
}
|
|
547
|
+
try {
|
|
548
|
+
const response = yield this.httpClient.post(url, content, {
|
|
549
|
+
headers,
|
|
550
|
+
transformRequest: (requestBody) => requestBody
|
|
551
|
+
});
|
|
552
|
+
return {
|
|
553
|
+
result: response.data,
|
|
554
|
+
etag: response.headers['etag']
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
catch (e) {
|
|
558
|
+
const response = e.response;
|
|
559
|
+
if ((response === null || response === void 0 ? void 0 : response.status) === 403 || (response === null || response === void 0 ? void 0 : response.status) === 449) {
|
|
560
|
+
this.parseAndSetFileUploadCsrfToken(response);
|
|
561
|
+
if (this.fileUploadCsrfToken) {
|
|
562
|
+
return this.uploadFile(url, content, accessToken);
|
|
563
|
+
}
|
|
564
|
+
throw e;
|
|
565
|
+
}
|
|
566
|
+
throw e;
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
parseResponse(response) {
|
|
571
|
+
const etag = (response === null || response === void 0 ? void 0 : response.headers) ? response.headers['etag'] : '';
|
|
572
|
+
let parsedResponse;
|
|
573
|
+
let includeSAS9Log = false;
|
|
574
|
+
try {
|
|
575
|
+
if (typeof response.data === 'string') {
|
|
576
|
+
parsedResponse = JSON.parse(response.data);
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
parsedResponse = response.data;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
catch (_a) {
|
|
583
|
+
try {
|
|
584
|
+
parsedResponse = JSON.parse((0, parseWeboutResponse_1.parseWeboutResponse)(response.data));
|
|
585
|
+
}
|
|
586
|
+
catch (_b) {
|
|
587
|
+
parsedResponse = response.data;
|
|
588
|
+
}
|
|
589
|
+
includeSAS9Log = true;
|
|
590
|
+
}
|
|
591
|
+
let responseToReturn = {
|
|
592
|
+
result: parsedResponse,
|
|
593
|
+
etag,
|
|
594
|
+
status: response.status
|
|
595
|
+
};
|
|
596
|
+
if (includeSAS9Log) {
|
|
597
|
+
responseToReturn.log = response.data;
|
|
598
|
+
}
|
|
599
|
+
return responseToReturn;
|
|
600
|
+
}
|
|
601
|
+
createHttpClient(baseUrl, httpsAgentOptions) {
|
|
602
|
+
// Node only: a browser has no https module, and axios uses XHR there.
|
|
603
|
+
const httpsAgent = (0, isNode_1.isNode)() && httpsAgentOptions
|
|
604
|
+
? new (require('https').Agent)(httpsAgentOptions)
|
|
605
|
+
: undefined;
|
|
606
|
+
this.httpClient = (0, utils_1.createAxiosInstance)(baseUrl, httpsAgent);
|
|
607
|
+
this.httpClient.defaults.validateStatus = (status) => {
|
|
608
|
+
return status >= 200 && status <= 401;
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
exports.RequestClient = RequestClient;
|
|
613
|
+
const throwIfError = (response) => {
|
|
614
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
615
|
+
switch (response.status) {
|
|
616
|
+
case 400:
|
|
617
|
+
if (typeof response.data === 'object' &&
|
|
618
|
+
response.data.error === 'invalid_grant') {
|
|
619
|
+
// In SASVIYA when trying to get access token, if auth code is wrong status code will be 400 so in such case we return login required error.
|
|
620
|
+
throw new errors_1.LoginRequiredError(response.data);
|
|
621
|
+
}
|
|
622
|
+
if (typeof response.data === 'string' &&
|
|
623
|
+
response.data.toLowerCase() === 'invalid csrf token!') {
|
|
624
|
+
throw new InvalidSASjsCsrfError_1.InvalidSASjsCsrfError();
|
|
625
|
+
}
|
|
626
|
+
break;
|
|
627
|
+
case 401:
|
|
628
|
+
if (typeof response.data === 'object') {
|
|
629
|
+
throw new errors_1.LoginRequiredError(response.data);
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
throw new errors_1.LoginRequiredError();
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
if ((_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.entityID) === null || _b === void 0 ? void 0 : _b.includes('login')) {
|
|
636
|
+
throw new errors_1.LoginRequiredError();
|
|
637
|
+
}
|
|
638
|
+
if (typeof response.data === 'string' &&
|
|
639
|
+
(0, auth_1.isAuthorizeFormRequired)(response.data)) {
|
|
640
|
+
throw new errors_1.AuthorizeError('Authorization required', response.request.responseURL);
|
|
641
|
+
}
|
|
642
|
+
if (typeof response.data === 'string' &&
|
|
643
|
+
(0, auth_1.isLogInRequired)(response.data) &&
|
|
644
|
+
!((_d = (_c = response.config) === null || _c === void 0 ? void 0 : _c.url) === null || _d === void 0 ? void 0 : _d.includes('/SASLogon/login'))) {
|
|
645
|
+
throw new errors_1.LoginRequiredError();
|
|
646
|
+
}
|
|
647
|
+
if ((_e = response.data) === null || _e === void 0 ? void 0 : _e.auth_request) {
|
|
648
|
+
const authorizeRequestUrl = response.request.responseURL;
|
|
649
|
+
throw new errors_1.AuthorizeError(response.data.message, authorizeRequestUrl);
|
|
650
|
+
}
|
|
651
|
+
if ((_g = (_f = response.config) === null || _f === void 0 ? void 0 : _f.url) === null || _g === void 0 ? void 0 : _g.includes('sasAuthError')) {
|
|
652
|
+
throw new SAS9AuthError_1.SAS9AuthError();
|
|
653
|
+
}
|
|
654
|
+
const error = parseError(response.data);
|
|
655
|
+
if (error) {
|
|
656
|
+
throw error;
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
exports.throwIfError = throwIfError;
|
|
660
|
+
const parseError = (data) => {
|
|
661
|
+
if (!data)
|
|
662
|
+
return null;
|
|
663
|
+
try {
|
|
664
|
+
const responseJson = JSON.parse(data === null || data === void 0 ? void 0 : data.replace(/[\n\r]/g, ' '));
|
|
665
|
+
if (responseJson.errorCode && responseJson.message) {
|
|
666
|
+
return new errors_1.JobExecutionError(responseJson.errorCode, responseJson.message, data === null || data === void 0 ? void 0 : data.replace(/[\n\r]/g, ' '));
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
catch (_) { }
|
|
670
|
+
try {
|
|
671
|
+
const hasError = data === null || data === void 0 ? void 0 : data.includes('{"errorCode');
|
|
672
|
+
if (hasError) {
|
|
673
|
+
const parts = data.split('{"errorCode');
|
|
674
|
+
if (parts.length > 1) {
|
|
675
|
+
const error = '{"errorCode' + parts[1].split('"}')[0] + '"}';
|
|
676
|
+
const errorJson = JSON.parse(error.replace(/[\n\r]/g, ' '));
|
|
677
|
+
return new errors_1.JobExecutionError(errorJson.errorCode, errorJson.message, data === null || data === void 0 ? void 0 : data.replace(/[\n\r]/g, '\n'));
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
catch (_) { }
|
|
682
|
+
try {
|
|
683
|
+
const hasError = !!(data === null || data === void 0 ? void 0 : data.match(/stored process not found: /i));
|
|
684
|
+
if (hasError) {
|
|
685
|
+
const parts = data.split(/stored process not found: /i);
|
|
686
|
+
if (parts.length > 1) {
|
|
687
|
+
const storedProcessPath = parts[1].split('<i>')[1].split('</i>')[0];
|
|
688
|
+
const message = storedProcessPath.endsWith('runner')
|
|
689
|
+
? `SASJS runner not found. Here's the link (https://cli.sasjs.io/auth/#sasjs-runner) to the SAS code for registering the SASjs runner`
|
|
690
|
+
: `Stored process not found: ${storedProcessPath}`;
|
|
691
|
+
return new errors_1.JobExecutionError(500, message, '');
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
catch (_) { }
|
|
696
|
+
try {
|
|
697
|
+
// There are some edge cases in which the SAS mp_abort macro
|
|
698
|
+
// (https://core.sasjs.io/mp__abort_8sas.html) is unable to
|
|
699
|
+
// provide a clean exit. In this case the JSON response will
|
|
700
|
+
// be wrapped in >>weboutBEGIN<< and >>weboutEND<< strings.
|
|
701
|
+
// Therefore, if the first string exists, we won't throw an
|
|
702
|
+
// error just yet (the parser may yet throw one instead)
|
|
703
|
+
const hasError = !(data === null || data === void 0 ? void 0 : data.match(/>>weboutBEGIN<</)) &&
|
|
704
|
+
!!(data === null || data === void 0 ? void 0 : data.match(/Stored Process Error/i)) &&
|
|
705
|
+
!!(data === null || data === void 0 ? void 0 : data.match(/This request completed with errors./i));
|
|
706
|
+
if (hasError) {
|
|
707
|
+
const parts = data.split('<h2>SAS Log</h2>');
|
|
708
|
+
if (parts.length > 1) {
|
|
709
|
+
const log = parts[1].split('<pre>')[1].split('</pre>')[0];
|
|
710
|
+
const message = `This request completed with errors.`;
|
|
711
|
+
return new errors_1.JobExecutionError(500, message, log);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
catch (_) { }
|
|
716
|
+
return null;
|
|
717
|
+
};
|