diff --git a/packages/api-server/package.json b/packages/api-server/package.json index 4a6657005f69..1f0e61c6cd55 100644 --- a/packages/api-server/package.json +++ b/packages/api-server/package.json @@ -33,6 +33,7 @@ "@fastify/static": "6.12.0", "@fastify/url-data": "5.4.0", "@redwoodjs/project-config": "6.0.7", + "@whatwg-node/server": "0.9.16", "ansi-colors": "4.1.3", "chalk": "4.1.2", "chokidar": "3.5.3", diff --git a/packages/api-server/src/plugins/lambdaLoader.ts b/packages/api-server/src/plugins/lambdaLoader.ts index c345150c36b4..2fd5797eb0dc 100644 --- a/packages/api-server/src/plugins/lambdaLoader.ts +++ b/packages/api-server/src/plugins/lambdaLoader.ts @@ -1,5 +1,6 @@ import path from 'path' +import { createServerAdapter } from '@whatwg-node/server' import c from 'ansi-colors' import type { Handler } from 'aws-lambda' import fg from 'fast-glob' @@ -82,6 +83,43 @@ interface LambdaHandlerRequest extends RequestGenericInterface { } } +// const myServerAdapter = createServerAdapter((_request: Request) => { +// console.log(`👉 \n ~ file: lambdaLoader.ts:87 ~ _request:`, _request) +// console.log( +// `👉 \n ~ file: lambdaLoader.ts:90 ~ LAMBDA_FUNCTIONS:`, +// LAMBDA_FUNCTIONS +// ) +// return LAMBDA_FUNCTIONS['bazinga'] +// }) + +export const fetchRequestHandler = async ( + req: FastifyRequest, + reply: FastifyReply +): Promise => { + const { routeName } = req.params + + // @ts-expect-error handle errors later + const myServerAdapter = createServerAdapter(LAMBDA_FUNCTIONS[routeName]) + + const response = await myServerAdapter.handleNodeRequest(req, { + req, + reply, + }) + + console.log(`👉 \n ~ file: lambdaLoader.ts:99 ~ response:`, response) + + response.headers.forEach((value, key) => { + reply.header(key, value) + }) + + reply.status(response.status) + + // Fastify doesn't accept `null` as a response body + reply.send(response.body || undefined) + + return reply +} + /** This will take a fastify request Then convert it to a lambdaEvent, and pass it to the the appropriate handler for the routeName diff --git a/packages/api-server/src/plugins/withFunctions.ts b/packages/api-server/src/plugins/withFunctions.ts index d985fcbb0697..bb07bbe6af28 100644 --- a/packages/api-server/src/plugins/withFunctions.ts +++ b/packages/api-server/src/plugins/withFunctions.ts @@ -5,7 +5,11 @@ import fastifyRawBody from 'fastify-raw-body' import { loadFastifyConfig } from '../fastify' import type { ApiServerArgs } from '../types' -import { lambdaRequestHandler, loadFunctionsFromDist } from './lambdaLoader' +import { + fetchRequestHandler, + lambdaRequestHandler, + loadFunctionsFromDist, +} from './lambdaLoader' const withFunctions = async ( fastify: FastifyInstance, @@ -31,6 +35,9 @@ const withFunctions = async ( await configureFastify(fastify, { side: 'api', ...options }) } + fastify.all(`/fetchReq`, fetchRequestHandler) + + // fastify.all(`${apiRootPath}:routeName`, createServerAdapter(genericRequestHandler)) fastify.all(`${apiRootPath}:routeName`, lambdaRequestHandler) fastify.all(`${apiRootPath}:routeName/*`, lambdaRequestHandler) diff --git a/yarn.lock b/yarn.lock index 020826005a9a..5d79c32e527b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7920,6 +7920,7 @@ __metadata: "@types/qs": 6.9.10 "@types/split2": 4.2.3 "@types/yargs": 17.0.31 + "@whatwg-node/server": 0.9.16 ansi-colors: 4.1.3 aws-lambda: 1.0.7 chalk: 4.1.2