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

Rest datasource configure bodies and headers #721

Merged
merged 30 commits into from
Sep 6, 2022
Merged

Rest datasource configure bodies and headers #721

merged 30 commits into from
Sep 6, 2022

Conversation

Janpot
Copy link
Member

@Janpot Janpot commented Aug 2, 2022

  • configure body
    • raw body, with selectable content type
    • x-www-form-urlencoded
  • configure url query parameters
  • configure headers
  • move everything but the method+url under tabs
  • move parameters panel to the same place as postgres and function datasources
Screen.Recording.2022-09-05.at.12.29.15.mov

Fixes #850

@render
Copy link

render bot commented Aug 2, 2022

@oliviertassinari oliviertassinari requested a deployment to fetch-body - toolpad-db PR #721 August 2, 2022 09:34 — with Render Abandoned
@Janpot Janpot mentioned this pull request Aug 4, 2022
2 tasks
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 9, 2022
@render
Copy link

render bot commented Aug 11, 2022

@oliviertassinari oliviertassinari requested a deployment to fetch-body - toolpad-db PR #721 August 11, 2022 06:56 — with Render Abandoned
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 11, 2022
@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Aug 23, 2022
@Janpot Janpot linked an issue Aug 23, 2022 that may be closed by this pull request
2 tasks
@render
Copy link

render bot commented Sep 2, 2022

@oliviertassinari oliviertassinari requested a deployment to fetch-body - toolpad-db PR #721 September 2, 2022 09:35 — with Render Abandoned
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Sep 2, 2022
@Janpot Janpot changed the title WIP: Rest datasource configure body Rest datasource configure bodies and headers Sep 5, 2022
@Janpot Janpot marked this pull request as ready for review September 5, 2022 10:34
@Janpot Janpot requested a review from a team September 5, 2022 10:34
@apedroferreira
Copy link
Member

cool!
a couple questions/thoughts:

  • i don't know what the "Parameters" below are. could they be included with those tab options if it makes sense?
  • the "transform" functionality is logically a bit different to the other tabs, as it's not something as inherent to the request itself. i wonder if we should separate it somehow? but i also don't dislike it being there in a tab, seems easy to use

@@ -201,6 +211,27 @@ function QueryEditor({
}));
}, []);

const handleBodyChange = React.useCallback((newBody: Maybe<Body>) => {
Copy link
Member

Choose a reason for hiding this comment

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

should we have a reusable function for these? the callbacks seem pretty similar.

Copy link
Member

@apedroferreira apedroferreira Sep 5, 2022

Choose a reason for hiding this comment

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

but if we predict these might change over time it's best to keep them separate though, just a thought

Copy link
Member Author

@Janpot Janpot Sep 5, 2022

Choose a reason for hiding this comment

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

One could invent a hook like:

function useQueryChangeHandler<F extends Function>(
  setInput: React.Dispatch<React.SetStateAction<QueryEditorModel<FetchQuery>>>,
  property: string,
  producer: F,
): F {
  return React.useCallback<F>(
    (...args: any[]) => {
      setInput((existing) => ({
        ...existing,
        query: { ...existing.query, [property]: producer(...args) },
      }));
    },
    [producer, property, setInput],
  );
}

and use as

const handleMethodChange = useQueryChangeHandler(setInput, 'method', (event) => event.target.value);
const handleMethodChange = useQueryChangeHandler(setInput, 'url', (newUrl) => newUrl);
// ...

But that produce parameter would invalidate the callback on each render. So we could declare those functions in module scope, or wrap them with useCallback as well, but at which point do all the abstractions become less readable again? The way it's done now is idiomatic and explicit and easy to understand. I kind of prefer it, even though it could probably be expressed in less lines of code. I was thinking for the future we could use immer to remove some of the duplication inside the setInput.

e.g. from

  const handleMethodChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
    setInput((existing) => ({
      ...existing,
      query: { ...existing.query, method: event.target.value },
    }));
  }, []);

to

  const handleMethodChange = React.useCallback(
    (event: React.ChangeEvent<HTMLInputElement>) =>
      setInput(
        produce((draft) => {
          draft.query.method = event.target.value;
        }),
      ),
    [],
  );

Copy link
Member

@apedroferreira apedroferreira Sep 5, 2022

Choose a reason for hiding this comment

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

yeah, it's probably not worth introducing something so complex yet at least (we could not use a producer function/argument, but then we would probably be making the callback less flexible)
it was just a thought, i think we can keep this as it is it's pretty easy to read and understand.
i agree with using immer later for this kind of operations though, makes them a bit cleaner!

@Janpot
Copy link
Member Author

Janpot commented Sep 5, 2022

i don't know what the "Parameters" below are. could they be included with those tab options if it makes sense?

It's parameters to the datasource query itself. this is where you can bind state of the page, which is usable inside of the query bindings. I moved it to this place because it's the same location as in the function and postgresql datasources.

@apedroferreira
Copy link
Member

apedroferreira commented Sep 5, 2022

i don't know what the "Parameters" below are. could they be included with those tab options if it makes sense?

It's parameters to the datasource query itself. this is where you can bind state of the page, which is usable inside of the query bindings. I moved it to this place because it's the same location as in the function and postgresql datasources.

ah i see, makes sense to have them outside the tabs then

@Janpot Janpot merged commit 6db5f2b into master Sep 6, 2022
@Janpot Janpot deleted the fetch-body branch September 6, 2022 08:15
@@ -40,7 +40,7 @@ import {
NodeRuntimeWrapper,
ResetNodeErrorsKeyProvider,
} from '@mui/toolpad-core/runtime';
import { pick } from 'lodash-es';
import * as _ from 'lodash-es';
Copy link
Contributor

Choose a reason for hiding this comment

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

minor thing - just wondering if we are using something to treeshake such imports?
In the past I've used babel-transform-imports maybe in the future it would be worth to look into that 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

It is tree shaken by the swc minifier in next.js. The import style doesn't influence the behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh nice, thanks, I didn't know that 👍

@Janpot Janpot mentioned this pull request Sep 6, 2022
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rest datasource: allow sending body with POST requests Support GET, POST, DELETE, query methods
4 participants