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

Make @Expose() in controllers optional #107

Closed
thosakwe opened this issue May 1, 2019 · 2 comments
Labels

Comments

@thosakwe
Copy link
Member

@thosakwe thosakwe commented May 1, 2019

Take the following example:

class HelloController extends Controller {
  String world() => 'Welcome to Angel!';

  @post
  Future<String> goodbye(RequestContext req) async {
     var body = await req.parseBody().then((_) => req.bodyAsMap);
     return 'Aw! See you! $body';
  }
}

produces the following routes:

  • GET /hello/world
  • POST /hello/goodbye

Why is @Expose() currently used?

It is more or less a legacy feature, inspired by what I saw in Redstone and other frameworks at the time. At this point, with the reflection capabilities provided by package:angel_container, there's little need for this in the common case.

A class named HelloThereController can produce /hello_there/* routes (see package:recase).

The same applies for public methods.

Route parameters can also be inferred for String, int, and double:

int timesTwo(int n) => n * 2

produces:

  • GET /hello/timesTwo/int:n
@thosakwe

This comment has been minimized.

Copy link
Member Author

@thosakwe thosakwe commented May 1, 2019

The implementation is not extremely hard. Currently, configureServer looks for an Expose() via findExpose. The simplest way to patch this up is to just modify findExpose to return a default value based on the name of the controller.

Similar logic would also be necessary in _routeBuilder.

@thosakwe

This comment has been minimized.

Copy link
Member Author

@thosakwe thosakwe commented May 1, 2019

Annotations:

  • const Expose get = Expose(null, method: 'GET');
  • const Expose post = Expose(null, method: 'POST');
  • const Expose patch = Expose(null, method: 'PATCH');
  • const Expose put = Expose(null, method: 'PUT');
  • const Expose delete = Expose(null, method: 'DELETE');
  • const Expose head = Expose(null, method: 'HEAD');

These could potentially also function well as Expose constructors, i.e.:

@Expose.post(middleware: [...]);

And this Expose constructor:

const Expose.method(this.method, {this.middleware}) : path = null;

Note that this would involve writing the code so that if the @Expose() has a null path, an inferred path will be given.

Finally, a @noExpose annotation may also be necessary.

@thosakwe thosakwe transferred this issue from angel-dart/framework Jun 12, 2019
@thosakwe thosakwe closed this Jul 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.