Skip to content

Commit

Permalink
Rename fetch lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranahmedse committed Apr 10, 2023
1 parent 8b285cc commit cdc7f08
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/components/AuthenticationFlow/EmailLoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { FunctionComponent } from 'preact';
import { useState } from 'preact/hooks';
import { TOKEN_COOKIE_NAME } from '../../lib/constants';
import Spinner from '../Spinner';
import {httpPost} from "../../lib/http";

const EmailLoginForm: FunctionComponent<{}> = () => {
const [email, setEmail] = useState<string>('');
Expand Down
4 changes: 2 additions & 2 deletions src/components/Profile/ForgotPasswordForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'preact/hooks';
import Spinner from '../Spinner';
import { callPostApi } from '../../lib/http';
import { httpPost } from '../../lib/http';

export default function ForgotPasswordForm() {
const [email, setEmail] = useState('');
Expand All @@ -13,7 +13,7 @@ export default function ForgotPasswordForm() {
setIsLoading(true);
setError('');

const { response, error } = await callPostApi(
const { response, error } = await httpPost(
`${import.meta.env.PUBLIC_API_URL}/v1-forgot-password`,
{
email,
Expand Down
71 changes: 42 additions & 29 deletions src/lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from './constants';

type AppResponse = Record<string, any>;
type FetchError = {
status: number;
message: string;
};

type AppError = {
status: number;
message: string;
errors?: { message: string; location: string }[];
};

type ApiReturn<ResponseType> = {
type ApiReturn<ResponseType, ErrorType> = {
response?: ResponseType;
error?: {
status: number;
message: string;
errors?: { message: string; location: string }[];
};
error?: ErrorType | FetchError;
};

/**
Expand All @@ -18,10 +24,13 @@ type ApiReturn<ResponseType> = {
* @param url
* @param options
*/
export async function callApi<ResponseType = AppResponse>(
export async function httpCall<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string,
options?: RequestInit
): Promise<ApiReturn<ResponseType>> {
): Promise<ApiReturn<ResponseType, ErrorType>> {
try {
const response = await fetch(url, {
credentials: 'include',
Expand All @@ -44,80 +53,84 @@ export async function callApi<ResponseType = AppResponse>(

return {
response: undefined,
error: {
status: response.status,
message: data.message || 'Something went wrong. Please try again later.',
errors: data.errors,
},
error: data as ErrorType,
};
} catch (error: any) {
return {
response: undefined,
error: {
status: 0,
message: error.message,
errors: [],
},
};
}
}

export async function callPostApi<ResponseType = AppResponse>(
export async function httpPost<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string,
body: Record<string, any>,
options?: RequestInit
): Promise<ApiReturn<ResponseType>> {
return callApi<ResponseType>(url, {
): Promise<ApiReturn<ResponseType, ErrorType>> {
return httpCall<ResponseType, ErrorType>(url, {
...options,
method: 'POST',
body: JSON.stringify(body),
});
}

export async function callGetApi<ResponseType = AppResponse>(
export async function httpGet<ResponseType = AppResponse, ErrorType = AppError>(
url: string,
queryParams?: Record<string, any>,
options?: RequestInit
): Promise<ApiReturn<ResponseType>> {
): Promise<ApiReturn<ResponseType, ErrorType>> {
const searchParams = new URLSearchParams(queryParams).toString();
const queryUrl = searchParams ? `${url}?${searchParams}` : url;

return callApi<ResponseType>(queryUrl, {
return httpCall<ResponseType, ErrorType>(queryUrl, {
credentials: 'include',
method: 'GET',
...options,
});
}

export async function callPatchApi<ResponseType = AppResponse>(
export async function httpPatch<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string,
body: Record<string, any>,
options?: RequestInit
): Promise<ApiReturn<ResponseType>> {
return callApi<ResponseType>(url, {
): Promise<ApiReturn<ResponseType, ErrorType>> {
return httpCall<ResponseType, ErrorType>(url, {
...options,
method: 'PATCH',
body: JSON.stringify(body),
});
}

export async function callPutApi<ResponseType = AppResponse>(
export async function httpPut<ResponseType = AppResponse, ErrorType = AppError>(
url: string,
body: Record<string, any>,
options?: RequestInit
): Promise<ApiReturn<ResponseType>> {
return callApi<ResponseType>(url, {
): Promise<ApiReturn<ResponseType, ErrorType>> {
return httpCall<ResponseType, ErrorType>(url, {
...options,
method: 'PUT',
body: JSON.stringify(body),
});
}

export async function callDeleteApi<ResponseType = AppResponse>(
export async function httpDelete<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string,
options?: RequestInit
): Promise<ApiReturn<ResponseType>> {
return callApi<ResponseType>(url, {
): Promise<ApiReturn<ResponseType, ErrorType>> {
return httpCall<ResponseType, ErrorType>(url, {
...options,
method: 'DELETE',
});
Expand Down

0 comments on commit cdc7f08

Please sign in to comment.