在Typescript函数项目中添加@ google-cloud/logging会出错 [英] Adding @google-cloud/logging in typescript functions project gives error

查看:180
本文介绍了在Typescript函数项目中添加@ google-cloud/logging会出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于其他具有相同问题的人-在类正文中将const Logging: any = require('@google-cloud/logging')var logger = Logging()一起使用就像一个魅力!

for others with the same problem - using const Logging: any = require('@google-cloud/logging') together with var logger = Logging() in the body of the class worked like a charm!

请记住使用var logger = Logging()实例化记录器库.否则,您仍然会得到 logger.log不是函数

Remember to use var logger = Logging() to instantiate the logger library. Otherwise you will still get the logger.log is not a function!

原始帖子

我有一个用打字稿写的firebase函数项目-用webpack编译.我目前正在尝试实现@ google-cloud/logging库,但是遇到了问题.出来

I've got a firebase functions project, written in typescript - transpiled with webpack. I'm currently trying to implement the @google-cloud/logging library, but I'm having issues. It comes out saying

找不到模块"google-cloud/logging"的声明文件.尝试npm install @types/@google-cloud/logging(如果存在)或添加包含declare module '@google-cloud/logging';

Could not find a declaration file for module "google-cloud/logging". Try npm install @types/@google-cloud/logging if it exists or add a new declaration (.d.ts) file containing declare module '@google-cloud/logging';

我尝试通过以下方式添加库:

I've tried adding the library the following ways:

import * as Logging from '@google-cloud/logging';
import * as logging from '@google-cloud/logging';
import { Logging } from '@google-cloud/logging';

但是我仍然收到此错误.尝试运行使用记录"功能的结果为

But I'm still getting this error. Trying to run a function which is using "logging" results in

logging.log不是函数

logging.log is not a function

我什至尝试了要求库的javascript方法,但仍然没有成功.

I have even tried the javascript way of requiring the library, still with no success.

我的tsconfig.json:

My tsconfig.json:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "outDir": "./",
        "noImplicitAny": true
    },
    "include": [
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

我已经读到有些人通过手动向项目中添加"d.ts"文件而取得了成功,尽管我不太了解.这是堆栈溢出中文章的链接-在打字稿中导入非打字稿模块

I've read that some people had success by adding a "d.ts" file to the project manually, though I did not understand much of it. Here is the link to the article in stack overflow - Importing non-typescript module in typescript

我应该如何在打字稿项目中添加库?

How might I go about adding the library in a typescript project?

如有需要,我可以提供更多详细信息.这就是我想到的.

I can provide more details if needed. This was what I could think of including.

推荐答案

@google-cloud/logging尚无类型定义.所以你需要提供一些! 在此期间,您可以

There is not yet type definitions for @google-cloud/logging. So you need to provide some ! In the meantime, you can do

const Logging: any = require('@google-cloud/logging')

如果已安装@types/node并且目标nodejs ,或者如果目标是浏览器但使用"moduleResolution": "CommonJS"(还需要提供节点typefs).

If you have @types/node installed and target nodejs or if you target the browser but use "moduleResolution": "CommonJS" (you also need to provide node typefs).

否则,您可以使用

import * as Logging from '@google-cloud/logging'

但是在这种情况下,您需要声明此模块的类型

But in that case you need to declare the types for this module

// logging.d.ts
declare module '@google-cloud/logging' {

  interface LogConfig {
    removeCircular: boolean
  }

  class Entry {
    metadata: object
    data: object
    constructor (metadata: object | null | undefined, data: object | string)
    constructor (data: object | string)
    toJSON (options?: LogConfig): any
  }

  interface WriteOptions {
    gaxOptions: object
    labels: object[]
    resource: object
  }

  type LogWriteCallback = (err: Error | null, apiResponse: object) => void
  type DeleteLogCallback = (err: Error | null, apiResponse: object) => void

  type LogWriteResponse = object[]
  type DeleteLogResponse = object[]

  type EntryArg = Entry | Entry[]

  class Log {
    constructor (logging: Logging, name: string, options: LogConfig)
    alert (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    critical (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    debug (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    emergency (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    info (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    notice (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    warning (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    error (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    write (entry: EntryArg, options?: WriteOptions, callback?: LogWriteCallback): Promise<LogWriteResponse>
    delete (gaxOptions: object): Promise<DeleteLogResponse>
    delete (gaxOptions: object, callback?: DeleteLogCallback): Promise<DeleteLogResponse>
    delete (callback?: DeleteLogCallback): Promise<DeleteLogResponse>
  }

  interface ClientConfig {
    projectId?: string
    keyFilename?: string
    email?: string
    credentials?: {
      client_email: string
      private_key: string
    }
    autoRetry?: boolean
    maxRetries?: number
    promise?: Function
  }

  class Logging {
    constructor (options: ClientConfig)
    log (name: string, options?: LogConfig): Log
    entry (resource: object | string | null | undefined, data: object | string): Entry
  }
  export = Logging
}

此定义只是草稿,缺少许多功能,但我想这是必要的第一步:-)

This definition is just a draft and a lot of functions are missing, but I guess it's a necessary first step :-)

这篇关于在Typescript函数项目中添加@ google-cloud/logging会出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