-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🔥 v3: update Ctx.Format to match Express's res.format #2766
Conversation
0d4d1c0
to
14e2cda
Compare
While the existing Ctx.Format provides a concise convenience method for basic content negotiation on simple structures, res.format allows developers to set their own custom handlers for each content type. The existing Ctx.Format is renamed to Ctx.AutoFormat.
63433fb
to
aa86933
Compare
Thanks @nickajacks1 quick glance on mobile looks good. I will review tomorrow. |
thanks really cool improvement |
- Rename Fmt to ResFmt - Add comments in several places - Return errors instead of panicking in Format - Add 'Accept' to the Vary header in Format to match res.format
a72a783
to
2b0cd34
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I missed it, but there's no test using a json object?
Do you mean for Format or AutoFormat? For Format, there's not a lot of significance on specifically which content types are tested since you'll be calling For AutoFormat there's this: c.Request().Header.Set(HeaderAccept, MIMEApplicationJSON)
err = c.AutoFormat("Hello, World!")
require.NoError(t, err)
require.Equal(t, `"Hello, World!"`, string(c.Response().Body())) But maybe it would be nice to add something that takes advantage of struct tags to get better use-case coverage. I can add that tomorrow. |
Description
While the existing Ctx.Format provides a concise convenience method for basic content negotiation on simple structures, res.format allows developers to set their own custom handlers for each content type.
Docs: moved the existing Ctx.Format docs to a section called AutoFormat and added docs for the new Ctx.Format.
I'm taking suggestions for better names than
Fmt
for the mediaType-handler struct...was just worried about making it overly long since people will need to type it over and over.Benchmarks
Performs ever so slightly better than AutoFormat (formerly known as Format)
Related to #2745
Migration
To make their code compatible with Fiber v3, users need only replace their uses of
c.Format
toc.AutoFormat
.To make full use of the new
c.Format
, users should note where in their application they are performing manual content negotiation and consider ifc.Format
will simplify their code.Changes Introduced
Type of Change
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/
directory for Fiber's documentation.