Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 committed Nov 30, 2023
1 parent 8f0e3f9 commit ff0e9c6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 38 additions & 0 deletions packages/api-server/src/plugins/lambdaLoader.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<LambdaHandlerRequest>,
reply: FastifyReply
): Promise<FastifyReply> => {
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
Expand Down
9 changes: 8 additions & 1 deletion packages/api-server/src/plugins/withFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff0e9c6

Please sign in to comment.