dart_frog icon indicating copy to clipboard operation
dart_frog copied to clipboard

feat: `dart_frog` new command

Open mtwichel opened this issue 1 year ago • 2 comments

Description

Per the roadmap: CLI new command to generate new routes and middleware.

As someone who as created a fairly large API using dart_frog, I can attest this would be a very helpful feature to have. Here's how I envision it

dart_frog new route

dart_frog new route would create a new route. Adding the --path flag would allow you to specify the path to the new route, for example

dart_frog new route --path dogs/[dogId]

Would create a file in routes/dogs/[dogId].dart unless the routes/dogs/[dogId] directory already exists, then it would create routes/dogs/[dogId]/index.dart.

The file would be:

import 'package:dart_frog/dart_frog.dart';

Response onRequest(RequestContext context) {
  // TODO Implement route logic here
  return Response(body: 'Hello <route>');
}

dart_frog new middleware

dart_frog new middleware would create a new route. Adding the --path flag would allow you to specify the path to the new middleware, for example

dart_frog new middleware --path dogs

Would create a file in routes/dogs/_middleware.dart

The file would be:

import 'package:dart_frog/dart_frog.dart';

Handler middleware(Handler handler) {
  return (context) async {
    // TODO execute code before request is handled.

    // Forward the request to the respective handler.
    final response = await handler(context);

    // TODO execute code after request is handled.

    // Return a response.
    return response;
  };
}

For either subcommand, in the event of a conflict (ie the file already exists), the CLI will display an error message.

Requirements

  • [ ] Add templates for new route and new middleware
  • [ ] Add new command to build the routes
  • [ ] Update docs and/or tutorials to use the new command

Thanks guys 💙

mtwichel avatar Aug 11 '22 00:08 mtwichel

I think this is a feature I'd be able to add and would love to contribute, I just wanted to make sure we are aligned on the use case and how it should be implemented 🙂

mtwichel avatar Aug 11 '22 00:08 mtwichel

I would like to try solving this issue if it's not assigned already

anandhakrishnanaji avatar Aug 28 '22 17:08 anandhakrishnanaji

@mtwichel thanks for opening this feature request! I think this would improve the developer experience (Related to https://github.com/VeryGoodOpenSource/dart_frog/issues/286). In addition, do you think an IDE extension with code actions and snippets would be useful?

alestiago avatar Apr 05 '23 12:04 alestiago

Updates on this: Added a design doc here as well as a POC here.

renancaraujo avatar Apr 20 '23 19:04 renancaraujo