File tree Expand file tree Collapse file tree 5 files changed +61
-1
lines changed Expand file tree Collapse file tree 5 files changed +61
-1
lines changed Original file line number Diff line number Diff line change 1+ import { type loginFormSchema } from "@/schemas/forms/login" ;
2+ import { type z } from "zod" ;
3+ import api from "." ;
4+ import { ApiResponse } from "../schemas/api" ;
5+ import { handleAPIError } from "@/lib/error" ;
6+
7+ export async function login ( body : z . infer < typeof loginFormSchema > ) {
8+ try {
9+ const { data } = await api . post < ApiResponse > ( `login` , body ) ;
10+ return data . message ;
11+ } catch ( e ) {
12+ throw handleAPIError ( e ) ;
13+ }
14+ }
15+
16+
17+
Original file line number Diff line number Diff line change 1+
2+ import axios , { type AxiosError } from "axios" ;
3+ import { ApiError } from "next/dist/server/api-utils" ;
4+ import { toSentenceCase } from "@/lib/utils" ;
5+
6+ export function handleAPIError ( err : unknown ) : ApiError {
7+ if ( axios . isAxiosError ( err ) ) {
8+ const error = err as AxiosError ;
9+ const response = error . response ;
10+ const data = response ?. data ;
11+ if ( data ) {
12+ const msg = ( data as { error : string } ) ?. error ;
13+ if ( msg ) {
14+ return new ApiError ( response . status , toSentenceCase ( msg ) ) ;
15+ }
16+ }
17+ }
18+ return new ApiError ( 500 , "Something went wrong" ) ;
19+ }
Original file line number Diff line number Diff line change @@ -4,3 +4,18 @@ import { twMerge } from "tailwind-merge"
44export function cn ( ...inputs : ClassValue [ ] ) {
55 return twMerge ( clsx ( inputs ) )
66}
7+
8+ export function toSentenceCase ( input : string ) : string {
9+ const trimmedInput = input . trim ( ) ;
10+ if ( trimmedInput . length === 0 ) {
11+ return "" ;
12+ }
13+
14+ const sentences = trimmedInput . split ( / (?< = [ . ! ? ] ) \s + / ) ;
15+
16+ const sentenceCased = sentences . map ( ( sentence ) => {
17+ return sentence . charAt ( 0 ) . toUpperCase ( ) + sentence . slice ( 1 ) ;
18+ } ) ;
19+
20+ return sentenceCased . join ( " " ) ;
21+ }
Original file line number Diff line number Diff line change 3232 "react-hot-toast" : " ^2.6.0" ,
3333 "react-icons" : " ^5.5.0" ,
3434 "react-markdown" : " ^10.1.0" ,
35- "tailwind-merge" : " ^3.3.1"
35+ "tailwind-merge" : " ^3.3.1" ,
36+ "zod" : " ^4.1.9"
3637 },
3738 "devDependencies" : {
3839 "@eslint/eslintrc" : " ^3" ,
You can’t perform that action at this time.
0 commit comments