import {z} from 'zod' export type GetApiEventsEventIdArgs = {event_id: string}; export type GetApiEventsEventIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const attribute = z.object({name: z.string().min(1).max(50), value: z.string(), unit: z.string().min(1).max(50).optional()}); export const dimensions = z.record(z.string(), z.string().min(1).max(200)); export const event = z.object({schemaName: z.string().min(1).max(50), id: z.string().max(512).optional(), timestamp: z.string(), accountId: z.string().max(512), attributes: z.array(attribute), dimensions: dimensions}); export const ingestionStatus = z.object({status: z.enum(["INGESTION_IN_PROGRESS", "INGESTION_FAILED", "INGESTION_FAILED_SCHEMA_NOT_DEFINED", "INGESTION_FAILED_ENRICHMENT_FAILED", "INGESTION_FAILED_UNITS_INVALID", "INGESTION_COMPLETED_NO_MATCHING_METERS", "INGESTION_COMPLETED_EVENT_METERED", "INGESTION_COMPLETED_EVENT_NOT_METERED", "INGESTION_FAILED_PAST_GRACE_PERIOD", "INGESTION_FAILED_ACCOUNT_NOT_FOUND", "INGESTION_FAILED_DUPLICATE_EVENT", "INGESTION_FAILED_NO_EVENT_ID", "INGESTION_FAILED_INVALID_NAMED_LICENSE_EVENT", "INGESTION_FAILED_INSUFFICIENT_CREDITS", "REVERTED", "UNKNOWN"]), statusDescription: z.string().max(250).optional()}); export const eventSource = z.object({id: z.string().max(100), type: z.string().max(100)}); export const enrichedField = z.object({name: z.string(), type: z.enum(["ATTRIBUTE", "DIMENSION"]), value: z.string()}); export const enrichmentDependency = z.object({key: z.string(), type: z.literal("SETTING"), name: z.string().optional(), value: z.string()}); export const eventPipelineInfo = z.object({eventSchema: z.object({name: z.string(), version: z.number().int()}).optional(), usageMeters: z.array(z.object({id: z.string(), name: z.string(), version: z.number().int(), status: z.enum(["PROCESSED_FILTERED_OUT", "PROCESSED_UNITS_COMPUTED"]), units: z.number().optional()})).optional(), pricePlans: z.array(z.object({name: z.string(), id: z.string(), scheduleId: z.string(), cycleStart: z.string(), cycleEnd: z.string(), usageMeters: z.array(z.string()).optional()})).optional(), account: z.object({id: z.string(), name: z.string(), alias: z.string().optional()}).optional(), customer: z.object({id: z.string(), name: z.string()}).optional(), featureDetails: z.object({featureId: z.string(), mappedAttribute: z.string(), overageInCycle: z.number().optional(), overageInEvent: z.number().optional()}).optional(), enrichments: z.object({attributes: z.array(enrichedField).optional(), dimensions: z.array(enrichedField).optional(), dependencies: z.array(enrichmentDependency).optional()}).optional(), revenueDetails: z.array(z.object({usageMeterId: z.string(), revenueBaseCurrency: z.number(), revenueInvoiceCurrency: z.number()})).optional(), statusBeforeReverting: z.string().optional()}); export const eventWithStatusAndEventPipelineInfo = z.object({referenceId: z.string(), eventPayload: event, ingestionStatus: ingestionStatus, customerId: z.string().max(50).optional(), source: eventSource.optional(), createdAt: z.string(), eventPipelineInfo: eventPipelineInfo}); export const getEventResponse = z.object({events: z.array(eventWithStatusAndEventPipelineInfo)}); 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 using event id * * GET /events/{event_id} */ export const getApiEventsEventId = async (args: GetApiEventsEventIdArgs, options?: GetApiEventsEventIdOptions) => { const {baseUrl = "https://api.togai.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/events/${encodeURIComponent(String(args.event_id))}${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(getEventResponse, await res.json()) };