diff --git a/src/main/webapp/resources/js/apis/export/ncbi.ts b/src/main/webapp/resources/js/apis/export/ncbi.ts index 9719af86548..5ba6fdf4774 100644 --- a/src/main/webapp/resources/js/apis/export/ncbi.ts +++ b/src/main/webapp/resources/js/apis/export/ncbi.ts @@ -96,5 +96,8 @@ export async function getNCBISelections(): Promise { export async function submitNcbiSubmissionRequest( request: NcbiSubmissionRequest ) { - return await post(setBaseUrl(`ajax/ncbi/submit`), request); + return await post( + setBaseUrl(`ajax/ncbi/submit`), + request + ); } diff --git a/src/main/webapp/resources/js/apis/projects/samples.ts b/src/main/webapp/resources/js/apis/projects/samples.ts index 12792fcf2c5..3f67533d268 100644 --- a/src/main/webapp/resources/js/apis/projects/samples.ts +++ b/src/main/webapp/resources/js/apis/projects/samples.ts @@ -3,6 +3,10 @@ import { PairedEndSequenceFile, SingleEndSequenceFile, } from "../../types/irida"; +import { + AjaxErrorResponse, + AjaxSuccessResponse, +} from "../../types/ajax-response"; import { getProjectIdFromUrl, setBaseUrl } from "../../utilities/url-utilities"; import { get, post } from "../requests"; @@ -123,14 +127,17 @@ export async function shareSamplesWithProject({ targetId: number; locked: boolean; remove: boolean; -}) { - return post(setBaseUrl(`ajax/samples/share`), { - currentId, - sampleIds, - targetId, - locked, - remove, - }); +}): Promise { + return post( + setBaseUrl(`ajax/samples/share`), + { + currentId, + sampleIds, + targetId, + locked, + remove, + } + ); } /** diff --git a/src/main/webapp/resources/js/apis/requests.ts b/src/main/webapp/resources/js/apis/requests.ts index 7a5ba6ee72b..fc90b098496 100644 --- a/src/main/webapp/resources/js/apis/requests.ts +++ b/src/main/webapp/resources/js/apis/requests.ts @@ -20,11 +20,10 @@ export async function get( } } -export async function post( - url: string, - params?: T, - config?: AxiosRequestConfig -): Promise { +export async function post< + T, + P = FormData | Record | undefined +>(url: string, params?: P, config?: AxiosRequestConfig): Promise { try { const { data } = await axios.post(url, params, config); return data; @@ -36,7 +35,14 @@ export async function post( return Promise.reject(error.message); } } else if (axios.isCancel(error)) { - return Promise.reject(error.message); + let message; + if (error instanceof Error) { + const { message: destructuredMessage } = error; + message = destructuredMessage; + } else { + message = String(error); + } + return Promise.reject(message); } else { return Promise.reject("An unexpected error occurred"); } diff --git a/src/main/webapp/resources/js/apis/samples/samples.ts b/src/main/webapp/resources/js/apis/samples/samples.ts index c2e81867ab4..f08d01eb8f6 100644 --- a/src/main/webapp/resources/js/apis/samples/samples.ts +++ b/src/main/webapp/resources/js/apis/samples/samples.ts @@ -394,10 +394,14 @@ export const uploadSequenceFiles = async ({ config, }: { sampleId: number; - formData: any; + formData: FormData; config: Record; }): Promise => { - return post(`${URL}/${sampleId}/sequenceFiles/upload`, formData, config); + return post( + `${URL}/${sampleId}/sequenceFiles/upload`, + formData, + config + ); }; /** @@ -413,10 +417,14 @@ export const uploadAssemblyFiles = ({ config, }: { sampleId: number; - formData: any; + formData: FormData; config: Record; }): Promise => { - return post(`${URL}/${sampleId}/assemblies/upload`, formData, config); + return post( + `${URL}/${sampleId}/assemblies/upload`, + formData, + config + ); }; /** @@ -432,8 +440,12 @@ export const uploadFast5Files = ({ config, }: { sampleId: number; - formData: any; + formData: FormData; config: Record; }): Promise => { - return post(`${URL}/${sampleId}/fast5/upload`, formData, config); + return post( + `${URL}/${sampleId}/fast5/upload`, + formData, + config + ); }; diff --git a/src/main/webapp/resources/js/types/ajax-response.d.ts b/src/main/webapp/resources/js/types/ajax-response.d.ts index 08a4051aaa5..a1c8c2881a4 100644 --- a/src/main/webapp/resources/js/types/ajax-response.d.ts +++ b/src/main/webapp/resources/js/types/ajax-response.d.ts @@ -1,7 +1,12 @@ -interface AjaxErrorResponse { - error: string; -} +export = AJAXRESPONSE; +export as namespace AJAXRESPONSE; + +declare namespace AJAXRESPONSE { + export type AjaxErrorResponse = { + error: string; + }; -interface AjaxSuccessResponse { - message: string; + export type AjaxSuccessResponse = { + message: string; + }; }