Skip to content

Commit

Permalink
feat(cli): add basic prop of func context in the middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed May 14, 2024
1 parent 2c98e68 commit 1b15204
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 59 deletions.
32 changes: 11 additions & 21 deletions examples/with-bun/src/main.ts
Original file line number Diff line number Diff line change
@@ -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',
});
}
);
Expand All @@ -42,5 +32,5 @@ console.log(`Start server on on http://localhost:${port}`);
export default {
port,
fetch: app.fetch,
func,
func: trigger,
};
6 changes: 3 additions & 3 deletions packages/main/src/command/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
71 changes: 36 additions & 35 deletions packages/main/src/lib.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,54 +15,52 @@ export class Nammatham {
}
}

// export interface HttpTriggerOptions {
// authLevel?: 'anonymous' | 'function' | 'admin';
// inputs?: Record<string, unknown>;
// outputs?: Record<string, unknown>;
// }

// type Env<Inputs extends Record<string, unknown> = any, Outputs extends Record<string, unknown> = any> = {
// Variables: {
// inputs: Inputs;
// outputs: Outputs;
// echo: (str: string) => string;
// };
// };

// export function createHttp(
// option: HttpTriggerOptions,
// handler: Handler<Env, any, Input, HandlerResponse<any>> | MiddlewareHandler<Env, any, Input>
// ) {
// const factory = createFactory<Env>();
// const middleware = factory.createMiddleware(async (c, next) => {
// // Do something
// await next();
// });

// const httpHandlers = factory.createHandlers(middleware, handler);
// return httpHandlers;
// }
export interface HttpTriggerOptions<TRoute extends string> {
authLevel?: 'anonymous' | 'function' | 'admin';
inputs?: Record<string, unknown>;
outputs?: Record<string, unknown>;
route?: TRoute;
}

type Env = {
type HonoEnv = {
Variables: {
func: {
invocationId: string;
inputs: Record<string, any>;
json: (data: any) => HandlerResponse<any>;
log: (message: string) => void;
};
};
};

export class HonoFunctionTrigger {
http<const TRoute extends string>(options: { route: TRoute }) {
const middleware = createMiddleware<Env>(async (c, next) => {
export class FunctionTrigger {
http<const TRoute extends string>(options: HttpTriggerOptions<TRoute>): [TRoute, MiddlewareHandler<HonoEnv>]{
const middleware = createMiddleware<HonoEnv>(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;
}
}

0 comments on commit 1b15204

Please sign in to comment.