Skip to content

Commit c4f8dac

Browse files
45407 refactor(api): add overloads to commonRequest for type-safe void responses
1 parent 5c788ec commit c4f8dac

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

frontend/src/public/api/commonRequest.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,23 @@ axiosInstance.interceptors.response.use(
9393
},
9494
);
9595

96+
export async function commonRequest(
97+
rawUrl: string,
98+
params: Partial<AxiosRequestConfig>,
99+
options: Partial<ICommonRequestOptions> & { responseType: 'empty' },
100+
): Promise<void>;
101+
export async function commonRequest<T>(
102+
rawUrl: string,
103+
params?: Partial<AxiosRequestConfig>,
104+
options?: Partial<ICommonRequestOptions>,
105+
): Promise<T>;
106+
107+
/* eslint-disable consistent-return */
96108
export async function commonRequest<T>(
97109
rawUrl: string,
98110
params: Partial<AxiosRequestConfig> = {},
99111
options: Partial<ICommonRequestOptions> = {},
100-
): Promise<T> {
112+
): Promise<T | void> {
101113
const {
102114
api: { publicUrl, urls },
103115
} = getBrowserConfigEnv();
@@ -121,7 +133,7 @@ export async function commonRequest<T>(
121133
const response = await axiosInstance(fullUrl, config);
122134

123135
if (options.responseType === 'empty') {
124-
return undefined as unknown as T;
136+
return;
125137
}
126138

127139
return response.data as T;
@@ -130,6 +142,7 @@ export async function commonRequest<T>(
130142
throw error;
131143
}
132144
logger.error(error);
133-
return undefined as unknown as T;
145+
134146
}
135147
}
148+
/* eslint-enable consistent-return */

frontend/src/public/redux/workflows/__tests__/saga.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { cloneWorkflowAction, deleteWorkflowAction, returnWorkflowToTaskAction,
1515
describe('workflows saga', () => {
1616
it('deleteWorkflowSaga work', () => {
1717
const saga = deleteWorkflowSaga({ type: deleteWorkflowAction.type, payload: { workflowId: 1 } });
18-
const deleteApiMock = jest.spyOn(deleteApi, 'deleteWorkflow').mockImplementation(() => Promise.resolve(null));
18+
const deleteApiMock = jest.spyOn(deleteApi, 'deleteWorkflow').mockImplementation(() => Promise.resolve());
1919
const notificationManagerSuccessMock = jest.spyOn(NotificationManager, 'success');
2020
saga.next();
2121
saga.next();
@@ -33,7 +33,7 @@ describe('workflows saga', () => {
3333
});
3434
const finishWorkflowApiMock = jest
3535
.spyOn(finishWorkflowApi, 'finishWorkflow')
36-
.mockImplementation(() => Promise.resolve(null));
36+
.mockImplementation(() => Promise.resolve());
3737
const notificationManagerSuccessMock = jest.spyOn(NotificationManager, 'success');
3838
saga.next();
3939
saga.next();

0 commit comments

Comments
 (0)