-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
280 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { z } from 'zod'; | ||
|
||
export const GetLoginStatusSchema = z.object({ | ||
message: z.enum(['Authenticated', 'Not Authenticated']), | ||
}); | ||
|
||
export type GetLoginStatus = z.infer<typeof GetLoginStatusSchema>; |
This file was deleted.
Oops, something went wrong.
17 changes: 9 additions & 8 deletions
17
packages/frontend/src/apis/queries/auth/useGetLoginStatus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { GetLoginStatusResponse } from './types'; | ||
import { instance } from '@/apis/config'; | ||
import { GetLoginStatusSchema, type GetLoginStatus } from './schema'; | ||
import { get } from '@/apis/utils/get'; | ||
|
||
const getLoginStatus = async (): Promise<GetLoginStatusResponse> => { | ||
const { data } = await instance.get('/api/auth/google/status'); | ||
return data; | ||
}; | ||
const getLoginStatus = () => | ||
get<GetLoginStatus>({ | ||
schema: GetLoginStatusSchema, | ||
url: '/api/auth/google/status', | ||
}); | ||
|
||
export const useGetLoginStatus = () => { | ||
return useQuery<GetLoginStatusResponse, Error>({ | ||
queryKey: ['login_status'], | ||
return useQuery({ | ||
queryKey: ['loginStatus'], | ||
queryFn: getLoginStatus, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useGetStockDetail'; | ||
export * from './usePostStockView'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { z } from 'zod'; | ||
|
||
export const GetStockRequestSchema = z.object({ | ||
stockId: z.string(), | ||
}); | ||
|
||
export type GetStockRequest = z.infer<typeof GetStockRequestSchema>; | ||
|
||
export const GetStockResponseSchema = z.object({ | ||
marketCap: z.number(), | ||
name: z.string(), | ||
eps: z.number(), | ||
per: z.number(), | ||
high52w: z.number(), | ||
low52w: z.number(), | ||
}); | ||
|
||
export type GetStockResponse = z.infer<typeof GetStockResponseSchema>; | ||
|
||
export const PostStockViewRequestSchema = z.object({ | ||
stockId: z.string(), | ||
}); | ||
|
||
export type PostStockViewRequest = z.infer<typeof PostStockViewRequestSchema>; | ||
|
||
export const PostViewResponseSchema = z.object({ | ||
id: z.string(), | ||
message: z.string(), | ||
date: z.date(), | ||
}); | ||
|
||
export type PostViewResponse = z.infer<typeof PostViewResponseSchema>; |
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
packages/frontend/src/apis/queries/stock-detail/useGetStockDetail.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { | ||
GetStockResponseSchema, | ||
type GetStockRequest, | ||
type GetStockResponse, | ||
} from './schema'; | ||
import { get } from '@/apis/utils/get'; | ||
|
||
const getStockDetail = ({ stockId }: GetStockRequest) => | ||
get<GetStockResponse>({ | ||
schema: GetStockResponseSchema, | ||
url: `/api/stock/${stockId}/detail`, | ||
}); | ||
|
||
export const useGetStockDetail = ({ stockId }: GetStockRequest) => { | ||
return useQuery({ | ||
queryKey: ['stockDetail'], | ||
queryFn: () => getStockDetail({ stockId }), | ||
enabled: !!stockId, | ||
}); | ||
}; |
34 changes: 18 additions & 16 deletions
34
packages/frontend/src/apis/queries/stock-detail/usePostStockView.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
import type { PostStockViewRequest, PostStockViewResponse } from './types'; | ||
import { useMutation, type UseMutationOptions } from '@tanstack/react-query'; | ||
import { instance } from '@/apis/config'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { | ||
PostViewResponseSchema, | ||
type PostStockViewRequest, | ||
type PostViewResponse, | ||
} from './schema'; | ||
import { post } from '@/apis/utils/post'; | ||
|
||
const postStockView = async ({ | ||
stockId, | ||
}: PostStockViewRequest): Promise<PostStockViewResponse> => { | ||
const { data } = await instance.post('/api/stock/view', { stockId }); | ||
return data; | ||
}; | ||
const postStockView = async ({ stockId }: PostStockViewRequest) => | ||
post<PostViewResponse>({ | ||
params: stockId, | ||
schema: PostViewResponseSchema, | ||
url: 'api/stock/view', | ||
}); | ||
|
||
export const usePostStockView = ( | ||
options?: UseMutationOptions<PostStockViewResponse, Error, string>, | ||
) => { | ||
return useMutation<PostStockViewResponse, Error, string>({ | ||
mutationKey: ['stock_view'], | ||
mutationFn: (stockId) => postStockView({ stockId }), | ||
...options, | ||
export const usePostStockView = () => { | ||
return useMutation({ | ||
mutationKey: ['stockView'], | ||
mutationFn: ({ stockId }: PostStockViewRequest) => | ||
postStockView({ stockId }), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './types'; | ||
export * from './schema'; | ||
export * from './useGetTopViews'; | ||
export * from './useGetStocksByPrice'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { z } from 'zod'; | ||
|
||
export const GetStockListRequestSchema = z.object({ | ||
limit: z.number(), | ||
sortType: z.enum(['increase', 'decrease']), | ||
}); | ||
|
||
export type GetStockListRequest = z.infer<typeof GetStockListRequestSchema>; | ||
|
||
export const GetStockListResponseSchema = z.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
currentPrice: z.number(), | ||
changeRate: z.number(), | ||
volume: z.number(), | ||
marketCap: z.string(), | ||
}); | ||
|
||
export type GetStockListResponse = z.infer<typeof GetStockListResponseSchema>; |
This file was deleted.
Oops, something went wrong.
45 changes: 24 additions & 21 deletions
45
packages/frontend/src/apis/queries/stocks/useGetStocksByPrice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
import type { GetStockListRequest, GetStockListResponse } from './types'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { instance } from '@/apis/config'; | ||
import { | ||
GetStockListResponseSchema, | ||
type GetStockListRequest, | ||
type GetStockListResponse, | ||
} from './schema'; | ||
import { get } from '@/apis/utils/get'; | ||
|
||
const getTopGainers = async ({ | ||
limit, | ||
}: GetStockListRequest): Promise<GetStockListResponse[]> => { | ||
const { data } = await instance.get(`/api/stock/topGainers?limit=${limit}`); | ||
return data; | ||
}; | ||
const getTopGainers = ({ limit }: Partial<GetStockListRequest>) => | ||
get<GetStockListResponse[]>({ | ||
schema: GetStockListResponseSchema, | ||
url: `/api/stock/topGainers?limit=${limit}`, | ||
}); | ||
|
||
const getTopLosers = async ({ | ||
limit, | ||
}: GetStockListRequest): Promise<GetStockListResponse[]> => { | ||
const { data } = await instance.get(`/api/stock/topLosers?limit=${limit}`); | ||
return data; | ||
}; | ||
const getTopLosers = ({ limit }: Partial<GetStockListRequest>) => | ||
get<GetStockListResponse[]>({ | ||
schema: GetStockListResponseSchema, | ||
url: `/api/stock/topLosers?limit=${limit}`, | ||
}); | ||
|
||
export const useGetStocksByPrice = ({ | ||
limit, | ||
isGaining, | ||
}: GetStockListRequest & { isGaining: boolean }) => { | ||
return useQuery<GetStockListResponse[], Error>({ | ||
queryKey: ['stocks', isGaining], | ||
queryFn: isGaining | ||
? () => getTopGainers({ limit }) | ||
: () => getTopLosers({ limit }), | ||
sortType, | ||
}: GetStockListRequest) => { | ||
return useQuery({ | ||
queryKey: ['stocks', sortType], | ||
queryFn: | ||
sortType === 'increase' | ||
? () => getTopGainers({ limit }) | ||
: () => getTopLosers({ limit }), | ||
}); | ||
}; |
23 changes: 13 additions & 10 deletions
23
packages/frontend/src/apis/queries/stocks/useGetTopViews.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { z } from 'zod'; | ||
|
||
export const formatZodError = (error: z.ZodError): string => { | ||
return error.errors | ||
.map((err) => `${err.path.join('.')}: ${err.message}`) | ||
.join(', '); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { z } from 'zod'; | ||
import { instance } from '../config'; | ||
import { formatZodError } from './formatZodError'; | ||
|
||
interface GetParams { | ||
schema: z.ZodType; | ||
url: string; | ||
} | ||
|
||
export const get = async <T>({ schema, url }: GetParams): Promise<T | null> => { | ||
try { | ||
const { data } = await instance.get(url); | ||
const result = schema.safeParse(data); | ||
|
||
if (!result.success) { | ||
throw new Error(formatZodError(result.error)); | ||
} | ||
|
||
return data; | ||
} catch (error) { | ||
if (process.env.NODE_ENV === 'development') { | ||
console.error('API error:', error); | ||
} | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { z } from 'zod'; | ||
import { instance } from '../config'; | ||
import { formatZodError } from './formatZodError'; | ||
|
||
interface PostParams { | ||
params: AxiosRequestConfig['params']; | ||
schema: z.ZodType; | ||
url: string; | ||
} | ||
|
||
export const post = async <T>({ | ||
params, | ||
schema, | ||
url, | ||
}: PostParams): Promise<T | null> => { | ||
try { | ||
const { data } = await instance.post(url, { params }); | ||
const result = schema.safeParse(data); | ||
|
||
if (!result.success) { | ||
throw new Error(formatZodError(result.error)); | ||
} | ||
|
||
return data; | ||
} catch (error) { | ||
if (process.env.NODE_ENV === 'development') { | ||
console.error('API error:', error); | ||
} | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.