diff --git a/packages/api-explorer/src/StandaloneApiExplorer.tsx b/packages/api-explorer/src/StandaloneApiExplorer.tsx index 26b3548f0..c9e43c270 100644 --- a/packages/api-explorer/src/StandaloneApiExplorer.tsx +++ b/packages/api-explorer/src/StandaloneApiExplorer.tsx @@ -26,11 +26,13 @@ import type { FC } from 'react' import React, { useEffect, useState } from 'react' -import { initRunItSdk, RunItProvider } from '@looker/run-it' +import { RunItConfigKey, RunItProvider } from '@looker/run-it' import type { OAuthConfigProvider } from '@looker/extension-utils' -import { OAuthScene } from '@looker/extension-utils' +import { initSdk, OAuthScene } from '@looker/extension-utils' import { Provider } from 'react-redux' import { useLocation } from 'react-router' +import { functionalSdk40 } from '@looker/sdk' +import type { BrowserSession } from '@looker/sdk-rtl' import { ApiExplorer } from './ApiExplorer' import { store } from './state' @@ -46,8 +48,19 @@ export const StandaloneApiExplorer: FC = ({ headless = false, }) => { const [browserAdaptor] = useState( - new ApixAdaptor(initRunItSdk(), window.origin) + () => + new ApixAdaptor( + initSdk({ + agentTag: 'RunIt 0.8', + configKey: RunItConfigKey, + clientId: 'looker.api-explorer', + createSdkCallback: (session: BrowserSession) => + functionalSdk40(session), + }), + window.origin + ) ) + const location = useLocation() const oauthReturn = location.pathname === `/${oAuthPath}` const sdk = browserAdaptor.sdk diff --git a/packages/api-explorer/src/state/specs/sagas.spec.ts b/packages/api-explorer/src/state/specs/sagas.spec.ts index 4274c2029..d3f9fcd9d 100644 --- a/packages/api-explorer/src/state/specs/sagas.spec.ts +++ b/packages/api-explorer/src/state/specs/sagas.spec.ts @@ -25,17 +25,17 @@ */ import ReduxSagaTester from 'redux-saga-tester' import { registerTestEnvAdaptor } from '@looker/extension-utils' -import { initRunItSdk } from '@looker/run-it' import cloneDeep from 'lodash/cloneDeep' import { ApixAdaptor } from '../../utils' import { getLoadedSpecs } from '../../test-data' +import { testSdk } from '../../test-utils' import { specActions, specsSlice } from './slice' import * as sagas from './sagas' describe('Specs Sagas', () => { let sagaTester: ReduxSagaTester - const adaptor = new ApixAdaptor(initRunItSdk(), '') + const adaptor = new ApixAdaptor(testSdk, '') registerTestEnvAdaptor(adaptor) const specState = getLoadedSpecs() const mockError = new Error('boom') diff --git a/packages/api-explorer/src/test-utils/index.ts b/packages/api-explorer/src/test-utils/index.ts index 6e421dbf5..f77720813 100644 --- a/packages/api-explorer/src/test-utils/index.ts +++ b/packages/api-explorer/src/test-utils/index.ts @@ -26,3 +26,4 @@ export * from './lodes' export * from './router' export * from './redux' +export * from './sdk' diff --git a/packages/api-explorer/src/test-utils/redux.tsx b/packages/api-explorer/src/test-utils/redux.tsx index f0f6023a8..c52948f92 100644 --- a/packages/api-explorer/src/test-utils/redux.tsx +++ b/packages/api-explorer/src/test-utils/redux.tsx @@ -32,7 +32,6 @@ import { renderWithTheme } from '@looker/components-test-utils' import type { RenderOptions } from '@testing-library/react' import { createStore } from '@looker/redux' import { BrowserAdaptor, registerEnvAdaptor } from '@looker/extension-utils' -import { initRunItSdk } from '@looker/run-it' import type { LodesState, RootState, SettingState, SpecState } from '../state' import { @@ -44,13 +43,14 @@ import { specsSlice, } from '../state' import { specState } from '../test-data' +import { testSdk } from './sdk' import { renderWithRouter } from './router' export const withReduxProvider = ( consumers: ReactElement, store: Store = createTestStore() ) => { - registerEnvAdaptor(new BrowserAdaptor(initRunItSdk())) + registerEnvAdaptor(new BrowserAdaptor(testSdk)) return {consumers} } diff --git a/packages/api-explorer/src/test-utils/sdk.ts b/packages/api-explorer/src/test-utils/sdk.ts new file mode 100644 index 000000000..331f9d991 --- /dev/null +++ b/packages/api-explorer/src/test-utils/sdk.ts @@ -0,0 +1,35 @@ +/* + + MIT License + + Copyright (c) 2021 Looker Data Sciences, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + */ +import { initSdk } from '@looker/extension-utils' +import type { BrowserSession } from '@looker/sdk-rtl' +import { functionalSdk40 } from '@looker/sdk' + +export const testSdk = initSdk({ + agentTag: 'RunIt', + configKey: 'ConfigKey', + clientId: 'looker.api-explorer', + createSdkCallback: (session: BrowserSession) => functionalSdk40(session), +}) diff --git a/packages/extension-utils/package.json b/packages/extension-utils/package.json index 6b4b5154c..5674c61f8 100644 --- a/packages/extension-utils/package.json +++ b/packages/extension-utils/package.json @@ -28,6 +28,7 @@ "@looker/components": "^2.8.1", "@looker/extension-sdk": "^22.0.0", "@looker/extension-sdk-react": "^22.0.0", + "@looker/sdk": "^22.0.0", "@looker/sdk-rtl": "^21.3.2", "react": "^16.13.1" }, diff --git a/packages/run-it/src/utils/RunItSDK.ts b/packages/extension-utils/src/authUtils.spec.ts similarity index 60% rename from packages/run-it/src/utils/RunItSDK.ts rename to packages/extension-utils/src/authUtils.spec.ts index 4c2d44f34..543d8b0e8 100644 --- a/packages/run-it/src/utils/RunItSDK.ts +++ b/packages/extension-utils/src/authUtils.spec.ts @@ -23,33 +23,25 @@ SOFTWARE. */ - -import type { IApiSettings } from '@looker/sdk-rtl' -import { - BrowserSession, - BrowserTransport, - DefaultSettings, -} from '@looker/sdk-rtl' import { functionalSdk40 } from '@looker/sdk' -import { OAuthConfigProvider } from '@looker/extension-utils' - -import { RunItConfigKey } from '../components' - -export const initRunItSdk = () => { - const settings = { - ...DefaultSettings(), - base_url: 'https://self-signed.looker.com:19999', - agentTag: 'RunIt 0.8', - } as IApiSettings - - const options = new OAuthConfigProvider(settings, RunItConfigKey) - const transport = new BrowserTransport(options) - const session = new BrowserSession(options, transport) - const sdk = functionalSdk40(session) - BrowserTransport.trackPerformance = true - return sdk -} - -export enum StoreConstants { - LOCALSTORAGE_SETTINGS_KEY = 'settings', -} +import type { BrowserSession } from '@looker/sdk-rtl' + +import type { InitSdkSettings } from './authUtils' +import { initSdk } from './authUtils' + +describe('Auth utilities', () => { + describe('initSdk', () => { + it('should initialize with provided settings', () => { + const settings: InitSdkSettings = { + agentTag: 'RunIt', + configKey: 'RunItConfigKey', + clientId: 'looker.api-explorer', + createSdkCallback: (session: BrowserSession) => + functionalSdk40(session), + trackPerformance: false, + } + const actual = initSdk(settings) + expect(actual.authSession.settings.agentTag).toEqual(settings.agentTag) + }) + }) +}) diff --git a/packages/extension-utils/src/authUtils.ts b/packages/extension-utils/src/authUtils.ts index 8bf755892..f697143bc 100644 --- a/packages/extension-utils/src/authUtils.ts +++ b/packages/extension-utils/src/authUtils.ts @@ -23,8 +23,13 @@ SOFTWARE. */ -import type { IApiSection, IApiSettings } from '@looker/sdk-rtl' -import { ApiSettings } from '@looker/sdk-rtl' +import type { APIMethods, IApiSection, IApiSettings } from '@looker/sdk-rtl' +import { + ApiSettings, + DefaultSettings, + BrowserTransport, + BrowserSession, +} from '@looker/sdk-rtl' export type StorageLocation = 'session' | 'local' @@ -42,7 +47,9 @@ export interface IStorageValue { export class OAuthConfigProvider extends ApiSettings { constructor( settings: Partial, - private readonly configKey: string + /** local storage key for retrieving auth config */ + private readonly configKey: string, + private readonly clientId: string ) { super(settings) } @@ -108,9 +115,46 @@ export class OAuthConfigProvider extends ApiSettings { ...{ base_url, looker_url, - client_id: 'looker.api-explorer', + client_id: this.clientId, redirect_uri: `${window.location.origin}/oauth`, }, } } } + +export interface InitSdkSettings { + /** agent tag to use for the SDK requests */ + agentTag: string + /** Local storage key for retrieving auth configuration settings */ + configKey: string + /** Id used to register this application on the Looker server */ + clientId: string + /** A callback function that returns an sdk instance */ + createSdkCallback: (session: BrowserSession) => APIMethods + /** Track request performance if the browser has the necessary APIs. Defaults to false. */ + trackPerformance?: boolean +} + +/** + * Initializes an sdk with provided settings + * @returns An sdk instance + */ +export const initSdk = (initSettings: InitSdkSettings) => { + const settings = { + ...DefaultSettings(), + /** Albeit not used, this has to be set otherwise ApiSettings throws */ + base_url: 'https://localhost:8080', + agentTag: initSettings.agentTag, + } as IApiSettings + + const options = new OAuthConfigProvider( + settings, + initSettings.configKey, + initSettings.clientId + ) + const transport = new BrowserTransport(options) + const session = new BrowserSession(options, transport) + const sdk = initSettings.createSdkCallback(session) + BrowserTransport.trackPerformance = initSettings.trackPerformance ?? false + return sdk +} diff --git a/packages/run-it/src/RunIt.spec.tsx b/packages/run-it/src/RunIt.spec.tsx index 331f63faf..b4e9cdebd 100644 --- a/packages/run-it/src/RunIt.spec.tsx +++ b/packages/run-it/src/RunIt.spec.tsx @@ -33,12 +33,11 @@ import type { ApiModel, IMethod } from '@looker/sdk-codegen' import { BrowserAdaptor, OAuthConfigProvider } from '@looker/extension-utils' import { RunIt } from './RunIt' import { api, testTextResponse } from './test-data' -import { initRunItSdk } from './utils' +import { testSdk as sdk } from './test-utils' import { RunItProvider } from './RunItProvider' describe('RunIt', () => { const run = 'Run' - const sdk = initRunItSdk() const adaptor = new BrowserAdaptor(sdk) const renderRunIt = ( diff --git a/packages/run-it/src/components/ConfigForm/ConfigForm.spec.tsx b/packages/run-it/src/components/ConfigForm/ConfigForm.spec.tsx index 320068726..4477978e4 100644 --- a/packages/run-it/src/components/ConfigForm/ConfigForm.spec.tsx +++ b/packages/run-it/src/components/ConfigForm/ConfigForm.spec.tsx @@ -30,11 +30,11 @@ import { renderWithTheme } from '@looker/components-test-utils' import userEvent from '@testing-library/user-event' import { BrowserAdaptor, registerTestEnvAdaptor } from '@looker/extension-utils' -import { initRunItSdk } from '../..' +import { testSdk } from '../../test-utils' import { ConfigForm, RunItConfigKey } from '.' describe('ConfigForm', () => { - const adaptor = new BrowserAdaptor(initRunItSdk()) + const adaptor = new BrowserAdaptor(testSdk) registerTestEnvAdaptor(adaptor) const apiLabel = /API server URL/i diff --git a/packages/run-it/src/components/LoginForm/LoginForm.spec.tsx b/packages/run-it/src/components/LoginForm/LoginForm.spec.tsx index 2a611976c..57a95c663 100644 --- a/packages/run-it/src/components/LoginForm/LoginForm.spec.tsx +++ b/packages/run-it/src/components/LoginForm/LoginForm.spec.tsx @@ -34,12 +34,12 @@ import { OAuthConfigProvider, } from '@looker/extension-utils' -import { initRunItSdk, readyToLogin } from '../..' +import { readyToLogin } from '../..' +import { testSdk } from '../../test-utils' import { LoginForm, notReadyToLogin } from './LoginForm' describe('LoginForm', () => { - const sdk = initRunItSdk() - registerTestEnvAdaptor(new BrowserAdaptor(sdk)) + registerTestEnvAdaptor(new BrowserAdaptor(testSdk)) test('it renders a login form with instructions if auth is not configured', async () => { renderWithTheme() diff --git a/packages/run-it/src/utils/RunItSDK.spec.ts b/packages/run-it/src/test-utils/index.ts similarity index 83% rename from packages/run-it/src/utils/RunItSDK.spec.ts rename to packages/run-it/src/test-utils/index.ts index 8c7ee78f5..6e4c48894 100644 --- a/packages/run-it/src/utils/RunItSDK.spec.ts +++ b/packages/run-it/src/test-utils/index.ts @@ -23,12 +23,4 @@ SOFTWARE. */ - -import { initRunItSdk } from '@looker/run-it' - -describe('RunItSDK', () => { - it('should keep agentTag', () => { - const actual = initRunItSdk() - expect(actual.authSession.settings.agentTag).toContain('RunIt') - }) -}) +export { testSdk } from './sdk' diff --git a/packages/run-it/src/test-utils/sdk.ts b/packages/run-it/src/test-utils/sdk.ts new file mode 100644 index 000000000..bd17eff44 --- /dev/null +++ b/packages/run-it/src/test-utils/sdk.ts @@ -0,0 +1,36 @@ +/* + + MIT License + + Copyright (c) 2021 Looker Data Sciences, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + */ + +import { initSdk } from '@looker/extension-utils' +import type { BrowserSession } from '@looker/sdk-rtl' +import { functionalSdk40 } from '@looker/sdk' + +export const testSdk = initSdk({ + agentTag: 'RunIt', + configKey: 'ConfigKey', + clientId: 'looker.api-explorer', + createSdkCallback: (session: BrowserSession) => functionalSdk40(session), +}) diff --git a/packages/run-it/src/utils/index.ts b/packages/run-it/src/utils/index.ts index cf4eb846a..1e148aeb2 100644 --- a/packages/run-it/src/utils/index.ts +++ b/packages/run-it/src/utils/index.ts @@ -23,5 +23,5 @@ SOFTWARE. */ -export * from './RunItSDK' export * from './requestUtils' +export * from './settings' diff --git a/packages/run-it/src/utils/requestUtils.spec.ts b/packages/run-it/src/utils/requestUtils.spec.ts index ad5f7a442..95edcd202 100644 --- a/packages/run-it/src/utils/requestUtils.spec.ts +++ b/packages/run-it/src/utils/requestUtils.spec.ts @@ -25,6 +25,7 @@ */ import type { RunItInput } from '../RunIt' import { testJsonResponse, api } from '../test-data' +import { testSdk as sdk } from '../test-utils' import { createRequestParams, pathify, @@ -32,9 +33,6 @@ import { createInputs, initRequestContent, } from './requestUtils' -import { initRunItSdk } from './RunItSDK' - -const sdk = initRunItSdk() describe('requestUtils', () => { afterEach(() => { diff --git a/packages/run-it/src/utils/settings.ts b/packages/run-it/src/utils/settings.ts new file mode 100644 index 000000000..3f0eb653f --- /dev/null +++ b/packages/run-it/src/utils/settings.ts @@ -0,0 +1,28 @@ +/* + + MIT License + + Copyright (c) 2021 Looker Data Sciences, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. + + */ +export enum StoreConstants { + LOCALSTORAGE_SETTINGS_KEY = 'settings', +}