Skip to content

Commit

Permalink
Merge pull request #1455 from deepsidhu85/update-post-request-typing
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsadam authored Jan 26, 2023
2 parents a796d05 + 04b8efe commit d60c04f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/main/webapp/resources/js/apis/export/ncbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@ export async function getNCBISelections(): Promise<NcbiSelection[]> {
export async function submitNcbiSubmissionRequest(
request: NcbiSubmissionRequest
) {
return await post(setBaseUrl(`ajax/ncbi/submit`), request);
return await post<void, NcbiSubmissionRequest>(
setBaseUrl(`ajax/ncbi/submit`),
request
);
}
23 changes: 15 additions & 8 deletions src/main/webapp/resources/js/apis/projects/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<AjaxErrorResponse | AjaxSuccessResponse> {
return post<AjaxErrorResponse | AjaxSuccessResponse>(
setBaseUrl(`ajax/samples/share`),
{
currentId,
sampleIds,
targetId,
locked,
remove,
}
);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions src/main/webapp/resources/js/apis/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ export async function get<T>(
}
}

export async function post<T>(
url: string,
params?: T,
config?: AxiosRequestConfig
): Promise<T> {
export async function post<
T,
P = FormData | Record<string, unknown> | undefined
>(url: string, params?: P, config?: AxiosRequestConfig): Promise<T> {
try {
const { data } = await axios.post(url, params, config);
return data;
Expand All @@ -36,7 +35,14 @@ export async function post<T>(
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");
}
Expand Down
24 changes: 18 additions & 6 deletions src/main/webapp/resources/js/apis/samples/samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,14 @@ export const uploadSequenceFiles = async ({
config,
}: {
sampleId: number;
formData: any;
formData: FormData;
config: Record<string, unknown>;
}): Promise<SampleSequencingObject[]> => {
return post(`${URL}/${sampleId}/sequenceFiles/upload`, formData, config);
return post<SampleSequencingObject[], FormData>(
`${URL}/${sampleId}/sequenceFiles/upload`,
formData,
config
);
};

/**
Expand All @@ -413,10 +417,14 @@ export const uploadAssemblyFiles = ({
config,
}: {
sampleId: number;
formData: any;
formData: FormData;
config: Record<string, unknown>;
}): Promise<SampleGenomeAssembly[]> => {
return post(`${URL}/${sampleId}/assemblies/upload`, formData, config);
return post<SampleGenomeAssembly[], FormData>(
`${URL}/${sampleId}/assemblies/upload`,
formData,
config
);
};

/**
Expand All @@ -432,8 +440,12 @@ export const uploadFast5Files = ({
config,
}: {
sampleId: number;
formData: any;
formData: FormData;
config: Record<string, unknown>;
}): Promise<SampleSequencingObject[]> => {
return post(`${URL}/${sampleId}/fast5/upload`, formData, config);
return post<SampleSequencingObject[], FormData>(
`${URL}/${sampleId}/fast5/upload`,
formData,
config
);
};
15 changes: 10 additions & 5 deletions src/main/webapp/resources/js/types/ajax-response.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}

0 comments on commit d60c04f

Please sign in to comment.