From 633ad5d753e3c243dd8d9b6ebbdeb6c0cbaa6db7 Mon Sep 17 00:00:00 2001 From: Deep Sidhu Date: Wed, 25 Jan 2023 15:27:30 -0600 Subject: [PATCH 1/2] Updated post function to accept record and formdata as a parameter --- .../webapp/resources/js/apis/export/ncbi.ts | 5 +++- .../resources/js/apis/projects/samples.ts | 23 +++++++++++------- src/main/webapp/resources/js/apis/requests.ts | 18 +++++++++----- .../resources/js/apis/samples/samples.ts | 24 ++++++++++++++----- .../resources/js/types/ajax-response.d.ts | 15 ++++++++---- 5 files changed, 59 insertions(+), 26 deletions(-) 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..06ac875ca72 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 interface AjaxErrorResponse { + error: string; + } -interface AjaxSuccessResponse { - message: string; + export interface AjaxSuccessResponse { + message: string; + } } From 04b8efed1da3f2b41b99f0a9a7edec22db2b6f06 Mon Sep 17 00:00:00 2001 From: Deep Sidhu Date: Wed, 25 Jan 2023 16:03:29 -0600 Subject: [PATCH 2/2] Converted to type from interface since we will not be extending ajaxsuccessresponse or ajaxerrorresponse in typescript --- src/main/webapp/resources/js/types/ajax-response.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 06ac875ca72..a1c8c2881a4 100644 --- a/src/main/webapp/resources/js/types/ajax-response.d.ts +++ b/src/main/webapp/resources/js/types/ajax-response.d.ts @@ -2,11 +2,11 @@ export = AJAXRESPONSE; export as namespace AJAXRESPONSE; declare namespace AJAXRESPONSE { - export interface AjaxErrorResponse { + export type AjaxErrorResponse = { error: string; - } + }; - export interface AjaxSuccessResponse { + export type AjaxSuccessResponse = { message: string; - } + }; }