-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(api): impl fav arts api client
- Loading branch information
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |