Skip to content

Commit

Permalink
refactor(type): replace 'any' with generic type in POST request.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilsabatp committed Jan 17, 2025
1 parent f91b377 commit afb42e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/features/user/CompleteSignup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default function CompleteSignup(): ReactElement | null {
setRequestSent(true);
setIsProcessing(true);
try {
const res = await postRequest<User>({
const res = await postRequest<User, CreateUserRequest>({
tenant: tenantConfig?.id,
url: `/app/profile`,
data: bodyToSend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ interface Props<
helperText?: React.ReactNode;
// mySpecies?: any;
}

type SubmitData = {
q: string;
t: 'species';
};
export default function SpeciesSelect<
TFieldValues extends FieldValues,
TName extends FieldPath<TFieldValues>
Expand Down Expand Up @@ -62,7 +65,7 @@ export default function SpeciesSelect<
// Todo: debouncing
if (value.length > 2) {
try {
const res = await postRequest<SpeciesSuggestionType[]>({
const res = await postRequest<SpeciesSuggestionType[], SubmitData>({
tenant: tenantConfig?.id,
url: `/suggest.php`,
data: {
Expand Down
10 changes: 7 additions & 3 deletions src/utils/apiRequests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ interface PutAuthRequestOptions<D> extends BaseRequestOptions {
data?: D;
logoutUser: (value?: string | undefined) => void;
}
interface PostRequestOptions extends BaseRequestOptions {
data: any;
interface PostRequestOptions<D> extends BaseRequestOptions {
data: D;
}
interface GetRequestOptions extends BaseRequestOptions {
queryParams?: { [key: string]: string };
Expand Down Expand Up @@ -221,7 +221,11 @@ export function postAuthenticatedRequest<T, D = unknown>({
});
}

export function postRequest<T>({ tenant, url, data }: PostRequestOptions) {
export function postRequest<T, D = unknown>({
tenant,
url,
data,
}: PostRequestOptions<D>) {
const lang = localStorage.getItem('language') || 'en';
return new Promise<T>((resolve, reject) => {
(async () => {
Expand Down

0 comments on commit afb42e3

Please sign in to comment.