-
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.
Merge pull request #10 from ks6088ts-labs/feature/issue-9_api-client-…
…util api service for product
- Loading branch information
Showing
11 changed files
with
2,502 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,77 @@ | ||
import type { ApiContext, Category, Condition, Product } from 'types' | ||
import { fetcher } from 'utils' | ||
|
||
export type GetAllProductsParams = { | ||
/** | ||
* 商品カテゴリ | ||
*/ | ||
category?: Category | ||
/** | ||
* 商品状態 | ||
*/ | ||
conditions?: Condition[] | ||
/** | ||
* 所有するユーザーID | ||
*/ | ||
userId?: number | ||
/** | ||
* ソートするキー | ||
*/ | ||
sort?: keyof Omit<Product, 'owner'> | ||
/** | ||
* 昇順or降順 | ||
*/ | ||
order?: 'asc' | 'desc' | ||
/** | ||
* 取得数 | ||
*/ | ||
limit?: number | ||
/** | ||
* ページ数 | ||
*/ | ||
page?: number | ||
} | ||
|
||
/** | ||
* プロダクトAPI(一覧取得) | ||
* @param context APIコンテキスト | ||
* @param params 検索条件 | ||
* @returns 商品一覧 | ||
*/ | ||
// eslint-disable-next-line complexity | ||
const getAllProducts = async ( | ||
context: ApiContext, | ||
{ | ||
category, | ||
conditions, | ||
userId, | ||
page, | ||
limit, | ||
sort = 'id', | ||
order = 'desc', | ||
}: GetAllProductsParams = {}, | ||
): Promise<Product[]> => { | ||
const path = `${context.apiRootUrl.replace(/\/$/g, '')}/products` | ||
const params = new URLSearchParams() | ||
|
||
category && params.append('category', category) | ||
conditions && | ||
conditions.forEach((condition) => params.append('condition', condition)) | ||
userId && params.append('owner.id', `${userId}`) | ||
page && params.append('_page', `${page}`) | ||
limit && params.append('_limit', `${limit}`) | ||
sort && params.append('_sort', sort) | ||
order && params.append('_order', order) | ||
const query = params.toString() | ||
|
||
return await fetcher(query.length > 0 ? `${path}?${query}` : path, { | ||
headers: { | ||
Origin: '*', | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
credentials: 'include', | ||
}, | ||
}) | ||
} | ||
|
||
export default getAllProducts |
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,33 @@ | ||
import type { ApiContext, Product } from 'types' | ||
import { fetcher } from 'utils' | ||
|
||
export type GetProductParams = { | ||
/** | ||
* 取得する商品 | ||
*/ | ||
id: number | ||
} | ||
|
||
/** | ||
* プロダクトAPI(個別取得) | ||
* @param context APIコンテキスト | ||
* @param params 商品ID | ||
* @returns 商品 | ||
*/ | ||
const getProduct = async ( | ||
context: ApiContext, | ||
{ id }: GetProductParams, | ||
): Promise<Product> => { | ||
return await fetcher( | ||
`${context.apiRootUrl.replace(/\/$/g, '')}/products/${id}`, | ||
{ | ||
headers: { | ||
Origin: '*', | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
export default getProduct |
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,32 @@ | ||
// 商品カテゴリ | ||
export type Category = 'shoes' | 'clothes' | 'book' | ||
// 商品の状態 | ||
export type Condition = 'new' | 'used' | ||
|
||
// ユーザー | ||
export type User = { | ||
id: number | ||
username: string | ||
displayName: string | ||
email: string | ||
profileImageUrl: string | ||
description: string | ||
} | ||
|
||
// 商品 | ||
export type Product = { | ||
id: number | ||
category: Category | ||
title: string | ||
description: string | ||
imageUrl: string | ||
blurDataUrl: string | ||
price: number | ||
condition: Condition | ||
owner: User | ||
} | ||
|
||
// APIコンテキスト | ||
export type ApiContext = { | ||
apiRootUrl: string | ||
} |
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 @@ | ||
export * from './data' |
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,16 @@ | ||
export const fetcher = async ( | ||
resource: RequestInfo, | ||
init?: RequestInit, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): Promise<any> => { | ||
const res = await fetch(resource, init) | ||
|
||
if (!res.ok) { | ||
const errorRes = await res.json() | ||
const error = new Error( | ||
errorRes.message ?? 'APIリクエスト中にエラーが発生しました', | ||
) | ||
throw error | ||
} | ||
return res.json() | ||
} |
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 @@ | ||
export * from './fetcher' |
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