Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for v3 API & Direction #144

Open
mildronize opened this issue Dec 22, 2024 · 0 comments
Open

Proposal for v3 API & Direction #144

mildronize opened this issue Dec 22, 2024 · 0 comments
Labels

Comments

@mildronize
Copy link
Collaborator

mildronize commented Dec 22, 2024

Background

In previous versions, creating Azure Functions with Hono required repetitive and verbose setups. For instance, setting up a simple HTTP trigger looked like this:

import { Hono } from 'hono';
import { HonoAzureMiddleware, register } from 'nammatham';

// DO NOT SET `basePath` for Hono App, Azure Functions will handle it
const app = new Hono();

const func = new HonoAzureMiddleware();

app.all(...func.get('/SimpleHttpTrigger'), c => {
  // Getting the function context
  const context = c.var.context;
  context.log(`invocationid is: ${context.invocationId}`);
  return context.json({
    hello: 'world',
  });
});

export default register({
  fetch: app.fetch,
  func,
});

This approach generated separate function.json files for each endpoint, such as SimpleHttpTrigger/function.json, creating redundant configuration and coupling Azure Functions routing with Hono routing.

Observations from Existing Solutions

The Marplex/hono-azurefunc-adapter, as referenced in the official Hono documentation, simplifies this process by:

  1. Using a single function.json for multiple endpoints.
  2. Delegating routing to Hono instead of Azure Functions.

This approach is cleaner and reduces overhead by leveraging Hono for routing while using Azure Functions primarily as a handler.

Proposal

To align with best practices and streamline API usage, the new version (v3) should:

1. Use HonoAzureMiddleware as a Global Middleware

By introducing HonoAzureMiddleware globally, we can intercept and format all requests/responses to conform with Azure Functions conventions. This results in a simpler API and better scalability:

import { Hono } from 'hono';
import { HonoAzureMiddleware, register } from 'nammatham';

const func = new HonoAzureMiddleware();
const app = new Hono().use(func.middleware);

app.get('/aaa', (c) => {
  const context = c.var.context;
  context.log(`invocationid is: ${context.invocationId}`);

  return context.json({
    hello: 'world',
  });
});

export default register({
  fetch: app.fetch,
  func,
});

2. Support Native Azure Functions Triggers

To ensure flexibility, HonoAzureMiddleware should natively support Azure Functions triggers (e.g., TimerTrigger). For example:

func.timer('myTimer','*/5 * * * * *', async (c) => {
  const context = c.var.context;
  context.log(`invocationid is: ${context.invocationId}`);
});

The framework should handle trigger registration and automatically generate the corresponding function.json file (e.g., myTimer/function.json).

Benefits

  • Cleaner API: Reduces boilerplate by centralizing middleware and routing logic.
  • Unified Routing: Keeps routing logic within Hono while simplifying Azure Functions configuration.
  • Extended Functionality: Supports various Azure Functions triggers natively.
  • Improved Maintainability: Minimizes redundancy in function.json generation.
@mildronize mildronize added the v3 label Dec 22, 2024
@mildronize mildronize changed the title Proposal v3 API & Direction Proposal for v3 API & Direction Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant