strawberryfields-demo / frontend

Apache License 2.0
0 stars 0 forks source link

Feat: API ํ˜ธ์ถœ ๊ตฌํ˜„ #11

Closed nakyoung98 closed 1 month ago

nakyoung98 commented 1 month ago

๐Ÿ“ƒ ๊ด€๋ จ ์ด์Šˆ

Closes: #10

โœจ ์ž‘์—… ๋‚ด์šฉ

1. API ํ˜ธ์ถœ ๊ธฐ๋ณธ ๊ตฌ์กฐ ๊ตฌํ˜„

[๋ชฉํ‘œ]

const apiCall = <RequestDTO, ResponseDTO>({
  method,
  endpoint,
  data,
  headers,
  params,
  option,
}: {
  method: HTTP_METHOD;
  endpoint: ENDPOINT;
  data?: RequestDTO;
  headers?: AxiosHeaders;
  params?: URLSearchParams;
  option?: OPTIONS<RequestDTO>;
}): Promise<AxiosResponse<ResponseDTO>> =>
  axiosInstance.request<ResponseDTO, AxiosResponse<ResponseDTO>, RequestDTO>({
    method,
    url: endpoint,
    data,
    headers,
    params,
    ...option,
  });

2. ํ† ํฐ ์ธํ„ฐ์…‰ํ„ฐ ๊ตฌํ˜„

[๋ชฉํ‘œ]

export const tokenRequestInterceptor = (config: InternalAxiosRequestConfig) => {
  const accessToken = localStorage.getItem("accessToken");
  if (accessToken) {
    config.headers.Authorization = `Bearer ${accessToken}`;
  }
  return config;
};

3. ์—๋Ÿฌ ์ฒ˜๋ฆฌ

[๋ชฉํ‘œ]

export class UnauthorizedError extends AxiosError {
  constructor(error: AxiosError) {
    super(error.message, error.code, error.config, error.request, error.response);
    this.name = "UnauthorizedError";
    this.message = "Please log in to access this resource";
  }
}

4. API URL ์ƒ์ˆ˜ ์ •์˜

[๋ชฉํ‘œ]

export const API_URLS: API_URL_TYPE = {
  HERO: {},
  SIGN_IN: {
    SIGN_IN: `${API_BASE_URL}/user/sign-in`,
  },
  // ...
} as const;

โš ๏ธ ์ฃผ์˜ ์‚ฌํ•ญ

๐Ÿ“ธ ์Šคํฌ๋ฆฐ์ƒท

signin_success

๐Ÿ“– ์ฐธ๊ณ  ์ž๋ฃŒ

๊ฐœ๋ฐœ ๊ธฐ๋ก

๐Ÿ“š ๋ฌธ์„œํ™” ๊ณ„ํš

โž• ์ถ”๊ฐ€ ์ž‘์—… ์‚ฌํ•ญ

โœ”๏ธ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

github-actions[bot] commented 1 month ago

์˜ฌ๋ฐ”๋ฅธ PR ์ปจ๋ฒค์…˜

rebase์— ๋Œ€ํ•œ PR ์ปจ๋ฒค์…˜์„ ์ž˜ ์ค€์ˆ˜ํ•˜์…จ๊ตฐ์š”! ์ฝ”๋“œ ์ž‘์„ฑํ•˜์‹œ๋Š๋ผ ์ˆ˜๊ณ  ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค :)