Skip to content

Commit

Permalink
refactor: api fetch 호출로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yeynii committed Nov 16, 2023
1 parent 08bb4c2 commit b4df8ff
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
tailwind.config.js
node_modules
src/apis/apiGenerator
dist
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tailwind.config.js
node_modules
src/apis/apiGenerator
dist
96 changes: 38 additions & 58 deletions src/apis/apiGenerator/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
/* eslint-disable */
// noinspection ES6UnusedImports,JSUnusedLocalSymbols
import Axios, { AxiosStatic, AxiosRequestConfig, AxiosResponse } from 'axios';

import {
CreatePictureResponse,
EmptyResponse,
GetMyRoleInAlbumResponse,
GetPictureInSlotResponse,
SearchTagResponse,
UserDuplicateResponse,
UserFindResponse,
} from './types';
import { integer, long, float, double, byte, binary, date, dateTime, password } from './types';

import Axios, { AxiosStatic, AxiosRequestConfig, AxiosResponse } from 'axios'
import { CreatePictureResponse, EmptyResponse, GetMyRoleInAlbumResponse, GetPictureInSlotResponse, SearchTagResponse, UserDuplicateResponse, UserFindResponse } from './types'
import { integer, long, float, double, byte, binary, date, dateTime, password } from './types'
export interface $customExtendResponse {}
type $R<T> = Promise<T> & { readonly response: Promise<AxiosResponse<T> & $customExtendResponse> };
export const $axiosConfig: Required<Parameters<AxiosStatic['create']>>[0] = {};
type $R<T> = Promise<T> & { readonly response: Promise<AxiosResponse<T> & $customExtendResponse> }
export const $axiosConfig: Required<Parameters<AxiosStatic['create']>>[0] = {}
const $ep = (_: any) => ({
api: {
v1: {
Expand All @@ -25,108 +16,97 @@ const $ep = (_: any) => ({
pictures: {
/**
* 앨범내 페이지별 사진 목록을 조회한다
*
*
* @returns 200
* @summary 앨범내 페이지별 사진 목록을 조회한다
*/
get: <$T = GetPictureInSlotResponse>($config?: AxiosRequestConfig): $R<$T> =>
_('get', `/api/v1/albums/${albumId}/pictures`, $config),
get: <$T = GetPictureInSlotResponse>($config?: AxiosRequestConfig): $R<$T> => _('get', `/api/v1/albums/${albumId}/pictures`, $config),
/**
* 사진을 업로드한다
*
*
* @returns 200
* @summary 사진을 업로드한다
*/
post: <$T = CreatePictureResponse>($config?: AxiosRequestConfig): $R<$T> =>
_('post', `/api/v1/albums/${albumId}/pictures`, undefined, $config),
post: <$T = CreatePictureResponse>($config?: AxiosRequestConfig): $R<$T> => _('post', `/api/v1/albums/${albumId}/pictures`, undefined, $config),
/** @param pictureId 사진 아이디 */
pictureId: (pictureId: number) => ({
/**
* 사진을 수정한다
*
*
* @returns 200
* @summary 사진을 수정한다
*/
patch: <$T = EmptyResponse>($config?: AxiosRequestConfig): $R<$T> =>
_('patch', `/api/v1/albums/${albumId}/pictures/${pictureId}`, undefined, $config),
}),
patch: <$T = EmptyResponse>($config?: AxiosRequestConfig): $R<$T> => _('patch', `/api/v1/albums/${albumId}/pictures/${pictureId}`, undefined, $config)
})
},
roles: {
me: {
/**
* 앨범에 대한 내 권한을 조회한다
*
*
* @returns 200
* @summary 앨범에 대한 내 권한을 조회한다
*/
get: <$T = GetMyRoleInAlbumResponse>($config?: AxiosRequestConfig): $R<$T> =>
_('get', `/api/v1/albums/${albumId}/roles/me`, $config),
},
get: <$T = GetMyRoleInAlbumResponse>($config?: AxiosRequestConfig): $R<$T> => _('get', `/api/v1/albums/${albumId}/roles/me`, $config)
}
},
tags: {
/**
* 태그를 검색한다
*
*
* @param keyword 검색어
* @returns 200
* @summary 태그를 검색한다
*/
get: <$T = SearchTagResponse>(keyword?: string, $config?: AxiosRequestConfig): $R<$T> =>
_('get', `/api/v1/albums/${albumId}/tags`, { params: { keyword }, ...$config }),
},
}),
get: <$T = SearchTagResponse>(keyword?: string, $config?: AxiosRequestConfig): $R<$T> => _('get', `/api/v1/albums/${albumId}/tags`, { params: { keyword }, ...$config })
}
})
},
users: {
/**
* 닉네임 유저 조회 API
*
*
* 404: 404
*
*
* @param nickname 유저 닉네임
* @returns 200
* @summary 닉네임 유저 조회 API
*/
get: <$T = UserFindResponse>(nickname: string, $config?: AxiosRequestConfig): $R<$T> =>
_('get', `/api/v1/users`, { params: { nickname }, ...$config }),
get: <$T = UserFindResponse>(nickname: string, $config?: AxiosRequestConfig): $R<$T> => _('get', `/api/v1/users`, { params: { nickname }, ...$config }),
duplicate: {
/**
* 닉네임으로 유저 조회 API
*
*
* @param nickname 유저 닉네임
* @returns 200
* @summary 닉네임으로 유저 조회 API
*/
get: <$T = UserDuplicateResponse>(nickname: string, $config?: AxiosRequestConfig): $R<$T> =>
_('get', `/api/v1/users/duplicate`, { params: { nickname }, ...$config }),
get: <$T = UserDuplicateResponse>(nickname: string, $config?: AxiosRequestConfig): $R<$T> => _('get', `/api/v1/users/duplicate`, { params: { nickname }, ...$config })
},
me: {
/**
* 마이페이지 정보 조회 API
*
*
* @returns 200
* @summary 마이페이지 정보 조회 API
*/
get: <$T = UserFindResponse>($config?: AxiosRequestConfig): $R<$T> => _('get', `/api/v1/users/me`, $config),
/**
* 유저정보 수정 API
*
*
* 204: 204
*
*
* @returns undefined
* @summary 유저정보를 수정한다.
*/
patch: <$T = any>($config?: AxiosRequestConfig): $R<$T> => _('patch', `/api/v1/users/me`, undefined, $config),
},
},
},
},
});
export const apiGenerator = ($axios = Axios.create($axiosConfig)) =>
$ep((method: string, ...args: any) => {
const promise = ($axios as any)[method](...args);
return Object.defineProperty(
promise.then((x: any) => x.data),
'response',
{ value: promise }
);
});
patch: <$T = any>($config?: AxiosRequestConfig): $R<$T> => _('patch', `/api/v1/users/me`, undefined, $config)
}
}
}
}
})
export const apiGenerator = ($axios = Axios.create($axiosConfig)) => $ep((method: string, ...args: any) => {
const promise = ($axios as any)[method](...args)
return Object.defineProperty(promise.then((x: any) => x.data), 'response', {value: promise})
})

