import {z} from 'zod' export type GetApiCustomersCustomerIdMetafieldsMetafieldIdArgs = {customer_id: string, metafield_id: string}; export type GetApiCustomersCustomerIdMetafieldsMetafieldIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metafieldValue = z.object({id: z.string().optional(), namespace: z.string().optional(), key: z.string().optional(), field_type: z.enum(["single_line_text_field", "multi_line_text_field", "number_integer", "number_decimal", "json", "boolean", "url"]).optional(), field_value: z.union([z.string(), z.number(), z.boolean(), z.object({})]).optional(), created_at: z.string().optional(), updated_at: z.string().optional(), metafield_type: z.enum(["merchant", "app"]).optional(), resource_id: z.string().optional(), resource_type: z.enum(["merchant", "customer", "product", "order", "order_item", "cart_item"]).optional(), merchant_id: z.string().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 specific metafield * * GET /customers/{customer_id}/metafields/{metafield_id} */ export const getApiCustomersCustomerIdMetafieldsMetafieldId = async (args: GetApiCustomersCustomerIdMetafieldsMetafieldIdArgs, options?: GetApiCustomersCustomerIdMetafieldsMetafieldIdOptions) => { const {baseUrl = "https://open.shopline.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/customers/${encodeURIComponent(String(args.customer_id))}/metafields/${encodeURIComponent(String(args.metafield_id))}${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(metafieldValue, await res.json()) };