hono icon indicating copy to clipboard operation
hono copied to clipboard

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

Open vanodevium opened this issue 1 year ago • 10 comments

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 avatar May 10 '24 15:05 vanodevium

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.

yusukebe avatar May 11 '24 13:05 yusukebe

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.

fzn0x avatar May 13 '24 03:05 fzn0x

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.

yusukebe avatar May 14 '24 08:05 yusukebe

@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!

vanodevium avatar May 14 '24 16:05 vanodevium

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 :)

fzn0x avatar May 14 '24 22:05 fzn0x

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?

yusukebe avatar May 15 '24 02:05 yusukebe

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

MathurAditya724 avatar May 15 '24 03:05 MathurAditya724

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

fzn0x avatar May 15 '24 03:05 fzn0x

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

MathurAditya724 avatar May 15 '24 03:05 MathurAditya724

PR is ready! 👍

fzn0x avatar May 15 '24 04:05 fzn0x

Hi @yusukebe I think we can close this issue :)

fzn0x avatar May 28 '24 04:05 fzn0x

Yes. Closing:)

yusukebe avatar May 28 '24 05:05 yusukebe