/*! * Copyright 2014 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /// import { GoogleAuth } from 'google-auth-library'; import * as gax from 'google-gax'; import { Schema, SchemaType, ISchema, SchemaView } from './schema'; import { Snapshot } from './snapshot'; import { Subscription, SubscriptionOptions, CreateSubscriptionOptions, CreateSubscriptionCallback, CreateSubscriptionResponse, DetachSubscriptionCallback, DetachSubscriptionResponse } from './subscription'; import { Topic, GetTopicSubscriptionsCallback, GetTopicSubscriptionsResponse, CreateTopicCallback, CreateTopicResponse, TopicMetadata } from './topic'; import { PublishOptions } from './publisher'; import { CallOptions } from 'google-gax'; import { Transform } from 'stream'; import { google } from '../protos/protos'; import { SchemaServiceClient } from './v1'; export declare type Omit = Pick>; export interface ClientConfig extends gax.GrpcClientOptions { apiEndpoint?: string; servicePath?: string; port?: string | number; sslCreds?: gax.grpc.ChannelCredentials; } export interface PageOptions { gaxOpts?: CallOptions; pageSize?: number; pageToken?: string; autoPaginate?: boolean; } export declare type GetSnapshotsCallback = RequestCallback; export declare type GetSnapshotsResponse = PagedResponse; export declare type GetSubscriptionsOptions = PageOptions & { topic?: string | Topic; }; declare type GetAllSubscriptionsCallback = RequestCallback; declare type GetAllSubscriptionsResponse = PagedResponse; export declare type GetSubscriptionsCallback = GetAllSubscriptionsCallback | GetTopicSubscriptionsCallback; export declare type GetSubscriptionsResponse = GetAllSubscriptionsResponse | GetTopicSubscriptionsResponse; export declare type GetTopicsCallback = RequestCallback; export declare type GetTopicsResponse = PagedResponse; export declare type EmptyCallback = RequestCallback; export declare type EmptyResponse = [google.protobuf.IEmpty]; export declare type ExistsCallback = RequestCallback; export declare type ExistsResponse = [boolean]; export declare type DetachedCallback = RequestCallback; export declare type DetachedResponse = [boolean]; export interface GetClientConfig { client: 'PublisherClient' | 'SubscriberClient'; } export interface RequestConfig extends GetClientConfig { method: string; reqOpts?: object; gaxOpts?: CallOptions; } export interface ResourceCallback { (err: gax.grpc.ServiceError | null, resource?: Resource | null, response?: Response | null): void; } export declare type RequestCallback = R extends void ? NormalCallback : PagedCallback; export interface NormalCallback { (err: gax.grpc.ServiceError | null, res?: TResponse | null): void; } export interface PagedCallback { (err: gax.grpc.ServiceError | null, results?: Item[] | null, nextQuery?: {} | null, response?: Response | null): void; } export declare type PagedResponse = [Item[]] | [Item[], {} | null, Response]; export declare type ObjectStream = { addListener(event: 'data', listener: (data: O) => void): ObjectStream; emit(event: 'data', data: O): boolean; on(event: 'data', listener: (data: O) => void): ObjectStream; once(event: 'data', listener: (data: O) => void): ObjectStream; prependListener(event: 'data', listener: (data: O) => void): ObjectStream; prependOnceListener(event: 'data', listener: (data: O) => void): ObjectStream; } & Transform; interface GetClientCallback { (err: Error | null, gaxClient?: gax.ClientStub): void; } /** * @typedef {object} ClientConfig * @property {string} [projectId] The project ID from the Google Developer's * Console, e.g. 'grape-spaceship-123'. We will also check the environment * variable `GCLOUD_PROJECT` for your project ID. If your app is running in * an environment which supports {@link * https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application * Application Default Credentials}, your project ID will be detected * automatically. * @property {string} [keyFilename] Full path to the a .json, .pem, or .p12 key * downloaded from the Google Developers Console. If you provide a path to a * JSON file, the `projectId` option above is not necessary. NOTE: .pem and * .p12 require you to specify the `email` option as well. * @property {string} [apiEndpoint] The `apiEndpoint` from options will set the * host. If not set, the `PUBSUB_EMULATOR_HOST` environment variable from the * gcloud SDK is honored. We also check the `CLOUD_API_ENDPOINT_OVERRIDES_PUBSUB` * environment variable used by `gcloud alpha pubsub`. Otherwise the actual API * endpoint will be used. Note that if the URL doesn't end in '.googleapis.com', * we will assume that it's an emulator and disable strict SSL checks. * @property {string} [email] Account email address. Required when using a .pem * or .p12 keyFilename. * @property {object} [credentials] Credentials object. * @property {string} [credentials.client_email] * @property {string} [credentials.private_key] * @property {boolean} [autoRetry=true] Automatically retry requests if the * response is related to rate limits or certain intermittent server errors. * We will exponentially backoff subsequent requests by default. * @property {Constructor} [promise] Custom promise module to use instead of * native Promises. */ /** * [Cloud Pub/Sub](https://developers.google.com/pubsub/overview) is a * reliable, many-to-many, asynchronous messaging service from Cloud * Platform. * * @class * * @see [Cloud Pub/Sub overview]{@link https://developers.google.com/pubsub/overview} * * @param {ClientConfig} [options] Configuration options. * * @example Import the client library * const {PubSub} = require('@google-cloud/pubsub'); * * @example Create a client that uses Application Default Credentials (ADC): * const pubsub = new PubSub(); * * @example Create a client with explicit credentials: * const pubsub = new PubSub({ * projectId: 'your-project-id', * keyFilename: '/path/to/keyfile.json' * }); * * @example include:samples/quickstart.js * region_tag:pubsub_quickstart_create_topic * Full quickstart example: */ export declare class PubSub { options: ClientConfig; isEmulator: boolean; api: { [key: string]: gax.ClientStub; }; auth: GoogleAuth; projectId: string; name?: string; Promise?: PromiseConstructor; getSubscriptionsStream: () => ObjectStream; getSnapshotsStream: () => ObjectStream; getTopicsStream: () => ObjectStream; isOpen: boolean; private schemaClient?; constructor(options?: ClientConfig); /** * Returns true if we have actually resolved the full project name. * * @returns {boolean} true if the name is resolved. */ get isIdResolved(): boolean; close(): Promise; close(callback: EmptyCallback): void; /** * Create a schema in the project. * * @see [Schemas: create API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/create} * @see {@link Schema#create} * * @throws {Error} If a schema ID or name is not provided. * @throws {Error} If an invalid SchemaType is provided. * @throws {Error} If an invalid schema definition is provided. * * @param {string} schemaId The name or ID of the subscription. * @param {SchemaType} type The type of the schema (Protobuf, Avro, etc). * @param {string} definition The text describing the schema in terms of the type. * @param {object} [options] Request configuration options, outlined * here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html. * @returns {Promise} * * @example Create a schema. * const {PubSub} = require('@google-cloud/pubsub'); * const pubsub = new PubSub(); * * await pubsub.createSchema( * 'messageType', * SchemaTypes.Avro, * '{...avro definition...}' * ); */ createSchema(schemaId: string, type: SchemaType, definition: string, gaxOpts?: CallOptions): Promise; createSubscription(topic: Topic | string, name: string, options?: CreateSubscriptionOptions): Promise; createSubscription(topic: Topic | string, name: string, callback: CreateSubscriptionCallback): void; createSubscription(topic: Topic | string, name: string, options: CreateSubscriptionOptions, callback: CreateSubscriptionCallback): void; createTopic(name: string | TopicMetadata, gaxOpts?: CallOptions): Promise; createTopic(name: string | TopicMetadata, callback: CreateTopicCallback): void; createTopic(name: string | TopicMetadata, gaxOpts: CallOptions, callback: CreateTopicCallback): void; detachSubscription(name: string, gaxOpts?: CallOptions): Promise; detachSubscription(name: string, callback: DetachSubscriptionCallback): void; detachSubscription(name: string, gaxOpts: CallOptions, callback: DetachSubscriptionCallback): void; /** * Determine the appropriate endpoint to use for API requests, first trying * the `apiEndpoint` parameter. If that isn't set, we try the Pub/Sub emulator * environment variable (PUBSUB_EMULATOR_HOST). If that is also null, we try * the standard `gcloud alpha pubsub` environment variable * (CLOUDSDK_API_ENDPOINT_OVERRIDES_PUBSUB). Otherwise the default production * API is used. * * Note that if the URL doesn't end in '.googleapis.com', we will assume that * it's an emulator and disable strict SSL checks. * * @private */ determineBaseUrl_(): void; /** * Get a list of schemas associated with your project. * * The returned AsyncIterable will resolve to {@link google.pubsub.v1.ISchema} objects. * * This method returns an async iterable. These objects can be adapted * to work in a Promise/then framework, as well as with callbacks, but * this discussion is considered out of scope for these docs. * * @see [Schemas: list API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/list} * @see [More about async iterators]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of} * * @param {google.pubsub.v1.SchemaView} [view] The type of schema objects * requested, which should be an enum value from {@link SchemaViews}. Defaults * to Full. * @param {object} [options] Request configuration options, outlined * here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html. * @returns {AsyncIterable} * * @example * for await (const s of pubsub.listSchemas()) { * const moreInfo = await s.get(); * } */ listSchemas(view?: SchemaView, options?: CallOptions): AsyncIterable; getSnapshots(options?: PageOptions): Promise; getSnapshots(callback: GetSnapshotsCallback): void; getSnapshots(options: PageOptions, callback: GetSnapshotsCallback): void; getSubscriptions(options?: GetSubscriptionsOptions): Promise; getSubscriptions(callback: GetSubscriptionsCallback): void; getSubscriptions(options: GetSubscriptionsOptions, callback: GetSubscriptionsCallback): void; getTopics(options?: PageOptions): Promise; getTopics(callback: GetTopicsCallback): void; getTopics(options: PageOptions, callback: GetTopicsCallback): void; /** * Retrieve a client configuration, suitable for passing into a GAPIC * 'v1' class constructor. This will fill out projectId, emulator URLs, * and so forth. * * @returns {Promise} the filled client configuration. */ getClientConfig(): Promise; /** * Gets a schema client, creating one if needed. * @private */ getSchemaClient_(): Promise; /** * Callback function to PubSub.getClient_(). * @private * @callback GetClientCallback * @param err - Error, if any. * @param gaxClient - The gax client specified in RequestConfig.client. * Typed any since it's importing Javascript source. */ /** * Get the PubSub client object. * * @private * * @param {object} config Configuration object. * @param {object} config.gaxOpts GAX options. * @param {function} config.method The gax method to call. * @param {object} config.reqOpts Request options. * @param {function} [callback] The callback function. */ getClient_(config: GetClientConfig, callback: GetClientCallback): void; /** * Get the PubSub client object. * * @private * * @param {object} config Configuration object. * @param {object} config.gaxOpts GAX options. * @param {function} config.method The gax method to call. * @param {object} config.reqOpts Request options. * @returns {Promise} */ getClientAsync_(config: GetClientConfig): Promise; /** * Close all open client objects. * * @private * * @returns {Promise} */ closeAllClients_(): Promise; /** * Funnel all API requests through this method, to be sure we have a project * ID. * * @private * * @param {object} config Configuration object. * @param {object} config.gaxOpts GAX options. * @param {function} config.method The gax method to call. * @param {object} config.reqOpts Request options. * @param {function} [callback] The callback function. */ request(config: RequestConfig, callback: RequestCallback): void; /** * Create a Schema object, representing a schema within the project. * See {@link PubSub#createSchema} or {@link Schema#create} to create a schema. * * @throws {Error} If a name is not provided. * * @param {string} name The ID or name of the schema. * @returns {Schema} A {@link Schema} instance. * * @example * const {PubSub} = require('@google-cloud/pubsub'); * const pubsub = new PubSub(); * * const schema = pubsub.schema('my-schema'); */ schema(idOrName: string): Schema; /** * Create a Snapshot object. See {@link Subscription#createSnapshot} to * create a snapshot. * * @throws {Error} If a name is not provided. * * @param {string} name The name of the snapshot. * @returns {Snapshot} A {@link Snapshot} instance. * * @example * const {PubSub} = require('@google-cloud/pubsub'); * const pubsub = new PubSub(); * * const snapshot = pubsub.snapshot('my-snapshot'); */ snapshot(name: string): Snapshot; /** * Create a Subscription object. This command by itself will not run any API * requests. You will receive a {@link Subscription} object, * which will allow you to interact with a subscription. * * @throws {Error} If subscription name is omitted. * * @param {string} name Name of the subscription. * @param {SubscriberOptions} [options] Configuration object. * @returns {Subscription} A {@link Subscription} instance. * * @example * const {PubSub} = require('@google-cloud/pubsub'); * const pubsub = new PubSub(); * * const subscription = pubsub.subscription('my-subscription'); * * // Register a listener for `message` events. * subscription.on('message', function(message) { * // Called every time a message is received. * // message.id = ID of the message. * // message.ackId = ID used to acknowledge the message receival. * // message.data = Contents of the message. * // message.attributes = Attributes of the message. * // message.publishTime = Date when Pub/Sub received the message. * }); */ subscription(name: string, options?: SubscriptionOptions): Subscription; /** * Create a Topic object. See {@link PubSub#createTopic} to create a topic. * * @throws {Error} If a name is not provided. * * @param {string} name The name of the topic. * @param {PublishOptions} [options] Publisher configuration object. * @returns {Topic} A {@link Topic} instance. * * @example * const {PubSub} = require('@google-cloud/pubsub'); * const pubsub = new PubSub(); * * const topic = pubsub.topic('my-topic'); */ topic(name: string, options?: PublishOptions): Topic; /** * Validate a schema definition. * * @see [Schemas: validateSchema API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas/validate} * * @throws {Error} if the validation fails. * * @param {ISchema} schema The schema definition you wish to validate. * @param {object} [options] Request configuration options, outlined * here: https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html. * @returns {Promise} */ validateSchema(schema: ISchema, gaxOpts?: CallOptions): Promise; /*! * Format the name of a project. A project's full name is in the * format of projects/{projectId}. * * The GAPIC client should do this for us, but since we maintain * names rather than IDs, this is simpler. * * @private */ static formatName_(name: string): string; } export {};