From 1b152049aaae38772f5eb5e88af47d45f39b0d95 Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Wed, 15 May 2024 00:22:56 +0700 Subject: [PATCH] feat(cli): add basic prop of func context in the middleware --- examples/with-bun/src/main.ts | 32 ++++------ packages/main/src/command/config/config.ts | 6 +- packages/main/src/lib.ts | 71 +++++++++++----------- 3 files changed, 50 insertions(+), 59 deletions(-) diff --git a/examples/with-bun/src/main.ts b/examples/with-bun/src/main.ts index b4fc7ec..ac6f79f 100644 --- a/examples/with-bun/src/main.ts +++ b/examples/with-bun/src/main.ts @@ -1,37 +1,27 @@ import { Hono } from 'hono'; import { logger } from 'hono/logger'; -import { HonoFunctionTrigger } from 'nammatham'; +import { FunctionTrigger } from 'nammatham'; // DO NOT SET `basePath` for Hono App, Azure Functions will handle it const app = new Hono(); app.use(logger()); -const func = new HonoFunctionTrigger(); +const trigger = new FunctionTrigger(); app.all( - ...func.http({ + ...trigger.http({ route: '/SimpleHttpTrigger', }), c => { - console.log('SimpleHttpTrigger'); - const userAgent = c.req.header('user-agent'); - console.log(`user agent is: ${userAgent}`); + // Getting the function context + const context = c.var.func; - const invocationId = c.req.header('x-azure-functions-invocationid'); - console.log(`invocationid is: ${invocationId}`); + context.log('JavaScript HTTP trigger function processed a request.'); + context.log(`invocationid is: ${context.invocationId}`); + context.log(`The third log message.`); - return c.json({ - Outputs: { - res: { - StatusCode: 200, - Body: 'my world', - headers: { - header1: 'header1Val', - }, - }, - }, - Logs: ['test log1', 'test log2'], - // ReturnValue: '{"hello":"world"}', + return context.json({ + hello: 'world', }); } ); @@ -42,5 +32,5 @@ console.log(`Start server on on http://localhost:${port}`); export default { port, fetch: app.fetch, - func, + func: trigger, }; diff --git a/packages/main/src/command/config/config.ts b/packages/main/src/command/config/config.ts index 73551d1..4061d57 100644 --- a/packages/main/src/command/config/config.ts +++ b/packages/main/src/command/config/config.ts @@ -34,13 +34,13 @@ export function constructHostConfig(config: NammathamConfigs, mode: 'dev' | 'bui description, /** * Enable forwarding HTTP request to the custom handler - * + * * If this is set to true, the custom handler will receive the HTTP request and response objects * However, nammatham will not be not needs to be used in the custom handler - * + * * Another reason for loggin, disabling this option will log on Azure Application Insights properly * @ref https://github.com/Azure/azure-functions-host/issues/6637 - * + * * @ref https://learn.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers#http-only-function */ enableForwardingHttpRequest: false, diff --git a/packages/main/src/lib.ts b/packages/main/src/lib.ts index 33d2767..4f85c21 100644 --- a/packages/main/src/lib.ts +++ b/packages/main/src/lib.ts @@ -1,5 +1,8 @@ +import type { Handler, Input } from 'hono'; +import type { HandlerResponse, MiddlewareHandler } from 'hono/types'; + import { Hono } from 'hono'; -import { createMiddleware } from 'hono/factory'; +import { createFactory, createMiddleware } from 'hono/factory'; export class Nammatham { protected hono: Hono; @@ -12,54 +15,52 @@ export class Nammatham { } } -// export interface HttpTriggerOptions { -// authLevel?: 'anonymous' | 'function' | 'admin'; -// inputs?: Record; -// outputs?: Record; -// } - -// type Env = any, Outputs extends Record = any> = { -// Variables: { -// inputs: Inputs; -// outputs: Outputs; -// echo: (str: string) => string; -// }; -// }; - -// export function createHttp( -// option: HttpTriggerOptions, -// handler: Handler> | MiddlewareHandler -// ) { -// const factory = createFactory(); -// const middleware = factory.createMiddleware(async (c, next) => { -// // Do something -// await next(); -// }); - -// const httpHandlers = factory.createHandlers(middleware, handler); -// return httpHandlers; -// } +export interface HttpTriggerOptions { + authLevel?: 'anonymous' | 'function' | 'admin'; + inputs?: Record; + outputs?: Record; + route?: TRoute; +} -type Env = { +type HonoEnv = { Variables: { func: { invocationId: string; inputs: Record; + json: (data: any) => HandlerResponse; + log: (message: string) => void; }; }; }; -export class HonoFunctionTrigger { - http(options: { route: TRoute }) { - const middleware = createMiddleware(async (c, next) => { +export class FunctionTrigger { + http(options: HttpTriggerOptions): [TRoute, MiddlewareHandler]{ + const middleware = createMiddleware(async (c, next) => { + const logMessages: string[] = []; c.set('func', { invocationId: c.req.header('x-azure-functions-invocationid') || '', inputs: {}, + log: (message: string) => { + logMessages.push(message); + }, + json: data => { + return c.json({ + Outputs: { + res: { + StatusCode: 200, + Body: data, + headers: { + 'content-type': 'application/json', + }, + }, + }, + Logs: logMessages, + // ReturnValue: '{"hello":"world"}', + }); + }, }); await next(); }); - return [options.route, middleware] as const; - // console.log('options.route', options.route); - // return middleware; + return [options.route as TRoute, middleware] as const; } }