Skip to content

Commit

Permalink
Fix parameters for fetch queries (#1140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Oct 12, 2022
1 parent 74f54bd commit a93887e
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions packages/toolpad-app/src/toolpadDataSources/rest/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
RestConnectionParams,
UrlEncodedBody,
} from './types';
import evalExpression from '../../server/evalExpression';
import evalExpression, { Serializable } from '../../server/evalExpression';
import { removePrefix } from '../../utils/strings';
import { Maybe } from '../../utils/types';
import { getAuthenticationHeaders, HTTP_NO_BODY, parseBaseUrl } from './shared';
Expand All @@ -26,15 +26,13 @@ import DEMO_BASE_URLS from './demoBaseUrls';

async function resolveBindable(
bindable: BindableAttrValue<string>,
boundValues: Record<string, string>,
scope: Record<string, Serializable>,
): Promise<any> {
if (bindable.type === 'const') {
return bindable.value;
}
if (bindable.type === 'jsExpression') {
return evalExpression(bindable.value, {
query: boundValues,
});
return evalExpression(bindable.value, scope);
}
throw new Error(
`Can't resolve bindable of type "${(bindable as BindableAttrValue<unknown>).type}"`,
Expand All @@ -43,19 +41,19 @@ async function resolveBindable(

async function resolveBindableEntries(
entries: BindableAttrEntries,
boundValues: Record<string, string>,
scope: Record<string, Serializable>,
): Promise<[string, any][]> {
return Promise.all(
entries.map(async ([key, value]) => [key, await resolveBindable(value, boundValues)]),
entries.map(async ([key, value]) => [key, await resolveBindable(value, scope)]),
);
}

async function resolveBindables<P>(
obj: BindableAttrValues<P>,
boundValues: Record<string, string>,
scope: Record<string, Serializable>,
): Promise<P> {
return Object.fromEntries(
await resolveBindableEntries(Object.entries(obj) as BindableAttrEntries, boundValues),
await resolveBindableEntries(Object.entries(obj) as BindableAttrEntries, scope),
) as P;
}

Expand All @@ -76,14 +74,14 @@ interface ResolvedRawBody {

async function resolveRawBody(
body: RawBody,
boundValues: Record<string, string>,
scope: Record<string, Serializable>,
): Promise<ResolvedRawBody> {
const { content, contentType } = await resolveBindables(
{
contentType: body.contentType,
content: body.content,
},
boundValues,
scope,
);
return {
kind: 'raw',
Expand All @@ -99,20 +97,20 @@ interface ResolveUrlEncodedBodyBody {

async function resolveUrlEncodedBody(
body: UrlEncodedBody,
boundValues: Record<string, string>,
scope: Record<string, Serializable>,
): Promise<ResolveUrlEncodedBodyBody> {
return {
kind: 'urlEncoded',
content: await resolveBindableEntries(body.content, boundValues),
content: await resolveBindableEntries(body.content, scope),
};
}

async function resolveBody(body: Body, boundValues: Record<string, string>) {
async function resolveBody(body: Body, scope: Record<string, Serializable>) {
switch (body.kind) {
case 'raw':
return resolveRawBody(body, boundValues);
return resolveRawBody(body, scope);
case 'urlEncoded':
return resolveUrlEncodedBody(body, boundValues);
return resolveUrlEncodedBody(body, scope);
default:
throw new Error(`Missing case for "${(body as Body).kind}"`);
}
Expand All @@ -133,10 +131,16 @@ async function execBase(
fetchQuery: FetchQuery,
params: Record<string, string>,
): Promise<FetchResult> {
const queryScope = {
// TODO: remove deprecated query after v1
query: params,
parameters: params,
};

const [resolvedUrl, resolvedSearchParams, resolvedHeaders] = await Promise.all([
resolveBindable(fetchQuery.url, params),
resolveBindableEntries(fetchQuery.searchParams || [], params),
resolveBindableEntries(fetchQuery.headers || [], params),
resolveBindable(fetchQuery.url, queryScope),
resolveBindableEntries(fetchQuery.searchParams || [], queryScope),
resolveBindableEntries(fetchQuery.headers || [], queryScope),
]);

if (process.env.TOOLPAD_DEMO) {
Expand Down

0 comments on commit a93887e

Please sign in to comment.