Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/reports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import type {
ReportPublish,
SetReportPublishStatusOptions,
SetReportPublishStatusResponse,
DeleteReportOptions,
DeleteReportResponse,
} from './types';
import * as constants from '../utils/constants';

Expand Down Expand Up @@ -87,6 +89,11 @@ export function create(options: CreateOptions): ReportsApi {
return requestor.put({ ...optionsToSend, ...urlOptions, ...putOptions }, callback);
};

const deleteReport = (deleteOptions: DeleteReportOptions, callback?: RequestCallback<DeleteReportResponse>) => {
const urlOptions = { url: options.apiUrls.reports + '/' + deleteOptions.reportId };
return requestor.delete({ ...optionsToSend, ...urlOptions, ...deleteOptions }, callback);
};

return {
listReports,
getReport,
Expand All @@ -95,6 +102,7 @@ export function create(options: CreateOptions): ReportsApi {
getReportAsCSV,
getReportPublishStatus,
setReportPublishStatus,
deleteReport,
...shares.create(options),
};
}
35 changes: 35 additions & 0 deletions lib/reports/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ export interface ReportsApi {
options: SetReportPublishStatusOptions,
callback?: RequestCallback<SetReportPublishStatusResponse>
) => Promise<SetReportPublishStatusResponse>;

/**
* 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<DeleteReportResponse>
) => Promise<DeleteReportResponse>;
}

// ============================================================================
Expand Down Expand Up @@ -722,3 +744,16 @@ export interface SetReportPublishStatusResponse extends BaseResponseStatus {
failedItems?: FailedItem[];
result: ReportPublish;
}

// ============================================================================
// Delete Report
// ============================================================================

export interface DeleteReportOptions extends RequestOptions<undefined, undefined> {
/**
* Report Id
*/
reportId: number;
}

export type DeleteReportResponse = BaseResponseStatus;
6 changes: 5 additions & 1 deletion test/functional/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
1 change: 1 addition & 0 deletions test/functional/endpoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
]
},
{
Expand Down
88 changes: 88 additions & 0 deletions test/mock-api/reports/delete-reports.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
Loading