Skip to content

🪟 🔧 Refactor webapp configuration provider #21456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
34f23b6
first pass
joeykmh Nov 29, 2022
28af138
Merge branch 'master' into joey/refactor-config-providers
joeykmh Dec 7, 2022
5fcc0f5
merge oss config providers in static object
joeykmh Dec 7, 2022
20bb6ff
Merge branch 'master' into joey/refactor-config-providers
joeykmh Jan 16, 2023
7bc28f3
make OSS config static
joeykmh Jan 16, 2023
7bbf7b7
rename config to be more specific
joeykmh Jan 17, 2023
0f4d870
fallback connector builder api url
joeykmh Jan 17, 2023
39c435a
remove comments
joeykmh Jan 17, 2023
d680628
expand default config object
joeykmh Jan 17, 2023
fb72243
remove separate cloud config object
joeykmh Jan 17, 2023
54196d0
remove connector builder api from webapp .env
joeykmh Jan 17, 2023
074fd82
newline
joeykmh Jan 18, 2023
7eb471a
remove configProvider logic
joeykmh Jan 18, 2023
54d1add
rename newStaticConfig > config
joeykmh Jan 18, 2023
bb7c231
ci bump
joeykmh Jan 18, 2023
87a6971
Merge branch 'master' into joey/refactor-config-providers
joeykmh Jan 18, 2023
d0444a7
fix free ab connectors
joeykmh Jan 18, 2023
399a930
Merge branch 'master' into joey/refactor-config-providers
Jan 18, 2023
da5e1c7
merge master
joeykmh Jan 19, 2023
cf9111b
Merge branch 'master' into joey/refactor-config-providers
Jan 19, 2023
b513266
segment_token on window object is optional
joeykmh Jan 19, 2023
8e751cf
add explicit protocol to urls
Jan 23, 2023
2d7e6d5
Merge branch 'master' into joey/refactor-config-providers
joeykmh Jan 23, 2023
cfbd267
remove unconfigurable health check interval from config
joeykmh Jan 23, 2023
7d15c84
Merge branch 'master' into joey/refactor-config-providers
joeykmh Jan 23, 2023
dfc5a6d
move datadog config to config.ts
joeykmh Jan 23, 2023
70a8f09
add sentry to config.ts
joeykmh Jan 23, 2023
54b672d
Merge branch 'master' into joey/refactor-config-providers
Jan 24, 2023
1550b3a
remove types related to configProvider
joeykmh Jan 24, 2023
4ab4102
Merge branch 'master' into joey/refactor-config-providers
Jan 25, 2023
de0cad3
Merge branch 'master' into joey/refactor-config-providers
joeykmh Jan 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions airbyte-webapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ThemeProvider } from "styled-components";

import { ApiErrorBoundary } from "components/common/ApiErrorBoundary";

import { config } from "config";
import { ApiServices } from "core/ApiServices";
import { I18nProvider } from "core/i18n";
import { ServicesProvider } from "core/servicesProvider";
Expand All @@ -18,14 +19,7 @@ import { AnalyticsProvider } from "views/common/AnalyticsProvider";
import { StoreProvider } from "views/common/StoreProvider";

import LoadingPage from "./components/LoadingPage";
import {
Config,
ConfigServiceProvider,
defaultConfig,
envConfigProvider,
ValueProvider,
windowConfigProvider,
} from "./config";
import { ConfigServiceProvider } from "./config";
import en from "./locales/en.json";
import { Routing } from "./pages/routes";
import { WorkspaceServiceProvider } from "./services/workspaces/WorkspacesService";
Expand All @@ -35,8 +29,6 @@ const StyleProvider: React.FC<React.PropsWithChildren<unknown>> = ({ children })
<ThemeProvider theme={theme}>{children}</ThemeProvider>
);

const configProviders: ValueProvider<Config> = [envConfigProvider, windowConfigProvider];

