Skip to content
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

Allow binding query parameters to environment variables #2777

Merged
merged 38 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
c9f813e
fix by second way
Oct 10, 2023
8841905
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 11, 2023
ceb878e
fix type error
Oct 11, 2023
8b342ef
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 12, 2023
c197c24
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 16, 2023
60ea47a
revert
Oct 16, 2023
df2428a
Merge branch '2616_add_node_env_second_way' of https://github.com/Jer…
Oct 16, 2023
c2b15d3
fix
Oct 16, 2023
6658227
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 17, 2023
4605334
fix
Oct 17, 2023
13d00d2
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 17, 2023
e3d0861
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 18, 2023
4f691d5
fix
Oct 18, 2023
ebc73a4
fix
Oct 18, 2023
ac8ff11
Merge branch '2616_add_node_env_second_way' of https://github.com/Jer…
Oct 18, 2023
98e548b
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 18, 2023
8e97f54
fix
Oct 20, 2023
553fbcd
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 20, 2023
81c5f75
fix
Oct 24, 2023
a5116b9
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 24, 2023
3fb2462
fix
Oct 24, 2023
c5467fe
Merge branch '2616_add_node_env_second_way' of https://github.com/Jer…
Oct 24, 2023
13f428f
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 24, 2023
3a81d5d
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 26, 2023
e116cf5
fix
Oct 26, 2023
b0f1ccc
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 30, 2023
7e237f7
display undefined if the variable included variable env
Oct 30, 2023
ca938a5
Merge branch '2616_add_node_env_second_way' of https://github.com/Jer…
Oct 30, 2023
79070a4
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Oct 31, 2023
c2d5bbf
optimized my code
Oct 31, 2023
e18045d
Merge branch '2616_add_node_env_second_way' of https://github.com/Jer…
Oct 31, 2023
2540902
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Nov 2, 2023
b924cdb
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Nov 4, 2023
568303e
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Nov 6, 2023
9c91fb3
fix
Nov 6, 2023
bd4eb99
Merge branch '2616_add_node_env_second_way' of https://github.com/Jer…
Nov 6, 2023
e8a7988
Merge branch 'master' into 2616_add_node_env_second_way
JerryWu1234 Nov 7, 2023
1517841
fix
Nov 7, 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
7 changes: 7 additions & 0 deletions packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ function parseBinding(
result: { value: bindable },
};
}

