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

Middleware support #32

Closed
chimon2000 opened this issue Aug 12, 2024 · 2 comments · Fixed by #33
Closed

Middleware support #32

chimon2000 opened this issue Aug 12, 2024 · 2 comments · Fixed by #33
Labels
enhancement New feature or request released

Comments

@chimon2000
Copy link
Contributor

Feature Request

Suggestion

I would like support for middleware similar to the JavaScript SDK

import 'package:nitric_sdk/nitric.dart';
import from '../middleware show logRequest, validate'

const customersApi = api('customers', {
  middleware: [logRequest, validate],
})

final customersApi = Nitric.api(
  "main",
  opts: ApiOptions(
   middleware: [logRequest, validate],
  )
);

Value

Two reasons:

  • Feature parity
  • Support for middleware at the API level rather than the request level.

Alternatives

Currently, I am defining middleware at the request level, which is tedious.

Other info

n/a

@nitric-bot
Copy link

🎉 This issue has been resolved in version 1.4.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@chimon2000
Copy link
Contributor Author

@HomelessDinosaur it seems like middleware works differently depending on the SDK used. When using the Node version, I can add a middleware to enable cors for the entire API like so:

import { HttpMiddleware } from '@nitric/sdk'

export const corsMiddleware: HttpMiddleware = (ctx, next) => {
  ctx.res.headers['Access-Control-Allow-Origin'] = ['*']
  ctx.res.headers['Access-Control-Allow-Headers'] = [
    'Origin, Content-Type, Accept, Authorization',
  ]
  ctx.res.headers['Access-Control-Allow-Methods'] = ['GET,POST,DELETE,OPTIONS']

  return next(ctx)
}

If I apply that same version in Dart, the middleware fails to work as expected.

import 'package:nitric_sdk/nitric.dart';

final HttpHandler corsMiddleware = (ctx) {
  ctx.res.headers['Access-Control-Allow-Origin'] = ['*'];
  ctx.res.headers['Access-Control-Allow-Headers'] = [
    'Origin, Content-Type, Accept, Authorization',
  ];
  ctx.res.headers['Access-Control-Allow-Methods'] = ['GET,POST,DELETE,OPTIONS'];

  return ctx.next();
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants