react(javascript) common parameters with spread operator(…)

◾️ How to refactoring common parameter with spread operator?

import axios from "axios";

const api = axios.create({
  baseURL: "https://www.googleapis.com/youtube/v3/",
  params: {
    key: process.env.REACT_APP_API_KEY,
  },
});

export const youtubeApi = {
  popularVideo: async () =>
    await api.get("videos", {
      params: {
        part: "snippet",
        maxResults: 100,
        regionCode: "JP",
        chart: "mostPopular",
      },
    }),
  detailVideo: async (id) =>
    await api.get("videos", {
      params: {
        part: "snippet",
        maxResults: 100,
        regionCode: "JP",
        id,
      },
    }),
};
import axios from "axios";

const api = axios.create({
  baseURL: "https://www.googleapis.com/youtube/v3/",
  params: {
    key: process.env.REACT_APP_API_KEY,
  },
});

const commonParams = {
  part: "snippet",
  maxResults: 100,
  regionCode: "JP",
};

export const youtubeApi = {
  popularVideo: async () =>
    await api.get("videos", {
      params: {
        ...commonParams,
        chart: "mostPopular",
      },
    }),
  detailVideo: async (id) =>
    await api.get("videos", {
      params: {
        ...commonParams,
        id,
      },
    }),
};

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です