if (bindingType === 'env') {
return {
scopePath,
result: { value: bindable },
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
};
}
if (bindingType === 'jsExpression') {
return {
scopePath,
Expand Down
18 changes: 9 additions & 9 deletions packages/toolpad-app/src/toolpad/AppEditor/BindingEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interface BindingEditorContext {
disabled?: boolean;
propType?: PropValueType;
liveBinding?: LiveBinding;
envVarNames?: string[];
env?: Record<string, string>;
}

const [useBindingEditorContext, BindingEditorContextProvider] =
Expand Down Expand Up @@ -138,8 +138,8 @@ function JsExpressionPreview({ jsRuntime, input, globalScope }: JsExpressionPrev
export interface EnvBindingEditorProps extends WithControlledProp<EnvAttrValue | null> {}

export function EnvBindingEditor({ value, onChange }: EnvBindingEditorProps) {
const { envVarNames = [] } = useBindingEditorContext();

const context = useBindingEditorContext();
const envVarNames = Object.keys(context.env ?? {});
const handleInputChange = React.useCallback(
(event: React.SyntheticEvent, newValue: string | null) => {
onChange({
Expand Down Expand Up @@ -195,10 +195,10 @@ export function ValueBindingEditor({ value, onChange, error }: ValueBindingEdito
globalScopeMeta = {},
jsRuntime,
propType,
envVarNames,
env,
} = useBindingEditorContext();

const hasEnv = Boolean(envVarNames);
const hasEnv = Boolean(env);

const [activeTab, setActiveTab] = React.useState<BindableType>(getValueBindingTab(value));
React.useEffect(() => {
Expand Down Expand Up @@ -616,7 +616,7 @@ export interface BindingEditorProps<V> extends WithControlledProp<BindableAttrVa
hidden?: boolean;
propType?: PropValueType;
liveBinding?: LiveBinding;
envVarNames?: string[];
env?: Record<string, string>;
}

export function BindingEditor<V>({
Expand All @@ -630,7 +630,7 @@ export function BindingEditor<V>({
value,
onChange,
liveBinding,
envVarNames,
env,
}: BindingEditorProps<V>) {
const [open, setOpen] = React.useState(false);
const handleOpen = React.useCallback(() => setOpen(true), []);
Expand Down Expand Up @@ -687,9 +687,9 @@ export function BindingEditor<V>({
disabled,
propType,
liveBinding,
envVarNames,
env,
}),
[disabled, envVarNames, globalScope, jsRuntime, label, liveBinding, propType, resolvedMeta],
[disabled, env, globalScope, jsRuntime, label, liveBinding, propType, resolvedMeta],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface BindableEditorProps<V> extends WithControlledProp<BindableAttrV
liveBinding?: LiveBinding;
globalScope?: Record<string, unknown>;
globalScopeMeta: ScopeMeta;
envVarNames?: string[];
env?: Record<string, string>;
sx?: SxProps;
}

Expand All @@ -47,7 +47,7 @@ export default function BindableEditor<V>({
liveBinding,
globalScope = {},
globalScopeMeta = {},
envVarNames,
env,
sx,
}: BindableEditorProps<V>) {
const propTypeControls = usePropControlsContext();
Expand Down Expand Up @@ -103,7 +103,7 @@ export default function BindableEditor<V>({
disabled={disabled || !bindable}
hidden={!bindable}
liveBinding={liveBinding}
envVarNames={envVarNames}
env={env}
/>
</React.Fragment>
</Stack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface StringRecordEntriesEditorProps
autoFocus?: boolean;
sx?: SxProps;
jsRuntime: JsRuntime;
envVarNames?: string[];
env?: Record<string, string>;
disabled?: boolean;
}

Expand All @@ -33,7 +33,7 @@ export default function ParametersEditor({
jsRuntime,
disabled,
globalScopeMeta,
envVarNames,
env,
}: StringRecordEntriesEditorProps) {
const fieldInputRef = React.useRef<HTMLInputElement>(null);

Expand Down Expand Up @@ -84,7 +84,7 @@ export default function ParametersEditor({
onChange(value.map((entry, i) => (i === index ? [entry[0], newBinding] : entry)))
}
disabled={disabled}
envVarNames={envVarNames}
env={env}
/>

<IconButton aria-label="Delete property" onClick={handleRemove(index)}>
Expand Down
16 changes: 7 additions & 9 deletions packages/toolpad-app/src/toolpadDataSources/rest/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
} from '@mui/material';
import { Controller, useForm } from 'react-hook-form';
import { TabContext, TabList } from '@mui/lab';
import { useBrowserJsRuntime } from '@mui/toolpad-core/jsBrowserRuntime';
import { useServerJsRuntime } from '@mui/toolpad-core/jsServerRuntime';
import { createServerJsRuntime } from '@mui/toolpad-core/jsServerRuntime';
import { Panel, PanelGroup, PanelResizeHandle } from '../../components/resizablePanels';
import { ClientDataSource, ConnectionEditorProps, QueryEditorProps } from '../../types';
import {
Expand Down Expand Up @@ -266,8 +265,8 @@ function QueryEditor({
},
{ retry: false },
);
const envVarNames = React.useMemo(() => introspection?.data?.envVarNames || [], [introspection]);

const env = React.useMemo(() => introspection?.data?.env, [introspection]);
const handleParamsChange = React.useCallback(
(newParams: [string, BindableAttrValue<string>][]) => {
setInput((existing) => ({ ...existing, params: newParams }));
Expand Down Expand Up @@ -334,12 +333,10 @@ function QueryEditor({
);

const paramsEntries = input.params || EMPTY_PARAMS;

const jsBrowserRuntime = useBrowserJsRuntime();
const jsServerRuntime = useServerJsRuntime();
const jsServerRuntime = React.useMemo(() => createServerJsRuntime(env ?? {}), [env]);

const paramsEditorLiveValue = useEvaluateLiveBindingEntries({
jsRuntime: jsBrowserRuntime,
jsRuntime: jsServerRuntime,
input: paramsEntries,
globalScope,
});
Expand Down Expand Up @@ -482,7 +479,7 @@ function QueryEditor({
globalScopeMeta={QUERY_SCOPE_META}
liveValue={liveHeaders}
jsRuntime={jsServerRuntime}
envVarNames={envVarNames}
env={env}
/>
</TabPanel>
<TabPanel disableGutters value="response">
Expand Down Expand Up @@ -530,7 +527,8 @@ function QueryEditor({
globalScope={globalScope}
globalScopeMeta={globalScopeMeta}
liveValue={paramsEditorLiveValue}
jsRuntime={jsBrowserRuntime}
jsRuntime={jsServerRuntime}
env={env}
/>
</Box>
</Panel>
Expand Down
14 changes: 7 additions & 7 deletions packages/toolpad-app/src/toolpadDataSources/rest/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,22 @@ async function execBase(
connection: Maybe<RestConnectionParams>,
fetchQuery: FetchQuery,
params: Record<string, string>,
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
envParams?: Record<string, string>,
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
): Promise<FetchResult> {
const har = createHarLog();
const instrumentedFetch = withHarInstrumentation(fetch, { har });
const jsRuntime = createServerJsRuntime(process.env);

const parameter = resolveBindableEntries(jsRuntime, Object.entries(params), envParams!);
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved

const queryScope = {
// @TODO: remove deprecated query after v1
query: params,
parameters: params,
parameters: Object.fromEntries(parameter),
};

const runtimeConfig = await project.getRuntimeConfig();
const urlvalue = fetchQuery.url || getDefaultUrl(runtimeConfig, connection);

const resolvedUrl = resolveBindable(jsRuntime, urlvalue, queryScope);
const resolvedSearchParams = resolveBindableEntries(
jsRuntime,
Expand Down Expand Up @@ -211,7 +213,6 @@ async function execBase(
} catch (rawError) {
error = serializeError(errorFrom(rawError));
}

return { data, untransformedData, error, har };
}

Expand All @@ -224,17 +225,16 @@ export default function createDatasource(
fetchQuery: FetchQuery,
params: Record<string, string>,
): Promise<ExecFetchResult<any>> {
const { data, error } = await execBase(project, connection, fetchQuery, params);
const env = await loadEnvFile(project);
const { data, error } = await execBase(project, connection, fetchQuery, params, env);
return { data, error };
},

async execPrivate(connection: Maybe<RestConnectionParams>, query: FetchPrivateQuery) {
switch (query.kind) {
case 'introspection': {
const env = await loadEnvFile(project);
const envVarNames = Object.keys(env);

return { envVarNames };
return { env };
}
case 'debugExec':
return execBase(project, connection, query.query, query.params);
Expand Down
2 changes: 1 addition & 1 deletion packages/toolpad-app/src/toolpadDataSources/rest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,5 @@ export interface FetchResult extends ExecFetchResult<any> {
}

export type IntrospectionResult = {
envVarNames: string[];
env: Record<string, string>;
};
Loading