Rename fetch lib

pull/3813/head
Kamran Ahmed 2 years ago
parent 8b285cc600
commit cdc7f081a3
  1. 1
      src/components/AuthenticationFlow/EmailLoginForm.tsx
  2. 4
      src/components/Profile/ForgotPasswordForm.tsx
  3. 67
      src/lib/http.ts

@ -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>('');

@ -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('');
@ -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,

@ -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 ApiReturn<ResponseType> = {
response?: ResponseType;
error?: {
type AppError = {
status: number;
message: string;
errors?: { message: string; location: string }[];
};
};
type ApiReturn<ResponseType, ErrorType> = {
response?: ResponseType;
error?: ErrorType | FetchError;
};
/**
@ -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',
@ -44,11 +53,7 @@ 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 {
@ -56,68 +61,76 @@ export async function callApi<ResponseType = AppResponse>(
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',
});

Loading…
Cancel
Save