Skip to content

feat: Added handler option.#465

Open
ShogunPanda wants to merge 1 commit into
fastify:mainfrom
ShogunPanda:custom-handler
Open

feat: Added handler option.#465
ShogunPanda wants to merge 1 commit into
fastify:mainfrom
ShogunPanda:custom-handler

Conversation

@ShogunPanda
Copy link
Copy Markdown
Contributor

Summary

Adds a new handler option to customize HTTP proxy response handling while preserving the existing default behavior.

Changes

  • Added opts.handler support in index.js.
  • Defaults to reply.from(dest, options) when no custom handler is provided.
  • Validates that handler, when provided, is a function.
  • Added runtime tests for:
    • custom response handling
    • delegating back to reply.from(dest, options)
    • invalid handler option validation
  • Added TypeScript definitions and type tests for the new option.
  • Documented the handler option in README.md.

Checklist

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
Copy link
Copy Markdown
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.

What is it different from using reply.fromParameters in preHandler ?

const { url, options } = reply.fromParameters('/updated', { ...request.params, serverId: 42 })
reply.from(url, options)

or onResponse ?

reply.from(source, {
  onResponse(request, reply, res) => {
    reply.removeHeader('content-length');
    reply.send('New body of different length');
  }
})

@ShogunPanda
Copy link
Copy Markdown
Contributor Author

@climba03003 Sometimes I want to totally skip reply.from but take advantage of all other goodies. Do you see a different way to do that?

@climba03003
Copy link
Copy Markdown
Member

@climba03003 Sometimes I want to totally skip reply.from but take advantage of all other goodies. Do you see a different way to do that?

That is exactly why you need to use reply.fromParameters.
I don't see a good use-case to change the handler only, but not using the reply.fromParameters.

Bypassing the handler means you only need to proxy the websocket? or websocket with some route?
If that is the case, reply.wsProxy also did the job for you.

@ShogunPanda
Copy link
Copy Markdown
Contributor Author

I use fromParameters.
Then I replace the final .from with the optional custom handler.
That happens AFTER checking web sockets.
In my use case I want to skip reply-from completely.

Copy link
Copy Markdown
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

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.

3 participants