import {z} from 'zod' export type ComponentReferenceRequest = {component_id?: string | undefined, socket_names?: Array | undefined}; export type DescriptionInEventStreamUpdateRequest = string | null | string | string; export type NameInEventStreamUpdateRequest = string | string; export type Instant = string; export type EventStreamUpdateRequest = {component_ids?: Array | undefined, component_references?: Array | undefined, description?: DescriptionInEventStreamUpdateRequest | undefined, name?: NameInEventStreamUpdateRequest | undefined, stop_at?: Instant | undefined, tags?: Array | undefined}; export type UpdateApiV6EventStreamsEventStreamIdArgs = {eventStreamId: string, body: EventStreamUpdateRequest}; export type UpdateApiV6EventStreamsEventStreamIdOptions = {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 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 }; /** * Update an event stream * * PUT /v6/event-streams/{eventStreamId} * * Also accepts auth: QUERY, COOKIE */ export const updateApiV6EventStreamsEventStreamId = async (args: UpdateApiV6EventStreamsEventStreamIdArgs, options?: UpdateApiV6EventStreamsEventStreamIdOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v6/event-streams/${encodeURIComponent(String(args.eventStreamId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(eventStreamResponse, await res.json()) };