Skip to content

Commit

Permalink
noImplicitAny for Snapshot and Publisher, done change request - 03
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen Kumar Singh committed Feb 8, 2019
1 parent a9a9103 commit a0c728e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 53 deletions.
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const {grpc} = new gax.GrpcClient(opts);
export type SeekCallback = RequestCallback<google.pubsub.v1.ISeekResponse>;

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 {
Expand Down Expand Up @@ -84,15 +84,15 @@ export interface SubscriptionCallOptions {
*/
export interface CreateSnapshotCallback {
(err: Error|null, snapshot?: Snapshot|null,
apiResponse?: google.pubsub.v1.Snapshot): void;
apiResponse?: google.pubsub.v1.ISnapshot): void;
}

/**
* @typedef {array} CreateSnapshotResponse
* @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.
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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';
Expand All @@ -157,7 +157,7 @@ export interface RequestCallback<TResponse> {
* @property {object} 1 The full API response.
*/
export type CreateSubscriptionResponse =
[Subscription, google.pubsub.v1.Subscription];
[Subscription, google.pubsub.v1.ISubscription];


export interface CreateSubscriptionOptions {
Expand Down Expand Up @@ -403,7 +403,7 @@ export class PubSub {
name: subscription.name,
});

this.request<google.pubsub.v1.Subscription>(
this.request<google.pubsub.v1.ISubscription>(
{
client: 'SubscriberClient',
method: 'createSubscription',
Expand Down
30 changes: 11 additions & 19 deletions src/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -262,7 +257,7 @@ export class Publisher {
topic: this.topic.name,
messages,
};
this.topic.request<google.pubsub.v1.PublishResponse>(
this.topic.request<google.pubsub.v1.IPublishResponse>(
{
client: 'PublisherClient',
method: 'publish',
Expand All @@ -271,14 +266,11 @@ export class Publisher {
},
(err, resp) => {
const messageIds = arrify(resp && resp.messageIds);
each(
callbacks,
(callback: RequestCallback<string>,
next: RequestCallback<string>) => {
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();
});
});
}
/**
Expand All @@ -291,8 +283,8 @@ export class Publisher {
* @param {function} callback The callback function.
*/
queue_(data: Buffer, attrs: Attributes): Promise<string>;
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<string> {
this.inventory_.queued.push({
data,
Expand Down
67 changes: 63 additions & 4 deletions src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CreateSnapshotResponse>;
create(callback: CreateSnapshotCallback): void;
create(gaxOpts: CallOptions, callback: CreateSnapshotCallback): void;
Expand All @@ -161,11 +195,36 @@ export class Snapshot {
.createSnapshot(this.name, gaxOpts! as CallOptions, callback!);
}

seek(gaxOpts?: CallOptions): Promise<google.pubsub.v1.SeekResponse>;
/**
* 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<google.pubsub.v1.ISeekResponse>;
seek(callback: SeekCallback): void;
seek(gaxOpts: CallOptions, callback: SeekCallback): void;
seek(gaxOpts?: CallOptions|SeekCallback, callback?: SeekCallback):
void|Promise<google.pubsub.v1.SeekResponse> {
void|Promise<google.pubsub.v1.ISeekResponse> {
if (!(this.parent instanceof Subscription)) {
throw new Error(
`This is only available if you accessed this object through Subscription#snapshot`);
Expand Down
24 changes: 12 additions & 12 deletions src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class Subscription extends EventEmitter {
name: snapshot.name,
subscription: this.name,
};
this.request<google.pubsub.v1.Snapshot>(
this.request<google.pubsub.v1.ISnapshot>(
{
client: 'SubscriberClient',
method: 'createSnapshot',
Expand Down Expand Up @@ -598,22 +598,22 @@ export class Subscription extends EventEmitter {
* const apiResponse = data[0];
* });
*/
getMetadata(gaxOpts?: CallOptions): Promise<google.pubsub.v1.Subscription>;
getMetadata(gaxOpts?: CallOptions): Promise<google.pubsub.v1.ISubscription>;
getMetadata(callback: GetSubscriptionMetadataCallback): void;
getMetadata(gaxOpts: CallOptions, callback: GetSubscriptionMetadataCallback):
void;
getMetadata(
gaxOptsOrCallback?: CallOptions|GetSubscriptionMetadataCallback,
callback?: GetSubscriptionMetadataCallback):
void|Promise<google.pubsub.v1.Subscription> {
void|Promise<google.pubsub.v1.ISubscription> {
const gaxOpts =
typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
callback =
typeof gaxOptsOrCallback === 'function' ? gaxOptsOrCallback : callback;
const reqOpts = {
subscription: this.name,
};
this.request<google.pubsub.v1.Subscription>(
this.request<google.pubsub.v1.ISubscription>(
{
client: 'SubscriberClient',
method: 'getSubscription',
Expand Down Expand Up @@ -769,13 +769,13 @@ export class Subscription extends EventEmitter {
* subscription.seek(date, callback);
*/
seek(snapshot: string|Date, gaxOpts?: CallOptions):
Promise<google.pubsub.v1.SeekResponse>;
Promise<google.pubsub.v1.ISeekResponse>;
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<google.pubsub.v1.SeekResponse> {
callback?: SeekCallback): void|Promise<google.pubsub.v1.ISeekResponse> {
const gaxOpts =
typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
callback =
Expand Down Expand Up @@ -846,19 +846,19 @@ export class Subscription extends EventEmitter {
* });
*/
setMetadata(metadata: Metadata, gaxOpts?: CallOptions):
Promise<google.pubsub.v1.Subscription>;
Promise<google.pubsub.v1.ISubscription>;
setMetadata(
metadata: Metadata,
callback: RequestCallback<google.pubsub.v1.Subscription>): void;
callback: RequestCallback<google.pubsub.v1.ISubscription>): void;
setMetadata(
metadata: Metadata, gaxOpts: CallOptions,
callback: RequestCallback<google.pubsub.v1.Subscription>): void;
callback: RequestCallback<google.pubsub.v1.ISubscription>): void;
setMetadata(
metadata: Metadata,
gaxOptsOrCallback?: CallOptions|
RequestCallback<google.pubsub.v1.Subscription>,
callback?: RequestCallback<google.pubsub.v1.Subscription>):
void|Promise<google.pubsub.v1.Subscription> {
RequestCallback<google.pubsub.v1.ISubscription>,
callback?: RequestCallback<google.pubsub.v1.ISubscription>):
void|Promise<google.pubsub.v1.ISubscription> {
const gaxOpts =
typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
callback =
Expand Down
20 changes: 10 additions & 10 deletions src/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ export class Topic {
* });
*/
get(callback: CreateTopicCallback): void;
get(gaxOpts?: CallOptions&GetCallOptions): Promise<Topic>;
get(gaxOpts: CallOptions&GetCallOptions, callback: CreateTopicCallback): void;
get(gaxOptsOrCallback?: CallOptions&GetCallOptions|CreateTopicCallback,
get(gaxOpts?: GetCallOptions): Promise<Topic>;
get(gaxOpts: GetCallOptions, callback: CreateTopicCallback): void;
get(gaxOptsOrCallback?: GetCallOptions|CreateTopicCallback,
callback?: CreateTopicCallback): void|Promise<Topic> {
const gaxOpts =
typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
Expand Down Expand Up @@ -423,23 +423,23 @@ export class Topic {
* const apiResponse = data[0];
* });
*/
getMetadata(callback: RequestCallback<google.pubsub.v1.Topic>): void;
getMetadata(callback: RequestCallback<google.pubsub.v1.ITopic>): void;
getMetadata(
gaxOpts: CallOptions,
callback: RequestCallback<google.pubsub.v1.Topic>): void;
getMetadata(gaxOpts?: CallOptions): Promise<google.pubsub.v1.Topic>;
callback: RequestCallback<google.pubsub.v1.ITopic>): void;
getMetadata(gaxOpts?: CallOptions): Promise<google.pubsub.v1.ITopic>;
getMetadata(
gaxOptsOrCallback?: CallOptions|RequestCallback<google.pubsub.v1.Topic>,
callback?: RequestCallback<google.pubsub.v1.Topic>):
void|Promise<google.pubsub.v1.Topic> {
gaxOptsOrCallback?: CallOptions|RequestCallback<google.pubsub.v1.ITopic>,
callback?: RequestCallback<google.pubsub.v1.ITopic>):
void|Promise<google.pubsub.v1.ITopic> {
const gaxOpts =
typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
callback =
typeof gaxOptsOrCallback === 'function' ? gaxOptsOrCallback : callback;
const reqOpts = {
topic: this.name,
};
this.request<google.pubsub.v1.Topic>(
this.request<google.pubsub.v1.ITopic>(
{
client: 'PublisherClient',
method: 'getTopic',
Expand Down

0 comments on commit a0c728e

Please sign in to comment.