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

Add Middleware #100

Open
mildronize opened this issue Jan 5, 2024 · 2 comments
Open

Add Middleware #100

mildronize opened this issue Jan 5, 2024 · 2 comments

Comments

@mildronize
Copy link
Collaborator

mildronize commented Jan 5, 2024

Consider to add middleware at 2 levels: app (NammathamApp) and func level.

However, NammathamApp doesn't handle request or triggers by itself, consider to add middleware for func level will be easier to manipulate.

Expected Behavior

import { initNammatham } from '@nammatham/core';
import { AzureFunctionsAdapter } from '@nammatham/azure-functions';
import { middleware, beforeMiddleware, afterMiddleware } from './middlewares';

const n = initNammatham.create(new AzureFunctionsAdapter());
export const func = n.func;

// Add middleware
func.use(middleware);

// Add middleware before each function to be invoked
func.useBefore(beforeMiddleware);

// Add middleware after each function to be invoked
func.useAfter(afterMiddleware);

// Add middleware to a specific function
export default func
  .httpGet('hello')
  .use(validationMiddleware)
  .handler(async ({ trigger, context } ) => ({ body: 'OK' }) )

Related Repo:

@mildronize mildronize added v2 Nammatham v2 Related issues and removed v2 Nammatham v2 Related issues labels Jan 5, 2024
@mildronize
Copy link
Collaborator Author

mildronize commented Jan 8, 2024

@mildronize
Copy link
Collaborator Author

Be make sure type safe during middleware

func
.httpGet('hello')
.use(( ctx, next )=> {
  return next({
    ctx,
    data: 'my injected data'
  });
})
.handler(async ({ data } ) => ({ body: data }) );
// The result will be 'my injected data'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant