feat: implement deleteFileById method

This commit is contained in:
StevanFreeborn
2023-02-06 22:16:43 -06:00
parent 04e1ebdf8d
commit a68ea4f63e
2 changed files with 233 additions and 0 deletions
+25
View File
@@ -253,6 +253,22 @@ export class OnspringClient {
return apiResponse.asCreatedWithIdResponseType();
}
public async deleteFileById(
recordId: number,
fieldId: number,
fileId: number
): Promise<ApiResponse<any>> {
const endpoint = EndpointFactory.getDeleteFileByIdEndpoint(
recordId,
fieldId,
fileId
);
const apiResponse = await this.delete<any>(endpoint);
return apiResponse;
}
/**
* @method get - Makes a GET request to the specified endpoint.
* @param {string} endpoint - The endpoint that will be used to make the request.
@@ -284,4 +300,13 @@ export class OnspringClient {
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
private async delete<T>(
endpoint: string,
config: AxiosRequestConfig = {}
): Promise<ApiResponse<T>> {
const response = await this._client.delete(endpoint, config);
const apiResponse = ApiResponseFactory.getApiResponse<T>(response);
return apiResponse;
}
}
+208
View File
@@ -1900,4 +1900,212 @@ describe('OnspringClient', function () {
expect(result.data).to.be.null;
});
});
describe('deleteFileById', function () {
it('should be defined', function () {
expect(OnspringClient.prototype.deleteFileById).to.not.be.undefined;
});
it('should be a function', function () {
expect(OnspringClient.prototype.deleteFileById).to.be.a('function');
});
it('should return a promise', function () {
expect(
new OnspringClient(baseUrl, apiKey).deleteFileById(1, 1, 1)
).to.be.instanceOf(Promise);
});
it('should return a promise that resolves to an api response when request is successful', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 204,
statusText: 'No Content',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteFileById(1, 1, 1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 204);
expect(result).to.have.property('isSuccessful', true);
expect(result).to.have.property('message', '');
expect(result).to.have.property('data', undefined);
});
it('should return a promise that resolves to an api response when request receives a 400 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 400,
statusText: 'Bad Request',
data: { field: ['Field requested is not a file type field.'] },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteFileById(1, 1, 1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 400);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property(
'message',
'{"field":["Field requested is not a file type field."]}'
);
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 401 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 401,
statusText: 'Unauthorized',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteFileById(1, 1, 1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 401);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 403 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 403,
statusText: 'Forbidden',
data: { message: 'Forbidden' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteFileById(1, 1, 1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 403);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Forbidden');
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 404 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 404,
statusText: 'Not Found',
data: { message: 'Not Found' },
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteFileById(1, 1, 1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 404);
expect(result).to.have.property('isSuccessful', false);
expect(result).to.have.property('message', 'Not Found');
expect(result.data).to.be.null;
});
it('should return a promise that resolves to an api response when request receives a 500 response', async function () {
const client = new OnspringClient(baseUrl, apiKey);
const mockAxiosClient = axios.create({
baseURL: baseUrl,
headers: {
'x-apikey': apiKey,
'x-api-version': '2',
},
});
sinon.stub(mockAxiosClient, 'delete').returns(
Promise.resolve({
status: 500,
statusText: 'Internal Server Error',
headers: {},
config: {} as InternalAxiosRequestConfig,
} as AxiosResponse)
);
sinon.stub(client, '_client' as any).value(mockAxiosClient);
const result = await client.deleteFileById(1, 1, 1);
expect(result).to.be.instanceOf(ApiResponse);
expect(result).to.have.property('statusCode', 500);
expect(result).to.have.property('isSuccessful', false);
expect(result.message).to.be.undefined;
expect(result.data).to.be.null;
});
});
});