diff --git a/src/models/OnspringClient.ts b/src/models/OnspringClient.ts index 9b9ebdf..1d03f1d 100644 --- a/src/models/OnspringClient.ts +++ b/src/models/OnspringClient.ts @@ -397,6 +397,24 @@ export class OnspringClient { return apiResponse.asGetPagedRecordsResponseType(); } + /** + * @method deleteRecordById - Deletes a record by its id. + * @param {number} appId - The id of the app that the record belongs to. + * @param {number} recordId - The id of the record to delete. + * @returns {Promise>} - A promise that resolves to an ApiResponse of type any. + */ + public async deleteRecordById( + appId: number, + recordId: number + ): Promise> { + const endpoint = EndpointFactory.getDeleteRecordByIdEndpoint( + appId, + recordId + ); + const apiResponse = await this.delete(endpoint); + return apiResponse; + } + /** * @method getReportsByAppId - Gets a paged list of reports by the app id. * @param {number} appId - The id of the app to get the reports for. diff --git a/tests/OnspringClient.spec.ts b/tests/OnspringClient.spec.ts index 5f79fee..8849e22 100644 --- a/tests/OnspringClient.spec.ts +++ b/tests/OnspringClient.spec.ts @@ -3615,4 +3615,146 @@ describe('OnspringClient', function () { expect(result).to.have.property('data', null); }); }); + + describe('deleteRecordById', function () { + it('should be defined', function () { + expect(OnspringClient.prototype.deleteRecordById).to.not.be.undefined; + }); + + it('should be a function', function () { + expect(OnspringClient.prototype.deleteRecordById).to.be.a('function'); + }); + + it('should return a promise', function () { + expect( + new OnspringClient(baseUrl, apiKey).deleteRecordById(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.deleteRecordById(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 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.deleteRecordById(1, 1); + + expect(result).to.be.instanceOf(ApiResponse); + expect(result).to.have.property('statusCode', 401); + expect(result).to.have.property('isSuccessful', false); + expect(result).to.have.property('message', undefined); + expect(result).to.have.property('data', 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.deleteRecordById(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).to.have.property('data', 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.deleteRecordById(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).to.have.property('data', null); + }); + }); });