mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-10 17:04:36 +00:00
chore(git): Merge branch 'master' of https://github.com/sasjs/adapter into macro-vars
This commit is contained in:
5449
package-lock.json
generated
5449
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
21
package.json
21
package.json
@@ -38,25 +38,26 @@
|
||||
},
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^26.0.22",
|
||||
"@types/jest": "^26.0.23",
|
||||
"@types/tough-cookie": "^4.0.0",
|
||||
"cp": "^0.2.0",
|
||||
"dotenv": "^8.2.0",
|
||||
"jest": "^26.6.3",
|
||||
"dotenv": "^10.0.0",
|
||||
"jest": "^27.0.4",
|
||||
"jest-extended": "^0.11.5",
|
||||
"path": "^0.12.7",
|
||||
"process": "^0.11.10",
|
||||
"rimraf": "^3.0.2",
|
||||
"semantic-release": "^17.4.2",
|
||||
"terser-webpack-plugin": "^4.2.3",
|
||||
"ts-jest": "^25.5.1",
|
||||
"ts-loader": "^9.1.2",
|
||||
"semantic-release": "^17.4.3",
|
||||
"terser-webpack-plugin": "^5.1.3",
|
||||
"ts-jest": "^27.0.2",
|
||||
"ts-loader": "^9.2.2",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0",
|
||||
"typedoc": "^0.20.36",
|
||||
"typedoc-neo-theme": "^1.1.0",
|
||||
"typedoc-neo-theme": "^1.1.1",
|
||||
"typedoc-plugin-external-module-name": "^4.0.6",
|
||||
"typescript": "^3.9.9",
|
||||
"webpack": "^5.33.2",
|
||||
"typescript": "^4.3.2",
|
||||
"webpack": "^5.38.1",
|
||||
"webpack-cli": "^4.7.0"
|
||||
},
|
||||
"main": "index.js",
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import axios, { AxiosInstance } from 'axios'
|
||||
import { generateTimestamp } from '@sasjs/utils/time'
|
||||
import * as NodeFormData from 'form-data'
|
||||
import { Sas9RequestClient } from './request/Sas9RequestClient'
|
||||
import { isUrl } from './utils'
|
||||
|
||||
/**
|
||||
@@ -6,11 +8,11 @@ import { isUrl } from './utils'
|
||||
*
|
||||
*/
|
||||
export class SAS9ApiClient {
|
||||
private httpClient: AxiosInstance
|
||||
private requestClient: Sas9RequestClient
|
||||
|
||||
constructor(private serverUrl: string) {
|
||||
constructor(private serverUrl: string, private jobsPath: string) {
|
||||
if (serverUrl) isUrl(serverUrl)
|
||||
this.httpClient = axios.create({ baseURL: this.serverUrl })
|
||||
this.requestClient = new Sas9RequestClient(serverUrl, false)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,27 +35,52 @@ export class SAS9ApiClient {
|
||||
/**
|
||||
* Executes code on a SAS9 server.
|
||||
* @param linesOfCode - an array of code lines to execute.
|
||||
* @param serverName - the server to execute the code on.
|
||||
* @param repositoryName - the repository to execute the code in.
|
||||
* @param userName - the user name to log into the current SAS server.
|
||||
* @param password - the password to log into the current SAS server.
|
||||
*/
|
||||
public async executeScript(
|
||||
linesOfCode: string[],
|
||||
serverName: string,
|
||||
repositoryName: string
|
||||
userName: string,
|
||||
password: string
|
||||
) {
|
||||
const requestPayload = linesOfCode.join('\n')
|
||||
await this.requestClient.login(userName, password, this.jobsPath)
|
||||
|
||||
const executeScriptResponse = await this.httpClient.put(
|
||||
`/sas/servers/${serverName}/cmd?repositoryName=${repositoryName}`,
|
||||
`command=${requestPayload}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: 'application/json'
|
||||
},
|
||||
responseType: 'text'
|
||||
}
|
||||
const formData = generateFileUploadForm(linesOfCode.join('\n'))
|
||||
|
||||
const codeInjectorPath = `/User Folders/${userName}/My Folder/sasjs/runner`
|
||||
const contentType =
|
||||
'multipart/form-data; boundary=' + formData.getBoundary()
|
||||
const contentLength = formData.getLengthSync()
|
||||
|
||||
const headers = {
|
||||
'cache-control': 'no-cache',
|
||||
Accept: '*/*',
|
||||
'Content-Type': contentType,
|
||||
'Content-Length': contentLength,
|
||||
Connection: 'keep-alive'
|
||||
}
|
||||
const storedProcessUrl = `${this.jobsPath}/?${
|
||||
'_program=' + codeInjectorPath + '&_debug=log'
|
||||
}`
|
||||
const response = await this.requestClient.post(
|
||||
storedProcessUrl,
|
||||
formData,
|
||||
undefined,
|
||||
contentType,
|
||||
headers
|
||||
)
|
||||
|
||||
return executeScriptResponse.data
|
||||
return response.result as string
|
||||
}
|
||||
}
|
||||
|
||||
const generateFileUploadForm = (data: any): NodeFormData => {
|
||||
const formData = new NodeFormData()
|
||||
const filename = `sasjs-execute-sas9-${generateTimestamp('')}.sas`
|
||||
formData.append(filename, data, {
|
||||
filename,
|
||||
contentType: 'text/plain'
|
||||
})
|
||||
|
||||
return formData
|
||||
}
|
||||
|
||||
@@ -725,13 +725,11 @@ export class SASViyaApiClient {
|
||||
let formData
|
||||
if (typeof FormData === 'undefined') {
|
||||
formData = new NodeFormData()
|
||||
formData.append('grant_type', 'authorization_code')
|
||||
formData.append('code', authCode)
|
||||
} else {
|
||||
formData = new FormData()
|
||||
formData.append('grant_type', 'authorization_code')
|
||||
formData.append('code', authCode)
|
||||
}
|
||||
formData.append('grant_type', 'authorization_code')
|
||||
formData.append('code', authCode)
|
||||
|
||||
const authResponse = await this.requestClient
|
||||
.post(
|
||||
|
||||
16
src/SASjs.ts
16
src/SASjs.ts
@@ -59,15 +59,15 @@ export default class SASjs {
|
||||
|
||||
public async executeScriptSAS9(
|
||||
linesOfCode: string[],
|
||||
serverName: string,
|
||||
repositoryName: string
|
||||
userName: string,
|
||||
password: string
|
||||
) {
|
||||
this.isMethodSupported('executeScriptSAS9', ServerType.Sas9)
|
||||
|
||||
return await this.sas9ApiClient?.executeScript(
|
||||
linesOfCode,
|
||||
serverName,
|
||||
repositoryName
|
||||
userName,
|
||||
password
|
||||
)
|
||||
}
|
||||
|
||||
@@ -624,7 +624,7 @@ export default class SASjs {
|
||||
)
|
||||
sasApiClient.debug = this.sasjsConfig.debug
|
||||
} else if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
||||
sasApiClient = new SAS9ApiClient(serverUrl)
|
||||
sasApiClient = new SAS9ApiClient(serverUrl, this.jobsPath)
|
||||
}
|
||||
} else {
|
||||
let sasClientConfig: any = null
|
||||
@@ -816,7 +816,11 @@ export default class SASjs {
|
||||
if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
||||
if (this.sas9ApiClient)
|
||||
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl)
|
||||
else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl)
|
||||
else
|
||||
this.sas9ApiClient = new SAS9ApiClient(
|
||||
this.sasjsConfig.serverUrl,
|
||||
this.jobsPath
|
||||
)
|
||||
}
|
||||
|
||||
this.fileUploader = new FileUploader(
|
||||
|
||||
@@ -57,7 +57,7 @@ describe('AuthManager', () => {
|
||||
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
|
||||
})
|
||||
|
||||
it('should call the auth callback and return when already logged in', async (done) => {
|
||||
it('should call the auth callback and return when already logged in', async () => {
|
||||
const authManager = new AuthManager(
|
||||
serverUrl,
|
||||
serverType,
|
||||
@@ -77,10 +77,9 @@ describe('AuthManager', () => {
|
||||
expect(loginResponse.isLoggedIn).toBeTruthy()
|
||||
expect(loginResponse.userName).toEqual(userName)
|
||||
expect(authCallback).toHaveBeenCalledTimes(1)
|
||||
done()
|
||||
})
|
||||
|
||||
it('should post a login request to the server if not logged in', async (done) => {
|
||||
it('should post a login request to the server if not logged in', async () => {
|
||||
const authManager = new AuthManager(
|
||||
serverUrl,
|
||||
serverType,
|
||||
@@ -121,10 +120,9 @@ describe('AuthManager', () => {
|
||||
}
|
||||
)
|
||||
expect(authCallback).toHaveBeenCalledTimes(1)
|
||||
done()
|
||||
})
|
||||
|
||||
it('should parse and submit the authorisation form when necessary', async (done) => {
|
||||
it('should parse and submit the authorisation form when necessary', async () => {
|
||||
const authManager = new AuthManager(
|
||||
serverUrl,
|
||||
serverType,
|
||||
@@ -160,10 +158,9 @@ describe('AuthManager', () => {
|
||||
expect(requestClient.authorize).toHaveBeenCalledWith(
|
||||
mockLoginAuthoriseRequiredResponse
|
||||
)
|
||||
done()
|
||||
})
|
||||
|
||||
it('should check and return session information if logged in', async (done) => {
|
||||
it('should check and return session information if logged in', async () => {
|
||||
const authManager = new AuthManager(
|
||||
serverUrl,
|
||||
serverType,
|
||||
@@ -189,7 +186,5 @@ describe('AuthManager', () => {
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import { FileUploader } from '../FileUploader'
|
||||
import { UploadFile } from '../types'
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
@@ -35,41 +39,40 @@ describe('FileUploader', () => {
|
||||
new RequestClient('https://sample.server.com')
|
||||
)
|
||||
|
||||
it('should upload successfully', async (done) => {
|
||||
it('should upload successfully', async () => {
|
||||
const sasJob = 'test/upload'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: sampleResponse })
|
||||
)
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then((res: any) => {
|
||||
expect(res).toEqual(JSON.parse(sampleResponse))
|
||||
done()
|
||||
})
|
||||
const res = await fileUploader.uploadFile(sasJob, files, params)
|
||||
|
||||
expect(res).toEqual(JSON.parse(sampleResponse))
|
||||
})
|
||||
|
||||
it('should an error when no files are provided', async (done) => {
|
||||
it('should an error when no files are provided', async () => {
|
||||
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()
|
||||
})
|
||||
const err = await fileUploader
|
||||
.uploadFile(sasJob, files, params)
|
||||
.catch((err: any) => err)
|
||||
expect(err.error.message).toEqual('At least one file must be provided.')
|
||||
})
|
||||
|
||||
it('should throw an error when no sasJob is provided', async (done) => {
|
||||
it('should throw an error when no sasJob is provided', async () => {
|
||||
const sasJob = ''
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('sasJob must be provided.')
|
||||
done()
|
||||
})
|
||||
const err = await fileUploader
|
||||
.uploadFile(sasJob, files, params)
|
||||
.catch((err: any) => err)
|
||||
expect(err.error.message).toEqual('sasJob must be provided.')
|
||||
})
|
||||
|
||||
it('should throw an error when login is required', async (done) => {
|
||||
it('should throw an error when login is required', async () => {
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: '<form action="Logon">' })
|
||||
)
|
||||
@@ -77,15 +80,13 @@ describe('FileUploader', () => {
|
||||
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()
|
||||
})
|
||||
const err = await fileUploader
|
||||
.uploadFile(sasJob, files, params)
|
||||
.catch((err: any) => err)
|
||||
expect(err.error.message).toEqual('You must be logged in to upload a file.')
|
||||
})
|
||||
|
||||
it('should throw an error when invalid JSON is returned by the server', async (done) => {
|
||||
it('should throw an error when invalid JSON is returned by the server', async () => {
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.resolve({ data: '{invalid: "json"' })
|
||||
)
|
||||
@@ -93,13 +94,13 @@ describe('FileUploader', () => {
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('File upload request failed.')
|
||||
done()
|
||||
})
|
||||
const err = await fileUploader
|
||||
.uploadFile(sasJob, files, params)
|
||||
.catch((err: any) => err)
|
||||
expect(err.error.message).toEqual('File upload request failed.')
|
||||
})
|
||||
|
||||
it('should throw an error when the server request fails', async (done) => {
|
||||
it('should throw an error when the server request fails', async () => {
|
||||
mockedAxios.post.mockImplementation(() =>
|
||||
Promise.reject({ data: '{message: "Server error"}' })
|
||||
)
|
||||
@@ -107,10 +108,9 @@ describe('FileUploader', () => {
|
||||
const sasJob = 'test'
|
||||
const { files, params } = prepareFilesAndParams()
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => {
|
||||
expect(err.error.message).toEqual('File upload request failed.')
|
||||
|
||||
done()
|
||||
})
|
||||
const err = await fileUploader
|
||||
.uploadFile(sasJob, files, params)
|
||||
.catch((err: any) => err)
|
||||
expect(err.error.message).toEqual('File upload request failed.')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,7 +14,7 @@ describe('FolderOperations', () => {
|
||||
|
||||
beforeEach(() => {})
|
||||
|
||||
it('should move and rename folder', async (done) => {
|
||||
it('should move and rename folder', async () => {
|
||||
mockFetchResponse(false)
|
||||
|
||||
let res: any = await sasViyaApiClient.moveFolder(
|
||||
@@ -26,11 +26,9 @@ describe('FolderOperations', () => {
|
||||
|
||||
expect(res.folder.name).toEqual('newName')
|
||||
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
it('should move and keep the name of folder', async (done) => {
|
||||
it('should move and keep the name of folder', async () => {
|
||||
mockFetchResponse(true)
|
||||
|
||||
let res: any = await sasViyaApiClient.moveFolder(
|
||||
@@ -42,11 +40,9 @@ describe('FolderOperations', () => {
|
||||
|
||||
expect(res.folder.name).toEqual('oldName')
|
||||
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
it('should only rename folder', async (done) => {
|
||||
it('should only rename folder', async () => {
|
||||
mockFetchResponse(false)
|
||||
|
||||
let res: any = await sasViyaApiClient.moveFolder(
|
||||
@@ -58,8 +54,6 @@ describe('FolderOperations', () => {
|
||||
|
||||
expect(res.folder.name).toEqual('newName')
|
||||
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { parseGeneratedCode } from '../../utils/index'
|
||||
|
||||
it('should parse generated code', async (done) => {
|
||||
it('should parse generated code', () => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
|
||||
const parsedGeneratedCode = parseGeneratedCode(sampleResponse)
|
||||
@@ -15,8 +15,6 @@ it('should parse generated code', async (done) => {
|
||||
expect(generatedCodeLines[2].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
|
||||
expect(generatedCodeLines[3].startsWith('MPRINT(MM_WEBRIGHT)')).toBeTruthy()
|
||||
expect(generatedCodeLines[4].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
/* tslint:disable */
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { parseSourceCode } from '../../utils/index'
|
||||
|
||||
it('should parse SAS9 source code', async (done) => {
|
||||
it('should parse SAS9 source code', async () => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
|
||||
const parsedSourceCode = parseSourceCode(sampleResponse)
|
||||
@@ -15,8 +15,6 @@ it('should parse SAS9 source code', async (done) => {
|
||||
expect(sourceCodeLines[2].startsWith('8')).toBeTruthy()
|
||||
expect(sourceCodeLines[3].startsWith('9')).toBeTruthy()
|
||||
expect(sourceCodeLines[4].startsWith('10')).toBeTruthy()
|
||||
|
||||
done()
|
||||
})
|
||||
|
||||
/* tslint:disable */
|
||||
|
||||
@@ -7,11 +7,10 @@ const browserConfig = {
|
||||
devtool: 'inline-source-map',
|
||||
mode: 'production',
|
||||
optimization: {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
new terserPlugin({
|
||||
cache: true,
|
||||
parallel: true,
|
||||
sourceMap: true,
|
||||
terserOptions: {}
|
||||
})
|
||||
]
|
||||
@@ -41,6 +40,9 @@ const browserConfig = {
|
||||
filename: null,
|
||||
exclude: [/node_modules/],
|
||||
test: /\.ts($|\?)/i
|
||||
}),
|
||||
new webpack.ProvidePlugin({
|
||||
process: 'process/browser'
|
||||
})
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user