import {z} from 'zod' export type GetApiPointHistoryArgs = {'Accept-Language'?: string | undefined, count?: number | undefined, offset?: number | undefined, sort_field?: string | undefined, sort_direction?: ('ASC' | 'DESC') | undefined, customer_id: string, store_id?: string | undefined, partner_id?: string | undefined, time_from?: string | undefined, time_to?: string | undefined, point_operation_type?: string | undefined, point_operation_note?: string | undefined, point_type_id?: string | undefined}; export type GetApiPointHistoryOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pointHistoryRecord = z.object({customer_id: z.string().optional(), point_amount: z.number().optional(), point_operation_type: z.string().optional(), point_operation_note: z.string().optional(), point_type_id: z.string().optional(), partner_id: z.string().optional(), store_id: z.string().optional(), point_history_record_time: z.string().optional(), extended_data: z.string().optional()}); export const getApiPointHistoryResponse = z.object({data: z.object({point_history: z.array(pointHistoryRecord).optional(), total_items: z.number().int().optional()}).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 }; /** * Get point transaction history * * GET /point-history * * Also accepts auth: basicAuth */ export const getApiPointHistory = async (args: GetApiPointHistoryArgs, options?: GetApiPointHistoryOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args.count != null) query.append("count", String(args.count)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sort_field != null) query.append("sort_field", String(args.sort_field)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.store_id != null) query.append("store_id", String(args.store_id)) if (args.partner_id != null) query.append("partner_id", String(args.partner_id)) if (args.time_from != null) query.append("time_from", String(args.time_from)) if (args.time_to != null) query.append("time_to", String(args.time_to)) if (args.point_operation_type != null) query.append("point_operation_type", String(args.point_operation_type)) if (args.point_operation_note != null) query.append("point_operation_note", String(args.point_operation_note)) if (args.point_type_id != null) query.append("point_type_id", String(args.point_type_id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/point-history${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(getApiPointHistoryResponse, await res.json()) };