2323 SOFTWARE.
2424
2525 */
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'
2833
2934export type StorageLocation = 'session' | 'local'
3035
@@ -42,7 +47,9 @@ export interface IStorageValue {
4247export class OAuthConfigProvider extends ApiSettings {
4348 constructor (
4449 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
4653 ) {
4754 super ( settings )
4855 }
@@ -108,9 +115,46 @@ export class OAuthConfigProvider extends ApiSettings {
108115 ...{
109116 base_url,
110117 looker_url,
111- client_id : 'looker.api-explorer' ,
118+ client_id : this . clientId ,
112119 redirect_uri : `${ window . location . origin } /oauth` ,
113120 } ,
114121 }
115122 }
116123}
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