UNPKG

inngest

Version:

Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.

1 lines 12.1 kB
{"version":3,"file":"types.cjs","names":["z"],"sources":["../../../src/components/realtime/types.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { z } from \"zod/v3\";\n\nexport namespace Realtime {\n export type ChannelInput = string | Realtime.ChannelInstance;\n\n export namespace Subscribe {\n export interface Token<\n TChannel extends Realtime.ChannelInput = Realtime.ChannelInput,\n TTopics extends string[] = string[],\n > {\n // key used to auth - could be undefined as then we can do a cold subscribe\n key?: string | undefined;\n channel: TChannel;\n topics: TTopics;\n apiBaseUrl?: string;\n }\n\n export interface ClientToken {\n key: string;\n apiBaseUrl?: string;\n }\n\n export type InferTopicSubscribeData<TTopic> =\n TTopic extends Realtime.TopicConfig\n ? Realtime.InferTopicData<TTopic>\n : // biome-ignore lint/suspicious/noExplicitAny: fallback for untyped topics\n any;\n\n export type StreamSubscription<\n TSubscribeToken extends Token = Token,\n TData extends Simplify<Token.InferMessage<TSubscribeToken>> = Simplify<\n Token.InferMessage<TSubscribeToken>\n >,\n > = ReadableStream<TData> & {\n /**\n * Get a new readable stream from the subscription that delivers JSON chunks.\n *\n * The stream starts when this function is called and will not contain any\n * messages that were sent before this function was called.\n */\n getJsonStream(): ReadableStream<TData>;\n\n /**\n * Get a new readable stream from the subscription that delivers\n * SSE-compatible chunks, which are compatible with the `EventSource` API\n * and generally used for streaming data from a server to the browser.\n *\n * The stream starts when this function is called and will not contain any\n * messages that were sent before this function was called.\n */\n getEncodedStream(): ReadableStream<Uint8Array>;\n\n /**\n * Close the underlying subscription connection.\n */\n close(reason?: string): void;\n\n /**\n * Alias for `close()` to match callback-style subscription semantics.\n */\n unsubscribe(reason?: string): void;\n };\n\n export type Callback<\n TSubscribeToken extends Subscribe.Token = Subscribe.Token,\n > = (message: Token.InferMessage<TSubscribeToken>) => MaybePromise<void>;\n\n export type CallbackSubscription = {\n close(reason?: string): void;\n unsubscribe(reason?: string): void;\n };\n\n export namespace Token {\n export type InferChannel<TToken extends Token> = TToken extends Token<\n infer IChannel,\n // biome-ignore lint/suspicious/noExplicitAny: fine in this generic\n any\n >\n ? IChannel\n : Realtime.ChannelInput;\n\n export type InferTopicData<\n TToken extends Token,\n TChannelTopics extends Record<string, unknown> = Channel.InferTopics<\n Token.InferChannel<TToken>\n >,\n // biome-ignore lint/suspicious/noExplicitAny: untargeted infer\n > = TToken extends Token<any, infer ITopics>\n ? { [K in ITopics[number]]: TChannelTopics[K] }\n : never;\n\n export type InferMessage<TToken extends Token> = Simplify<\n Realtime.Message<\n Channel.InferId<Token.InferChannel<TToken>>,\n Token.InferTopicData<TToken>\n >\n >;\n }\n }\n\n // We need to use a `Message` type so that we can appropriately type incoming\n // and outgoing messages with generics, but we also need to validate these at\n // runtime.\n //\n // Ideally in the future we use protobuf for this, but for now we use Zod.\n // This type is used to assert that the Zod schema matches the generic type.\n type _AssertMessageSchemaMatchesGeneric = Expect<\n IsEqual<z.output<typeof messageSchema>, Message.Raw>\n >;\n\n export const messageSchema = z\n .object({\n channel: z.string().optional(),\n topic: z.string().optional(),\n data: z.any(),\n run_id: z.string().optional(),\n fn_id: z.string().optional(),\n created_at: z\n .string()\n .optional()\n .transform((v) => (v ? new Date(v) : undefined)),\n\n env_id: z.string().optional(),\n stream_id: z.string().optional(),\n kind: z.enum([\n \"step\",\n \"run\",\n \"data\",\n \"ping\",\n \"pong\",\n \"closing\",\n \"event\",\n \"sub\",\n \"unsub\",\n \"datastream-start\",\n \"datastream-end\",\n \"chunk\",\n ]),\n })\n .transform(({ data, ...rest }) => {\n return {\n ...rest,\n data: data ?? undefined,\n };\n });\n\n // Subscribe (output) msg\n export type Message<\n TChannelId extends string = string,\n TTopics extends Record<string, unknown> = Record<string, unknown>,\n > =\n | {\n [K in keyof TTopics]:\n | {\n topic: K;\n channel: TChannelId;\n data: Subscribe.InferTopicSubscribeData<TTopics[K]>;\n runId?: string;\n fnId?: string;\n createdAt: Date;\n envId?: string;\n kind: \"data\";\n }\n | {\n topic: K;\n channel: TChannelId;\n data: Subscribe.InferTopicSubscribeData<TTopics[K]>;\n runId?: string;\n fnId?: string;\n kind: \"datastream-start\" | \"datastream-end\" | \"chunk\";\n streamId: string;\n stream: ReadableStream<\n Subscribe.InferTopicSubscribeData<TTopics[K]>\n >;\n };\n }[keyof TTopics]\n | {\n channel?: TChannelId;\n topic?: string;\n data: unknown;\n runId?: string;\n fnId?: string;\n createdAt: Date;\n envId?: string;\n kind: \"run\";\n };\n\n export namespace Message {\n // Publish (input) msg\n export type Input<\n TChannelId extends string = string,\n TTopicId extends string = string,\n // biome-ignore lint/suspicious/noExplicitAny: data can be anything\n TData = any,\n > = {\n channel: TChannelId;\n topic: TTopicId;\n data: TData;\n };\n\n export type Raw<\n TChannelId extends string = string,\n TTopics extends Record<string, unknown> = Record<string, unknown>,\n > = {\n [K in keyof TTopics]: {\n topic?: K;\n stream_id?: string;\n data: Subscribe.InferTopicSubscribeData<TTopics[K]>;\n channel?: TChannelId;\n run_id?: string;\n fn_id?: string;\n created_at?: Date;\n env_id?: string;\n kind:\n | \"step\" // step data\n | \"run\" // run results\n | \"data\" // misc stream data from `inngest.realtime.publish()`\n | \"datastream-start\"\n | \"datastream-end\"\n | \"ping\" // keepalive server -> client\n | \"pong\" // keepalive client -> server\n | \"closing\" // server is closing connection, client should reconnect\n | \"event\" // event sent to inngest\n | \"sub\"\n | \"unsub\"\n | \"chunk\";\n };\n }[keyof TTopics];\n }\n\n export namespace Channel {\n export type InferId<\n TChannel extends Realtime.ChannelInstance | Realtime.ChannelDef | string,\n > = TChannel extends Realtime.ChannelInstance<\n infer IId,\n Realtime.TopicsConfig\n >\n ? IId\n : TChannel extends Realtime.ChannelDef<\n infer TNameFn,\n Realtime.TopicsConfig\n >\n ? ReturnType<TNameFn>\n : TChannel extends string\n ? TChannel\n : string;\n\n export type InferTopics<\n TChannel extends Realtime.ChannelInstance | Realtime.ChannelDef | string,\n > = TChannel extends Realtime.ChannelDef<infer _NameFn, infer ITopics>\n ? ITopics\n : TChannel extends Realtime.ChannelInstance<infer _Name, infer ITopics>\n ? ITopics\n : TChannel extends string\n ? Record<string, unknown>\n : Record<string, unknown>;\n }\n\n //\n // A TopicConfig is one entry in a channel's `topics` record.\n // Always uses `{ schema }` — for type-only topics, use staticSchema<T>()\n // which returns a passthrough Standard Schema with zero validation cost.\n export type TopicConfig = { schema: StandardSchemaV1 };\n\n export type TopicsConfig = Record<string, TopicConfig>;\n\n export type InferTopicData<T extends TopicConfig> = T extends {\n schema: infer S extends StandardSchemaV1;\n }\n ? StandardSchemaV1.InferInput<S>\n : unknown;\n\n //\n // A TopicRef is a lightweight value carrying the resolved channel name,\n // topic name, topic config, and payload type. Created by dot-accessing\n // a topic on a channel instance (e.g. `chat.status`).\n export interface TopicRef<_TData = unknown> {\n channel: string;\n topic: string;\n config: TopicConfig;\n }\n\n //\n // Maps a TopicsConfig into dot-access topic accessors that return TopicRefs.\n export type TopicAccessors<\n _TName extends string,\n TTopics extends TopicsConfig,\n > = {\n [K in string & keyof TTopics]: TopicRef<InferTopicData<TTopics[K]>>;\n };\n\n export type ChannelInstance<\n TName extends string = string,\n TTopics extends TopicsConfig = {},\n > = {\n name: TName;\n topics: TTopics;\n } & TopicAccessors<TName, TTopics>;\n\n export type ChannelDef<\n // biome-ignore lint/suspicious/noExplicitAny: broad fn definition\n TNameFn extends (...args: any[]) => string = (...args: any[]) => string,\n TTopics extends TopicsConfig = TopicsConfig,\n > = ((\n ...args: Parameters<TNameFn>\n ) => ChannelInstance<ReturnType<TNameFn>, TTopics>) & {\n topics: TTopics;\n $params: Parameters<TNameFn>[0];\n };\n\n //\n // publish(topicRef, data) — two-arg form using topic accessors\n export type TypedPublishFn = <TData>(\n topicRef: TopicRef<TData>,\n data: TData,\n ) => Promise<void>;\n}\n\n/**\n * Expects that a value resolves to `true`, useful for asserting type checks.\n */\nexport type Expect<T extends true> = T;\n\n/**\nReturns a boolean for whether the two given types are equal.\n\n{@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650}\n{@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796}\n\nUse-cases:\n- If you want to make a conditional branch based on the result of a comparison of two types.\n\n@example\n```\nimport type {IsEqual} from 'type-fest';\n\n// This type returns a boolean for whether the given array includes the given item.\n// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.\ntype Includes<Value extends readonly any[], Item> =\n\tValue extends readonly [Value[0], ...infer rest]\n\t\t? IsEqual<Value[0], Item> extends true\n\t\t\t? true\n\t\t\t: Includes<rest, Item>\n\t\t: false;\n```\n*/\nexport type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <\n G,\n>() => G extends B ? 1 : 2\n ? true\n : false;\n\n/**\n * Returns the given generic as either itself or a promise of itself.\n */\nexport type MaybePromise<T> = T | Promise<T>;\n\nexport type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};\n"],"mappings":";;;;;;2BA+G+BA,SAC1B,OAAO;EACN,SAASA,SAAE,QAAQ,CAAC,UAAU;EAC9B,OAAOA,SAAE,QAAQ,CAAC,UAAU;EAC5B,MAAMA,SAAE,KAAK;EACb,QAAQA,SAAE,QAAQ,CAAC,UAAU;EAC7B,OAAOA,SAAE,QAAQ,CAAC,UAAU;EAC5B,YAAYA,SACT,QAAQ,CACR,UAAU,CACV,WAAW,MAAO,IAAI,IAAI,KAAK,EAAE,GAAG,OAAW;EAElD,QAAQA,SAAE,QAAQ,CAAC,UAAU;EAC7B,WAAWA,SAAE,QAAQ,CAAC,UAAU;EAChC,MAAMA,SAAE,KAAK;GACX;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CAAC;EACH,CAAC,CACD,WAAW,EAAE,MAAM,GAAG,WAAW;AAChC,SAAO;GACL,GAAG;GACH,MAAM,QAAQ;GACf;GACD"}