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

feat: add support for custom request types #190

Merged
merged 10 commits into from May 4, 2022
Merged

Conversation

wilkmaia
Copy link
Contributor

@wilkmaia wilkmaia commented May 3, 2022

This PR adds support for a new option parameter: customRequestType. It represents a custom type from which the request object should inherit besides stream.Readable.

Closes #189.

Checklist

lib/request.js Outdated
@@ -121,6 +122,10 @@ function Request (options) {
simulate: options.simulate || {}
}

if (options.customRequestType) {
util.inherits(this.constructor, options.customRequestType)
Copy link
Member

Choose a reason for hiding this comment

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

This alters the constructor, which is a singleton property. This means that after the first execution, all the constructors will have this set. Moreover concurrent usage will be broken too.

Could we move this block next to the other util.inherits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mcollina you're right about the constructor. I've updated the code so that custom requests are dealt independently from Request.


About moving this util.inherits call down next to the other one: that'd remove the proposed generality, ie users wouldn't be able to provide specific classes/functions from which the request would inherit. Or am I missing something?

In any case this is more of a curiosity than anything. With the new approach this shouldn't be needed.

@wilkmaia wilkmaia requested a review from mcollina May 3, 2022 17:36
lib/request.js Outdated
if (options.customRequestType) {
assert(options.customRequestType.prototype !== undefined)
return new MakeCustomRequest(this, options)
}
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 move this if to where Request is instantiated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My idea was to keep it transparent to the lib/request clients. But sure, will do

@wilkmaia wilkmaia requested a review from mcollina May 3, 2022 19:41
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

@mcollina
Copy link
Member

mcollina commented May 3, 2022

good work!

Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

Suggestion only.
Will it be better if the option call Request / RequestPrototype instead of customRequestType?

index.js Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

mcollina commented May 4, 2022

CI is failing

Co-authored-by: KaKa <climba03003@gmail.com>
@wilkmaia
Copy link
Contributor Author

wilkmaia commented May 4, 2022

Suggestion only.
Will it be better if the option call Request / RequestPrototype instead of customRequestType?

I'd be ok with either keeping it as is or updating. I'm usually not great at naming things haha. Instead of RequestPrototype, though, I suggest something like RequestClass or RequestType b/c we expect the base class/constructor, not the prototype itself.

One more thing: did you use pascal case intentionally? Given the other options aren't capitalized wouldn't that be a, yet small, inconsistency in the lib's public api?

@climba03003
Copy link
Member

climba03003 commented May 4, 2022

Suggestion only.
Will it be better if the option call Request / RequestPrototype instead of customRequestType?

I'd be ok with either keeping it as is or updating. I'm usually not great at naming things haha. Instead of RequestPrototype, though, I suggest something like RequestClass or RequestType b/c we expect the base class/constructor, not the prototype itself.

One more thing: did you use pascal case intentionally? Given the other options aren't capitalized wouldn't that be a, yet small, inconsistency in the lib's public api?

Since it can be a class or function, I am intended to make it as PascalCase.
If it is RequestClass, I would leave it as Request only.
Normally, people would know it should be a class if it use PascalCase.

I think it is acceptable for the option to be PascelCase. For example ws use it as well. To extends the WebSocket.

@wilkmaia wilkmaia requested a review from climba03003 May 4, 2022 12:20
Copy link
Member

@climba03003 climba03003 left a comment

Choose a reason for hiding this comment

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

LGTM.

@mcollina mcollina merged commit 2e4dcd8 into fastify:master May 4, 2022
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.

Support custom Request types
3 participants