import {z} from 'zod' export type GetApiV6EventStreamsArgs = {include_archived?: boolean | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV6EventStreamsOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const instant = z.string(); export const descriptionInEventStreamResponse = z.union([z.string(), z.string(), 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 nameInEventStreamResponse = z.union([z.string(), z.string()]); export const eventStreamResponse = z.object({component_ids: z.array(z.string()), created_date: instant, description: descriptionInEventStreamResponse, filters: z.array(eventStreamFilterResponse), id: z.string(), name: nameInEventStreamResponse, stop_at: instant, tags: z.array(z.string()), updated_date: instant}); export const getApiV6EventStreamsResponse = z.array(eventStreamResponse); 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 event streams * * GET /v6/event-streams * * Also accepts auth: QUERY, COOKIE */ export const getApiV6EventStreams = async (args: GetApiV6EventStreamsArgs, options?: GetApiV6EventStreamsOptions) => { 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 (args.include_archived != null) query.append("include_archived", String(args.include_archived)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/event-streams${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(getApiV6EventStreamsResponse, await res.json()) };