Files
juwan-frontend/lib/api/shops.ts
T

53 lines
1.2 KiB
TypeScript

import { isApiError } from "@/lib/errors"
import type { Shop } from "@/lib/types"
import { httpJson } from "./http"
type Paginated<T> = {
items: T[]
meta: {
total: number
offset: number
limit: number
}
}
export async function listShops(): Promise<Shop[]> {
const res = await httpJson<Paginated<Shop>>("/api/v1/shops?offset=0&limit=100", {
cache: "no-store",
})
return res.items
}
export async function getShopById(shopId: string): Promise<Shop | undefined> {
try {
return await httpJson<Shop>(`/api/v1/shops/${encodeURIComponent(shopId)}`, {
cache: "no-store",
})
} catch (error) {
if (error instanceof Error && error.message === "UNAUTHORIZED") {
throw error
}
if (isApiError(error) && error.code === 404) {
return undefined
}
throw error
}
}
export async function getShopByOwnerId(ownerId: string): Promise<Shop | undefined> {
try {
return await httpJson<Shop>(`/api/v1/users/${encodeURIComponent(ownerId)}/shop`, {
cache: "no-store",
})
} catch (error) {
if (error instanceof Error && error.message === "UNAUTHORIZED") {
throw error
}
if (isApiError(error) && error.code === 404) {
return undefined
}
throw error
}
}