From 9b1d295b823dba1e158d95949ce44cf6f7b66269 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Sat, 26 Sep 2020 11:41:18 +0300 Subject: [PATCH 1/2] feat(folder): made 'deleteFolder' method public --- src/SASViyaApiClient.ts | 2 ++ src/SASjs.ts | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 3dcf583..a248e9c 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -1508,6 +1508,8 @@ export class SASViyaApiClient { `${this.serverUrl}${url}`, requestInfo ).catch((err) => { + // FIXME: handle 'code: 'ENOTFOUND'' properly + throw err }) diff --git a/src/SASjs.ts b/src/SASjs.ts index 6ecc4fa..fd46024 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -243,8 +243,6 @@ export default class SASjs { sasApiClient?: SASViyaApiClient, isForced?: boolean ) { - this.isMethodSupported('createFolder', ServerType.SASViya) - if (sasApiClient) return await sasApiClient.createFolder( folderName, @@ -261,6 +259,17 @@ export default class SASjs { ) } + /** + * For performance (and in case of accidental error) the `deleteFolder` function does not actually delete the folder (and all its content and subfolder content). Instead the folder is simply moved to the recycle bin. Deletion time will be added to the folder name. + * @param folderPath - the full path (eg `/Public/example/deleteThis`) of the folder to be deleted. + * @param accessToken - an access token for authorizing the request. + */ + public async deleteFolder(folderPath: string, accessToken: string) { + this.isMethodSupported('deleteFolder', ServerType.SASViya) + + return await this.sasViyaApiClient?.deleteFolder(folderPath, accessToken) + } + public async createJobDefinition( jobName: string, code: string, From fc9056c1ac7bf01f4213a22b46dc90434051f5cb Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Mon, 28 Sep 2020 14:59:27 +0300 Subject: [PATCH 2/2] chore(folder-management): made 'moveFolder' method public and fixed 'createFolder' method --- src/SASViyaApiClient.ts | 50 ++++++++++++++--------------------------- src/SASjs.ts | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index a248e9c..47ac34b 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -43,7 +43,6 @@ export class SASViyaApiClient { this.contextName, this.setCsrfToken ) - private isForceDeploy: boolean = false private folderMap = new Map() /** @@ -625,8 +624,6 @@ export class SASViyaApiClient { if (!parentFolderUri && parentFolderPath) { parentFolderUri = await this.getFolderUri(parentFolderPath, accessToken) if (!parentFolderUri) { - if (isForced) this.isForceDeploy = true - console.log( `Parent folder at path '${parentFolderPath}' is not present.` ) @@ -652,37 +649,16 @@ export class SASViyaApiClient { `Parent folder '${newFolderName}' has been successfully created.` ) parentFolderUri = `/folders/folders/${parentFolder.id}` - } else if (isForced && accessToken && !this.isForceDeploy) { - this.isForceDeploy = true + } else if (isForced && accessToken) { + const folderPath = parentFolderPath + '/' + folderName + const folderUri = await this.getFolderUri(folderPath, accessToken) - await this.deleteFolder(parentFolderPath, accessToken) - - const newParentFolderPath = parentFolderPath.substring( - 0, - parentFolderPath.lastIndexOf('/') - ) - const newFolderName = `${parentFolderPath.split('/').pop()}` - - if (newParentFolderPath === '') { - throw new Error(`Root folder has to be present on the server.`) + if (folderUri) { + await this.deleteFolder( + parentFolderPath + '/' + folderName, + accessToken + ) } - - console.log( - `Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'` - ) - - const parentFolder = await this.createFolder( - newFolderName, - newParentFolderPath, - undefined, - accessToken - ) - - console.log( - `Parent folder '${newFolderName}' has been successfully created.` - ) - - parentFolderUri = `/folders/folders/${parentFolder.id}` } } @@ -1508,7 +1484,15 @@ export class SASViyaApiClient { `${this.serverUrl}${url}`, requestInfo ).catch((err) => { - // FIXME: handle 'code: 'ENOTFOUND'' properly + if (err.code && err.code === 'ENOTFOUND') { + const notFoundError = { + body: JSON.stringify({ + message: `Folder '${sourceFolder.split('/').pop()}' was not found.` + }) + } + + throw notFoundError + } throw err }) diff --git a/src/SASjs.ts b/src/SASjs.ts index fd46024..955d1ac 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -270,6 +270,29 @@ export default class SASjs { return await this.sasViyaApiClient?.deleteFolder(folderPath, accessToken) } + /** + * Moves folder to a new location. The folder may be renamed at the same time. + * @param sourceFolder - the full path (eg `/Public/example/myFolder`) or URI of the source folder to be moved. Providing URI instead of path will save one extra request. + * @param targetParentFolder - the full path or URI of the _parent_ folder to which the `sourceFolder` will be moved (eg `/Public/newDestination`). To move a folder, a user has to have write permissions in targetParentFolder. Providing URI instead of path will save one extra request. + * @param targetFolderName - the name of the "moved" folder. If left blank, the original folder name will be used (eg `myFolder` in `/Public/newDestination/myFolder` for the example above). Optional field. + * @param accessToken - an access token for authorizing the request. + */ + public async moveFolder( + sourceFolder: string, + targetParentFolder: string, + targetFolderName: string, + accessToken: string + ) { + this.isMethodSupported('moveFolder', ServerType.SASViya) + + return await this.sasViyaApiClient?.moveFolder( + sourceFolder, + targetParentFolder, + targetFolderName, + accessToken + ) + } + public async createJobDefinition( jobName: string, code: string,