Skip to content

Commit

Permalink
feat: HandleConfigOptions and easyRouteKey
Browse files Browse the repository at this point in the history
  • Loading branch information
NamesMT committed Sep 4, 2024
1 parent d1f78fc commit b9b7044
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Readable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import type { Env, Hono, Schema } from 'hono'
import type { LambdaEvent } from '@namesmt/utils-lambda'
import type { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from 'aws-lambda'

import type { APIGatewayProxyStructuredResultV2 } from 'aws-lambda'
import type { LambdaContext, LambdaHandler, LambdaHandlerResult } from './types'

import { getProcessor } from './common'
import { minimalEvent } from './utils/event'

// @ts-expect-error CryptoKey missing
globalThis.crypto ??= crypto
Expand Down Expand Up @@ -53,14 +53,23 @@ function responseToStreamMetadata(res: Response) {
}
}

export interface HandleConfigOptions {
/**
* This allows you to easier invoke HTTP routes manually by parsing `event.routeKey` for method and path.
*/
easyRouteKey?: boolean
}

export function streamHandle<
E extends Env = Env,
S extends Schema = {},
BasePath extends string = '/',
>(app: Hono<E, S, BasePath>): LambdaHandler<LambdaEvent> {
>(app: Hono<E, S, BasePath>, handleConfig?: HandleConfigOptions): LambdaHandler<LambdaEvent> {
// @ts-expect-error awslambda is not a standard API
return awslambda.streamifyResponse(
async (event: LambdaEvent, responseStream: NodeJS.WritableStream, context: LambdaContext) => {
await processConfig(event, context, handleConfig)

const processor = getProcessor(event)
try {
const req = processor.createRequest(event)
Expand Down Expand Up @@ -106,15 +115,17 @@ export function streamHandle<
/**
* Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)
*/
export function handle<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(app: Hono<E, S, BasePath>): LambdaHandler<LambdaEvent, LambdaHandlerResult> {
return async (event, lambdaContext?) => {
export function handle<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'>(app: Hono<E, S, BasePath>, handleConfig?: HandleConfigOptions): LambdaHandler<LambdaEvent, LambdaHandlerResult> {
return async (event, context?) => {
await processConfig(event, context, handleConfig)

const processor = getProcessor(event)

const req = processor.createRequest(event)

const res = await app.fetch(req, {
event,
lambdaContext,
context,
})

if (res.headers.get('$HAAL-returnBody'))
Expand All @@ -123,3 +134,16 @@ export function handle<E extends Env = Env, S extends Schema = {}, BasePath exte
return processor.createResult(event, res)
}
}

async function processConfig(event: LambdaEvent, context?: LambdaContext, handleConfig?: HandleConfigOptions) {
const _eventCastV2 = event as APIGatewayProxyEventV2
if (handleConfig?.easyRouteKey) {
if (!_eventCastV2.routeKey)
return

if (!_eventCastV2.requestContext?.http?.path && !_eventCastV2.requestContext?.http?.method) {
const [method, path] = _eventCastV2.routeKey.split(' ')
minimalEvent(method, path, _eventCastV2)
}
}
}
22 changes: 22 additions & 0 deletions src/utils/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { APIGatewayProxyEventV2 } from 'aws-lambda'

/**
* Creates/populates a minimal event with path & method for routing usage purpose.
*/
export function minimalEvent(method: string, path: string, baseEvent?: APIGatewayProxyEventV2): APIGatewayProxyEventV2 {
const event = baseEvent ?? {} as any

event.requestContext ??= {
http: {
method: '',
path: '',
},
routeKey: '',
}

event.routeKey = event.requestContext.routeKey = `${method} ${path}`
event.requestContext.http.method = method
event.rawPath = event.requestContext.http.path = path

return event as APIGatewayProxyEventV2
}

0 comments on commit b9b7044

Please sign in to comment.