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 { useState } from 'preact/hooks';
import { TOKEN_COOKIE_NAME } from '../../lib/constants'; import { TOKEN_COOKIE_NAME } from '../../lib/constants';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import {httpPost} from "../../lib/http";
const EmailLoginForm: FunctionComponent<{}> = () => { const EmailLoginForm: FunctionComponent<{}> = () => {
const [email, setEmail] = useState<string>(''); const [email, setEmail] = useState<string>('');

@ -1,6 +1,6 @@
import { useState } from 'preact/hooks'; import { useState } from 'preact/hooks';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import { callPostApi } from '../../lib/http'; import { httpPost } from '../../lib/http';
export default function ForgotPasswordForm() { export default function ForgotPasswordForm() {
const [email, setEmail] = useState(''); const [email, setEmail] = useState('');
@ -13,7 +13,7 @@ export default function ForgotPasswordForm() {
setIsLoading(true); setIsLoading(true);
setError(''); setError('');
const { response, error } = await callPostApi( const { response, error } = await httpPost(
`${import.meta.env.PUBLIC_API_URL}/v1-forgot-password`, `${import.meta.env.PUBLIC_API_URL}/v1-forgot-password`,
{ {
email, email,

@ -2,14 +2,20 @@ import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from './constants'; import { TOKEN_COOKIE_NAME } from './constants';
type AppResponse = Record<string, any>; type AppResponse = Record<string, any>;
type FetchError = {
status: number;
message: string;
};
type ApiReturn<ResponseType> = { type AppError = {
response?: ResponseType;
error?: {
status: number; status: number;
message: string; message: string;
errors?: { message: string; location: 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 url
* @param options * @param options
*/ */
export async function callApi<ResponseType = AppResponse>( export async function httpCall<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string, url: string,
options?: RequestInit options?: RequestInit
): Promise<ApiReturn<ResponseType>> { ): Promise<ApiReturn<ResponseType, ErrorType>> {
try { try {
const response = await fetch(url, { const response = await fetch(url, {
credentials: 'include', credentials: 'include',
@ -44,11 +53,7 @@ export async function callApi<ResponseType = AppResponse>(
return { return {
response: undefined, response: undefined,
error: { error: data as ErrorType,
status: response.status,
message: data.message || 'Something went wrong. Please try again later.',
errors: data.errors,
},
}; };
} catch (error: any) { } catch (error: any) {
return { return {
@ -56,68 +61,76 @@ export async function callApi<ResponseType = AppResponse>(
error: { error: {
status: 0, status: 0,
message: error.message, message: error.message,
errors: [],
}, },
}; };
} }
} }
export async function callPostApi<ResponseType = AppResponse>( export async function httpPost<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string, url: string,
body: Record<string, any>, body: Record<string, any>,
options?: RequestInit options?: RequestInit
): Promise<ApiReturn<ResponseType>> { ): Promise<ApiReturn<ResponseType, ErrorType>> {
return callApi<ResponseType>(url, { return httpCall<ResponseType, ErrorType>(url, {
...options, ...options,
method: 'POST', method: 'POST',
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
} }
export async function callGetApi<ResponseType = AppResponse>( export async function httpGet<ResponseType = AppResponse, ErrorType = AppError>(
url: string, url: string,
queryParams?: Record<string, any>, queryParams?: Record<string, any>,
options?: RequestInit options?: RequestInit
): Promise<ApiReturn<ResponseType>> { ): Promise<ApiReturn<ResponseType, ErrorType>> {
const searchParams = new URLSearchParams(queryParams).toString(); const searchParams = new URLSearchParams(queryParams).toString();
const queryUrl = searchParams ? `${url}?${searchParams}` : url; const queryUrl = searchParams ? `${url}?${searchParams}` : url;
return callApi<ResponseType>(queryUrl, { return httpCall<ResponseType, ErrorType>(queryUrl, {
credentials: 'include', credentials: 'include',
method: 'GET', method: 'GET',
...options, ...options,
}); });
} }
export async function callPatchApi<ResponseType = AppResponse>( export async function httpPatch<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string, url: string,
body: Record<string, any>, body: Record<string, any>,
options?: RequestInit options?: RequestInit
): Promise<ApiReturn<ResponseType>> { ): Promise<ApiReturn<ResponseType, ErrorType>> {
return callApi<ResponseType>(url, { return httpCall<ResponseType, ErrorType>(url, {
...options, ...options,
method: 'PATCH', method: 'PATCH',
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
} }
export async function callPutApi<ResponseType = AppResponse>( export async function httpPut<ResponseType = AppResponse, ErrorType = AppError>(
url: string, url: string,
body: Record<string, any>, body: Record<string, any>,
options?: RequestInit options?: RequestInit
): Promise<ApiReturn<ResponseType>> { ): Promise<ApiReturn<ResponseType, ErrorType>> {
return callApi<ResponseType>(url, { return httpCall<ResponseType, ErrorType>(url, {
...options, ...options,
method: 'PUT', method: 'PUT',
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
} }
export async function callDeleteApi<ResponseType = AppResponse>( export async function httpDelete<
ResponseType = AppResponse,
ErrorType = AppError
>(
url: string, url: string,
options?: RequestInit options?: RequestInit
): Promise<ApiReturn<ResponseType>> { ): Promise<ApiReturn<ResponseType, ErrorType>> {
return callApi<ResponseType>(url, { return httpCall<ResponseType, ErrorType>(url, {
...options, ...options,
method: 'DELETE', method: 'DELETE',
}); });

Loading…
Cancel
Save