import {z} from 'zod' export type GetApiChannelV1EventsArgs = {fromDate?: number | undefined, orderId?: number | undefined, page?: string | undefined, searchText?: string | undefined, size?: string | undefined, sortOrder?: ('ASC' | 'DESC') | undefined, toDate?: number | undefined, type?: string | undefined}; export type GetApiChannelV1EventsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), rel: z.string().optional()}).nullable(); export const pageMetadata = z.object({number: z.number(), size: z.number(), totalElements: z.number(), totalPages: z.number()}); export const pagedResourcesOfEventWS = z.object({links: z.array(link).optional(), content: z.array(z.object({})).optional(), page: pageMetadata.optional()}).nullable(); 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 }; /** * List all events * * GET /channel/v1/events */ export const getApiChannelV1Events = async (args: GetApiChannelV1EventsArgs, options?: GetApiChannelV1EventsOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.orderId != null) query.append("orderId", String(args.orderId)) if (args.page != null) query.append("page", String(args.page)) if (args.searchText != null) query.append("searchText", String(args.searchText)) if (args.size != null) query.append("size", String(args.size)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.type != null) query.append("type", String(args.type)) const url = `${baseUrl.replace(/\/+$/, '')}/channel/v1/events${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(pagedResourcesOfEventWS, await res.json()) };