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

Return reply from fastify handler #6431

Closed
wants to merge 2 commits into from

Conversation

mwoenker
Copy link

See https://www.fastify.io/docs/latest/Guides/Migration-Guide-V4/#need-to-return-reply-to-signal-a-fork-of-the-promise-chain

This fixes returning a Stream as the body of the response from an api function.

@jtoar
Copy link
Contributor

jtoar commented Sep 27, 2022

Hey @mwoenker thanks for your PR and patience! I thought I saw this brought up somewhere else, but I can't remember whether it was an issue, or on the forums or Discord. (I tried searching each briefly but couldn't find anything.) This change seems simple enough and adds up based on the doc you linked to. @dthyresson any thoughts?

@jtoar
Copy link
Contributor

jtoar commented Sep 27, 2022

Do we need to make the change in handlerCallback too?

const handlerCallback =
(reply: FastifyReply) =>
(error: Error, lambdaResult: APIGatewayProxyResult) => {
if (error) {
fastifyResponseForLambdaError(req, reply, error)
return
}
fastifyResponseForLambdaResult(reply, lambdaResult)
}

@jtoar jtoar assigned jtoar and unassigned simoncrypta Sep 27, 2022
@dthyresson dthyresson self-assigned this Sep 27, 2022
@dthyresson dthyresson self-requested a review September 27, 2022 16:24
@dthyresson
Copy link
Contributor

Thanks @mwoenker

Is there a way to test this behavior in packages/api-server/src/__tests__/requestHandlers/awsLambdaFastify.test.ts?

Something like:

  test('requestHandler replies with simple body', async () => {
    jest.spyOn(mockedReply, 'send')
    jest.spyOn(mockedReply, 'status')

    const handler = async (req, mockedReply) => {
      mockedReply = { body: { foo: 'bar' } }
      return mockedReply
    }

    await requestHandler(request, mockedReply, handler)

    expect(mockedReply.send).toHaveBeenCalledWith({ foo: 'bar' })
    expect(mockedReply.status).toHaveBeenCalledWith(200)
  })

But for a stream?

@mwoenker
Copy link
Author

mwoenker commented Oct 6, 2022

@dthyresson I've pushed a test. It tests to see if the stream is passed to send(), but also whether the reply is returned from requestHandler which is the thing that will actually change as a result of this patch. Maybe the other tests should check that as well?

@dthyresson
Copy link
Contributor

dthyresson commented Oct 31, 2022

@dac09 I think this is the issue we fixed this morning #6765. I wonder if we should add his test as well?

@dthyresson dthyresson closed this Nov 9, 2022
@jtoar
Copy link
Contributor

jtoar commented Nov 9, 2022

@mwoenker thanks for your work here! @dthyresson confirming we don't actually want to add the test here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix topic/api
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants