Typescript Import @google-cloud/pubsub
I want to import a non-TypeScript module into a TypeScript project. This project does not have own declarations or @types declarations, so I created my own declarations for the mod
Solution 1:
I've come across the same issue when I was trying to write definitions for another untyped module. What I found is that if you're writing definitions for untyped modules you need to make sure the declare module
encompasses the entire definition file.
So for instance the following definition file compiled for me when I wrote a simple test project and imported @google-cloud/pubsub
module. Unfortunately I haven't found any documentation that would explain why this works.
declaremodule'@google-cloud/pubsub' {
import * as stream from'stream';
import * as events from'events';
interfaceConfigurationObjectextendsObject {
projectId?: string
keyFilename?: string
email?: string
credentials?: CredentialsObject
autoRetry?: boolean
maxRetries?: number
promise?: Function
}
interfaceCredentialsObjectextendsObject {
client_email?: string
private_key?: string
}
interfaceQueryOptionsextendsObject {
autoPaginate?: boolean
maxApiCalls?: number
maxResults?: number
pageSize?: number
pageToken?: string
}
interfaceSnapshotQueryOptionsextendsQueryOptions { }
interfaceTopicsQueryOptionsextendsObject { }
interfaceSubscriptionQueryOptionsextendsObject {
topic?: string
}
interfaceSubscribeOptionsextendsObject {
ackDeadlineSeconds: numberautoAck: booleanencoding: stringinterval: numbermaxInProgress: numberpushEndpoint: stringtimeout: number
}
interfaceSubscriptionOptionsextendsObject {
autoAck?: boolean
encoding?: string
interval?: number
maxInProgress?: number
timeout?: number
}
interfaceSubscriptionObjectextendsObject {
name: stringtopic: stringpushConfig: PushConfigObjectackDeadlineSeconds: number
}
interfacePushConfigObjectextendsObject {
pushEndpoint: stringattributes: {
[key: string]: string
}
}
interfaceTopicObjectextendsObject {
name: string
}
interfaceSnapshotObjectextendsObject {
name: string
}
interfaceMessage {
id: stringackId: stringdata: anyattributes: anytimestamp: numberack(callback: Function): voidskip(): void
}
exporttypeApiCallbackFunction<T> = (err: Error | null, data: T, apiResponse: any) =>voidexporttypeCallbackFunction<T> = (err: Error | null, data: T) =>voidexporttypeApiPromiseResult<T> = [T, any]
exportclassSubscriptionextendsevents.EventEmitter {
ack(
ackIds: string | string[],
options?: {
timeout: number
},
callback?: () =>void
): Promise<void> | voidcreate(
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | voidcreateSnapshot(
name: string,
callback?: ApiCallbackFunction<SnapshotObject>
): Promise<ApiPromiseResult<SnapshotObject>> | void
}
exportclassPubSub {
constructor(
config: ConfigurationObject
)
createTopic(
name: string,
callback?: ApiCallbackFunction<TopicObject>
): Promise<ApiPromiseResult<TopicObject>> | voidgetSnapshots(
options?: SnapshotQueryOptions,
callback?: CallbackFunction<SnapshotObject[]>
): Promise<any[]> | voidgetSnapshotsStream(
options?: SnapshotQueryOptions
): stream.ReadablegetSubscriptions(
options?: SubscriptionQueryOptions,
callback?: ApiCallbackFunction<SubscriptionObject[]>
): Promise<ApiPromiseResult<SubscriptionObject[]>> | voidgetSubscriptionsStream(
options?: SubscriptionQueryOptions
): stream.ReadablegetTopics(
options?: TopicsQueryOptions,
callback?: ApiCallbackFunction<TopicObject[]>
): Promise<ApiPromiseResult<TopicObject[]>> | voidgetTopicsStream(
options?: TopicsQueryOptions
): stream.Readablesnapshot(
name: string
): anysubscribe(
topic: TopicObject | string,
subName?: stream,
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | voidsubscription(
name?: string,
options?: SubscriptionOptions
): voidtopic(
name: string
): TopicObject
}
}
After playing around with the Pub/Sub a bit I came up with the following proof of concept code:
index.d.ts
declaremodule'@google-cloud/pubsub' {
namespace pubsub {
classPubSub {
topic (name: string) : Topic;
}
classTopic {
subscribe (subscriptionName: string, options: Object, callback: Function): void;
}
}
functionpubsub(options: any): pubsub.PubSub;
export = pubsub;
}
subscribe.ts
import * as pubsub from'@google-cloud/pubsub';
let ps = pubsub({
projectId: 'project-id',
keyFilename: 'key.json'
});
console.log('Subscribed to pubsub...');
ps.topic('test').subscribe('test', {autoAck: true}, (err: any, subscription: any) => {
if (err) {
console.log(err);
} else {
subscription.on('error', (err: any) => {
console.log(err);
});
subscription.on('message', (message: any) => {
console.log(message);
});
}
});
Solution 2:
Just for reference, if using pubsub in Node
// Does not workimport {PubSub} from'@google-cloud/pubsub';
import * asPubSubfrom'@google-cloud/pubsub';
// Worksconst {PubSub} = require('@google-cloud/pubsub');
const pubsub = newPubSub();
Solution 3:
There's lots(?) of activity on this. Probably the above answers won't work as intended. Now you need the verbose:
importPubSub = require("@google-cloud/pubsub");
constpubSub: PubSub.PubSub = new (PubSubas any).PubSub()
Post a Comment for "Typescript Import @google-cloud/pubsub"