diff --git a/src/components/AuthenticationFlow/EmailLoginForm.tsx b/src/components/AuthenticationFlow/EmailLoginForm.tsx index a1065c490..6a3917806 100644 --- a/src/components/AuthenticationFlow/EmailLoginForm.tsx +++ b/src/components/AuthenticationFlow/EmailLoginForm.tsx @@ -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(''); diff --git a/src/components/Profile/ForgotPasswordForm.tsx b/src/components/Profile/ForgotPasswordForm.tsx index 10e1f8157..299dd15d7 100644 --- a/src/components/Profile/ForgotPasswordForm.tsx +++ b/src/components/Profile/ForgotPasswordForm.tsx @@ -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, diff --git a/src/lib/http.ts b/src/lib/http.ts index 74e9c319d..df60708d5 100644 --- a/src/lib/http.ts +++ b/src/lib/http.ts @@ -2,14 +2,20 @@ import Cookies from 'js-cookie'; import { TOKEN_COOKIE_NAME } from './constants'; type AppResponse = Record; +type FetchError = { + status: number; + message: string; +}; + +type AppError = { + status: number; + message: string; + errors?: { message: string; location: string }[]; +}; -type ApiReturn = { +type ApiReturn = { response?: ResponseType; - error?: { - status: number; - message: string; - errors?: { message: string; location: string }[]; - }; + error?: ErrorType | FetchError; }; /** @@ -18,10 +24,13 @@ type ApiReturn = { * @param url * @param options */ -export async function callApi( +export async function httpCall< + ResponseType = AppResponse, + ErrorType = AppError +>( url: string, options?: RequestInit -): Promise> { +): Promise> { try { const response = await fetch(url, { credentials: 'include', @@ -44,11 +53,7 @@ export async function callApi( 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( error: { status: 0, message: error.message, - errors: [], }, }; } } -export async function callPostApi( +export async function httpPost< + ResponseType = AppResponse, + ErrorType = AppError +>( url: string, body: Record, options?: RequestInit -): Promise> { - return callApi(url, { +): Promise> { + return httpCall(url, { ...options, method: 'POST', body: JSON.stringify(body), }); } -export async function callGetApi( +export async function httpGet( url: string, queryParams?: Record, options?: RequestInit -): Promise> { +): Promise> { const searchParams = new URLSearchParams(queryParams).toString(); const queryUrl = searchParams ? `${url}?${searchParams}` : url; - return callApi(queryUrl, { + return httpCall(queryUrl, { credentials: 'include', method: 'GET', ...options, }); } -export async function callPatchApi( +export async function httpPatch< + ResponseType = AppResponse, + ErrorType = AppError +>( url: string, body: Record, options?: RequestInit -): Promise> { - return callApi(url, { +): Promise> { + return httpCall(url, { ...options, method: 'PATCH', body: JSON.stringify(body), }); } -export async function callPutApi( +export async function httpPut( url: string, body: Record, options?: RequestInit -): Promise> { - return callApi(url, { +): Promise> { + return httpCall(url, { ...options, method: 'PUT', body: JSON.stringify(body), }); } -export async function callDeleteApi( +export async function httpDelete< + ResponseType = AppResponse, + ErrorType = AppError +>( url: string, options?: RequestInit -): Promise> { - return callApi(url, { +): Promise> { + return httpCall(url, { ...options, method: 'DELETE', });