Typescript import @ google-cloud / pubsub [英] Typescript import @google-cloud/pubsub
问题描述
这个项目没有自己的声明或@types声明,所以我创建了自己的声明为模块。但是,当我在声明文件中声明模块时,出现以下错误:
扩充中的模块名称无效。模块'@ google-cloud / pubsub'
解析为
'./node_modules/@google-cloud/pubsub/src/index.js'的无类型模块,它不能被
增强。
我使用TypeScript 2.2.2
这里是完整的声明文件:
从'stream'导入流
从'events'导入事件
interface ConfigurationObject extends Object {
projectId?:string
keyFilename ?: string
email ?: string
credentials ?: CredentialsObject $ b $ autoRetry?:boolean
maxRetries?:number
promise ?:函数
}
接口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 {}
$ b interface SubscriptionQueryOptions extends Object {
topic ?: string
}
interface SubscribeOptions extends Object {
ackDeadlineSeconds:number
autoAck:boolean
encoding:string
interval:number
maxInProgress:number
pushEndpoint:string
timeout:number
}
接口SubscriptionOptions extends Object {
autoAck ?:布尔
编码?:字符串
interval? :数字
maxInProgress?:数字
超时数字::数字
}
接口SubscriptionObject扩展对象{
名称:字符串
主题:字符串
pushConfig:PushConfigObject
ackDeadlineSeconds:number
}
接口PushConfigObject extends Object {
pushEndpoint:string
attributes:{
[key:string]:str
接口TopicObject extends Object {
名称:字符串
}
接口SnapshotObject extends Object {
name:string
}
接口消息{
id:string
ckId:string
data:any
attributes:any
timestamp:number
ack(callback:Function):void
skip():void
}
声明类型ApiCallbackFunction< T> ; =(err:Error | null,data:T,apiResponse:any)=> void
声明类型CallbackFunction< T> =(err:Error | null,data:T)=> void
声明类型ApiPromiseResult< T> = [T,any]
declare class Subscription扩展events.EventEmitter {
ack(
ckIds: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 {
构造函数(
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,
回叫?:ApiCallbackFunction< SubscriptionObject []>
):Promise< ApiPromiseResult< SubscriptionObject []>> | void
getSubscriptionsStream(
options ?: SubscriptionQueryOptions
):stream.Readable
getTopics(
options ?: TopicsQueryOptions,
回调?:ApiCallbackFunction< TopicObject []>
):Promise< ApiPromiseResult< TopicObject []>> | void
getTopicsStream(
options ?: TopicsQueryOptions
):stream.Readable
快照(
名称:string
) :任何
订阅(
主题:TopicObject |字符串,
subName?:流,
选项?:SubscribeOptions,
回调?:ApiCallbackFunction< SubscriptionObject> ;
):Promise< ApiPromiseResult< SubscriptionObject>> |无效
订阅(
名称?:字符串,
选项?:SubscriptionOptions
):无效
主题(
名称:string
):TopicObject
}
declare module'@ google-cloud / pubsub'{
export = PubSub
}
当我试图编写定义时遇到同样的问题另一个无类型模块。我发现如果你正在编写无类型模块的定义,你需要确保 declare module
包含整个定义文件。
因此,例如,当我编写简单的测试项目并导入 @ google-cloud / pubsub
模块时,为我编译的以下定义文件。不幸的是,我还没有找到任何文件解释为什么这会起作用。
申报模块'@ google-cloud / pubsub'{
从'stream'中导入*;
导入*作为来自'events'的事件;
interface ConfigurationObject extends Object {
projectId?:string
keyFilename ?: string
email ?: string
credentials ?: CredentialsObject
autoRetry ?:boolean
maxRetries?:number
promise ?:函数
}
接口CredentialsObject扩展对象{
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 $ b $ autoAck:boolean
encoding:string
interval:number
maxInProgress:number
pushEndpoint:string
timeout:number
}
interface SubscriptionOptions extends Object {
autoAck?:布尔
编码?:字符串
间隔?:数字
maxInProgress?:数字
超时?:数字
}
interface SubscriptionObject extends Object {
name:string
topic:string
pushConfig:PushConfigObject
ackDeadlineSeconds:number
}
接口PushConfigObject extends Object Object {
pushEndpoint:string
attributes:{
[key:string]:string
}
}
接口TopicObject extends Object {
名称:字符串
}
接口SnapshotObject扩展对象{
名称:字符串
}
接口消息{
id:string
ckId:string
数据:任何
属性:任何
时间戳:数字
ack(callback:Function):void
skip():void
}
导出类型ApiCallbackFunction< T> =(err:Error | null,data:T,apiResponse:any)=> void
导出类型CallbackFunction< T> =(err:Error | null,data:T)=> void
导出类型ApiPromiseResult< T> = [T,任意]
导出类Subscription扩展events.EventEmitter {
ack(
ckIds: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>> |无效
}
导出类PubSub {
构造函数(
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,
回叫?:ApiCallbackFunction< SubscriptionObject []>
):Promise< ApiPromiseResult< SubscriptionObject []>> | void
getSubscriptionsStream(
options ?: SubscriptionQueryOptions
):stream.Readable
getTopics(
options ?: TopicsQueryOptions,
回调?:ApiCallbackFunction< TopicObject []>
):Promise< ApiPromiseResult< TopicObject []>> | void
getTopicsStream(
options ?: TopicsQueryOptions
):stream.Readable
快照(
名称:string
) :任何
订阅(
主题:TopicObject |字符串,
subName?:流,
选项?:SubscribeOptions,
回调?:ApiCallbackFunction< SubscriptionObject> ;
):Promise< ApiPromiseResult< SubscriptionObject>> |无效
订阅(
名称?:字符串,
选项?:SubscriptionOptions
):无效
主题(
名称:string
):TopicObject
}
}
与Pub / Sub有点相似,我提出了以下概念验证码:
index.d.ts
declare module'@ google-cloud / pubsub'{
namespace pubsub {
class PubSub {
topic (名称:字符串):主题;
}
class主题{
subscribe(subscriptionName:string,options:Object,callback:Function):void;
}
}
函数pubsub(options:any):pubsub.PubSub;
export = pubsub;
}
subscribe.ts
import *作为'@ google-cloud / pubsub'的pubsub;
let ps = pubsub({
projectId:'project-id',
keyFilename:'key.json'
});
console.log('订阅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);
});
}
});
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 module. But when I declare the module in the declaration file, I get the following error:
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.
I'm using TypeScript 2.2.2
Here is the complete declaration file:
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
}
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.
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
}
}
After playing around with the Pub/Sub a bit I came up with the following proof of concept code:
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);
});
}
});
这篇关于Typescript import @ google-cloud / pubsub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!