Added definitions for @google-cloud/pubsub (#21057)

* Added definitions for @google-cloud/pubsub

* Changed Project as requested by review comments
This commit is contained in:
pheromonez
2017-10-28 11:18:58 +11:00
committed by Sheetal Nandi
parent 2b67b8a4e3
commit 160b4e5701
4 changed files with 951 additions and 0 deletions

View File

@@ -0,0 +1,578 @@
import * as PubSub from '@google-cloud/pubsub';
// AUTHOR NOTES: We use the examples directly from the library documentation
// where possible. If there is a problem with a given example (e.g. undocumented
// feature or option), we make a note of it and provide an alternative example
// call instead.
///////////////////////////////////////////////////////////////////////////////
// PUBSUB
///////////////////////////////////////////////////////////////////////////////
{
let pubsub: PubSub.PubSub;
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=PubSub
// When running on Google Cloud Platform:
pubsub = PubSub();
// When running elsewhere:
pubsub = PubSub({
projectId: 'grape-spaceship-123',
keyFilename: '/path/to/keyfile.json',
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=createSubscription
// Subscribe to a topic:
pubsub.createSubscription('messageCenter', 'newMessages', (err, subscription, apiResponse) => { });
// Customize the subscription:
// NOTE: ackDeadline, as given in the example, is undocumented, so create a subscription only with the KNOWN options
pubsub.createSubscription('messageCenter', 'newMessages', {
retainAckedMessages: true,
}, (err, subscription, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
pubsub.createSubscription('messageCenter', 'newMessages').then((data) => {
const subscription = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=createTopic
// Create topic with callback
pubsub.createTopic('my-new-topic', (err, topic, apiResponse) => {
if (!err) {
// The topic was created successfully.
}
});
// If the callback is omitted, we'll return a Promise.
pubsub.createTopic('my-new-topic').then((data) => {
const topic = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=getSnapshots
// Get snapshots:
pubsub.getSnapshots((err, snapshots) => {
if (!err) {
// snapshots is an array of Snapshot objects.
}
});
// If the callback is omitted, we'll return a Promise.
pubsub.getSnapshots().then((data) => {
const snapshots = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=getSnapshotsStream
// Get snapshots stream
pubsub.getSnapshotsStream()
.on('error', console.error)
.on('data', (snapshot) => {
// snapshot is a Snapshot object.
})
.on('end', () => {
// All snapshots retrieved.
});
// If you anticipate many results, you can end a stream early to prevent unnecessary processing and API requests.
// NOTE: this had to be modified to work around the 'this' keyword as used in the example
{
const stream = pubsub.getSnapshotsStream();
stream.on('data', (snapshot) => {
stream.end();
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=getSubscriptions
// Get subscriptions:
pubsub.getSubscriptions((err, subscriptions) => {
if (!err) {
// subscriptions is an array of Subscription objects.
}
});
// If the callback is omitted, we'll return a Promise.
pubsub.getSubscriptions().then((data) => {
const subscriptions = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=getSubscriptionsStream
// Get subscriptions stream
pubsub.getSubscriptionsStream()
.on('error', console.error)
.on('data', (subscription) => {
// subscription is a Subscription object.
})
.on('end', () => {
// All subscriptions retrieved.
});
// If you anticipate many results, you can end a stream early to prevent unnecessary processing and API requests.
// Note: this had to be modified to work around the 'this' keyword as used in the example.
{
const stream = pubsub.getSubscriptionsStream();
stream.on('data', (subscription) => {
stream.end();
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=getTopics
// Get topics:
pubsub.getTopics((err, topics) => {
if (!err) {
// topics is an array of Topic objects.
}
});
// Customize the query:
pubsub.getTopics({
pageSize: 3
}, (err, topics) => { });
// If the callback is omitted, we'll return a Promise.
pubsub.getTopics().then((data) => {
const topics = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=getTopicsStream
// Get topics stream:
pubsub.getTopicsStream()
.on('error', console.error)
.on('data', (topic) => {
// topic is a Topic object.
})
.on('end', () => {
// All topics retrieved.
});
// If you anticipate many results, you can end a stream early to prevent unnecessary processing and API requests.
// Note: this had to be modified to work around the 'this' keyword as used in the example.
{
const stream = pubsub.getTopicsStream();
stream.on('data', (topic) => {
stream.end();
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=snapshot
// Snapshot:
{
const snapshot = pubsub.snapshot('my-snapshot');
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=subscription
// Subscription:
{
const subscription = pubsub.subscription('my-subscription');
// Register a listener for `message` events.
subscription.on('message', (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 = Timestamp when Pub/Sub received the message.
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub?method=topic
// Topic:
{
const topic = pubsub.topic('my-topic');
}
}
///////////////////////////////////////////////////////////////////////////////
// PUBLISHER
///////////////////////////////////////////////////////////////////////////////
{
const pubsub = PubSub();
const topic = pubsub.topic('my-topic');
const publisher = topic.publisher();
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/publisher?method=publish
// Publish:
publisher.publish(new Buffer('Hello, world!'), (err, messageId) => {
if (err) {
// Error handling omitted.
}
});
// Optionally you can provide an object containing attributes for the message.
publisher.publish(new Buffer('Hello, world!'), { key: 'value' }, (err, messageId) => {
if (err) {
// Error handling omitted.
}
});
}
///////////////////////////////////////////////////////////////////////////////
// SNAPSHOT
///////////////////////////////////////////////////////////////////////////////
{
const pubsub = PubSub();
const subscription = pubsub.subscription('my-subscription');
// There are two type of snapshots; the ones obtained from subscription.createSnapshot() have more functionality
const snapshot = pubsub.snapshot('my-snapshot');
const snapshotFromSubscription = subscription.snapshot('my-snapshot');
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/snapshot?method=create
// Note: Only available to snapshots created via methods of Subscription
// Create snapshot
snapshotFromSubscription.create('my-snapshot', (err, snapshot, apiResponse) => {
if (!err) {
// The snapshot was created successfully.
}
});
// If the callback is omitted, we'll return a Promise.
snapshotFromSubscription.create('my-snapshot').then((data) => {
const snapshot = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/snapshot?method=delete
// Delete the snapshot
snapshot.delete((err, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
snapshot.delete().then((data) => {
const apiResponse = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/snapshot?method=seek
// Note: Only available to snapshots created via methods of Subscription
// Seek:
snapshotFromSubscription.seek((err, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
snapshotFromSubscription.seek().then((data) => {
const apiResponse = data[0];
});
}
///////////////////////////////////////////////////////////////////////////////
// SUBSCRIPTION
///////////////////////////////////////////////////////////////////////////////
{
const pubsub = PubSub();
const topic = pubsub.topic('my-topic');
const subscription = topic.subscription('my-subscription');
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=close
// Close:
subscription.close((err) => {
if (err) {
// Error handling omitted.
}
});
// If the callback is omitted, we'll return a Promise.
subscription.close().then(() => { });
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=createSnapshot
// Create snapshot:
subscription.createSnapshot('my-snapshot', (err, snapshot, apiResponse) => {
if (!err) {
// The snapshot was created successfully.
}
});
// If the callback is omitted, we'll return a Promise.
subscription.createSnapshot('my-snapshot').then((data) => {
const snapshot = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=delete
// Delete:
subscription.delete((err, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
subscription.delete().then((data) => {
const apiResponse = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=exists
// Exists:
subscription.exists((err, exists) => { });
// If the callback is omitted, we'll return a Promise.
subscription.exists().then((data) => {
const exists = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=get
// Get:
subscription.get((err, subscription, apiResponse) => {
// The `subscription` data has been populated.
});
// If the callback is omitted, we'll return a Promise.
subscription.get().then((data) => {
const subscription = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=getMetadata
// Get metadata:
subscription.getMetadata((err, apiResponse) => {
if (err) {
// Error handling omitted.
}
});
// If the callback is omitted, we'll return a Promise.
subscription.getMetadata().then((data) => {
const apiResponse = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=modifyPushConfig
// Modify push config:
// Note: Had to modify the code to force typings
{
const pushConfig: PubSub.Subscription.PushConfig = {
pushEndpoint: 'https://mydomain.com/push',
attributes: {
'x-goog-version': 'v1',
}
};
subscription.modifyPushConfig(pushConfig, (err, apiResponse) => {
if (err) {
// Error handling omitted.
}
});
// If the callback is omitted, we'll return a Promise.
subscription.modifyPushConfig(pushConfig).then((data) => {
const apiResponse = data[0];
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=seek
// Seek:
{
const callback: PubSub.Subscription.SeekCallback = (err, resp) => {
if (!err) {
// Seek was successful.
}
};
subscription.seek('my-snapshot', callback);
// Alternatively, to specify a certain point in time, you can provide a Date object.
subscription.seek(new Date('October 21 2015'), callback);
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=setMetadata
{
const metadata = {
key: 'value'
};
// Set metadata
subscription.setMetadata(metadata, (err, apiResponse) => {
if (err) {
// Error handling omitted.
}
});
// If the callback is omitted, we'll return a Promise.
subscription.setMetadata(metadata).then((data) => {
const apiResponse = data[0];
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=snapshot
// Snapshot:
subscription.snapshot('my-snapshot');
}
///////////////////////////////////////////////////////////////////////////////
// TOPIC
///////////////////////////////////////////////////////////////////////////////
{
const pubsub = PubSub();
const topic = pubsub.topic('my-topic');
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=create
// Create:
topic.create((err, topic, apiResponse) => {
if (!err) {
// The topic was created successfully.
}
});
// If the callback is omitted, we'll return a Promise.
topic.create().then((data) => {
const topic = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=createSubscription
{
const callback: PubSub.Topic.CreateSubscriptionCallback = (err, subscription, apiResponse) => { };
// Without specifying any options.
topic.createSubscription('newMessages', callback);
// With options.
// Note: ackDeadline not documented, so we use a different option
topic.createSubscription('newMessages', {
// ackDeadline: 90000 // 90 seconds
retainAckedMessages: true,
}, callback);
// If the callback is omitted, we'll return a Promise.
topic.createSubscription('newMessages').then((data) => {
const subscription = data[0];
const apiResponse = data[1];
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=delete
// Delete:
topic.delete((err, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
topic.delete().then((data) => {
const apiResponse = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=exists
// Exists:
topic.exists((err, exists) => { });
// If the callback is omitted, we'll return a Promise.
topic.exists().then((data) => {
const exists = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=get
// Get:
topic.get((err, topic, apiResponse) => {
// The `topic` data has been populated.
});
// If the callback is omitted, we'll return a Promise.
topic.get().then((data) => {
const topic = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=getMetadata
// Get metadata
topic.getMetadata((err, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
topic.getMetadata().then((data) => {
const apiResponse = data[0];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=getSubscriptions
// Get subscriptions:
// Note: Modified so that the callback is a constant
{
const callback: PubSub.Topic.GetSubscriptionsCallback = (err, subscriptions) => {
// subscriptions is an array of `Subscription` objects.
};
topic.getSubscriptions(callback);
// Customize the query.
topic.getSubscriptions({
pageSize: 3
}, callback);
// If the callback is omitted, we'll return a Promise.
topic.getSubscriptions().then((data) => {
const subscriptions = data[0];
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=getSubscriptionsStream
// Get subscriptions stream:
topic.getSubscriptionsStream()
.on('error', console.error)
.on('data', (subscription) => {
// subscription is a Subscription object.
})
.on('end', () => {
// All subscriptions retrieved.
});
// If you anticipate many results, you can end a stream early to prevent unnecessary processing and API requests.
{
const stream = topic.getSubscriptionsStream();
stream.on('data', (subscription) => {
stream.end();
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=publisher
topic.publisher().publish(new Buffer('Hello, world!'), (err, messageId) => {
if (err) {
// Error handling omitted.
}
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=subscription
// Register a listener for `message` events.
topic.subscription('my-subscription').on('message', (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 = Timestamp when Pub/Sub received the message.
});
}
///////////////////////////////////////////////////////////////////////////////
// IAM
///////////////////////////////////////////////////////////////////////////////
{
const pubsub = PubSub();
const topic = pubsub.topic('my-topic');
const subscription = topic.subscription('my-subscription');
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=iam.getPolicy
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=iam.getPolicy
// Get policy:
topic.iam.getPolicy((err, policy, apiResponse) => { });
subscription.iam.getPolicy((err, policy, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
topic.iam.getPolicy().then((data) => {
const policy = data[0];
const apiResponse = data[1];
});
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=iam.setPolicy
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=iam.setPolicy
{
const myPolicy = {
bindings: [
{
role: 'roles/pubsub.subscriber',
members: ['serviceAccount:myotherproject@appspot.gserviceaccount.com']
}
]
};
// Set policy:
topic.iam.setPolicy(myPolicy, (err, policy, apiResponse) => { });
subscription.iam.setPolicy(myPolicy, (err, policy, apiResponse) => { });
// If the callback is omitted, we'll return a Promise.
topic.iam.setPolicy(myPolicy).then((data) => {
const policy = data[0];
const apiResponse = data[1];
});
}
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/subscription?method=iam.testPermissions
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=iam.testPermissions
{
const test = 'pubsub.topics.update';
// Test permission
topic.iam.testPermissions(test, (err, permissions, apiResponse) => {
console.log(permissions);
// {
// "pubsub.topics.update": true
// }
});
// Test several permissions at once.
const tests = [
'pubsub.subscriptions.consume',
'pubsub.subscriptions.update'
];
subscription.iam.testPermissions(tests, (err, permissions) => {
console.log(permissions);
// {
// "pubsub.subscriptions.consume": true,
// "pubsub.subscriptions.update": false
// }
});
// If the callback is omitted, we'll return a Promise.
topic.iam.testPermissions(test).then((data) => {
const permissions = data[0];
const apiResponse = data[1];
});
}
}

344
types/google-cloud__pubsub/index.d.ts vendored Normal file
View File

@@ -0,0 +1,344 @@
// Type definitions for @google-cloud/pubsub 0.14
// Project: https://github.com/GoogleCloudPlatform/google-cloud-node/tree/master/packages/pubsub
// Definitions by: Paul Huynh <https://github.com/pheromonez>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
/// <reference types="node"/>
import { EventEmitter } from "events";
import { Duplex } from "stream";
declare namespace PubSub {
// TODO write definitions for the for v1
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/v1
function v1(config?: GCloudConfiguration): any;
interface GCloudConfiguration {
projectId?: string;
keyFilename?: string;
email?: string;
credentials?: {
client_email?: string;
private_key?: string
};
autoRetry?: boolean;
maxRetries?: number;
promise?: any;
}
interface PubSub {
createSubscription(topic: Topic | string, name: string, options?: PubSub.CreateSubscriptionOptions): Promise<any[]>;
createSubscription(topic: Topic | string, name: string, callback: PubSub.CreateSubscriptionCallback): void;
createSubscription(topic: Topic | string, name: string, options: PubSub.CreateSubscriptionOptions, callback: PubSub.CreateSubscriptionCallback): void;
createTopic(name: string, gaxOpts?: GAX.CallOptions): Promise<any[]>;
createTopic(name: string, callback: PubSub.CreateTopicCallback): void;
createTopic(name: string, gaxOpts: GAX.CallOptions, callback: PubSub.CreateTopicCallback): void;
getSnapshots(options?: PubSub.GetSnapshotsOptions): Promise<any[]>;
getSnapshots(callback: PubSub.GetSnapshotsCallback): void;
getSnapshots(options: PubSub.GetSnapshotsOptions, callback: PubSub.GetSnapshotsCallback): void;
getSnapshotsStream(options?: PubSub.GetSnapshotsOptions): Duplex;
getSubscriptions(options?: PubSub.GetSubscriptionsOptions): Promise<any[]>;
getSubscriptions(callback: PubSub.GetSubscriptionsCallback): void;
getSubscriptions(options: PubSub.GetSubscriptionsOptions, callback: PubSub.GetSubscriptionsCallback): void;
getSubscriptionsStream(options?: PubSub.GetSubscriptionsOptions): Duplex;
getTopics(query?: PubSub.GetTopicsQuery): Promise<any[]>;
getTopics(callback: PubSub.GetTopicsCallback): void;
getTopics(query: PubSub.GetTopicsQuery, callback: PubSub.GetTopicsCallback): void;
getTopicsStream(query?: PubSub.GetTopicsQuery): Duplex;
snapshot(name: string): Snapshot;
subscription(name: string, options?: PubSub.SubscriptionOptions): Subscription;
topic(name: string): Topic;
}
namespace PubSub {
interface CreateSubscriptionOptions {
flowControl?: {
maxBytes?: number;
maxMessages?: number;
};
gaxOpts?: GAX.CallOptions;
messageRetentionDuration?: number | Date;
pushEndpoint?: string;
retainAckedMessages?: boolean;
}
type CreateSubscriptionCallback = (err: Error | null, subscription: Subscription, apiResponse: object) => void;
type CreateTopicCallback = (err: Error | null, topic: Topic, apiResponse: object) => void;
interface GetSnapshotsOptions {
autoPaginate?: boolean;
gaxOpts?: GAX.CallOptions;
pageSize?: number;
pageToken?: string;
}
type GetSnapshotsCallback = (err: Error | null, snapshots: Snapshot[]) => void;
interface GetSubscriptionsOptions {
autoPaginate?: boolean;
gaxOpts?: GAX.CallOptions;
pageSize?: number;
pageToken?: string;
topic?: Topic | string;
}
type GetSubscriptionsCallback = (err: Error | null, subscriptions: Subscription[], apiResponse: object) => void;
interface GetTopicsQuery {
autoPaginate?: boolean;
gaxOpts?: GAX.CallOptions;
pageSize?: number;
pageToken?: string;
}
type GetTopicsCallback = (err: Error | null, topics: Topic[], apiResponse: object) => void;
interface SubscriptionOptions {
flowControl?: {
maxBytes?: number;
maxMessages?: number;
};
maxConnections?: number;
}
}
interface Publisher {
publish(data: Buffer, callback: Publisher.PublishCallback): void;
publish(data: Buffer, attributes: object, callback: Publisher.PublishCallback): void;
publish(data: Buffer, attributes?: object): Promise<any[]>;
}
namespace Publisher {
type PublishCallback = (error: Error | null, messageId: string) => void;
}
interface Snapshot {
delete(): Promise<any[]>;
delete(callback: Snapshot.DeleteCallback): void;
}
interface SnapshotFromSubscription extends Snapshot {
create(name: string): Promise<any[]>;
create(name: string, callback: Snapshot.CreateCallback): void;
seek(): Promise<any[]>;
seek(callback: Snapshot.SeekCallback): void;
}
namespace Snapshot {
type DeleteCallback = (err: Error | null, apiResponse: object) => void;
type CreateCallback = (err: Error | null, snapshot: Snapshot, apiResponse: object) => void;
type SeekCallback = (err: Error | null, apiResponse: object) => void;
}
interface Subscription extends EventEmitter {
close(): Promise<void>;
close(callback: Subscription.CloseCallback): void;
createSnapshot(name: string, gaxOpts?: GAX.CallOptions): Promise<any[]>;
createSnapshot(name: string, callback: Subscription.CreateSnapshotCallback): void;
createSnapshot(name: string, gaxOpts: GAX.CallOptions, callback: Subscription.CreateSnapshotCallback): void;
delete(gaxOpts?: GAX.CallOptions): Promise<any[]>;
delete(callback: Subscription.DeleteCallback): void;
delete(gaxOpts: GAX.CallOptions, callback: Subscription.DeleteCallback): void;
exists(): Promise<any[]>;
exists(callback: Subscription.ExistsCallback): void;
get(gaxOpts?: GAX.CallOptions): Promise<any[]>; // TODO: only expose autoCreate
// NOTE: The following are not documented, but are possible signatures base on the source code
get(callback: Subscription.GetCallback): void;
get(gaxOpts: GAX.CallOptions, callback: Subscription.GetCallback): void;
getMetadata(gaxOpts?: GAX.CallOptions): Promise<any[]>;
getMetadata(callback: Subscription.GetMetadataCallback): void;
getMetadata(gaxOpts: GAX.CallOptions, callback: Subscription.GetMetadataCallback): void;
iam: IAM;
modifyPushConfig(config: Subscription.PushConfig, gaxOpts?: GAX.CallOptions): Promise<any[]>;
modifyPushConfig(config: Subscription.PushConfig, callback: Subscription.ModifyPushConfigCallback): void;
modifyPushConfig(config: Subscription.PushConfig, gaxOpts: GAX.CallOptions, callback: Subscription.ModifyPushConfigCallback): void;
seek(snapshot: string | Date, callback: Subscription.SeekCallback): void;
seek(snapshot: string | Date, gaxOpts: GAX.CallOptions, callback: Subscription.SeekCallback): void;
setMetadata(metadata: object, gaxOpts?: GAX.CallOptions): Promise<any[]>;
setMetadata(metadata: object, callback: Subscription.SetMetadataCallback): void;
setMetadata(metadata: object, gaxOpts: GAX.CallOptions, callback: Subscription.SetMetadataCallback): void;
snapshot(name: string): SnapshotFromSubscription;
}
namespace Subscription {
type CloseCallback = (err: Error | null) => void;
type CreateSnapshotCallback = (err: Error | null, snapshot: SnapshotFromSubscription, apiResponse: object) => void;
type DeleteCallback = (err: Error | null, apiResponse: object) => void;
type ExistsCallback = (err: Error | null, exists: boolean) => void;
type GetCallback = (err: Error | null, subscription: Subscription, apiResponse: object) => void;
type GetMetadataCallback = (err: Error | null, apiResponse: object) => void;
interface PushConfig {
pushEndpoint?: string;
// https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions#pushconfig
attributes?: PushConfigAttributes;
}
interface PushConfigAttributes {
'x-goog-version': 'v1beta' | 'v1' | 'v1beta2';
}
type ModifyPushConfigCallback = (err: Error | null, apiResponse: object) => void;
type SeekCallback = (err: Error | null, apiResponse: object) => void;
type SetMetadataCallback = (err: Error | null, apiResponse: object) => void;
}
interface Topic {
create(gaxOpts?: GAX.CallOptions): Promise<any[]>;
create(callback: Topic.CreateCallback): void;
create(gaxOpts: GAX.CallOptions, callback: Topic.CreateCallback): void;
createSubscription(nameOrOptions?: string | Topic.CreateSubscriptionOptions): Promise<any[]>;
createSubscription(name: string, options: Topic.CreateSubscriptionOptions): Promise<any[]>;
createSubscription(callback: Topic.CreateSubscriptionCallback): void;
createSubscription(nameOrOptions: string | Topic.CreateSubscriptionOptions, callback: Topic.CreateSubscriptionCallback): void;
createSubscription(name: string, options: Topic.CreateSubscriptionOptions, callback: Topic.CreateSubscriptionCallback): void;
delete(gaxOpts?: GAX.CallOptions): Promise<any[]>;
delete(callback: Topic.DeleteCallback): void;
delete(gaxOpts: GAX.CallOptions, callback: Topic.DeleteCallback): void;
exists(): Promise<any[]>;
exists(callback: Topic.ExistsCallback): void;
// NOTE: The documentation in the link is incomplete; the function takes a callback
// as second argument (in the source):
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=get
get(gaxOpts?: GAX.CallOptions): Promise<any[]>;
get(callback: Topic.GetCallback): void;
get(gaxOpts: GAX.CallOptions, callback: Topic.GetCallback): void;
getMetadata(gaxOpts?: GAX.CallOptions): Promise<any[]>;
getMetadata(callback: Topic.GetMetadataCallback): void;
getMetadata(gaxOpts: GAX.CallOptions, callback: Topic.GetMetadataCallback): void;
getSubscriptions(options?: Topic.GetSubscriptionsOptions): Promise<any[]>;
getSubscriptions(callback: Topic.GetSubscriptionsCallback): void;
getSubscriptions(options: Topic.GetSubscriptionsOptions, callback: Topic.GetSubscriptionsCallback): void;
// Note: The documention lists the parameter as 'query', when it probably should be 'options'.
getSubscriptionsStream(options?: Topic.GetSubscriptionsOptions): Duplex;
iam: IAM;
publisher(options?: Topic.PublisherOptions): Publisher;
subscription(name: string, options?: Topic.SubscriptionOptions): Subscription;
}
namespace Topic {
type CreateCallback = PubSub.CreateTopicCallback;
type CreateSubscriptionOptions = PubSub.CreateSubscriptionOptions;
type CreateSubscriptionCallback = PubSub.CreateSubscriptionCallback;
// Note: This is not fully documented in the link; browse the source code to find the callback parameters
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=delete
type DeleteCallback = (err: Error | null, apiResponse: object) => void;
type ExistsCallback = (err: Error | null, exists: boolean) => void;
// Note: This is not fully documented in the link; browse the source code to find the callback parameters
// https://googlecloudplatform.github.io/google-cloud-node/#/docs/pubsub/0.14.1/pubsub/topic?method=get
type GetCallback = (err: Error | null, topic: Topic, apiResponse: object) => void;
type GetMetadataCallback = (err: Error | null, apiResponse: object) => void;
// Options are SLIGHTLY different to PubSub.getSubscriptions(...), so we can't just reuse it
interface GetSubscriptionsOptions {
autoPaginate?: boolean;
gaxOpts?: GAX.CallOptions;
pageSize?: number;
pageToken?: string;
}
// Callback signature also slightly different to PubSub.getSubscriptions(callback), so we can't just reuse it
type GetSubscriptionsCallback = (err: Error | null, subscriptions: Subscription[]) => void;
interface PublisherOptions {
batching?: {
maxBytes?: number;
maxMessages?: number;
maxMilliseconds?: number;
};
}
type SubscriptionOptions = PubSub.SubscriptionOptions;
}
// Allow this interface to start with 'I', since it's an acronym!
// tslint:disable-next-line interface-name
interface IAM {
getPolicy(): Promise<any[]>;
getPolicy(callback: IAM.GetPolicyCallback): void;
setPolicy(policy: IAM.Policy): Promise<any[]>;
setPolicy(policy: IAM.Policy, callback: IAM.SetPolicyCallback): void;
testPermissions(permissions: string | string[]): Promise<any[]>;
testPermissions(permissions: string | string[], callback: IAM.TestPermissionsCallback): void;
}
namespace IAM {
type GetPolicyCallback = (err: Error | null, policy: Policy, apiResponse: object) => void;
type SetPolicyCallback = (err: Error | null, policy: Policy, apiResponse: object) => void;
type TestPermissionsCallback = (err: Error | null, permissions: string | string[], apiResponse: object) => void;
interface Policy {
bindings?: any[];
rules?: object[];
etag?: string;
}
}
namespace GAX {
/** https://googleapis.github.io/gax-nodejs/global.html#CallOptions */
interface CallOptions {
timeout?: number;
retry?: RetryOptions;
autoPaginate?: boolean;
pageToken?: object;
isBundling?: boolean;
longrunning?: BackoffSettings;
promise?: PromiseConstructor; // FIXME Unsure if this is the correct type; remove this comment if it is
}
/** https://googleapis.github.io/gax-nodejs/global.html#RetryOptions */
interface RetryOptions {
retryCodes: string[];
backoffSettings: BackoffSettings;
}
/** https://googleapis.github.io/gax-nodejs/global.html#BackoffSettings */
interface BackoffSettings {
initialRetryDelayMillis: number;
retryDelayMultiplier: number;
maxRetryDelayMillis: number;
initialRpcTimeoutMillis: number;
maxRpcTimeoutMillis: number;
totalTimeoutMillis: number;
}
}
}
declare function PubSub(config?: PubSub.GCloudConfiguration): PubSub.PubSub;
export = PubSub;

View File

@@ -0,0 +1,28 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"paths": {
"@google-cloud/pubsub": [
"google-cloud__pubsub"
]
},
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"google-cloud__pubsub-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }