angular - 找不到“请求”的类型定义文件:node_modules/@looker/sdk-rtl/lib/oauthSession.d.ts:1:23 中的错误 - 错误 TS2688
问题描述
我正在尝试将 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')
}
}
解决方案
我不精通 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 实现了相同的方法,所以一切都应该像文档中描述的那样工作。
推荐阅读
- sql - 如何在不舍入或截断 0 的情况下截断 Presto 中的纬度和经度?
- reactjs - 如何使用 MaterialUI Button 调用功能组件中按钮的组件 onClick
- python - matplotlib 中颜色条上缺少结束刻度
- c# - 如何在 DI 设置期间自动验证 appSettings.json 文件中的配置值?
- postgresql - Windows 上的 Postgresql 调试器
- jsf - p:selectBooleanCheckbox 可见性属性
- python - 在python中创建一个类列表,其中包含一个类列表
- r - 如何对R中文件夹中的每个文件执行相同的功能?
- android - 为什么我的撇号没有正确显示?
- python - pandas:带有 loc[] 的 NaN 结果