import {z} from 'zod' export type GetApiV6EventStreamsEventStreamIdBuiltArgs = {eventStreamId: string}; export type GetApiV6EventStreamsEventStreamIdBuiltOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const instant = z.string(); export const eventStreamFilterResponse = z.discriminatedUnion("type", [z.object({id: z.string(), type: z.literal("APPLICATION_TYPE"), app_types: z.array(z.string())}), z.object({id: z.string(), type: z.literal("EVENT_NAME"), event_names: z.array(z.string())}), z.object({id: z.string(), type: z.literal("EVENT_TYPE"), event_types: z.array(z.enum(["ADD_SHAREABLE", "ASSET", "AUDIENCE_MEMBERSHIP_CREATED", "AUDIENCE_MEMBERSHIP_REMOVED", "AUDIENCE_MEMBERSHIP_UPDATED", "DATA_INTELLIGENCE", "DISPLACED", "DISPLACED_BY", "FORWARDED", "IDENTIFIED", "INCENTIVIZED", "INPUT", "INTERNAL", "LEGACY_CONSUMER_EVENT", "MESSAGE", "MESSAGE_NOT_DELIVERED", "REDEEMED", "REFERRED", "REFERRED_BY", "REWARD", "SEND_REWARD", "SHARE", "SHAREABLE", "STEP"]))}), z.object({id: z.string(), type: z.literal("PERSON_ID"), person_ids: z.array(z.string())}), z.object({id: z.string(), type: z.literal("SANDBOX"), containers: z.array(z.string()), sandboxes: z.array(z.string())})]); export const builtEventStreamResponse = z.object({component_ids: z.array(z.string()), created_date: instant, description: z.string(), filters: z.array(eventStreamFilterResponse), id: z.string(), name: z.string(), stop_at: instant, tags: z.array(z.string()), updated_date: instant}); 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 a built event stream * * GET /v6/event-streams/{eventStreamId}/built * * Also accepts auth: QUERY, COOKIE */ export const getApiV6EventStreamsEventStreamIdBuilt = async (args: GetApiV6EventStreamsEventStreamIdBuiltArgs, options?: GetApiV6EventStreamsEventStreamIdBuiltOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/event-streams/${encodeURIComponent(String(args.eventStreamId))}/built${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(builtEventStreamResponse, await res.json()) };