-
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.
* feat: Endpoint 객체 생성 * feat: 에러 발생 추가 * feat: makers 엔드포인트 적용 * feat: 검색 엔드포인트에 적용 * refactor: 폴더 추가 * feat: 코드리뷰 반영
- Loading branch information
Showing
6 changed files
with
105 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { z } from 'zod'; | ||
|
||
import { createEndpoint } from '@/api/typedAxios'; | ||
|
||
export const getMakersProfile = createEndpoint({ | ||
request: { | ||
method: 'GET', | ||
url: `makers/profile`, | ||
}, | ||
serverResponseScheme: z.array( | ||
z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
profileImage: z.string().nullable(), | ||
activities: z.array( | ||
z.object({ | ||
id: z.number(), | ||
generation: z.number(), | ||
}), | ||
), | ||
careers: z.array( | ||
z.object({ | ||
id: z.number(), | ||
companyName: z.string(), | ||
title: z.string(), | ||
isCurrent: z.boolean(), | ||
}), | ||
), | ||
}), | ||
), | ||
}); |
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,22 @@ | ||
import { z } from 'zod'; | ||
|
||
import { createEndpoint } from '@/api/typedAxios'; | ||
|
||
export const getMembersSearchByName = createEndpoint({ | ||
request: (name: string) => ({ | ||
method: 'GET', | ||
url: `api/v1/members/search?name=${encodeURIComponent(name)}`, | ||
}), | ||
serverResponseScheme: z.array( | ||
z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
generation: z.number(), | ||
hasProfile: z.boolean(), | ||
profileImage: z | ||
.string() | ||
.nullable() | ||
.transform((str) => str ?? ''), | ||
}), | ||
), | ||
}); |
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,46 @@ | ||
import { AxiosRequestConfig } from 'axios'; | ||
import { z } from 'zod'; | ||
|
||
import { axiosInstance } from '@/api'; | ||
|
||
interface Endpoint<ServerResponse, Params extends unknown[]> { | ||
request(...params: Params): Promise<ServerResponse>; | ||
} | ||
|
||
export function createEndpoint< | ||
Validator extends z.ZodType, | ||
Param extends unknown[] = [], | ||
Transformed = z.infer<Validator>, | ||
>(config: { | ||
request: AxiosRequestConfig | ((...params: Param) => AxiosRequestConfig); | ||
serverResponseScheme: Validator; | ||
transformer?: (original: z.infer<Validator>) => Transformed; | ||
}): Endpoint<Transformed, Param> { | ||
return { | ||
async request(...params) { | ||
const getConfig = () => { | ||
if (typeof config.request === 'function') { | ||
return config.request(...params); | ||
} | ||
return config.request; | ||
}; | ||
|
||
const axiosConfig = getConfig(); | ||
|
||
const { data } = await axiosInstance.request<unknown>(axiosConfig); | ||
|
||
const res = config.serverResponseScheme.safeParse(data); | ||
|
||
if (!res.success) { | ||
const zodError = String(res.error).slice(0, 1000) + '\n...'; | ||
const message = `서버 타입 검증에 실패했습니다. (${axiosConfig.method} ${axiosConfig.url})\n${zodError}`; | ||
console.error(zodError); | ||
throw new Error(message); | ||
} | ||
|
||
return res.data; | ||
}, | ||
}; | ||
} | ||
|
||
export type GetResponseType<T> = T extends Endpoint<infer R, never> ? R : never; |
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