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

Support overriding upstream HTTP method #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

geek
Copy link
Member

@geek geek commented Oct 16, 2022

I was thinking this can be a useful feature for when you want to replace a legacy API that might only support POST with more RESTful methods via the proxy. I don't know if others will find this valuable or not, so I can go either way on if we want to add it.

@geek geek added the feature New functionality or improvement label Oct 16, 2022
Copy link
Contributor

@Marsup Marsup left a comment

Choose a reason for hiding this comment

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

I, personally, have no use for it, but why not.

@@ -92,7 +92,7 @@ internals.handler = function (route, handlerOptions) {

return async function (request, h) {

const { uri, headers } = await settings.mapUri(request);
let { uri, headers, method } = await settings.mapUri(request);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather keep it that way:

Suggested change
let { uri, headers, method } = await settings.mapUri(request);
const { uri, headers, method = request.method } = await settings.mapUri(request);

Copy link
Contributor

@kanongil kanongil Oct 17, 2022

Choose a reason for hiding this comment

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

The proposed change by @Marsup would break my existing code, that changes request.method in mapUri().

Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, will it break? I assumed the it fetches the request.method before calling mapUri(), but that is probably not the case, since it needs the result to know whether to fetch the default.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say it's working as expected: https://runkit.com/embed/03jrtcm2e10x

Copy link
Contributor

Choose a reason for hiding this comment

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

Great! Reminder to self: Get my morning cup of coffee, before reviewing code.

@@ -47,6 +47,7 @@ The proxy handler object has the following properties:
* `request` - is the incoming [request object](http://hapijs.com/api#request-object). The response from this function should be an object with the following properties:
* `uri` - the absolute proxy URI.
* `headers` - optional object where each key is an HTTP request header and the value is the header content.
* `method` - optional HTTP method to use when making request to upstream server.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add a word that it defaults to the same verb as the route?

lib/index.js Show resolved Hide resolved
Copy link
Contributor

@kanongil kanongil left a comment

Choose a reason for hiding this comment

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

There are definitely use-cases for changing the HTTP verb. I have some code, which already does this in a hacky way, by changing the value of request.method in a mapUri() hook.

However, this PR won't help me much, since I'm changing a web form GET to the equivalent POST. This transform requires the query parameters to be serialized into the request.payload, which I still can only do by modifying the request.payload during mapUri().

Maybe you can support a payload return value as well?

Copy link
Member

@Nargonath Nargonath left a comment

Choose a reason for hiding this comment

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

LGTM and I agree with the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants