From a0c728e5873036ee2767d0fd0d80f8988431a5c7 Mon Sep 17 00:00:00 2001 From: Praveen Kumar Singh Date: Fri, 8 Feb 2019 13:36:54 +0530 Subject: [PATCH] noImplicitAny for Snapshot and Publisher, done change request - 03 --- src/index.ts | 16 +++++------ src/publisher.ts | 30 ++++++++------------ src/snapshot.ts | 67 ++++++++++++++++++++++++++++++++++++++++++--- src/subscription.ts | 24 ++++++++-------- src/topic.ts | 20 +++++++------- 5 files changed, 104 insertions(+), 53 deletions(-) diff --git a/src/index.ts b/src/index.ts index a6b7771b4..e9aebf2d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,7 +48,7 @@ const {grpc} = new gax.GrpcClient(opts); export type SeekCallback = RequestCallback; export interface GetSubscriptionMetadataCallback { - (err: ServiceError|null, res?: google.pubsub.v1.Subscription|null): void; + (err: ServiceError|null, res?: google.pubsub.v1.ISubscription|null): void; } export interface ExistsCallback { @@ -84,7 +84,7 @@ export interface SubscriptionCallOptions { */ export interface CreateSnapshotCallback { (err: Error|null, snapshot?: Snapshot|null, - apiResponse?: google.pubsub.v1.Snapshot): void; + apiResponse?: google.pubsub.v1.ISnapshot): void; } /** @@ -92,7 +92,7 @@ export interface CreateSnapshotCallback { * @property {Snapshot}. * @property {object} 1 The full API response. */ -export type CreateSnapshotResponse = [Snapshot, google.pubsub.v1.Snapshot]; +export type CreateSnapshotResponse = [Snapshot, google.pubsub.v1.ISnapshot]; /** * Project ID placeholder. @@ -109,7 +109,7 @@ export type Metadata = any; * @property {Topic} 0 The new {@link Topic}. * @property {object} 1 The full API response. */ -export type CreateTopicResponse = [Topic, google.pubsub.v1.Topic]; +export type CreateTopicResponse = [Topic, google.pubsub.v1.ITopic]; /** * @callback CreateTopicCallback @@ -119,7 +119,7 @@ export type CreateTopicResponse = [Topic, google.pubsub.v1.Topic]; */ export interface CreateTopicCallback { (err?: Error|null, topic?: Topic|null, - apiResponse?: google.pubsub.v1.Topic): void; + apiResponse?: google.pubsub.v1.ITopic): void; } /** @@ -130,7 +130,7 @@ export interface CreateTopicCallback { */ export interface CreateSubscriptionCallback { (err?: Error|null, subscription?: Subscription|null, - apiResponse?: google.pubsub.v1.Subscription): void; + apiResponse?: google.pubsub.v1.ISubscription): void; } export type Client = 'PublisherClient'|'SubscriberClient'; @@ -157,7 +157,7 @@ export interface RequestCallback { * @property {object} 1 The full API response. */ export type CreateSubscriptionResponse = - [Subscription, google.pubsub.v1.Subscription]; + [Subscription, google.pubsub.v1.ISubscription]; export interface CreateSubscriptionOptions { @@ -403,7 +403,7 @@ export class PubSub { name: subscription.name, }); - this.request( + this.request( { client: 'SubscriberClient', method: 'createSubscription', diff --git a/src/publisher.ts b/src/publisher.ts index 3ffe3d5f8..a738268e7 100644 --- a/src/publisher.ts +++ b/src/publisher.ts @@ -23,24 +23,19 @@ const each = require('async-each'); import * as extend from 'extend'; import * as is from 'is'; import {Topic} from './topic'; -import {RequestCallback, Attributes} from '.'; +import {Attributes} from '.'; import {ServiceError} from 'grpc'; interface Inventory { - callbacks: QueueCallback[]; + callbacks: PublishCallback[]; queued: google.pubsub.v1.IPubsubMessage[]; bytes: number; } export interface PublishCallback { - (err: ServiceError, messageId?: null): void; - (err: null, messageId: string): void; + (err?: ServiceError|null, messageId?: string|null): void; } -interface QueueCallback { - (err: ServiceError, res?: null): void; - (err: null, res: string): void; -} /** * @typedef BatchPublishOptions @@ -262,7 +257,7 @@ export class Publisher { topic: this.topic.name, messages, }; - this.topic.request( + this.topic.request( { client: 'PublisherClient', method: 'publish', @@ -271,14 +266,11 @@ export class Publisher { }, (err, resp) => { const messageIds = arrify(resp && resp.messageIds); - each( - callbacks, - (callback: RequestCallback, - next: RequestCallback) => { - const messageId = messageIds[callbacks.indexOf(callback)]; - callback(err, messageId); - next(); - }); + each(callbacks, (callback: PublishCallback, next: Function) => { + const messageId = messageIds[callbacks.indexOf(callback)]; + callback(err, messageId); + next(); + }); }); } /** @@ -291,8 +283,8 @@ export class Publisher { * @param {function} callback The callback function. */ queue_(data: Buffer, attrs: Attributes): Promise; - queue_(data: Buffer, attrs: Attributes, callback: QueueCallback): void; - queue_(data: Buffer, attrs: Attributes, callback?: QueueCallback): + queue_(data: Buffer, attrs: Attributes, callback: PublishCallback): void; + queue_(data: Buffer, attrs: Attributes, callback?: PublishCallback): void|Promise { this.inventory_.queued.push({ data, diff --git a/src/snapshot.ts b/src/snapshot.ts index 57e4c6bd1..b5940397c 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -92,7 +92,7 @@ export class Snapshot { name: string; // tslint:disable-next-line variable-name Promise?: PromiseConstructor; - metadata!: google.pubsub.v1.Snapshot; + metadata!: google.pubsub.v1.ISnapshot; constructor(parent: Subscription|PubSub, name: string) { if (parent instanceof PubSub) { this.Promise = parent.Promise; @@ -146,7 +146,41 @@ export class Snapshot { return 'projects/' + projectId + '/snapshots/' + name.split('/').pop(); } - + /** + * Create a snapshot with the given name. + * + * **This is only available if you accessed this object through + * {@link Subscription#snapshot}.** + * + * @method Snapshot#create + * @param {string} name Name of the snapshot. + * @param {function} [callback] The callback function. + * @param {?error} callback.err An error from the API call, may be null. + * @param {Snapshot} callback.snapshot The newly created + * snapshot. + * @param {object} callback.apiResponse The full API response from the + * service. + * + * @example + * const subscription = pubsub.subscription('my-subscription'); + * const snapshot = subscription.snapshot('my-snapshot'); + * + * const callback = (err, snapshot, apiResponse) => { + * if (!err) { + * // The snapshot was created successfully. + * } + * }; + * + * snapshot.create('my-snapshot', callback); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * snapshot.create('my-snapshot').then((data) => { + * const snapshot = data[0]; + * const apiResponse = data[1]; + * }); + */ create(gaxOpts?: CallOptions): Promise; create(callback: CreateSnapshotCallback): void; create(gaxOpts: CallOptions, callback: CreateSnapshotCallback): void; @@ -161,11 +195,36 @@ export class Snapshot { .createSnapshot(this.name, gaxOpts! as CallOptions, callback!); } - seek(gaxOpts?: CallOptions): Promise; + /** + * Seeks an existing subscription to the snapshot. + * + * **This is only available if you accessed this object through + * {@link Subscription#snapshot}.** + * + * @method Snapshot#seek + * @param {function} callback The callback function. + * @param {?error} callback.err An error from the API call, may be null. + * @param {object} callback.apiResponse The full API response from the + * service. + * + * @example + * const subscription = pubsub.subscription('my-subscription'); + * const snapshot = subscription.snapshot('my-snapshot'); + * + * snapshot.seek((err, apiResponse) => {}); + * + * //- + * // If the callback is omitted, we'll return a Promise. + * //- + * snapshot.seek().then((data) => { + * const apiResponse = data[0]; + * }); + */ + seek(gaxOpts?: CallOptions): Promise; seek(callback: SeekCallback): void; seek(gaxOpts: CallOptions, callback: SeekCallback): void; seek(gaxOpts?: CallOptions|SeekCallback, callback?: SeekCallback): - void|Promise { + void|Promise { if (!(this.parent instanceof Subscription)) { throw new Error( `This is only available if you accessed this object through Subscription#snapshot`); diff --git a/src/subscription.ts b/src/subscription.ts index 3d41bc479..43317028d 100644 --- a/src/subscription.ts +++ b/src/subscription.ts @@ -368,7 +368,7 @@ export class Subscription extends EventEmitter { name: snapshot.name, subscription: this.name, }; - this.request( + this.request( { client: 'SubscriberClient', method: 'createSnapshot', @@ -598,14 +598,14 @@ export class Subscription extends EventEmitter { * const apiResponse = data[0]; * }); */ - getMetadata(gaxOpts?: CallOptions): Promise; + getMetadata(gaxOpts?: CallOptions): Promise; getMetadata(callback: GetSubscriptionMetadataCallback): void; getMetadata(gaxOpts: CallOptions, callback: GetSubscriptionMetadataCallback): void; getMetadata( gaxOptsOrCallback?: CallOptions|GetSubscriptionMetadataCallback, callback?: GetSubscriptionMetadataCallback): - void|Promise { + void|Promise { const gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {}; callback = @@ -613,7 +613,7 @@ export class Subscription extends EventEmitter { const reqOpts = { subscription: this.name, }; - this.request( + this.request( { client: 'SubscriberClient', method: 'getSubscription', @@ -769,13 +769,13 @@ export class Subscription extends EventEmitter { * subscription.seek(date, callback); */ seek(snapshot: string|Date, gaxOpts?: CallOptions): - Promise; + Promise; seek(snapshot: string|Date, callback: SeekCallback): void; seek(snapshot: string|Date, gaxOpts: CallOptions, callback: SeekCallback): void; seek( snapshot: string|Date, gaxOptsOrCallback?: CallOptions|SeekCallback, - callback?: SeekCallback): void|Promise { + callback?: SeekCallback): void|Promise { const gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {}; callback = @@ -846,19 +846,19 @@ export class Subscription extends EventEmitter { * }); */ setMetadata(metadata: Metadata, gaxOpts?: CallOptions): - Promise; + Promise; setMetadata( metadata: Metadata, - callback: RequestCallback): void; + callback: RequestCallback): void; setMetadata( metadata: Metadata, gaxOpts: CallOptions, - callback: RequestCallback): void; + callback: RequestCallback): void; setMetadata( metadata: Metadata, gaxOptsOrCallback?: CallOptions| - RequestCallback, - callback?: RequestCallback): - void|Promise { + RequestCallback, + callback?: RequestCallback): + void|Promise { const gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {}; callback = diff --git a/src/topic.ts b/src/topic.ts index f4cee5ce1..7eda9d188 100644 --- a/src/topic.ts +++ b/src/topic.ts @@ -361,9 +361,9 @@ export class Topic { * }); */ get(callback: CreateTopicCallback): void; - get(gaxOpts?: CallOptions&GetCallOptions): Promise; - get(gaxOpts: CallOptions&GetCallOptions, callback: CreateTopicCallback): void; - get(gaxOptsOrCallback?: CallOptions&GetCallOptions|CreateTopicCallback, + get(gaxOpts?: GetCallOptions): Promise; + get(gaxOpts: GetCallOptions, callback: CreateTopicCallback): void; + get(gaxOptsOrCallback?: GetCallOptions|CreateTopicCallback, callback?: CreateTopicCallback): void|Promise { const gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {}; @@ -423,15 +423,15 @@ export class Topic { * const apiResponse = data[0]; * }); */ - getMetadata(callback: RequestCallback): void; + getMetadata(callback: RequestCallback): void; getMetadata( gaxOpts: CallOptions, - callback: RequestCallback): void; - getMetadata(gaxOpts?: CallOptions): Promise; + callback: RequestCallback): void; + getMetadata(gaxOpts?: CallOptions): Promise; getMetadata( - gaxOptsOrCallback?: CallOptions|RequestCallback, - callback?: RequestCallback): - void|Promise { + gaxOptsOrCallback?: CallOptions|RequestCallback, + callback?: RequestCallback): + void|Promise { const gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {}; callback = @@ -439,7 +439,7 @@ export class Topic { const reqOpts = { topic: this.name, }; - this.request( + this.request( { client: 'PublisherClient', method: 'getTopic',