打字稿导入@google-cloud/pubsub
Typescript import @google-cloud/pubsub
我想将非 TypeScript 模块导入 TypeScript 项目。
此项目没有自己的声明或@types 声明,因此我为模块创建了自己的声明。但是当我在声明文件中声明模块时,出现以下错误:
Invalid module name in augmentation. Module '@google-cloud/pubsub'
resolves to an untyped module at
'./node_modules/@google-cloud/pubsub/src/index.js', which cannot be
augmented.
我正在使用 TypeScript 2.2.2
完整的声明文件如下:
import stream from 'stream'
import events from 'events'
interface ConfigurationObject extends Object {
projectId?: string
keyFilename?: string
email?: string
credentials?: CredentialsObject
autoRetry?: boolean
maxRetries?: number
promise?: Function
}
interface CredentialsObject extends Object {
client_email?: string
private_key?: string
}
interface QueryOptions extends Object {
autoPaginate?: boolean
maxApiCalls?: number
maxResults?: number
pageSize?: number
pageToken?: string
}
interface SnapshotQueryOptions extends QueryOptions { }
interface TopicsQueryOptions extends Object { }
interface SubscriptionQueryOptions extends Object {
topic?: string
}
interface SubscribeOptions extends Object {
ackDeadlineSeconds: number
autoAck: boolean
encoding: string
interval: number
maxInProgress: number
pushEndpoint: string
timeout: number
}
interface SubscriptionOptions extends Object {
autoAck?: boolean
encoding?: string
interval?: number
maxInProgress?: number
timeout?: number
}
interface SubscriptionObject extends Object {
name: string
topic: string
pushConfig: PushConfigObject
ackDeadlineSeconds: number
}
interface PushConfigObject extends Object {
pushEndpoint: string
attributes: {
[key: string]: string
}
}
interface TopicObject extends Object {
name: string
}
interface SnapshotObject extends Object {
name: string
}
interface Message {
id: string
ackId: string
data: any
attributes: any
timestamp: number
ack(callback: Function): void
skip(): void
}
declare type ApiCallbackFunction<T> = (err: Error | null, data: T, apiResponse: any) => void
declare type CallbackFunction<T> = (err: Error | null, data: T) => void
declare type ApiPromiseResult<T> = [T, any]
declare class Subscription extends events.EventEmitter {
ack(
ackIds: string | string[],
options?: {
timeout: number
},
callback?: () => void
): Promise<void> | void
create(
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
createSnapshot(
name: string,
callback?: ApiCallbackFunction<SnapshotObject>
): Promise<ApiPromiseResult<SnapshotObject>> | void
}
declare class PubSub {
constructor(
config: ConfigurationObject
)
createTopic(
name: string,
callback?: ApiCallbackFunction<TopicObject>
): Promise<ApiPromiseResult<TopicObject>> | void
getSnapshots(
options?: SnapshotQueryOptions,
callback?: CallbackFunction<SnapshotObject[]>
): Promise<any[]> | void
getSnapshotsStream(
options?: SnapshotQueryOptions
): stream.Readable
getSubscriptions(
options?: SubscriptionQueryOptions,
callback?: ApiCallbackFunction<SubscriptionObject[]>
): Promise<ApiPromiseResult<SubscriptionObject[]>> | void
getSubscriptionsStream(
options?: SubscriptionQueryOptions
): stream.Readable
getTopics(
options?: TopicsQueryOptions,
callback?: ApiCallbackFunction<TopicObject[]>
): Promise<ApiPromiseResult<TopicObject[]>> | void
getTopicsStream(
options?: TopicsQueryOptions
): stream.Readable
snapshot(
name: string
): any
subscribe(
topic: TopicObject | string,
subName?: stream,
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
subscription(
name?: string,
options?: SubscriptionOptions
): void
topic(
name: string
): TopicObject
}
declare module '@google-cloud/pubsub' {
export = PubSub
}
我在尝试为另一个非类型化模块编写定义时遇到了同样的问题。我发现,如果您正在为非类型化模块编写定义,您需要确保 declare module
包含整个定义文件。
例如,当我编写一个简单的测试项目并导入 @google-cloud/pubsub
模块时,为我编译了以下定义文件。不幸的是,我还没有找到任何文档来解释为什么会这样。
declare module '@google-cloud/pubsub' {
import * as stream from 'stream';
import * as events from 'events';
interface ConfigurationObject extends Object {
projectId?: string
keyFilename?: string
email?: string
credentials?: CredentialsObject
autoRetry?: boolean
maxRetries?: number
promise?: Function
}
interface CredentialsObject extends Object {
client_email?: string
private_key?: string
}
interface QueryOptions extends Object {
autoPaginate?: boolean
maxApiCalls?: number
maxResults?: number
pageSize?: number
pageToken?: string
}
interface SnapshotQueryOptions extends QueryOptions { }
interface TopicsQueryOptions extends Object { }
interface SubscriptionQueryOptions extends Object {
topic?: string
}
interface SubscribeOptions extends Object {
ackDeadlineSeconds: number
autoAck: boolean
encoding: string
interval: number
maxInProgress: number
pushEndpoint: string
timeout: number
}
interface SubscriptionOptions extends Object {
autoAck?: boolean
encoding?: string
interval?: number
maxInProgress?: number
timeout?: number
}
interface SubscriptionObject extends Object {
name: string
topic: string
pushConfig: PushConfigObject
ackDeadlineSeconds: number
}
interface PushConfigObject extends Object {
pushEndpoint: string
attributes: {
[key: string]: string
}
}
interface TopicObject extends Object {
name: string
}
interface SnapshotObject extends Object {
name: string
}
interface Message {
id: string
ackId: string
data: any
attributes: any
timestamp: number
ack(callback: Function): void
skip(): void
}
export type ApiCallbackFunction<T> = (err: Error | null, data: T, apiResponse: any) => void
export type CallbackFunction<T> = (err: Error | null, data: T) => void
export type ApiPromiseResult<T> = [T, any]
export class Subscription extends events.EventEmitter {
ack(
ackIds: string | string[],
options?: {
timeout: number
},
callback?: () => void
): Promise<void> | void
create(
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
createSnapshot(
name: string,
callback?: ApiCallbackFunction<SnapshotObject>
): Promise<ApiPromiseResult<SnapshotObject>> | void
}
export class PubSub {
constructor(
config: ConfigurationObject
)
createTopic(
name: string,
callback?: ApiCallbackFunction<TopicObject>
): Promise<ApiPromiseResult<TopicObject>> | void
getSnapshots(
options?: SnapshotQueryOptions,
callback?: CallbackFunction<SnapshotObject[]>
): Promise<any[]> | void
getSnapshotsStream(
options?: SnapshotQueryOptions
): stream.Readable
getSubscriptions(
options?: SubscriptionQueryOptions,
callback?: ApiCallbackFunction<SubscriptionObject[]>
): Promise<ApiPromiseResult<SubscriptionObject[]>> | void
getSubscriptionsStream(
options?: SubscriptionQueryOptions
): stream.Readable
getTopics(
options?: TopicsQueryOptions,
callback?: ApiCallbackFunction<TopicObject[]>
): Promise<ApiPromiseResult<TopicObject[]>> | void
getTopicsStream(
options?: TopicsQueryOptions
): stream.Readable
snapshot(
name: string
): any
subscribe(
topic: TopicObject | string,
subName?: stream,
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
subscription(
name?: string,
options?: SubscriptionOptions
): void
topic(
name: string
): TopicObject
}
}
在尝试 Pub/Sub 之后,我想出了以下概念验证代码:
index.d.ts
declare module '@google-cloud/pubsub' {
namespace pubsub {
class PubSub {
topic (name: string) : Topic;
}
class Topic {
subscribe (subscriptionName: string, options: Object, callback: Function): void;
}
}
function pubsub(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);
});
}
});
仅供参考,如果在 Node 中使用 pubsub
// Does not work
import {PubSub} from '@google-cloud/pubsub';
import * as PubSub from '@google-cloud/pubsub';
// Works
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
activity on this 有很多(?)。可能上述答案不会按预期工作。现在你需要详细的:
import PubSub = require("@google-cloud/pubsub");
const pubSub: PubSub.PubSub = new (PubSub as any).PubSub()
我想将非 TypeScript 模块导入 TypeScript 项目。
此项目没有自己的声明或@types 声明,因此我为模块创建了自己的声明。但是当我在声明文件中声明模块时,出现以下错误:
Invalid module name in augmentation. Module '@google-cloud/pubsub' resolves to an untyped module at './node_modules/@google-cloud/pubsub/src/index.js', which cannot be augmented.
我正在使用 TypeScript 2.2.2
完整的声明文件如下:
import stream from 'stream'
import events from 'events'
interface ConfigurationObject extends Object {
projectId?: string
keyFilename?: string
email?: string
credentials?: CredentialsObject
autoRetry?: boolean
maxRetries?: number
promise?: Function
}
interface CredentialsObject extends Object {
client_email?: string
private_key?: string
}
interface QueryOptions extends Object {
autoPaginate?: boolean
maxApiCalls?: number
maxResults?: number
pageSize?: number
pageToken?: string
}
interface SnapshotQueryOptions extends QueryOptions { }
interface TopicsQueryOptions extends Object { }
interface SubscriptionQueryOptions extends Object {
topic?: string
}
interface SubscribeOptions extends Object {
ackDeadlineSeconds: number
autoAck: boolean
encoding: string
interval: number
maxInProgress: number
pushEndpoint: string
timeout: number
}
interface SubscriptionOptions extends Object {
autoAck?: boolean
encoding?: string
interval?: number
maxInProgress?: number
timeout?: number
}
interface SubscriptionObject extends Object {
name: string
topic: string
pushConfig: PushConfigObject
ackDeadlineSeconds: number
}
interface PushConfigObject extends Object {
pushEndpoint: string
attributes: {
[key: string]: string
}
}
interface TopicObject extends Object {
name: string
}
interface SnapshotObject extends Object {
name: string
}
interface Message {
id: string
ackId: string
data: any
attributes: any
timestamp: number
ack(callback: Function): void
skip(): void
}
declare type ApiCallbackFunction<T> = (err: Error | null, data: T, apiResponse: any) => void
declare type CallbackFunction<T> = (err: Error | null, data: T) => void
declare type ApiPromiseResult<T> = [T, any]
declare class Subscription extends events.EventEmitter {
ack(
ackIds: string | string[],
options?: {
timeout: number
},
callback?: () => void
): Promise<void> | void
create(
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
createSnapshot(
name: string,
callback?: ApiCallbackFunction<SnapshotObject>
): Promise<ApiPromiseResult<SnapshotObject>> | void
}
declare class PubSub {
constructor(
config: ConfigurationObject
)
createTopic(
name: string,
callback?: ApiCallbackFunction<TopicObject>
): Promise<ApiPromiseResult<TopicObject>> | void
getSnapshots(
options?: SnapshotQueryOptions,
callback?: CallbackFunction<SnapshotObject[]>
): Promise<any[]> | void
getSnapshotsStream(
options?: SnapshotQueryOptions
): stream.Readable
getSubscriptions(
options?: SubscriptionQueryOptions,
callback?: ApiCallbackFunction<SubscriptionObject[]>
): Promise<ApiPromiseResult<SubscriptionObject[]>> | void
getSubscriptionsStream(
options?: SubscriptionQueryOptions
): stream.Readable
getTopics(
options?: TopicsQueryOptions,
callback?: ApiCallbackFunction<TopicObject[]>
): Promise<ApiPromiseResult<TopicObject[]>> | void
getTopicsStream(
options?: TopicsQueryOptions
): stream.Readable
snapshot(
name: string
): any
subscribe(
topic: TopicObject | string,
subName?: stream,
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
subscription(
name?: string,
options?: SubscriptionOptions
): void
topic(
name: string
): TopicObject
}
declare module '@google-cloud/pubsub' {
export = PubSub
}
我在尝试为另一个非类型化模块编写定义时遇到了同样的问题。我发现,如果您正在为非类型化模块编写定义,您需要确保 declare module
包含整个定义文件。
例如,当我编写一个简单的测试项目并导入 @google-cloud/pubsub
模块时,为我编译了以下定义文件。不幸的是,我还没有找到任何文档来解释为什么会这样。
declare module '@google-cloud/pubsub' {
import * as stream from 'stream';
import * as events from 'events';
interface ConfigurationObject extends Object {
projectId?: string
keyFilename?: string
email?: string
credentials?: CredentialsObject
autoRetry?: boolean
maxRetries?: number
promise?: Function
}
interface CredentialsObject extends Object {
client_email?: string
private_key?: string
}
interface QueryOptions extends Object {
autoPaginate?: boolean
maxApiCalls?: number
maxResults?: number
pageSize?: number
pageToken?: string
}
interface SnapshotQueryOptions extends QueryOptions { }
interface TopicsQueryOptions extends Object { }
interface SubscriptionQueryOptions extends Object {
topic?: string
}
interface SubscribeOptions extends Object {
ackDeadlineSeconds: number
autoAck: boolean
encoding: string
interval: number
maxInProgress: number
pushEndpoint: string
timeout: number
}
interface SubscriptionOptions extends Object {
autoAck?: boolean
encoding?: string
interval?: number
maxInProgress?: number
timeout?: number
}
interface SubscriptionObject extends Object {
name: string
topic: string
pushConfig: PushConfigObject
ackDeadlineSeconds: number
}
interface PushConfigObject extends Object {
pushEndpoint: string
attributes: {
[key: string]: string
}
}
interface TopicObject extends Object {
name: string
}
interface SnapshotObject extends Object {
name: string
}
interface Message {
id: string
ackId: string
data: any
attributes: any
timestamp: number
ack(callback: Function): void
skip(): void
}
export type ApiCallbackFunction<T> = (err: Error | null, data: T, apiResponse: any) => void
export type CallbackFunction<T> = (err: Error | null, data: T) => void
export type ApiPromiseResult<T> = [T, any]
export class Subscription extends events.EventEmitter {
ack(
ackIds: string | string[],
options?: {
timeout: number
},
callback?: () => void
): Promise<void> | void
create(
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
createSnapshot(
name: string,
callback?: ApiCallbackFunction<SnapshotObject>
): Promise<ApiPromiseResult<SnapshotObject>> | void
}
export class PubSub {
constructor(
config: ConfigurationObject
)
createTopic(
name: string,
callback?: ApiCallbackFunction<TopicObject>
): Promise<ApiPromiseResult<TopicObject>> | void
getSnapshots(
options?: SnapshotQueryOptions,
callback?: CallbackFunction<SnapshotObject[]>
): Promise<any[]> | void
getSnapshotsStream(
options?: SnapshotQueryOptions
): stream.Readable
getSubscriptions(
options?: SubscriptionQueryOptions,
callback?: ApiCallbackFunction<SubscriptionObject[]>
): Promise<ApiPromiseResult<SubscriptionObject[]>> | void
getSubscriptionsStream(
options?: SubscriptionQueryOptions
): stream.Readable
getTopics(
options?: TopicsQueryOptions,
callback?: ApiCallbackFunction<TopicObject[]>
): Promise<ApiPromiseResult<TopicObject[]>> | void
getTopicsStream(
options?: TopicsQueryOptions
): stream.Readable
snapshot(
name: string
): any
subscribe(
topic: TopicObject | string,
subName?: stream,
options?: SubscribeOptions,
callback?: ApiCallbackFunction<SubscriptionObject>
): Promise<ApiPromiseResult<SubscriptionObject>> | void
subscription(
name?: string,
options?: SubscriptionOptions
): void
topic(
name: string
): TopicObject
}
}
在尝试 Pub/Sub 之后,我想出了以下概念验证代码:
index.d.ts
declare module '@google-cloud/pubsub' {
namespace pubsub {
class PubSub {
topic (name: string) : Topic;
}
class Topic {
subscribe (subscriptionName: string, options: Object, callback: Function): void;
}
}
function pubsub(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);
});
}
});
仅供参考,如果在 Node 中使用 pubsub
// Does not work
import {PubSub} from '@google-cloud/pubsub';
import * as PubSub from '@google-cloud/pubsub';
// Works
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
activity on this 有很多(?)。可能上述答案不会按预期工作。现在你需要详细的:
import PubSub = require("@google-cloud/pubsub");
const pubSub: PubSub.PubSub = new (PubSub as any).PubSub()