1

我正在尝试将 Looker Embedded Client SDK 与 Angular 集成,因此已安装 @looker/SDK 并按照官方 npm 页面上提供的指导方针配置设置,以便 @looker/sdk 访问 API。

但是当我尝试访问一些方法和类时,角度应用程序开始失败。外观
错误:

ERROR in node_modules/@looker/sdk-rtl/lib/oauthSession.d.ts:1:23 - error TS2688: Cannot find type definition file for 'request'.

1 /// <reference types="request" />

和配置:

// package.json 

{ 
   //package.json
   
 "dependencies": {
    "@angular/core": "~10.2.3",
    "@looker/sdk": "^7.20.3",
    // few other libs
  },
  "devDependencies": {
   // few other libs  
  "@angular/cli": "~10.2.0",
     "typescript": "~4.0.5"
  },
 
//tsconfig.json
"compilerOptions": {
  // few other settings
  "module": "es2020",
  "target": "es5",
   "lib": [
    "es2020",
    "dom"
  ],
 

/ /And in component file:

import { LookerNodeSDK } from '@looker/sdk/lib/node'

@Component({
  selector: 'rs-reports',
  templateUrl: './reports.component.html',
  providers: [ReportsService]
})
export class ReportsComponent implements OnInit {

  constructor() {
  }
  ngOnInit() {
    this.getLookerConfig()
      .then(response => {
        console.log(response, 'Success')
      }).catch(error => {
      console.log(error, 'Error')
    });
  }

  async getLookerConfig() {
    // create a Node SDK object for API 3.1
    const sdk = LookerNodeSDK.init31();
    // retrieve your user account to verify correct credentials
    const me = await sdk.ok(sdk.me(
      "id, first_name, last_name, display_name, email, personal_space_id, home_space_id, group_ids, role_ids"))
    console.log({me})
    // make any other calls to the Looker SDK
    const dashboards = await sdk.ok(
      sdk.search_dashboards({title: 'My SDK dashboard'})
    )
    if (dashboards.length === 0) {
      console.log('Dashboard not found')
    }


    await sdk.authSession.logout()
    if (!sdk.authSession.isAuthenticated()) {
      console.log('Logout successful')
    }
  }


4

1 回答 1

0

我不精通 Angular,但我认为这个错误是一个红鲱鱼。您正在尝试在 AngularLookerNodeSDK应用程序中使用 Looker SDK ( ) 的 Node 版本,这可能不会很好地工作。NodeSDK 用于 Node.js 后端,我们提供了一个LookerBrowserSDK用于前端应用程序。

不幸的是,这不是很好的文档(我将尝试改进它),但我敢打赌,如果您尝试使用 browserSDK,您会获得更大的成功。

import { Looker31SDK, LookerBrowserSDK } from '@looker/sdk/lib/browser'

就像我说的,我真的不了解 Angular,但是如果你将它放在 ngOnInit 中,这里有一些 Javascript 应该会更好地工作。browserSDK 和 nodeSDK 实现了相同的方法,所以一切都应该像文档中描述的那样工作。唯一需要注意的重要区别是 SDK 的初始化不同,不能传递 settings.ini 文件或任何东西。如果您不想使用 Oauth,可以创建自定义配置阅读器并将其传递给该阅读器。一探究竟:

import { Looker31SDK, LookerBrowserSDK } from '@looker/sdk/lib/browser'
import { ApiSettings, IApiSettings, IApiSection } from '@looker/sdk-rtl'

/**
 * @class CustomConfigReader
 *
 * A custom configuration reader that overrides the readConfig() method
 * in NodeSettings to allow fetching client_id and client_secret
 * from anywhere.
 */
class CustomConfigReader extends ApiSettings {
  constructor(settings: IApiSettings) {
    super(settings)
  }

  /**
   * @returns an IApiSection object containing client_id and client_secret
   */
  readConfig(): IApiSection {
    return {
      client_id: 'clientId',
      client_secret: 'clientSecret',
    }
  }
}

;(async () => {
  const sdk = LookerBrowserSDK.init31(
    new CustomConfigReader({
      base_url: 'https://<your-looker-server>:19999 or https://your.cloud.looker.com',
    } as IApiSettings)
  )

  const me = await sdk.ok(
    sdk.me(
      'id, first_name, last_name, display_name, email, personal_space_id, home_space_id, group_ids, role_ids'
    )
  )
  console.log({ me })
})()

然后实现你的功能。browserSDK 和 nodeSDK 实现了相同的方法,所以一切都应该像文档中描述的那样工作。

于 2021-01-26T20:46:49.227 回答