import {z} from 'zod' export type GetApiShopsSHOPIDCustomersPointLogsArgs = {SHOP_ID: number, shop_customer_id: string, page_size?: number | undefined, page_number?: number | undefined}; export type GetApiShopsSHOPIDCustomersPointLogsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiShopsSHOPIDCustomersPointLogsResponse = z.object({data: z.array(z.object({amount: z.number().optional(), creator: z.object({id: z.string().optional(), name: z.string().optional(), fb_id: z.string().optional(), avatar_url: z.string().optional()}).optional(), current_point: z.number().optional(), inserted_at: z.string().optional(), note: z.string().optional()})).optional(), page_number: z.number().int().optional(), page_size: z.number().int().optional(), success: z.boolean().optional(), total_entries: z.number().int().optional(), total_pages: z.number().int().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Customer reward point history * * GET /shops/{SHOP_ID}/customers/point_logs */ export const getApiShopsSHOPIDCustomersPointLogs = async (args: GetApiShopsSHOPIDCustomersPointLogsArgs, options?: GetApiShopsSHOPIDCustomersPointLogsOptions) => { const {baseUrl = "https://pos.pages.fm/api/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.shop_customer_id != null) query.append("shop_customer_id", String(args.shop_customer_id)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_number != null) query.append("page_number", String(args.page_number)) if (ApiKeyAuth !== undefined) query.set("api_key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/shops/${encodeURIComponent(String(args.SHOP_ID))}/customers/point_logs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiShopsSHOPIDCustomersPointLogsResponse, await res.json()) };