import {z} from 'zod' export type OcapiV1VouchersVoucherType = 'Ticket' | 'Subscription' | 'Deal'; export type OcapiV1VouchersVoucherCredentials = {barcode: string, pin?: (string | null) | undefined}; export type OcapiV1OrdersVouchersRequestsOrderDealVoucherRequest = {type: OcapiV1VouchersVoucherType, credentials: OcapiV1VouchersVoucherCredentials}; export type OcapiV1OrdersVouchersRequestsOrderSubscriptionVoucherRequest = {type: OcapiV1VouchersVoucherType, credentials: OcapiV1VouchersVoucherCredentials}; export type OcapiV1OrdersVouchersRequestsOrderTicketVoucherRequest = {type: OcapiV1VouchersVoucherType, credentials: OcapiV1VouchersVoucherCredentials}; export type CreateApiOcapiV1OrdersOrderIdVouchersArgs = {orderId: string, 'Connect-Region-Code'?: string | undefined, body: OcapiV1OrdersVouchersRequestsOrderDealVoucherRequest | OcapiV1OrdersVouchersRequestsOrderSubscriptionVoucherRequest | OcapiV1OrdersVouchersRequestsOrderTicketVoucherRequest}; export type CreateApiOcapiV1OrdersOrderIdVouchersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const ocapiV1VouchersVoucherType = z.enum(["Ticket", "Subscription", "Deal"]); export const ocapiV1Translation = z.object({languageTag: z.string().min(1), text: z.string().min(1)}); export const ocapiV1Translatable = z.object({text: z.string().min(1), translations: z.array(ocapiV1Translation)}); export const ocapiV1OrdersVouchersOrderVoucher = z.object({id: z.string().min(1), type: ocapiV1VouchersVoucherType, maskedBarcode: z.string().min(1), voucherTypeCode: z.string().min(1), voucherTypeDescription: ocapiV1Translatable.optional()}); export const ocapiV1OrdersVouchersOrderVoucherDetails = z.object({voucher: ocapiV1OrdersVouchersOrderVoucher, availableRedemptions: z.number().int()}); 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 }; /** * Adds a voucher to an in progress order base * * POST /ocapi/v1/orders/{orderId}/vouchers */ export const createApiOcapiV1OrdersOrderIdVouchers = async (args: CreateApiOcapiV1OrdersOrderIdVouchersArgs, options: CreateApiOcapiV1OrdersOrderIdVouchersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Connect-Region-Code'] != null) headers.set("Connect-Region-Code", String(args['Connect-Region-Code'])) const url = `${baseUrl.replace(/\/+$/, '')}/ocapi/v1/orders/${encodeURIComponent(String(args.orderId))}/vouchers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(ocapiV1OrdersVouchersOrderVoucherDetails, await res.json()) };