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

Extend content type parser #3210

Merged
merged 3 commits into from Jul 28, 2021
Merged

Conversation

matthyk
Copy link
Contributor

@matthyk matthyk commented Jul 24, 2021

Checklist

Closes #2994. This PR adds the 2 methods resetContentTypeParsers and removeContentTypeParser to the Fastify instance. With resetContentTypeParsers all current content type parser can be deleted (including the both built-in content type parsers). This is especially useful if you want to implement a catch-all content type parser that should be called even if the content type is application/json. With removeContentTypeParser a single or an array of content type parsers can be removed. Both APIs support encapsulation.

const app = fastify()

app.removeContentTypeParser('application/json')

app.resetContentTypeParsers()

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

good work! Just a nit

}

if (Array.isArray(contentType)) {
contentType.forEach((type) => this[kContentTypeParser].remove(type))
Copy link
Member

Choose a reason for hiding this comment

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

Can you please use a for..of loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change it later! I used forEach because it is also used here.

fastiy.removeContentTypeParser(['application/json', 'text/plain'])
```

#### resetContentTypeParsers
Copy link
Member

Choose a reason for hiding this comment

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

I think the name can be improved. Reset sounds to me reset to some point which mostly reset to default value.

It would be better if it called removeAll.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me! I will update the name

@Eomm Eomm added the semver-minor Issue or PR that should land as semver minor label Jul 24, 2021
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

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

Good one!

@mcollina mcollina merged commit 5cf61fc into fastify:main Jul 28, 2021
This was referenced Aug 2, 2021
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
semver-minor Issue or PR that should land as semver minor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

catch-all addContentTypeParser doesn't fire if no content-type header in request
6 participants