Skip to content

Commit b50e8c2

Browse files
committed
feat: initSdk helper fn in @looker/extension-utils
1 parent da01692 commit b50e8c2

File tree

16 files changed

+199
-60
lines changed

16 files changed

+199
-60
lines changed

packages/api-explorer/src/StandaloneApiExplorer.tsx

+16-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626

2727
import type { FC } from 'react'
2828
import React, { useEffect, useState } from 'react'
29-
import { initRunItSdk, RunItProvider } from '@looker/run-it'
29+
import { RunItConfigKey, RunItProvider } from '@looker/run-it'
3030
import type { OAuthConfigProvider } from '@looker/extension-utils'
31-
import { OAuthScene } from '@looker/extension-utils'
31+
import { initSdk, OAuthScene } from '@looker/extension-utils'
3232
import { Provider } from 'react-redux'
3333
import { useLocation } from 'react-router'
34+
import { functionalSdk40 } from '@looker/sdk'
35+
import type { BrowserSession } from '@looker/sdk-rtl'
3436

3537
import { ApiExplorer } from './ApiExplorer'
3638
import { store } from './state'
@@ -46,8 +48,19 @@ export const StandaloneApiExplorer: FC<StandaloneApiExplorerProps> = ({
4648
headless = false,
4749
}) => {
4850
const [browserAdaptor] = useState(
49-
new ApixAdaptor(initRunItSdk(), window.origin)
51+
() =>
52+
new ApixAdaptor(
53+
initSdk({
54+
agentTag: 'RunIt 0.8',
55+
configKey: RunItConfigKey,
56+
clientId: 'looker.api-explorer',
57+
createSdkCallback: (session: BrowserSession) =>
58+
functionalSdk40(session),
59+
}),
60+
window.origin
61+
)
5062
)
63+
5164
const location = useLocation()
5265
const oauthReturn = location.pathname === `/${oAuthPath}`
5366
const sdk = browserAdaptor.sdk

packages/api-explorer/src/state/specs/sagas.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
*/
2626
import ReduxSagaTester from 'redux-saga-tester'
2727
import { registerTestEnvAdaptor } from '@looker/extension-utils'
28-
import { initRunItSdk } from '@looker/run-it'
2928
import cloneDeep from 'lodash/cloneDeep'
3029
import { ApixAdaptor } from '../../utils'
3130

3231
import { getLoadedSpecs } from '../../test-data'
32+
import { testSdk } from '../../test-utils'
3333
import { specActions, specsSlice } from './slice'
3434
import * as sagas from './sagas'
3535

3636
describe('Specs Sagas', () => {
3737
let sagaTester: ReduxSagaTester<any>
38-
const adaptor = new ApixAdaptor(initRunItSdk(), '')
38+
const adaptor = new ApixAdaptor(testSdk, '')
3939
registerTestEnvAdaptor(adaptor)
4040
const specState = getLoadedSpecs()
4141
const mockError = new Error('boom')

packages/api-explorer/src/test-utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
export * from './lodes'
2727
export * from './router'
2828
export * from './redux'
29+
export * from './sdk'

packages/api-explorer/src/test-utils/redux.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import { renderWithTheme } from '@looker/components-test-utils'
3232
import type { RenderOptions } from '@testing-library/react'
3333
import { createStore } from '@looker/redux'
3434
import { BrowserAdaptor, registerEnvAdaptor } from '@looker/extension-utils'
35-
import { initRunItSdk } from '@looker/run-it'
3635

3736
import type { LodesState, RootState, SettingState, SpecState } from '../state'
3837
import {
@@ -44,13 +43,14 @@ import {
4443
specsSlice,
4544
} from '../state'
4645
import { specState } from '../test-data'
46+
import { testSdk } from './sdk'
4747
import { renderWithRouter } from './router'
4848

4949
export const withReduxProvider = (
5050
consumers: ReactElement<any>,
5151
store: Store<RootState> = createTestStore()
5252
) => {
53-
registerEnvAdaptor(new BrowserAdaptor(initRunItSdk()))
53+
registerEnvAdaptor(new BrowserAdaptor(testSdk))
5454
return <Provider store={store}>{consumers}</Provider>
5555
}
5656

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2021 Looker Data Sciences, Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
import { initSdk } from '@looker/extension-utils'
27+
import type { BrowserSession } from '@looker/sdk-rtl'
28+
import { functionalSdk40 } from '@looker/sdk'
29+
30+
export const testSdk = initSdk({
31+
agentTag: 'RunIt',
32+
configKey: 'ConfigKey',
33+
clientId: 'looker.api-explorer',
34+
createSdkCallback: (session: BrowserSession) => functionalSdk40(session),
35+
})

packages/extension-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@looker/components": "^2.8.1",
2929
"@looker/extension-sdk": "^22.0.0",
3030
"@looker/extension-sdk-react": "^22.0.0",
31+
"@looker/sdk": "^22.0.0",
3132
"@looker/sdk-rtl": "^21.3.2",
3233
"react": "^16.13.1"
3334
},

packages/run-it/src/utils/RunItSDK.ts renamed to packages/extension-utils/src/authUtils.spec.ts

+21-29
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,25 @@
2323
SOFTWARE.
2424
2525
*/
26-
27-
import type { IApiSettings } from '@looker/sdk-rtl'
28-
import {
29-
BrowserSession,
30-
BrowserTransport,
31-
DefaultSettings,
32-
} from '@looker/sdk-rtl'
3326
import { functionalSdk40 } from '@looker/sdk'
34-
import { OAuthConfigProvider } from '@looker/extension-utils'
35-
36-
import { RunItConfigKey } from '../components'
37-
38-
export const initRunItSdk = () => {
39-
const settings = {
40-
...DefaultSettings(),
41-
base_url: 'https://self-signed.looker.com:19999',
42-
agentTag: 'RunIt 0.8',
43-
} as IApiSettings
44-
45-
const options = new OAuthConfigProvider(settings, RunItConfigKey)
46-
const transport = new BrowserTransport(options)
47-
const session = new BrowserSession(options, transport)
48-
const sdk = functionalSdk40(session)
49-
BrowserTransport.trackPerformance = true
50-
return sdk
51-
}
52-
53-
export enum StoreConstants {
54-
LOCALSTORAGE_SETTINGS_KEY = 'settings',
55-
}
27+
import type { BrowserSession } from '@looker/sdk-rtl'
28+
29+
import type { InitSdkSettings } from './authUtils'
30+
import { initSdk } from './authUtils'
31+
32+
describe('Auth utilities', () => {
33+
describe('initSdk', () => {
34+
it('should initialize with provided settings', () => {
35+
const settings: InitSdkSettings = {
36+
agentTag: 'RunIt',
37+
configKey: 'RunItConfigKey',
38+
clientId: 'looker.api-explorer',
39+
createSdkCallback: (session: BrowserSession) =>
40+
functionalSdk40(session),
41+
trackPerformance: false,
42+
}
43+
const actual = initSdk(settings)
44+
expect(actual.authSession.settings.agentTag).toEqual(settings.agentTag)
45+
})
46+
})
47+
})

packages/extension-utils/src/authUtils.ts

+48-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
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

2934
export type StorageLocation = 'session' | 'local'
3035

@@ -42,7 +47,9 @@ export interface IStorageValue {
4247
export 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+
}

packages/run-it/src/RunIt.spec.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ import type { ApiModel, IMethod } from '@looker/sdk-codegen'
3333
import { BrowserAdaptor, OAuthConfigProvider } from '@looker/extension-utils'
3434
import { RunIt } from './RunIt'
3535
import { api, testTextResponse } from './test-data'
36-
import { initRunItSdk } from './utils'
36+
import { testSdk as sdk } from './test-utils'
3737
import { RunItProvider } from './RunItProvider'
3838

3939
describe('RunIt', () => {
4040
const run = 'Run'
41-
const sdk = initRunItSdk()
4241
const adaptor = new BrowserAdaptor(sdk)
4342

4443
const renderRunIt = (

packages/run-it/src/components/ConfigForm/ConfigForm.spec.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ import { renderWithTheme } from '@looker/components-test-utils'
3030
import userEvent from '@testing-library/user-event'
3131
import { BrowserAdaptor, registerTestEnvAdaptor } from '@looker/extension-utils'
3232

33-
import { initRunItSdk } from '../..'
33+
import { testSdk } from '../../test-utils'
3434
import { ConfigForm, RunItConfigKey } from '.'
3535

3636
describe('ConfigForm', () => {
37-
const adaptor = new BrowserAdaptor(initRunItSdk())
37+
const adaptor = new BrowserAdaptor(testSdk)
3838
registerTestEnvAdaptor(adaptor)
3939

4040
const apiLabel = /API server URL/i

packages/run-it/src/components/LoginForm/LoginForm.spec.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ import {
3434
OAuthConfigProvider,
3535
} from '@looker/extension-utils'
3636

37-
import { initRunItSdk, readyToLogin } from '../..'
37+
import { readyToLogin } from '../..'
38+
import { testSdk } from '../../test-utils'
3839
import { LoginForm, notReadyToLogin } from './LoginForm'
3940

4041
describe('LoginForm', () => {
41-
const sdk = initRunItSdk()
42-
registerTestEnvAdaptor(new BrowserAdaptor(sdk))
42+
registerTestEnvAdaptor(new BrowserAdaptor(testSdk))
4343

4444
test('it renders a login form with instructions if auth is not configured', async () => {
4545
renderWithTheme(<LoginForm requestContent={{}} />)

packages/run-it/src/utils/RunItSDK.spec.ts renamed to packages/run-it/src/test-utils/index.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,4 @@
2323
SOFTWARE.
2424
2525
*/
26-
27-
import { initRunItSdk } from '@looker/run-it'
28-
29-
describe('RunItSDK', () => {
30-
it('should keep agentTag', () => {
31-
const actual = initRunItSdk()
32-
expect(actual.authSession.settings.agentTag).toContain('RunIt')
33-
})
34-
})
26+
export { testSdk } from './sdk'

packages/run-it/src/test-utils/sdk.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2021 Looker Data Sciences, Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
import { initSdk } from '@looker/extension-utils'
28+
import type { BrowserSession } from '@looker/sdk-rtl'
29+
import { functionalSdk40 } from '@looker/sdk'
30+
31+
export const testSdk = initSdk({
32+
agentTag: 'RunIt',
33+
configKey: 'ConfigKey',
34+
clientId: 'looker.api-explorer',
35+
createSdkCallback: (session: BrowserSession) => functionalSdk40(session),
36+
})

packages/run-it/src/utils/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
SOFTWARE.
2424
2525
*/
26-
export * from './RunItSDK'
2726
export * from './requestUtils'
27+
export * from './settings'

packages/run-it/src/utils/requestUtils.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@
2525
*/
2626
import type { RunItInput } from '../RunIt'
2727
import { testJsonResponse, api } from '../test-data'
28+
import { testSdk as sdk } from '../test-utils'
2829
import {
2930
createRequestParams,
3031
pathify,
3132
runRequest,
3233
createInputs,
3334
initRequestContent,
3435
} from './requestUtils'
35-
import { initRunItSdk } from './RunItSDK'
36-
37-
const sdk = initRunItSdk()
3836

3937
describe('requestUtils', () => {
4038
afterEach(() => {

0 commit comments

Comments
 (0)