import {z} from 'zod' export type GetApiChannelV1EventsEventIdArgs = {eventId: string}; export type GetApiChannelV1EventsEventIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), rel: z.string().optional()}).nullable(); export const companyWS = z.object({links: z.array(link).optional(), companyId: z.string().optional(), name: z.string().optional()}).nullable(); export const errorWS = z.object({code: z.string().nullable().optional(), message: z.string().nullable().optional()}); export const eventProductWS = z.object({links: z.array(link).optional(), name: z.string().optional(), productId: z.number().optional()}).nullable(); export const eventType = z.enum(["SUBSCRIPTION_ORDER", "SUBSCRIPTION_CHANGE", "SUBSCRIPTION_CANCEL", "SUBSCRIPTION_NOTICE", "USER_ASSIGNMENT", "USER_UNASSIGNMENT", "USER_UPDATED", "ADDON_ORDER", "ADDON_CHANGE", "ADDON_CANCEL", "ADDON_BIND", "ADDON_UNBIND"]); export const eventWS = z.object({asynchronous: z.boolean().optional(), company: companyWS.optional(), consumed: z.boolean().optional(), creationDate: z.number().optional(), error: errorWS.optional(), manuallyResolved: z.boolean().optional(), orderId: z.number().optional(), product: eventProductWS.optional(), subscriptionId: z.number().optional(), successful: z.boolean().optional(), type: eventType.optional(), uuid: 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 }; /** * Retrieve an event * * GET /channel/v1/events/{eventId} */ export const getApiChannelV1EventsEventId = async (args: GetApiChannelV1EventsEventIdArgs, options?: GetApiChannelV1EventsEventIdOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/channel/v1/events/${encodeURIComponent(String(args.eventId))}${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(eventWS, await res.json()) };