import {z} from 'zod' export type GetApiEventsEventRefArgs = {Authorization: string, eventRef: string}; export type GetApiEventsEventRefOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const eventGetResponse = z.object({id: z.string().optional(), accountRef: z.string().optional(), type: z.string().optional(), defaultLocale: z.string().optional(), customer: z.object({firstName: z.string().optional(), lastName: z.string().optional(), email: z.string(), address: z.string().optional(), mobile: z.string().optional(), reference: z.string().optional()}).optional(), channel: z.object({id: z.string(), type: z.enum(["user_defined", "etrusted"]).optional()}).optional(), tracking: z.object({client: z.string().optional(), medium: z.enum(["API", "WIDGET", "WEB_APP", "MOBILE_APP"]).optional()}).optional(), transaction: z.object({reference: z.string(), date: z.string().optional()}).optional(), estimatedDeliveryDate: z.string().optional(), products: z.array(z.object({gtin: z.string().optional(), imageUrl: z.string().optional(), name: z.string(), mpn: z.string().optional(), sku: z.string(), brand: z.string().optional(), url: z.string()})).optional(), system: z.string().optional(), systemVersion: z.string().optional(), metadata: z.object({}).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 an event * * GET /events/{eventRef} */ export const getApiEventsEventRef = async (args: GetApiEventsEventRefArgs, options: GetApiEventsEventRefOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/events/${encodeURIComponent(String(args.eventRef))}${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(eventGetResponse, await res.json()) };