Skip to content

Commit

Permalink
✨ feat(api): impl fav arts api client
Browse files Browse the repository at this point in the history
  • Loading branch information
syakoo committed Nov 5, 2023
1 parent 9abe847 commit 32932f6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/api/_shared/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* API ベースパスをくっつける関数
*/
export const withAPIBasePath = (path: string) => {
const basePath = process.env.NEXT_PUBLIC_API_BASE_PATH;

if (!basePath) {
throw new Error("API ベースパスが未設定です");
}

return `${basePath}${path}`;
};

/**
* API キーをくっつける関数
*/
export const withAPIKeyHeader = (headers: Record<string, string>) => {
const apiKey = process.env.NEXT_PUBLIC_API_KEY;

if (!apiKey) {
throw new Error("API キーが未設定です");
}

return { ...headers, "X-Api-Key": apiKey };
};
18 changes: 18 additions & 0 deletions src/api/fav/arts/fetchArtFav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { withAPIBasePath, withAPIKeyHeader } from "@/api/_shared/config";

type FetchArtFavRequestParams = {
id: string;
};

type FetchArtFavResponseBody = {
fav: number;
};

export const fetchArtFav = async (params: FetchArtFavRequestParams) => {
const res = await fetch(withAPIBasePath(`/fav/arts/${params.id}`), {
method: "get",
headers: withAPIKeyHeader({}),
});

return await (res.json() as Promise<FetchArtFavResponseBody>);
};
18 changes: 18 additions & 0 deletions src/api/fav/arts/incrementArtFav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { withAPIBasePath, withAPIKeyHeader } from "@/api/_shared/config";

type IncrementArtFavRequestParams = {
id: string;
};

type IncrementArtFavResponseBody = {
fav: number;
};

export const incrementArtFav = async (params: IncrementArtFavRequestParams) => {
const res = await fetch(withAPIBasePath(`/fav/arts/${params.id}`), {
method: "post",
headers: withAPIKeyHeader({}),
});

return await (res.json() as Promise<IncrementArtFavResponseBody>);
};
18 changes: 18 additions & 0 deletions src/api/fav/arts/syncArtsFav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { withAPIBasePath, withAPIKeyHeader } from "@/api/_shared/config";

type SyncArtsFavRequestParams = {
id: string;
};

type SyncArtsFavResponseBody = {
fav: number;
};

export const syncArtsFav = async (params: SyncArtsFavRequestParams) => {
const res = await fetch(withAPIBasePath(`/fav/arts/${params.id}`), {
method: "post",
headers: withAPIKeyHeader({}),
});

return await (res.json() as Promise<SyncArtsFavResponseBody>);
};
9 changes: 9 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { fetchArtFav } from "./fav/arts/fetchArtFav";
import { incrementArtFav } from "./fav/arts/incrementArtFav";
import { syncArtsFav } from "./fav/arts/syncArtsFav";

export const api = {
fetchArtFav,
incrementArtFav,
syncArtsFav,
};

0 comments on commit 32932f6

Please sign in to comment.