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

Fastify inject method instead of fetch or axios #721

Open
2 tasks done
officefish opened this issue Mar 31, 2023 · 2 comments
Open
2 tasks done

Fastify inject method instead of fetch or axios #721

officefish opened this issue Mar 31, 2023 · 2 comments

Comments

@officefish
Copy link

officefish commented Mar 31, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

May be there is some way to accept fastify instance to NextRequest, then got it from getServerSideProps context, save it on page params and then use fastify.inject instead of fetch or axios?

I suppose it should be faster than ku_universal, axois and other libraries calls, but do not exactly understand how to pass fastify instance in render function.

I have an idea to extends RawRequest interface like

declare module 'fastify' {
    interface RawRequest {
        server: FastifyInstance
    }
} 

then give instance in gccp objects requests like :

 server.next(`/log-in`, async (app:NextServer, req:FastifyRequest, reply:FastifyReply) => {   
        // here we potentialy can accept fastify instance to req.raw
        raw.req.server = server
        await app.render(req.raw, reply.raw, '/log-in')
        reply.hijack()
})

and finally got ot from props

export const getServerSideProps: GetServerSideProps = async (context) => {
  const data = {server: context.req.server}
  return { props: { data } }
}

But unfortunatly there is no RawRequest interface in Fastify types.

@mcollina
Copy link
Member

mcollina commented Apr 2, 2023

I would recommend to implement it in JavaScript and figure out what's missing in types afterwards.

@officefish
Copy link
Author

I found a way to forward fastify instance with Incoming Message but got some undefined behavior when trying to get fastify instance in next getter method.

import { IncomingMessage } from "http"
 declare module 'http' {
     interface IncomingMessage {
         server?: FastifyInstance
 }
 }

async function routes(server:FastifyInstance)  {

    server.next(`/log-in`, async (app:NextServer, req:FastifyRequest, reply:FastifyReply) => {   
        req.raw.server = req.server
        await app.render(req.raw, reply.raw, '/log-in', {})
        reply.hijack()
    })
    
}

It will be great if plugin iteslf would be able decorate fastify instance for NextRequest when it nessecary. For example like that:

server.next('/home', { forwardFastify: true} )
or

server.nextWithFastify('/home')

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

No branches or pull requests

2 participants