@codecompose/typed-pubsub
Version:
A type-safe pub/sub abstraction for Google Cloud and Firebase
81 lines (80 loc) • 3.66 kB
text/typescript
import * as firebase_functions_v2_pubsub0 from "firebase-functions/v2/pubsub";
import { PubSubOptions, onMessagePublished as onMessagePublished$1 } from "firebase-functions/v2/pubsub";
import { PubSub } from "@google-cloud/pubsub";
import { z } from "zod";
//#region src/types.d.ts
/** Type definition for a pubsub handler function returned by createHandler */
type PubsubHandlerFunction = ReturnType<typeof firebase_functions_v2_pubsub0.onMessagePublished>;
/** Type representing valid pubsub topic names */
type PubsubTopic = string;
/** Record of schema types for each topic */
type SchemaRecord<TopicName extends PubsubTopic> = Record<TopicName, z.ZodType>;
/**
* Type to extract the payload type for a given PubSub topic using the schema's
* type definition
*/
type PubsubTopicPayload<Schemas extends SchemaRecord<string>, T extends keyof Schemas & string> = z.infer<Schemas[T]>;
/** Event marking functions used for tracking processed events */
type EventMarkingFunctions = {
/** Checks if an event has already been processed */
isEventProcessed: (eventId: string) => Promise<boolean>;
/** Marks an event as processed */
markEventAsProcessed: (eventId: string) => Promise<void>;
};
/** Options for configuring the typed Pubsub client */
type TypedPubsubOptions = {
/** Optional event marking functions */
eventMarkingFunctions?: EventMarkingFunctions;
/** Default options for all handlers */
defaultHandlerOptions?: HandlerOptions;
};
/** Helper type for tuple creation to track recursion depth */
type LengthTuple<N extends number, T extends unknown[] = []> = T["length"] extends N ? T : LengthTuple<N, [...T, unknown]>;
/** Type definition for a typed Pubsub client */
type TypedPubsubClient<Schemas extends SchemaRecord<string>> = {
/** Creates a type-safe publisher function for the specified topic */
createPublisher: <T extends keyof Schemas & string>(topic: T) => (data: PubsubTopicPayload<Schemas, T>) => Promise<void>;
/** Creates a type-safe handler function for processing messages from a topic */
createHandler: <T extends keyof Schemas & string>(options: {
topic: T;
handler: (payload: PubsubTopicPayload<Schemas, T>) => Promise<void>;
options?: HandlerOptions;
}) => PubsubHandlerFunction;
};
/** Options for configuring a Pubsub message handler */
type HandlerOptions = Omit<PubSubOptions, "region" | "topic"> & {
/** Whether to enable retries (true) or disable retries (false) */
retry?: boolean;
/** Maximum age of an event in minutes before it will be dropped */
retryMaxAgeMinutes?: number;
/** Whether to mark events as processed to prevent duplicates */
markEvent?: boolean;
};
//#endregion
//#region src/factory.d.ts
/**
* Creates a type-safe Pubsub client for handling messages with schema
* validation
*
* @param pubsubClient - Google Cloud Pubsub client instance
* @param schemas - Zod schemas for validating messages for each topic
* @param region - GCP region for the Pubsub functions
* @param options - Global defaults for all handlers
* @returns Type-safe Pubsub client with publisher and handler factories
*/
declare function createTypedPubsub<Schemas extends SchemaRecord<string>>({
client,
schemas,
region,
options,
onMessagePublished
}: {
client: PubSub;
schemas: Schemas;
region: string;
options?: TypedPubsubOptions;
onMessagePublished?: typeof onMessagePublished$1;
}): TypedPubsubClient<Schemas>;
//#endregion
export { EventMarkingFunctions, HandlerOptions, LengthTuple, PubsubHandlerFunction, PubsubTopic, PubsubTopicPayload, SchemaRecord, TypedPubsubClient, TypedPubsubOptions, createTypedPubsub };
//# sourceMappingURL=index.d.mts.map