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

context.req.parseBody(): option for support dot notation #2656

Open
vanodevium opened this issue May 10, 2024 · 10 comments · Fixed by #2675
Open

context.req.parseBody(): option for support dot notation #2656

vanodevium opened this issue May 10, 2024 · 10 comments · Fixed by #2675
Labels
enhancement New feature or request.

Comments

@vanodevium
Copy link

What is the feature you are proposing?

For now this method has only option all for array support. It is awesome!

In my opinion, it would be very convenient to add an option to support dot notation (dot: boolean), which would greatly simplify the processing of nested objects.

Example:

let formData = new FormData();
formData.append('obj.key1', 'value1');
formData.append('obj.key2', 'value2');

// processed without dot notation support, like `await context.req.parseBody()`
{
	"obj.key1": "value1",
    "obj.key2": "value2",
} 

// processed with dot notation support, like `await context.req.parseBody({ dot: true })`
{
	"obj": {
		"key1": "value1",
		"key2": "value2"
	}
}
@vanodevium vanodevium added the enhancement New feature or request. label May 10, 2024
@yusukebe
Copy link
Member

Hi @vanodevium

Are you talking about the "FormData"?

If so, it's standard Web API, and it's not Hono matter. There is nothing we can do about it.

@fzn0x
Copy link
Contributor

fzn0x commented May 13, 2024

It's something from the Browser API. For Hono, I think you can build a custom middleware for it.

app.use(async (c, next) => {
    const parsedBody = await c.req.parseBody();

    // do the dot notation logic

    c.req.bodyCache.parsedBody = parsedBody;
  } catch (_err) {
  } finally {
    await next();
  }
});

But becareful about modifying data in middleware, while modifying data in middleware is a common and often necessary practice in web development, it must be done responsibly, securely, and in compliance with legal and ethical standards.

@yusukebe
Copy link
Member

Ah, I see. I miss understanding. This is proposing adding a dot option to c.req.parseBody()

That is a good proposal. I'd like to know if others want this feature.

@vanodevium
Copy link
Author

@yusukebe

I discussed this functionality with colleagues and programmer friends.

The answer is the same for everyone - it is a very convenient and useful thing.

Moreover, by default this thing will be turned off, so there will be no breaking changes for existing systems.

Of course, the decision is yours. Thank you!

@fzn0x
Copy link
Contributor

fzn0x commented May 14, 2024

Ah, I see. I miss understanding. This is proposing adding a dot option to c.req.parseBody()

That is a good proposal. I'd like to know if others want this feature.

I think it's something great to add :)

@yusukebe
Copy link
Member

Thanks to both. Let's add the feature. I'll do it, but now I'm busy working on other matters. Does anyone implement it?

@MathurAditya724
Copy link
Contributor

Fine, I will do it. I am thinking about the API cause {dot: true} isn't expressive enough, maybe something like {json: true}?

@fzn0x
Copy link
Contributor

fzn0x commented May 15, 2024

Hi @MathurAditya724 actually I'm already building it, only unit tests left

@MathurAditya724
Copy link
Contributor

@fzn0x Great! Do let me know when the PR is ready, maybe I can add some pointers

@fzn0x
Copy link
Contributor

fzn0x commented May 15, 2024

PR is ready! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants