-
Notifications
You must be signed in to change notification settings - Fork 191
Implement a .setHeaders() method for WPAPI and WPRequest objects
#315
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
Conversation
In response to #299, #312, and #314, this introduces a new .setHeaders() prototype method for both WPAPI and WPRequest. `.setHeaders()` allows a single WPRequest, or all requests generated from a WPAPI site client instance, to be augmented with one or more custom HTTP headers. Providing this functionality allows consumers of the wpapi library to utilize custom authentication schemes (such as JWT) with the Authentication header, to specify the preferred language of the response endpoints with the Accept-Language header, and so on. Providing a general-purpose interface is preferred in place of specifically implementing individual methods for JWT auth, language preferences, or other specific headers that a consumer of this library may want to send. Closes #299, fixes #312, fixes #314 **Usage:** If you need to send additional HTTP headers along with your request (for example to provide a specific `Authorization` header for use with alternative authentication schemes), you can use the `.setHeaders()` method to specify one or more headers to send with the dispatched request: ```js // Specify a single header to send with the outgoing request wp.posts().setHeaders( 'Authorization', 'Bearer xxxxx.yyyyy.zzzzz' )... // Specify multiple headers to send with the outgoing request wp.posts().setHeaders({ Authorization: 'Bearer xxxxx.yyyyy.zzzzz', 'Accept-Language': 'pt-BR' })... ``` You can also set headers on the WPAPI instance itself, which will then be used for all subsequent requests created from that site instance: ```js wp.setHeaders( 'Authorization', 'Bearer xxxxx.yyyyy.zzzzz' ); wp.users().me()... wp.posts().id( unpublishedPostId )... ```
|
Feedback is welcome on this interface, especially the method's name: I stuck with |
|
I think I might entertain something like |
|
Looks great, easy to understand and use. I don't have any complaints about the name, "setHeaders" is descriptive and makes sense. |
|
Also, have you considered allowing a callback as a parameter? Just thinking from an OAuth1 point of view:
( Sorry if this isn't the right place for this comment ) |
|
Seems fine by me. The documentation is great, tests are explicit. Can't wait to test it out. Should the headers be also configurable fron higher level? Like |
|
@mnivoliez Thanks! Regarding global headers, currently the signature is the same: I think that the repetition is preferable to introducing a new word "global"; it mirrors what we do with auth, where |
|
@gnarf thank you for the review, I always appreciate it! Regarding
That's not a side of things I'd considered, I appreciate your bringing it up. However at the moment we wrap superagent, and I'm hesitant to introduce too much that would be specific to the fetch API even though it will be more broadly applicable. At present the HTTP transport mechanism is abstracted into a module that lets the user wrap or replace some or all of the request methods, and I've deliberately limited the transport-specific methods on the query builder objects themselves. I think a switch to fetch would probably entail a broader version bump that could justify making a general-purpose config method, but I think for now we'll just introduce the method at hand. |
props to @anagio, @andreasvirkus, @gnarf, @Matthewnie, @mnivoliez, and @mzalewski for the feature request, feedback & discussion
This is an interesting suggestion. We do not currently do that anywhere else in this library, but the pattern's pretty well established in other domains... I think I'm going to get a point release out that has the base functionality here and explore that further in a separate issue. |
|
Without any objection, I am going to merge this! Thanks again to y'all for raising the issue, this is a solid addition to the library's interface. |
|
Thanks for responding (and solving the multiple issues so) quickly! 💯 |
|
@andreasvirkus It is my pleasure. I'm resolving some docs issues and should get the 1.1 release out in the next day or two thanks to you all. |
In response to #299, #312, and #314, this introduces a new .setHeaders() prototype method for both WPAPI and WPRequest.
.setHeaders()allows a single WPRequest, or all requests generated from a WPAPI site client instance, to be augmented with one or more custom HTTP headers. Providing this functionality allows consumers of the wpapi library to utilize custom authentication schemes (such as JWT) with the Authentication header, to specify the preferred language of the response endpoints with the Accept-Language header, and so on. Providing a general-purpose interface is preferred in place of specifically implementing individual methods for JWT auth, language preferences, or other specific headers that a consumer of this library may want to send.Closes #299, fixes #312, fixes #314
Usage:
If you need to send additional HTTP headers along with your request (for example to provide a specific
Authorizationheader for use with alternative authentication schemes), you can use the.setHeaders()method to specify one or more headers to send with the dispatched request:You can also set headers on the WPAPI instance itself, which will then be used for all subsequent requests created from that site instance: