import {z} from 'zod' export type GetApiEventsArgs = {page?: number | undefined, per_page?: number | undefined, external_subscription_id?: string | undefined, metric_code?: string | undefined, from_date?: string | undefined, to_date?: string | undefined}; export type GetApiEventsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTimeWithZone = z.string(); export const eventResponse = z.object({transaction_id: z.string(), external_subscription_id: z.string(), metric_code: z.string(), timestamp: dateTimeWithZone.optional(), properties: z.object({}).optional(), id: z.string().optional(), created_at: dateTimeWithZone.optional()}); export const paginationMetadata = z.object({total_count: z.number().int().optional(), total_pages: z.number().int().optional(), current_page: z.number().int().optional()}); export const getApiEventsResponse = z.object({events: z.array(eventResponse).optional(), metadata: paginationMetadata.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 }; /** * Query usage events * * GET /events */ export const getApiEvents = async (args: GetApiEventsArgs, options?: GetApiEventsOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.external_subscription_id != null) query.append("external_subscription_id", String(args.external_subscription_id)) if (args.metric_code != null) query.append("metric_code", String(args.metric_code)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiEventsResponse, await res.json()) };