import {z} from 'zod' export type StreamField = {name: string, type: string}; export type CrossLinkField = {alias?: (string | null) | undefined, name: string}; export type CrossLink = {fields?: Array | undefined, name: string, url: string}; export type DistinctField = {added_ts: number, name: string}; export type TimeRange = {end: number, start: number}; export type StreamPartitionType = 'value' | {hash: number} | 'prefix'; export type StreamPartition = {disabled?: boolean | undefined, field: string, types?: StreamPartitionType | undefined}; export type PartitionTimeLevel = 'unset' | 'hourly' | 'daily'; export type PartitionTimeLevelChange = {level: PartitionTimeLevel, since: number}; export type StreamPartitionTimeLevel = {changes?: Array | undefined, dynamic?: (boolean | null) | undefined}; export type StorageType = 'normal' | 'compliance'; export type StreamSettings = {approx_partition?: boolean | undefined, bloom_filter_fields?: Array | undefined, cross_links?: Array | undefined, data_retention?: number | undefined, defined_schema_fields?: Array | undefined, distinct_value_fields?: Array | undefined, enable_distinct_fields?: boolean | undefined, enable_log_patterns_extraction?: boolean | undefined, extended_retention_days?: Array | undefined, flatten_level?: (number | null) | undefined, full_text_search_keys?: Array | undefined, index_all_values?: boolean | undefined, index_fields?: Array | undefined, index_fields_updated_at?: {[key: string]: number} | undefined, index_original_data?: boolean | undefined, index_updated_at?: number | undefined, is_llm_stream?: boolean | undefined, max_query_range?: number | undefined, partition_keys?: Array | undefined, partition_time_level?: StreamPartitionTimeLevel | undefined, storage_type?: StorageType | undefined, store_original_data?: boolean | undefined}; export type CreateApiApiOrgIdStreamsStreamNameArgs = {org_id: string, stream_name: string, type?: string | undefined, body: {fields: Array, settings: StreamSettings}}; export type CreateApiApiOrgIdStreamsStreamNameOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const httpResponse = z.object({code: z.number().int().gte(0), error_detail: z.string().nullable().optional(), hint: z.string().nullable().optional(), id: z.string().nullable().optional(), message: z.string(), name: z.string().nullable().optional(), suggestions: z.array(z.string()).nullable().optional(), trace_id: z.string().nullable().optional(), warnings: z.array(z.string()).nullable().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 }; /** * Create new stream * * POST /api/{org_id}/streams/{stream_name} */ export const createApiApiOrgIdStreamsStreamName = async (args: CreateApiApiOrgIdStreamsStreamNameArgs, options: CreateApiApiOrgIdStreamsStreamNameOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/streams/${encodeURIComponent(String(args.stream_name))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(httpResponse, await res.json()) };