const Services: React.FC<React.PropsWithChildren<unknown>> = ({ children }) => (
<AnalyticsProvider>
<AppMonitoringServiceProvider>
Expand Down Expand Up @@ -69,7 +61,7 @@ const App: React.FC = () => {
<StoreProvider>
<ServicesProvider>
<Suspense fallback={<LoadingPage />}>
<ConfigServiceProvider defaultConfig={defaultConfig} providers={configProviders}>
<ConfigServiceProvider config={config}>
<Router>
<Services>
<Routing />
Expand Down
37 changes: 10 additions & 27 deletions airbyte-webapp/src/config/ConfigServiceProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
import React, { useContext, useMemo } from "react";
import { useAsync } from "react-use";
import React, { useContext } from "react";

import { LoadingPage } from "components";
import { AirbyteWebappConfig } from "./types";

import { applyProviders } from "./configProviders";
import { Config, ValueProvider } from "./types";

export interface ConfigContextData<T extends Config = Config> {
config: T;
export interface ConfigContextData {
config: AirbyteWebappConfig;
}

export const ConfigContext = React.createContext<ConfigContextData | null>(null);

export function useConfig<T extends Config>(): T {
export function useConfig(): AirbyteWebappConfig {
const configService = useContext(ConfigContext);

if (configService === null) {
throw new Error("useConfig must be used within a ConfigProvider");
}

return useMemo(() => configService.config as unknown as T, [configService.config]);
return configService.config;
}

const ConfigServiceInner: React.FC<
export const ConfigServiceProvider: React.FC<
React.PropsWithChildren<{
defaultConfig: Config;
providers?: ValueProvider<Config>;
config: AirbyteWebappConfig;
}>
> = ({ children, defaultConfig, providers }) => {
const { loading, value } = useAsync(
async () => (providers ? applyProviders(defaultConfig, providers) : defaultConfig),
[providers]
);
const config: ConfigContextData | null = useMemo(() => (value ? { config: value } : null), [value]);

if (loading) {
return <LoadingPage />;
}

return <ConfigContext.Provider value={config}>{children}</ConfigContext.Provider>;
> = ({ children, config }) => {
return <ConfigContext.Provider value={{ config }}>{children}</ConfigContext.Provider>;
};

export const ConfigServiceProvider = React.memo(ConfigServiceInner);
36 changes: 36 additions & 0 deletions airbyte-webapp/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { AirbyteWebappConfig } from "./types";

export const config: AirbyteWebappConfig = {
segment: {
token: window.SEGMENT_TOKEN ?? process.env.REACT_APP_SEGMENT_TOKEN,
enabled: window.TRACKING_STRATEGY === "segment",
},
apiUrl:
window.API_URL ??
process.env.REACT_APP_API_URL ??
`${window.location.protocol}//${window.location.hostname}:8001/api`,
connectorBuilderApiUrl:
process.env.REACT_APP_CONNECTOR_BUILDER_API_URL ?? `${window.location.protocol}//${window.location.hostname}:8003`,
healthCheckInterval: 20000,
version: window.AIRBYTE_VERSION ?? "dev",
integrationUrl: process.env.REACT_APP_INTEGRATION_DOCS_URLS ?? "/docs",
oauthRedirectUrl: `${window.location.protocol}//${window.location.host}`,
cloudApiUrl: window.CLOUD_API_URL ?? process.env.REACT_APP_CLOUD_API_URL,
cloudPublicApiUrl: process.env.REACT_APP_CLOUD_PUBLIC_API_URL,
firebase: {
apiKey: window.FIREBASE_API_KEY ?? process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: window.FIREBASE_AUTH_DOMAIN ?? process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
authEmulatorHost: window.FIREBASE_AUTH_EMULATOR_HOST ?? process.env.REACT_APP_FIREBASE_AUTH_EMULATOR_HOST,
},
intercom: {
appId: process.env.REACT_APP_INTERCOM_APP_ID,
},
launchDarkly: window.LAUNCHDARKLY_KEY ?? process.env.REACT_APP_LAUNCHDARKLY_KEY,
};

export class MissingConfigError extends Error {
constructor(message: string) {
super(message);
this.name = "MissingConfigError";
}
}
58 changes: 0 additions & 58 deletions airbyte-webapp/src/config/configProviders.test.ts

This file was deleted.

45 changes: 0 additions & 45 deletions airbyte-webapp/src/config/configProviders.ts

This file was deleted.

13 changes: 0 additions & 13 deletions airbyte-webapp/src/config/defaultConfig.ts

This file was deleted.

3 changes: 1 addition & 2 deletions airbyte-webapp/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./defaultConfig";
export * from "./configProviders";
export * from "./config";
export * from "./ConfigServiceProvider";
export * from "./types";
26 changes: 21 additions & 5 deletions airbyte-webapp/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,33 @@ declare global {
SEGMENT_TOKEN?: string;
LAUNCHDARKLY_KEY?: string;
analytics: SegmentAnalytics.AnalyticsJS;
// Cloud specific properties
FIREBASE_API_KEY?: string;
FIREBASE_AUTH_DOMAIN?: string;
FIREBASE_AUTH_EMULATOR_HOST?: string;
CLOUD_API_URL?: string;
CLOUD_PUBLIC_API_URL?: string;
}
}

export interface Config {
segment: { token: string; enabled: boolean };
export interface AirbyteWebappConfig {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to make it more clear what this is a config for

segment: { token?: string; enabled: boolean };
apiUrl: string;
connectorBuilderApiUrl: string;
oauthRedirectUrl: string;
healthCheckInterval: number;
version?: string;
version: string;
integrationUrl: string;
oauthRedirectUrl: string;
cloudApiUrl?: string;
cloudPublicApiUrl?: string;
firebase: {
apiKey?: string;
authDomain?: string;
authEmulatorHost?: string;
};
intercom: {
appId?: string;
};
launchDarkly?: string;
}

Expand All @@ -39,4 +55,4 @@ export type Provider<T> = () => T;

export type ValueProvider<T> = Array<ProviderAsync<DeepPartial<T>>>;

export type ConfigProvider<T extends Config = Config> = ProviderAsync<DeepPartial<T>>;
export type ConfigProvider<T extends AirbyteWebappConfig = AirbyteWebappConfig> = ProviderAsync<DeepPartial<T>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think most of those types could still be removed or the Provider which seems to be used in the auth service, just inlined there instead (and most likely doesn't need a named type).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, forgot that I wanted to clean up (remove) these unnecessary types!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed here 1550b3a

4 changes: 2 additions & 2 deletions airbyte-webapp/src/core/request/apiOverride.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Config } from "../../config";
import { AirbyteWebappConfig } from "../../config";
import { CommonRequestError } from "./CommonRequestError";
import { RequestMiddleware } from "./RequestMiddleware";
import { VersionError } from "./VersionError";

export interface ApiOverrideRequestOptions {
config: Pick<Config, "apiUrl">;
config: Pick<AirbyteWebappConfig, "apiUrl">;
middlewares: RequestMiddleware[];
signal?: RequestInit["signal"];
}
Expand Down
6 changes: 3 additions & 3 deletions airbyte-webapp/src/packages/cloud/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ThemeProvider } from "styled-components";
import { ApiErrorBoundary } from "components/common/ApiErrorBoundary";
import LoadingPage from "components/LoadingPage";

import { ConfigServiceProvider, config } from "config";
import { I18nProvider } from "core/i18n";
import { AppMonitoringServiceProvider } from "hooks/services/AppMonitoringService";
import { ConfirmationModalService } from "hooks/services/ConfirmationModal";
Expand All @@ -22,7 +23,6 @@ import { AnalyticsProvider } from "views/common/AnalyticsProvider";
import { StoreProvider } from "views/common/StoreProvider";

import { AppServicesProvider } from "./services/AppServicesProvider";
import { ConfigProvider } from "./services/ConfigProvider";
import { IntercomProvider } from "./services/thirdParty/intercom/IntercomProvider";

const messages = { ...en, ...cloudLocales };
Expand Down Expand Up @@ -64,13 +64,13 @@ const App: React.FC = () => {
<I18nProvider locale="en" messages={messages}>
<StoreProvider>
<Suspense fallback={<LoadingPage />}>
<ConfigProvider>
<ConfigServiceProvider config={config}>
<Router>
<Services>
<Routing />
</Services>
</Router>
</ConfigProvider>
</ConfigServiceProvider>
</Suspense>
</StoreProvider>
</I18nProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQuery } from "react-query";

import { MissingConfigError, useConfig } from "config";
import { useExperiment } from "hooks/services/Experiment";
import { useConfig } from "packages/cloud/services/config";
import { useDefaultRequestMiddlewares } from "services/useDefaultRequestMiddlewares";
import { useCurrentWorkspaceId } from "services/workspaces/WorkspacesService";

Expand All @@ -10,6 +10,9 @@ import { webBackendGetFreeConnectorProgramInfoForWorkspace } from "../lib/api";
export const useFreeConnectorProgramInfo = () => {
const workspaceId = useCurrentWorkspaceId();
const { cloudApiUrl } = useConfig();
if (!cloudApiUrl) {
throw new MissingConfigError("Missing required configuration cloudApiUrl");
}
const config = { apiUrl: cloudApiUrl };
const middlewares = useDefaultRequestMiddlewares();
const requestOptions = { config, middlewares };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import React, { useMemo } from "react";

import { LoadingPage } from "components";

import { MissingConfigError, useConfig } from "config";
import { ApiServices } from "core/ApiServices";
import { RequestMiddleware } from "core/request/RequestMiddleware";
import { ServicesProvider, useGetService, useInjectServices } from "core/servicesProvider";
import { RequestAuthMiddleware } from "packages/cloud/lib/auth/RequestAuthMiddleware";
import { UserService } from "packages/cloud/lib/domain/users";
import { useAuth } from "packages/firebaseReact";

import { useConfig } from "./config";
import { FirebaseSdkProvider } from "./FirebaseSdkProvider";

/**
Expand Down Expand Up @@ -43,6 +43,10 @@ const ServiceOverrides: React.FC<React.PropsWithChildren<unknown>> = React.memo(

const { cloudApiUrl } = useConfig();

if (!cloudApiUrl) {
throw new MissingConfigError("Missing required configuration cloudApiUrl");
}
Comment on lines +46 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to log the error with our AppMonitoringService() instead of throwing it here. The downside is that this would not be surfaced to the user in environments without datadog.


const inject = useMemo(
() => ({
UserService: new UserService(cloudApiUrl, middlewares),
Expand Down
Loading