Open4

TypeScript

佐藤さとる佐藤さとる

typescript で fetch 簡単にする関数

参考: asdf

/**
 * @description GETリクエストを送信する
 * @param path リクエストパス
 * @returns {Promise<T | undefined>} レスポンスデータ
 */
export const commonGetFetch = async <T>(
  path: string
): Promise<T | undefined> => {
  const url = new URL(path, BASE_URL).href;

  return axios
    .get(path)
    .then((res) => {
      return res.data as T;
    })
    .catch((err) => {
      console.error(err);
      return undefined;
    });
};

https://github.com/SystemEngineeringTeam/geekcamp-2023-vol9/blob/61f10d0b1bdd3e211c91845ce091010396808fcd/frontend/components/api/api.ts#L7-L19