@@ -3,10 +3,27 @@ import { ManifestEndpoint } from '../../runtime/manifest';
33import { CloudEvent , CloudFunction } from '../core' ;
44import * as options from '../options' ;
55
6+ /**
7+ * A PubSub Topic is:
8+ * <ul>
9+ * <li>A resource that you can publish messages to and then consume those messages via subscriptions.
10+ * <li>An isolated data stream for Pub/Sub messages.
11+ * <li>Messages are published to a topic.
12+ * <li>Messages are listened to via a subscription.
13+ * <li>Each subscription listens to the messages published to exactly one topic.
14+ */
15+ export type PubSubTopic = string ;
16+
17+ /**
18+ * Resource that listens to the messages published by exactly one topic.
19+ */
20+ export type PubSubSubscription = string ;
21+
622/**
723 * Interface representing a Google Cloud Pub/Sub message.
824 *
9- * @param data Payload of a Pub/Sub message.
25+ * @param data - Payload of a Pub/Sub message.
26+ * @typeParam T - Type representing `Message.data`'s JSON format
1027 */
1128export class Message < T > {
1229 /**
@@ -68,7 +85,7 @@ export class Message<T> {
6885 /**
6986 * Returns a JSON-serializable representation of this object.
7087 *
71- * @return A JSON-serializable representation of this object.
88+ * @returns A JSON-serializable representation of this object.
7289 */
7390 toJSON ( ) : any {
7491 const json : Record < string , any > = {
@@ -86,29 +103,51 @@ export class Message<T> {
86103 }
87104}
88105
89- /** The interface published in a Pub/Sub publish subscription. */
106+ /**
107+ * The interface published in a Pub/Sub publish subscription.
108+ * @typeParam T - Type representing `Message.data`'s JSON format
109+ */
90110export interface MessagePublishedData < T = any > {
111+ /** Google Cloud Pub/Sub message. */
91112 readonly message : Message < T > ;
92- readonly subscription : string ;
113+ /** A subscription resource. */
114+ readonly subscription : PubSubSubscription ;
93115}
94116
95117/** PubSubOptions extend EventHandlerOptions but must include a topic. */
96118export interface PubSubOptions extends options . EventHandlerOptions {
97- topic : string ;
119+ /** The Pub/Sub topic to watch for message events */
120+ topic : PubSubTopic ;
98121}
99122
100- /** Handle a message being published to a Pub/Sub topic. */
123+ /**
124+ * Handle a message being published to a Pub/Sub topic.
125+ * @param topic - The Pub/Sub topic to watch for message events.
126+ * @param handler - runs every time a Cloud Pub/Sub message is published
127+ * @typeParam T - Type representing `Message.data`'s JSON format
128+ */
101129export function onMessagePublished < T = any > (
102- topic : string ,
130+ topic : PubSubTopic ,
103131 handler : ( event : CloudEvent < MessagePublishedData < T > > ) => any | Promise < any >
104132) : CloudFunction < CloudEvent < MessagePublishedData < T > > > ;
105133
106- /** Handle a message being published to a Pub/Sub topic. */
134+ /**
135+ * Handle a message being published to a Pub/Sub topic.
136+ * @param options - Option containing information (topic) for event
137+ * @param handler - runs every time a Cloud Pub/Sub message is published
138+ * @typeParam T - Type representing `Message.data`'s JSON format
139+ */
107140export function onMessagePublished < T = any > (
108141 options : PubSubOptions ,
109142 handler : ( event : CloudEvent < MessagePublishedData < T > > ) => any | Promise < any >
110143) : CloudFunction < CloudEvent < MessagePublishedData < T > > > ;
111144
145+ /**
146+ * Handle a message being published to a Pub/Sub topic.
147+ * @param topicOrOptions - A string representing the PubSub topic or an option (which contains the topic)
148+ * @param handler - runs every time a Cloud Pub/Sub message is published
149+ * @typeParam T - Type representing `Message.data`'s JSON format
150+ */
112151export function onMessagePublished < T = any > (
113152 topicOrOptions : string | PubSubOptions ,
114153 handler : ( event : CloudEvent < MessagePublishedData < T > > ) => any | Promise < any >
@@ -127,7 +166,7 @@ export function onMessagePublished<T = any>(
127166 const func = ( raw : CloudEvent < unknown > ) => {
128167 const messagePublishedData = raw . data as {
129168 message : unknown ;
130- subscription : string ;
169+ subscription : PubSubSubscription ;
131170 } ;
132171 messagePublishedData . message = new Message ( messagePublishedData . message ) ;
133172 return handler ( raw as CloudEvent < MessagePublishedData < T > > ) ;
0 commit comments