1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-05 03:30:05 +00:00

feat(delete-context): delete context by name

This commit is contained in:
Krishna Acondy
2020-09-11 12:28:44 +01:00
parent 8ca79b1ccb
commit 6492833653
2 changed files with 19 additions and 8 deletions

View File

@@ -338,12 +338,12 @@ export class SASViyaApiClient {
/**
* Deletes a compute context on the given server.
* @param contextId - the ID of the context to be deleted.
* @param contextName - the name of the context to be deleted.
* @param accessToken - an access token for an authorized user.
*/
public async deleteContext(contextId: string, accessToken?: string) {
if (!contextId) {
throw new Error('Invalid context ID.')
public async deleteContext(contextName: string, accessToken?: string) {
if (!contextName) {
throw new Error('Invalid context Name.')
}
const headers: any = {
@@ -354,13 +354,24 @@ export class SASViyaApiClient {
headers.Authorization = `Bearer ${accessToken}`
}
const { result: contexts } = await this.request<{ items: Context[] }>(
`${this.serverUrl}/compute/contexts?filter=eq(name, "${contextName}")`,
{ headers }
)
if (!contexts || !(contexts.items && contexts.items.length)) {
throw new Error(
`The context ${contextName} was not found on ${this.serverUrl}.`
)
}
const deleteContextRequest: RequestInit = {
method: 'DELETE',
headers
}
return await this.request<Context>(
`${this.serverUrl}/compute/contexts/${contextId}`,
`${this.serverUrl}/compute/contexts/${contexts.items[0].id}`,
deleteContextRequest
)
}