diff --git a/lib/reports/index.ts b/lib/reports/index.ts index b4fd0bb..0f75adf 100644 --- a/lib/reports/index.ts +++ b/lib/reports/index.ts @@ -16,6 +16,8 @@ import type { ReportPublish, SetReportPublishStatusOptions, SetReportPublishStatusResponse, + DeleteReportOptions, + DeleteReportResponse, } from './types'; import * as constants from '../utils/constants'; @@ -87,6 +89,11 @@ export function create(options: CreateOptions): ReportsApi { return requestor.put({ ...optionsToSend, ...urlOptions, ...putOptions }, callback); }; + const deleteReport = (deleteOptions: DeleteReportOptions, callback?: RequestCallback) => { + const urlOptions = { url: options.apiUrls.reports + '/' + deleteOptions.reportId }; + return requestor.delete({ ...optionsToSend, ...urlOptions, ...deleteOptions }, callback); + }; + return { listReports, getReport, @@ -95,6 +102,7 @@ export function create(options: CreateOptions): ReportsApi { getReportAsCSV, getReportPublishStatus, setReportPublishStatus, + deleteReport, ...shares.create(options), }; } diff --git a/lib/reports/types.ts b/lib/reports/types.ts index 891cdb3..e3b4bc9 100644 --- a/lib/reports/types.ts +++ b/lib/reports/types.ts @@ -175,6 +175,28 @@ export interface ReportsApi { options: SetReportPublishStatusOptions, callback?: RequestCallback ) => Promise; + + /** + * Deletes the specified Report. + * + * @param options - {@link DeleteReportOptions} - Configuration options for the request + * @param callback - {@link RequestCallback}\<{@link DeleteReportResponse}\> - Optional callback function + * @returns Promise\<{@link DeleteReportResponse}\> + * + * @remarks + * It mirrors to the following Smartsheet REST API method: `DELETE /2.0/reports/{reportId}` + * + * @example + * ```typescript + * const result = await client.reports.deleteReport({ + * reportId: 11235813 + * }); + * ``` + */ + deleteReport: ( + options: DeleteReportOptions, + callback?: RequestCallback + ) => Promise; } // ============================================================================ @@ -722,3 +744,16 @@ export interface SetReportPublishStatusResponse extends BaseResponseStatus { failedItems?: FailedItem[]; result: ReportPublish; } + +// ============================================================================ +// Delete Report +// ============================================================================ + +export interface DeleteReportOptions extends RequestOptions { + /** + * Report Id + */ + reportId: number; +} + +export type DeleteReportResponse = BaseResponseStatus; diff --git a/test/functional/client.spec.ts b/test/functional/client.spec.ts index 65375a8..da092b0 100644 --- a/test/functional/client.spec.ts +++ b/test/functional/client.spec.ts @@ -180,7 +180,7 @@ describe('Client Unit Tests', () => { describe('#reports', () => { it('should have reports object', () => { expect(smartsheet).toHaveProperty('reports'); - expect(Object.keys(smartsheet.reports)).toHaveLength(12); + expect(Object.keys(smartsheet.reports)).toHaveLength(13); }); it('should have get methods', () => { @@ -195,6 +195,10 @@ describe('Client Unit Tests', () => { expect(smartsheet.reports).toHaveProperty('setReportPublishStatus'); expect(smartsheet.reports).toHaveProperty('sendReportViaEmail'); }); + + it('should have delete methods', () => { + expect(smartsheet.reports).toHaveProperty('deleteReport'); + }); }); describe('#server', () => { diff --git a/test/functional/endpoints.spec.ts b/test/functional/endpoints.spec.ts index 9dd5d14..01e5864 100644 --- a/test/functional/endpoints.spec.ts +++ b/test/functional/endpoints.spec.ts @@ -103,6 +103,7 @@ describe('Method Unit Tests', () => { { name: 'getReportAsCSV', stub: 'get', options: {reportId: 123}, expectedRequest: {url: "reports/123", accept: constants.acceptHeaders.textCsv }}, { name: 'getReportPublishStatus', stub: 'get', options: {reportId: 123}, expectedRequest: {url: "reports/123/publish" }}, { name: 'setReportPublishStatus', stub: 'put', options: {reportId: 123}, expectedRequest: {url: "reports/123/publish" }}, + { name: 'deleteReport', stub: 'delete', options: {reportId:123}, expectedRequest: {url: "reports/123"}}, ] }, { diff --git a/test/mock-api/reports/delete-reports.spec.ts b/test/mock-api/reports/delete-reports.spec.ts new file mode 100644 index 0000000..4b5eb65 --- /dev/null +++ b/test/mock-api/reports/delete-reports.spec.ts @@ -0,0 +1,88 @@ +import crypto from 'crypto'; +import { createClient, findWireMockRequest } from '../utils/utils'; +import { expect } from '@jest/globals'; +import { + TEST_REPORT_ID, + TEST_SUCCESS_MESSAGE, + TEST_SUCCESS_RESULT_CODE, + ERROR_500_STATUS_CODE, + ERROR_500_MESSAGE, + ERROR_400_STATUS_CODE, + ERROR_400_MESSAGE +} from './common_test_constants'; + +describe('Reports - deleteReport endpoint tests', () => { + const client = createClient(); + + it('deleteReport generated url is correct', async () => { + const requestId = crypto.randomUUID(); + const options = { + reportId: TEST_REPORT_ID, + customProperties: { + 'x-request-id': requestId, + 'x-test-name': '/reports/delete-report/all-response-body-properties' + + } + }; + await client.reports.deleteReport(options); + const matchedRequest = await findWireMockRequest(requestId); + const parsedUrl = new URL(matchedRequest.absoluteUrl); + expect(parsedUrl.pathname).toEqual("/2.0/reports/" + TEST_REPORT_ID); + }); + + it('deleteReport all response body properties', async () => { + const requestId = crypto.randomUUID(); + const options = { + reportId: TEST_REPORT_ID, + customProperties: { + 'x-request-id': requestId, + 'x-test-name': '/reports/delete-report/all-response-body-properties' + } + }; + + const response = await client.reports.deleteReport(options); + + expect(response).toEqual({ + message: TEST_SUCCESS_MESSAGE, + resultCode: TEST_SUCCESS_RESULT_CODE + }); + }); + + it('deleteReport error 500 response', async () => { + const requestId = crypto.randomUUID(); + const options = { + reportId: TEST_REPORT_ID, + customProperties: { + 'x-request-id': requestId, + 'x-test-name': '/errors/500-response' + } + }; + + try { + await client.reports.deleteReport(options); + expect(true).toBe(false); // Expected an error to be thrown + } catch (error) { + expect(error.statusCode).toBe(ERROR_500_STATUS_CODE); + expect(error.message).toBe(ERROR_500_MESSAGE); + } + }); + + it('deleteReport error 400 response', async () => { + const requestId = crypto.randomUUID(); + const options = { + reportId: TEST_REPORT_ID, + customProperties: { + 'x-request-id': requestId, + 'x-test-name': '/errors/400-response' + } + }; + + try { + await client.reports.deleteReport(options); + expect(true).toBe(false); // Expected an error to be thrown + } catch (error) { + expect(error.statusCode).toBe(ERROR_400_STATUS_CODE); + expect(error.message).toBe(ERROR_400_MESSAGE); + } + }); +});