import {z} from 'zod' export type GetApiV2ShopsShopIdentifierCustomersArgs = {shop_identifier: string, since_id?: number | undefined, updated_at_min?: string | undefined, updated_at_max?: string | undefined, search?: string | undefined, ids?: string | undefined, orderby?: string | undefined, orderdirection?: string | undefined, filter?: Array | undefined, label?: string | undefined, limit?: number | undefined, page?: number | undefined}; export type GetApiV2ShopsShopIdentifierCustomersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v2PaginationResponse = z.object({total: z.number().int().optional(), count: z.number().int().optional(), per_page: z.number().int().optional(), current_page: z.number().int().optional(), total_pages: z.number().int().optional(), next_url: z.string().optional(), previous_url: z.string().optional()}); export const customersv2Label = z.object({id: z.number().int().optional(), customer_id: z.number().int().optional(), name: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional(), deleted_at: z.string().nullable().optional()}); export const v2Group = z.object({id: z.number().int().optional(), name: z.string().optional(), description: z.string().optional(), shop_identifier: z.string().optional(), platform_id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional(), deleted_at: z.string().nullable().optional()}); export const v2Address = z.object({city: z.string().optional(), province: z.string().optional(), province_code: z.string().optional(), country: z.string().optional(), created_at: z.string().optional(), zip: z.string().optional(), address_type: z.string().optional(), platform_customer_id: z.string().optional(), deleted_at: z.string().nullable().optional(), address_use: z.enum(["shipping", "billing"]).optional(), company: z.string().optional(), street_2: z.string().optional(), platform_id: z.string().optional(), shop_identifier: z.string().optional(), platform_type: z.string().optional(), first_name: z.string().optional(), address_source: z.enum(["platform", "bold"]).optional(), last_name: z.string().optional(), updated_at: z.string().optional(), phone: z.string().optional(), is_default: z.boolean().optional(), bold_customer_id: z.number().int().optional(), country_iso2: z.string().optional(), street_1: z.string().optional(), id: z.number().int().optional()}); export const v2Customer = z.object({tags: z.string().optional(), deleted_at: z.string().nullable().optional(), shop_identifier: z.string().optional(), platform_created_at: z.string().optional(), id: z.number().int().optional(), platform_type: z.string().optional(), last_name: z.string().optional(), first_name: z.string().optional(), labels: z.array(customersv2Label).optional(), groups: z.array(v2Group).optional(), updated_at: z.string().nullable().optional(), phone: z.string().optional(), platform_id: z.string().optional(), addresses: z.array(v2Address).optional(), created_at: z.string().optional(), email: z.string().optional(), default_address: v2Address.optional()}); export const v2CustomerListResponse = z.object({pagination: v2PaginationResponse.optional(), customers: z.array(v2Customer).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 }; /** * List Customers * * GET /v2/shops/{shop_identifier}/customers */ export const getApiV2ShopsShopIdentifierCustomers = async (args: GetApiV2ShopsShopIdentifierCustomersArgs, options?: GetApiV2ShopsShopIdentifierCustomersOptions) => { const {baseUrl = "http://customer-api.prod.svc.cluster.local:9000", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.since_id != null) query.append("since_id", String(args.since_id)) if (args.updated_at_min != null) query.append("updated_at_min", String(args.updated_at_min)) if (args.updated_at_max != null) query.append("updated_at_max", String(args.updated_at_max)) if (args.search != null) query.append("search", String(args.search)) if (args.ids != null) query.append("ids", String(args.ids)) if (args.orderby != null) query.append("orderby", String(args.orderby)) if (args.orderdirection != null) query.append("orderdirection", String(args.orderdirection)) for (const item of args.filter ?? []) query.append("filter", String(item)) if (args.label != null) query.append("label", String(args.label)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/shops/${encodeURIComponent(String(args.shop_identifier))}/customers${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(v2CustomerListResponse, await res.json()) };