UNPKG

14.3 kBTypeScriptView Raw
1/*!
2 * Copyright 2014 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 { GoogleAuth } from 'google-auth-library';
18import * as gax from 'google-gax';
19import { Snapshot } from './snapshot';
20import { Subscription, SubscriptionOptions, CreateSubscriptionOptions, CreateSubscriptionCallback, CreateSubscriptionResponse, DetachSubscriptionCallback, DetachSubscriptionResponse } from './subscription';
21import { Topic, GetTopicSubscriptionsCallback, GetTopicSubscriptionsResponse, CreateTopicCallback, CreateTopicResponse } from './topic';
22import { PublishOptions } from './publisher';
23import { CallOptions } from 'google-gax';
24import { Transform } from 'stream';
25import { google } from '../protos/protos';
26export declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
27export interface ClientConfig extends gax.GrpcClientOptions {
28 apiEndpoint?: string;
29 servicePath?: string;
30 port?: string | number;
31 sslCreds?: gax.grpc.ChannelCredentials;
32}
33export interface PageOptions {
34 gaxOpts?: CallOptions;
35 pageSize?: number;
36 pageToken?: string;
37 autoPaginate?: boolean;
38}
39export declare type GetSnapshotsCallback = RequestCallback<Snapshot, google.pubsub.v1.IListSnapshotsResponse>;
40export declare type GetSnapshotsResponse = PagedResponse<Snapshot, google.pubsub.v1.IListSnapshotsResponse>;
41export declare type GetSubscriptionsOptions = PageOptions & {
42 topic?: string | Topic;
43};
44declare type GetAllSubscriptionsCallback = RequestCallback<Subscription, google.pubsub.v1.IListSubscriptionsResponse>;
45declare type GetAllSubscriptionsResponse = PagedResponse<Subscription, google.pubsub.v1.IListSubscriptionsResponse>;
46export declare type GetSubscriptionsCallback = GetAllSubscriptionsCallback | GetTopicSubscriptionsCallback;
47export declare type GetSubscriptionsResponse = GetAllSubscriptionsResponse | GetTopicSubscriptionsResponse;
48export declare type GetTopicsCallback = RequestCallback<Topic, google.pubsub.v1.IListTopicsResponse>;
49export declare type GetTopicsResponse = PagedResponse<Topic, google.pubsub.v1.IListTopicsResponse>;
50export declare type EmptyCallback = RequestCallback<google.protobuf.IEmpty>;
51export declare type EmptyResponse = [google.protobuf.IEmpty];
52export declare type ExistsCallback = RequestCallback<boolean>;
53export declare type ExistsResponse = [boolean];
54export declare type DetachedCallback = RequestCallback<boolean>;
55export declare type DetachedResponse = [boolean];
56export interface GetClientConfig {
57 client: 'PublisherClient' | 'SubscriberClient';
58}
59export interface RequestConfig extends GetClientConfig {
60 method: string;
61 reqOpts?: object;
62 gaxOpts?: CallOptions;
63}
64export interface ResourceCallback<Resource, Response> {
65 (err: gax.grpc.ServiceError | null, resource?: Resource | null, response?: Response | null): void;
66}
67export declare type RequestCallback<T, R = void> = R extends void ? NormalCallback<T> : PagedCallback<T, R>;
68export interface NormalCallback<TResponse> {
69 (err: gax.grpc.ServiceError | null, res?: TResponse | null): void;
70}
71export interface PagedCallback<Item, Response> {
72 (err: gax.grpc.ServiceError | null, results?: Item[] | null, nextQuery?: {} | null, response?: Response | null): void;
73}
74export declare type PagedResponse<Item, Response> = [Item[]] | [Item[], {} | null, Response];
75export declare type ObjectStream<O> = {
76 addListener(event: 'data', listener: (data: O) => void): ObjectStream<O>;
77 emit(event: 'data', data: O): boolean;
78 on(event: 'data', listener: (data: O) => void): ObjectStream<O>;
79 once(event: 'data', listener: (data: O) => void): ObjectStream<O>;
80 prependListener(event: 'data', listener: (data: O) => void): ObjectStream<O>;
81 prependOnceListener(event: 'data', listener: (data: O) => void): ObjectStream<O>;
82} & Transform;
83interface GetClientCallback {
84 (err: Error | null, gaxClient?: gax.ClientStub): void;
85}
86/**
87 * @typedef {object} ClientConfig
88 * @property {string} [projectId] The project ID from the Google Developer's
89 * Console, e.g. 'grape-spaceship-123'. We will also check the environment
90 * variable `GCLOUD_PROJECT` for your project ID. If your app is running in
91 * an environment which supports {@link
92 * https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
93 * Application Default Credentials}, your project ID will be detected
94 * automatically.
95 * @property {string} [keyFilename] Full path to the a .json, .pem, or .p12 key
96 * downloaded from the Google Developers Console. If you provide a path to a
97 * JSON file, the `projectId` option above is not necessary. NOTE: .pem and
98 * .p12 require you to specify the `email` option as well.
99 * @property {string} [apiEndpoint] The `apiEndpoint` from options will set the
100 * host. If not set, the `PUBSUB_EMULATOR_HOST` environment variable from the
101 * gcloud SDK is honored. We also check the `CLOUD_API_ENDPOINT_OVERRIDES_PUBSUB`
102 * environment variable used by `gcloud alpha pubsub`. Otherwise the actual API
103 * endpoint will be used. Note that if the URL doesn't end in '.googleapis.com',
104 * we will assume that it's an emulator and disable strict SSL checks.
105 * @property {string} [email] Account email address. Required when using a .pem
106 * or .p12 keyFilename.
107 * @property {object} [credentials] Credentials object.
108 * @property {string} [credentials.client_email]
109 * @property {string} [credentials.private_key]
110 * @property {boolean} [autoRetry=true] Automatically retry requests if the
111 * response is related to rate limits or certain intermittent server errors.
112 * We will exponentially backoff subsequent requests by default.
113 * @property {Constructor} [promise] Custom promise module to use instead of
114 * native Promises.
115 */
116/**
117 * [Cloud Pub/Sub](https://developers.google.com/pubsub/overview) is a
118 * reliable, many-to-many, asynchronous messaging service from Cloud
119 * Platform.
120 *
121 * @class
122 *
123 * @see [Cloud Pub/Sub overview]{@link https://developers.google.com/pubsub/overview}
124 *
125 * @param {ClientConfig} [options] Configuration options.
126 *
127 * @example <caption>Import the client library</caption>
128 * const {PubSub} = require('@google-cloud/pubsub');
129 *
130 * @example <caption>Create a client that uses <a href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application Default Credentials (ADC)</a>:</caption>
131 * const pubsub = new PubSub();
132 *
133 * @example <caption>Create a client with <a href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit credentials</a>:</caption>
134 * const pubsub = new PubSub({
135 * projectId: 'your-project-id',
136 * keyFilename: '/path/to/keyfile.json'
137 * });
138 *
139 * @example <caption>include:samples/quickstart.js</caption>
140 * region_tag:pubsub_quickstart_create_topic
141 * Full quickstart example:
142 */
143export declare class PubSub {
144 options: ClientConfig;
145 isEmulator: boolean;
146 api: {
147 [key: string]: gax.ClientStub;
148 };
149 auth: GoogleAuth;
150 projectId: string;
151 Promise?: PromiseConstructor;
152 getSubscriptionsStream: () => ObjectStream<Subscription>;
153 getSnapshotsStream: () => ObjectStream<Snapshot>;
154 getTopicsStream: () => ObjectStream<Topic>;
155 isOpen: boolean;
156 constructor(options?: ClientConfig);
157 close(): Promise<void>;
158 close(callback: EmptyCallback): void;
159 createSubscription(topic: Topic | string, name: string, options?: CreateSubscriptionOptions): Promise<CreateSubscriptionResponse>;
160 createSubscription(topic: Topic | string, name: string, callback: CreateSubscriptionCallback): void;
161 createSubscription(topic: Topic | string, name: string, options: CreateSubscriptionOptions, callback: CreateSubscriptionCallback): void;
162 createTopic(name: string, gaxOpts?: CallOptions): Promise<CreateTopicResponse>;
163 createTopic(name: string, callback: CreateTopicCallback): void;
164 createTopic(name: string, gaxOpts: CallOptions, callback: CreateTopicCallback): void;
165 detachSubscription(name: string, gaxOpts?: CallOptions): Promise<DetachSubscriptionResponse>;
166 detachSubscription(name: string, callback: DetachSubscriptionCallback): void;
167 detachSubscription(name: string, gaxOpts: CallOptions, callback: DetachSubscriptionCallback): void;
168 /**
169 * Determine the appropriate endpoint to use for API requests, first trying
170 * the `apiEndpoint` parameter. If that isn't set, we try the Pub/Sub emulator
171 * environment variable (PUBSUB_EMULATOR_HOST). If that is also null, we try
172 * the standard `gcloud alpha pubsub` environment variable
173 * (CLOUDSDK_API_ENDPOINT_OVERRIDES_PUBSUB). Otherwise the default production
174 * API is used.
175 *
176 * Note that if the URL doesn't end in '.googleapis.com', we will assume that
177 * it's an emulator and disable strict SSL checks.
178 *
179 * @private
180 */
181 determineBaseUrl_(): void;
182 getSnapshots(options?: PageOptions): Promise<GetSnapshotsResponse>;
183 getSnapshots(callback: GetSnapshotsCallback): void;
184 getSnapshots(options: PageOptions, callback: GetSnapshotsCallback): void;
185 getSubscriptions(options?: GetSubscriptionsOptions): Promise<GetSubscriptionsResponse>;
186 getSubscriptions(callback: GetSubscriptionsCallback): void;
187 getSubscriptions(options: GetSubscriptionsOptions, callback: GetSubscriptionsCallback): void;
188 getTopics(options?: PageOptions): Promise<GetTopicsResponse>;
189 getTopics(callback: GetTopicsCallback): void;
190 getTopics(options: PageOptions, callback: GetTopicsCallback): void;
191 /**
192 * Callback function to PubSub.getClient_().
193 * @private
194 * @callback GetClientCallback
195 * @param err - Error, if any.
196 * @param gaxClient - The gax client specified in RequestConfig.client.
197 * Typed any since it's importing Javascript source.
198 */
199 /**
200 * Get the PubSub client object.
201 *
202 * @private
203 *
204 * @param {object} config Configuration object.
205 * @param {object} config.gaxOpts GAX options.
206 * @param {function} config.method The gax method to call.
207 * @param {object} config.reqOpts Request options.
208 * @param {function} [callback] The callback function.
209 */
210 getClient_(config: GetClientConfig, callback: GetClientCallback): void;
211 /**
212 * Get the PubSub client object.
213 *
214 * @private
215 *
216 * @param {object} config Configuration object.
217 * @param {object} config.gaxOpts GAX options.
218 * @param {function} config.method The gax method to call.
219 * @param {object} config.reqOpts Request options.
220 * @returns {Promise}
221 */
222 getClientAsync_(config: GetClientConfig): Promise<gax.ClientStub>;
223 /**
224 * Close all open client objects.
225 *
226 * @private
227 *
228 * @returns {Promise}
229 */
230 closeAllClients_(): Promise<void>;
231 /**
232 * Funnel all API requests through this method, to be sure we have a project
233 * ID.
234 *
235 * @private
236 *
237 * @param {object} config Configuration object.
238 * @param {object} config.gaxOpts GAX options.
239 * @param {function} config.method The gax method to call.
240 * @param {object} config.reqOpts Request options.
241 * @param {function} [callback] The callback function.
242 */
243 request<T, R = void>(config: RequestConfig, callback: RequestCallback<T, R>): void;
244 /**
245 * Create a Snapshot object. See {@link Subscription#createSnapshot} to
246 * create a snapshot.
247 *
248 * @throws {Error} If a name is not provided.
249 *
250 * @param {string} name The name of the snapshot.
251 * @returns {Snapshot} A {@link Snapshot} instance.
252 *
253 * @example
254 * const {PubSub} = require('@google-cloud/pubsub');
255 * const pubsub = new PubSub();
256 *
257 * const snapshot = pubsub.snapshot('my-snapshot');
258 */
259 snapshot(name: string): Snapshot;
260 /**
261 * Create a Subscription object. This command by itself will not run any API
262 * requests. You will receive a {@link Subscription} object,
263 * which will allow you to interact with a subscription.
264 *
265 * @throws {Error} If subscription name is omitted.
266 *
267 * @param {string} name Name of the subscription.
268 * @param {SubscriberOptions} [options] Configuration object.
269 * @returns {Subscription} A {@link Subscription} instance.
270 *
271 * @example
272 * const {PubSub} = require('@google-cloud/pubsub');
273 * const pubsub = new PubSub();
274 *
275 * const subscription = pubsub.subscription('my-subscription');
276 *
277 * // Register a listener for `message` events.
278 * subscription.on('message', function(message) {
279 * // Called every time a message is received.
280 * // message.id = ID of the message.
281 * // message.ackId = ID used to acknowledge the message receival.
282 * // message.data = Contents of the message.
283 * // message.attributes = Attributes of the message.
284 * // message.publishTime = Date when Pub/Sub received the message.
285 * });
286 */
287 subscription(name: string, options?: SubscriptionOptions): Subscription;
288 /**
289 * Create a Topic object. See {@link PubSub#createTopic} to create a topic.
290 *
291 * @throws {Error} If a name is not provided.
292 *
293 * @param {string} name The name of the topic.
294 * @param {PublishOptions} [options] Publisher configuration object.
295 * @returns {Topic} A {@link Topic} instance.
296 *
297 * @example
298 * const {PubSub} = require('@google-cloud/pubsub');
299 * const pubsub = new PubSub();
300 *
301 * const topic = pubsub.topic('my-topic');
302 */
303 topic(name: string, options?: PublishOptions): Topic;
304}
305export {};