UNPKG

9.03 kBTypeScriptView Raw
1/*!
2 * Copyright 2017 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/// <reference types="node" />
17import { CallOptions } from 'google-gax';
18import { google } from '../protos/protos';
19import { IAM } from './iam';
20import { Attributes, PublishCallback, Publisher, PublishOptions, PubsubMessage } from './publisher';
21import { EmptyCallback, EmptyResponse, ExistsCallback, ExistsResponse, ObjectStream, PagedResponse, PageOptions, PubSub, RequestCallback, ResourceCallback } from './pubsub';
22import { CreateSubscriptionCallback, CreateSubscriptionOptions, CreateSubscriptionResponse, Subscription, SubscriptionOptions } from './subscription';
23export declare type TopicMetadata = google.pubsub.v1.ITopic;
24declare type TopicCallback = ResourceCallback<Topic, TopicMetadata>;
25declare type TopicResponse = [Topic, TopicMetadata];
26export declare type CreateTopicCallback = TopicCallback;
27export declare type CreateTopicResponse = TopicResponse;
28export declare type GetTopicCallback = TopicCallback;
29export declare type GetTopicResponse = TopicResponse;
30export declare type GetTopicOptions = CallOptions & {
31 autoCreate?: boolean;
32};
33declare type MetadataCallback = RequestCallback<TopicMetadata>;
34declare type MetadataResponse = [TopicMetadata];
35export declare type GetTopicMetadataCallback = MetadataCallback;
36export declare type GetTopicMetadataResponse = MetadataResponse;
37export declare type SetTopicMetadataCallback = MetadataCallback;
38export declare type SetTopicMetadataResponse = MetadataResponse;
39export declare type GetTopicSubscriptionsCallback = RequestCallback<Subscription, google.pubsub.v1.IListTopicSubscriptionsResponse>;
40export declare type GetTopicSubscriptionsResponse = PagedResponse<Subscription, google.pubsub.v1.IListTopicSubscriptionsResponse>;
41export declare type MessageOptions = PubsubMessage & {
42 json?: any;
43};
44/**
45 * A Topic object allows you to interact with a Cloud Pub/Sub topic.
46 *
47 * @class
48 * @param {PubSub} pubsub PubSub object.
49 * @param {string} name Name of the topic.
50 * @param {PublishOptions} [options] Publisher configuration object.
51 *
52 * @example
53 * const {PubSub} = require('@google-cloud/pubsub');
54 * const pubsub = new PubSub();
55 *
56 * const topic = pubsub.topic('my-topic');
57 *
58 * @example <caption>To enable message ordering, set `enableMessageOrdering` to true. Please note that this does not persist to an actual topic.</caption>
59 * const topic = pubsub.topic('ordered-topic', {enableMessageOrdering: true});
60 */
61export declare class Topic {
62 name: string;
63 parent: PubSub;
64 pubsub: PubSub;
65 request: typeof PubSub.prototype.request;
66 iam: IAM;
67 metadata?: TopicMetadata;
68 publisher: Publisher;
69 getSubscriptionsStream: () => ObjectStream<Subscription>;
70 constructor(pubsub: PubSub, name: string, options?: PublishOptions);
71 flush(): Promise<void>;
72 flush(callback: EmptyCallback): void;
73 create(gaxOpts?: CallOptions): Promise<CreateTopicResponse>;
74 create(callback: CreateTopicCallback): void;
75 create(gaxOpts: CallOptions, callback: CreateTopicCallback): void;
76 createSubscription(name: string, callback: CreateSubscriptionCallback): void;
77 createSubscription(name: string, options?: CreateSubscriptionOptions): Promise<CreateSubscriptionResponse>;
78 createSubscription(name: string, options: CreateSubscriptionOptions, callback: CreateSubscriptionCallback): void;
79 delete(callback: EmptyCallback): void;
80 delete(gaxOpts?: CallOptions): Promise<EmptyResponse>;
81 delete(gaxOpts: CallOptions, callback: EmptyCallback): void;
82 exists(): Promise<ExistsResponse>;
83 exists(callback: ExistsCallback): void;
84 get(callback: GetTopicCallback): void;
85 get(gaxOpts?: GetTopicOptions): Promise<GetTopicResponse>;
86 get(gaxOpts: GetTopicOptions, callback: GetTopicCallback): void;
87 getMetadata(callback: GetTopicMetadataCallback): void;
88 getMetadata(gaxOpts: CallOptions, callback: GetTopicMetadataCallback): void;
89 getMetadata(gaxOpts?: CallOptions): Promise<GetTopicMetadataResponse>;
90 getSubscriptions(callback: GetTopicSubscriptionsCallback): void;
91 getSubscriptions(options: PageOptions, callback: GetTopicSubscriptionsCallback): void;
92 getSubscriptions(options?: PageOptions): Promise<GetTopicSubscriptionsResponse>;
93 publish(data: Buffer, attributes?: Attributes): Promise<string>;
94 publish(data: Buffer, callback: PublishCallback): void;
95 publish(data: Buffer, attributes: Attributes, callback: PublishCallback): void;
96 publishJSON(json: object, attributes?: Attributes): Promise<string>;
97 publishJSON(json: object, callback: PublishCallback): void;
98 publishJSON(json: object, attributes: Attributes, callback: PublishCallback): void;
99 publishMessage(message: MessageOptions): Promise<[string]>;
100 publishMessage(message: MessageOptions, callback: PublishCallback): void;
101 /**
102 * In the event that the client fails to publish an ordered message, all
103 * subsequent publish calls using the same ordering key will fail. Calling
104 * this method will disregard the publish failure, allowing the supplied
105 * ordering key to be used again in the future.
106 *
107 * @param {string} orderingKey The ordering key in question.
108 *
109 * @example
110 * const {PubSub} = require('@google-cloud/pubsub');
111 * const pubsub = new PubSub();
112 * const topic = pubsub.topic('my-topic', {messageOrdering: true});
113 *
114 * const orderingKey = 'foo';
115 * const data = Buffer.from('Hello, order!');
116 *
117 * topic.publishMessage({data, orderingKey}, err => {
118 * if (err) {
119 * topic.resumePublishing(orderingKey);
120 * }
121 * });
122 */
123 resumePublishing(orderingKey: string): void;
124 setMetadata(options: TopicMetadata, gaxOpts?: CallOptions): Promise<SetTopicMetadataResponse>;
125 setMetadata(options: TopicMetadata, callback: SetTopicMetadataCallback): void;
126 setMetadata(options: TopicMetadata, gaxOpts: CallOptions, callback: SetTopicMetadataCallback): void;
127 /**
128 * Set the publisher options.
129 *
130 * @param {PublishOptions} options The publisher options.
131 *
132 * @example
133 * const {PubSub} = require('@google-cloud/pubsub');
134 * const pubsub = new PubSub();
135 *
136 * const topic = pubsub.topic('my-topic');
137 *
138 * topic.setPublishOptions({
139 * batching: {
140 * maxMilliseconds: 10
141 * }
142 * });
143 */
144 setPublishOptions(options: PublishOptions): void;
145 /**
146 * Get the default publisher options. These may be modified and passed
147 * back into {@link Topic#setPublishOptions}.
148 *
149 * @example
150 * const {PubSub} = require('@google-cloud/pubsub');
151 * const pubsub = new PubSub();
152 *
153 * const topic = pubsub.topic('my-topic');
154 *
155 * const defaults = topic.getPublishOptionDefaults();
156 * defaults.batching.maxMilliseconds = 10;
157 * topic.setPublishOptions(defaults);
158 */
159 getPublishOptionDefaults(): PublishOptions;
160 /**
161 * Create a Subscription object. This command by itself will not run any API
162 * requests. You will receive a {module:pubsub/subscription} object,
163 * which will allow you to interact with a subscription.
164 *
165 * @throws {Error} If subscription name is omitted.
166 *
167 * @param {string} name Name of the subscription.
168 * @param {SubscriberOptions} [options] Configuration object.
169 * @return {Subscription}
170 *
171 * @example
172 * const {PubSub} = require('@google-cloud/pubsub');
173 * const pubsub = new PubSub();
174 *
175 * const topic = pubsub.topic('my-topic');
176 * const subscription = topic.subscription('my-subscription');
177 *
178 * // Register a listener for `message` events.
179 * subscription.on('message', (message) => {
180 * // Called every time a message is received.
181 * // message.id = ID of the message.
182 * // message.ackId = ID used to acknowledge the message receival.
183 * // message.data = Contents of the message.
184 * // message.attributes = Attributes of the message.
185 * // message.publishTime = Timestamp when Pub/Sub received the message.
186 * });
187 */
188 subscription(name: string, options?: SubscriptionOptions): Subscription;
189 /**
190 * Format the name of a topic. A Topic's full name is in the format of
191 * 'projects/{projectId}/topics/{topicName}'.
192 *
193 * @private
194 *
195 * @return {string}
196 */
197 static formatName_(projectId: string, name: string): string;
198}
199export { PublishOptions };