23
23
SOFTWARE.
24
24
25
25
*/
26
- import type { IApiSection , IApiSettings } from '@looker/sdk-rtl'
27
- import { ApiSettings } from '@looker/sdk-rtl'
26
+ import type { APIMethods , IApiSection , IApiSettings } from '@looker/sdk-rtl'
27
+ import {
28
+ ApiSettings ,
29
+ DefaultSettings ,
30
+ BrowserTransport ,
31
+ BrowserSession ,
32
+ } from '@looker/sdk-rtl'
28
33
29
34
export type StorageLocation = 'session' | 'local'
30
35
@@ -42,7 +47,9 @@ export interface IStorageValue {
42
47
export class OAuthConfigProvider extends ApiSettings {
43
48
constructor (
44
49
settings : Partial < IApiSettings > ,
45
- private readonly configKey : string
50
+ /** local storage key for retrieving auth config */
51
+ private readonly configKey : string ,
52
+ private readonly clientId : string
46
53
) {
47
54
super ( settings )
48
55
}
@@ -108,9 +115,46 @@ export class OAuthConfigProvider extends ApiSettings {
108
115
...{
109
116
base_url,
110
117
looker_url,
111
- client_id : 'looker.api-explorer' ,
118
+ client_id : this . clientId ,
112
119
redirect_uri : `${ window . location . origin } /oauth` ,
113
120
} ,
114
121
}
115
122
}
116
123
}
124
+
125
+ export interface InitSdkSettings {
126
+ /** agent tag to use for the SDK requests */
127
+ agentTag : string
128
+ /** Local storage key for retrieving auth configuration settings */
129
+ configKey : string
130
+ /** Id used to register this application on the Looker server */
131
+ clientId : string
132
+ /** A callback function that returns an sdk instance */
133
+ createSdkCallback : ( session : BrowserSession ) => APIMethods
134
+ /** Track request performance if the browser has the necessary APIs. Defaults to false. */
135
+ trackPerformance ?: boolean
136
+ }
137
+
138
+ /**
139
+ * Initializes an sdk with provided settings
140
+ * @returns An sdk instance
141
+ */
142
+ export const initSdk = ( initSettings : InitSdkSettings ) => {
143
+ const settings = {
144
+ ...DefaultSettings ( ) ,
145
+ /** Albeit not used, this has to be set otherwise ApiSettings throws */
146
+ base_url : 'https://localhost:8080' ,
147
+ agentTag : initSettings . agentTag ,
148
+ } as IApiSettings
149
+
150
+ const options = new OAuthConfigProvider (
151
+ settings ,
152
+ initSettings . configKey ,
153
+ initSettings . clientId
154
+ )
155
+ const transport = new BrowserTransport ( options )
156
+ const session = new BrowserSession ( options , transport )
157
+ const sdk = initSettings . createSdkCallback ( session )
158
+ BrowserTransport . trackPerformance = initSettings . trackPerformance ?? false
159
+ return sdk
160
+ }
0 commit comments