70 changes: 30 additions & 40 deletions src/apis/apiGenerator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,54 @@

/** CreatePictureResponse */
export type CreatePictureResponse = {
data?: { id?: number };
data?: { id?: number }
/** 응답 메시지 */
message?: string;
};
message?: string
}
/** EmptyResponse */
export type EmptyResponse = {
/** 데이터 */
data?: any;
data?: any
/** 응답 메시지 */
message?: string;
};
message?: string
}
/** GetMyRoleInAlbumResponse */
export type GetMyRoleInAlbumResponse = {
data?: { role?: string };
data?: { role?: string }
/** 응답 메시지 */
message?: string;
};
message?: string
}
/** GetPictureInSlotResponse */
export type GetPictureInSlotResponse = {
data?: {
pictures?: Array<{
layout?: string;
path?: string;
pictureId?: number;
location?: string;
tagNames?: Array<any | boolean | string | number>;
picturedAt?: string;
content?: string;
}>;
};
data?: { pictures?: Array<{ layout?: string, path?: string, pictureId?: number, location?: string, tagNames?: Array<any | boolean | string | number>, picturedAt?: string, content?: string }> }
/** 응답 메시지 */
message?: string;
};
message?: string
}
/** SearchTagResponse */
export type SearchTagResponse = {
data?: { tags?: Array<{ name?: string; id?: number }> };
data?: { tags?: Array<{ name?: string, id?: number }> }
/** 응답 메시지 */
message?: string;
};
message?: string
}
/** UserDuplicateResponse */
export type UserDuplicateResponse = {
data?: { isDuplicated?: boolean };
data?: { isDuplicated?: boolean }
/** 응답 메시지 */
message?: string;
};
message?: string
}
/** UserFindResponse */
export type UserFindResponse = {
/** 데이터 */
data?: { nickname?: string; profilePath?: string; userId?: number; email?: string };
data?: { nickname?: string, profilePath?: string, userId?: number, email?: string }
/** 응답 메시지 */
message?: string;
};
export type integer = number;
export type long = number;
export type float = number;
export type double = number;
export type byte = string;
export type binary = string;
export type date = string;
export type dateTime = string;
export type password = string;
message?: string
}
export type integer = number
export type long = number
export type float = number
export type double = number
export type byte = string
export type binary = string
export type date = string
export type dateTime = string
export type password = string
2 changes: 1 addition & 1 deletion src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ const instance = axios.create({
timeout: 30000,
});

export const api = apiGenerator(instance);
export const api = apiGenerator(instance).api.v1;
2 changes: 1 addition & 1 deletion src/apis/queries/useUserNicknameQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function useUserNicknameQuery({ nickname }: useUserNicknameQueryParams) {
const { data } = useQuery(
['userNicknameInfo'],
async () => {
const { data } = await api.api.v1.users.get(nickname);
const { data } = await api.users.get(nickname);
return data;
},
{
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
],
"exclude": [
"node_modules",
"src/apis/apiGenerator",
"src/apis/apiGenerator"
]
}

0 comments on commit b4df8ff

Please sign in to comment.