import {z} from 'zod' export type GetApiV1InstoreOrdersReferencesArgs = {orderToken?: string | undefined, merchantOrderReference?: string | undefined, merchantProcessorReference?: string | undefined}; export type GetApiV1InstoreOrdersReferencesOptions = {baseUrl?: string | undefined, InstoreApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1InstoreOrdersReferencesResponse = z.array(z.object({orderToken: z.string().optional(), merchantOrderReference: z.string().optional(), merchantProcessorReference: z.string().optional(), createdAt: 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 instore orders by reference * * GET /v1/instore/orders/references */ export const getApiV1InstoreOrdersReferences = async (args: GetApiV1InstoreOrdersReferencesArgs, options?: GetApiV1InstoreOrdersReferencesOptions) => { const {baseUrl = "https://integration.api.scalapay.com", fetch: fetchFn = fetch, InstoreApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orderToken != null) query.append("orderToken", String(args.orderToken)) if (args.merchantOrderReference != null) query.append("merchantOrderReference", String(args.merchantOrderReference)) if (args.merchantProcessorReference != null) query.append("merchantProcessorReference", String(args.merchantProcessorReference)) if (InstoreApiKeyAuth !== undefined) headers.set("Authorization", InstoreApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/instore/orders/references${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(getApiV1InstoreOrdersReferencesResponse, await res.json()) };