nitrictech / dart-sdk

Dart integration for Nitric.
https://nitric.io
Apache License 2.0
5 stars 4 forks source link

Middleware support #32

Closed chimon2000 closed 3 months ago

chimon2000 commented 3 months ago

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:

Alternatives

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

Other info

n/a

nitric-bot commented 3 months ago

:tada: This issue has been resolved in version 1.4.0 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

chimon2000 commented 3 months ago

@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();
};