-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: exceptions to exception * chore: CustomException ์๋ฌ ํ์ ์ถ๊ฐ * feature: api ํจํค์ง ์์ฑ * feat: web์ ๋ด๋ถ ํจํค์ง ์ถ๊ฐ * feat: instance * feat: interceptor * feat: getUser * chore: lock * chore: lock * chore: directory name * chore: zod to dev deps
- Loading branch information
Showing
17 changed files
with
194 additions
and
9 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,13 @@ | ||
{ | ||
"name": "@gitanimals/api", | ||
"version": "0.0.0", | ||
"main": "./src/index.ts", | ||
"devDependencies": { | ||
"@gitanimals/exception": "workspace:*", | ||
"@gitanimals/eslint-config": "workspace:*", | ||
"@gitanimals/typescript-config": "workspace:*", | ||
"axios": "^1.6.8", | ||
"typescript": "*", | ||
"zod": "^3.23.8" | ||
} | ||
} |
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 axios from 'axios'; | ||
import { setInterceptors } from '../_interceptor'; | ||
|
||
const API_URL = 'https://api.gitanimals.org'; | ||
|
||
const instance = setInterceptors( | ||
axios.create({ | ||
baseURL: API_URL, | ||
timeout: 15000, | ||
}), | ||
); | ||
|
||
export const get = <T>(...args: Parameters<typeof instance.get>) => { | ||
return instance.get<T, T>(...args); | ||
}; | ||
|
||
export const post = <T>(...args: Parameters<typeof instance.post>) => { | ||
return instance.post<T, T>(...args); | ||
}; | ||
|
||
export const put = <T>(...args: Parameters<typeof instance.put>) => { | ||
return instance.put<T, T>(...args); | ||
}; | ||
|
||
export const patch = <T>(...args: Parameters<typeof instance.patch>) => { | ||
return instance.patch<T, T>(...args); | ||
}; | ||
|
||
export const del = <T>(...args: Parameters<typeof instance.delete>) => { | ||
return instance.delete<T, T>(...args); | ||
}; |
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,2 @@ | ||
export * from './default'; | ||
export * from './render'; |
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 axios from 'axios'; | ||
import { setInterceptors } from '../_interceptor'; | ||
|
||
const API_URL = 'https://render.gitanimals.org'; | ||
|
||
const instance = setInterceptors( | ||
axios.create({ | ||
baseURL: API_URL, | ||
timeout: 15000, | ||
}), | ||
); | ||
|
||
export const renderGet = <T>(...args: Parameters<typeof instance.get>) => { | ||
return instance.get<T, T>(...args); | ||
}; | ||
|
||
export const renderPost = <T>(...args: Parameters<typeof instance.post>) => { | ||
return instance.post<T, T>(...args); | ||
}; | ||
|
||
export const renderPut = <T>(...args: Parameters<typeof instance.put>) => { | ||
return instance.put<T, T>(...args); | ||
}; | ||
|
||
export const renderPatch = <T>(...args: Parameters<typeof instance.patch>) => { | ||
return instance.patch<T, T>(...args); | ||
}; | ||
|
||
export const renderDelete = <T>(...args: Parameters<typeof instance.delete>) => { | ||
return instance.delete<T, T>(...args); | ||
}; |
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,44 @@ | ||
import type { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'; | ||
|
||
import { ApiErrorScheme } from '../type'; | ||
|
||
const interceptorRequestFulfilled = (config: InternalAxiosRequestConfig) => { | ||
if (typeof window === 'undefined') return config; | ||
|
||
// TODO: cookie ์ฒ๋ฆฌ ์ ๋ณ๊ฒฝ ํ์ | ||
// const accessToken = getToken(); | ||
if (!config.headers) return config; | ||
// if (!accessToken) return config; | ||
|
||
// config.headers.Authorization = `Bearer ${accessToken}`; | ||
|
||
return config; | ||
}; | ||
|
||
// Response interceptor | ||
const interceptorResponseFulfilled = (res: AxiosResponse) => { | ||
if (200 <= res.status && res.status < 300) { | ||
return res.data; | ||
} | ||
|
||
return Promise.reject(res.data); | ||
}; | ||
|
||
// Response interceptor | ||
const interceptorResponseRejected = (error: AxiosError<ApiErrorScheme>) => { | ||
if (error.response?.status === 401) { | ||
// TODO : logout and refresh login | ||
// TODO : logout ์๋ด | ||
} | ||
|
||
// 403 ์ฒ๋ฆฌ | ||
|
||
return Promise.reject(error); | ||
}; | ||
|
||
export const setInterceptors = (instance: AxiosInstance) => { | ||
instance.interceptors.request.use(interceptorRequestFulfilled); | ||
instance.interceptors.response.use(interceptorResponseFulfilled, interceptorResponseRejected); | ||
|
||
return instance; | ||
}; |
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,3 @@ | ||
export * from './_instance'; | ||
export * from './type'; | ||
export * from './user'; |
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,3 @@ | ||
export interface ApiErrorScheme { | ||
message: string; | ||
} |
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 { z } from 'zod'; | ||
import { CustomException } from '@gitanimals/exception'; | ||
import { get } from '../_instance'; | ||
|
||
const UserSchema = z.object({ | ||
id: z.string(), | ||
username: z.string(), | ||
points: z.string(), | ||
profileImage: z.string(), | ||
}); | ||
|
||
type User = z.infer<typeof UserSchema>; | ||
|
||
export const getUser = async () => { | ||
const res = await get<User>(`/users`); | ||
|
||
const parsed = UserSchema.safeParse(res); | ||
if (parsed.error) throw new CustomException('API_TYPE_NOT_MATCH'); | ||
|
||
return res; | ||
}; |
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 @@ | ||
export * from './getUser'; |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
packages/exceptions/package.json โ packages/exception/package.json
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
File renamed without changes.
9 changes: 5 additions & 4 deletions
9
packages/exceptions/src/CustomException.ts โ packages/exception/src/CustomException.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
File renamed without changes.
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,5 @@ | ||
{ | ||
"extends": "@gitanimals/typescript-config/base.json", | ||
"include": ["src"], | ||
"exclude": ["node_modules"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.