Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkgs/shelf_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,29 @@
import 'dart:collection' show UnmodifiableMapView;
import 'dart:convert';

import 'package:http_methods/http_methods.dart';
import 'package:meta/meta.dart' show sealed;
import 'package:shelf/shelf.dart';

import 'trie.dart';

/// Returns true if [method] is a valid HTTP method.
bool isHttpMethod(String method) {
switch (method.toUpperCase()) {
case 'GET':
case 'HEAD':
case 'POST':
case 'PUT':
case 'DELETE':
case 'CONNECT':
case 'OPTIONS':
case 'TRACE':
case 'PATCH':
return true;
default:
return false;
}
}
Comment on lines +24 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and maintainability, you could use a Set of HTTP methods for this check. Using a const set is efficient and makes the collection of valid methods explicit and easier to manage.

bool isHttpMethod(String method) {
  const validHttpMethods = <String>{
    'GET',
    'HEAD',
    'POST',
    'PUT',
    'DELETE',
    'CONNECT',
    'OPTIONS',
    'TRACE',
    'PATCH',
  };
  return validHttpMethods.contains(method.toUpperCase());
}


/// Get a URL parameter captured by the [Router].
@Deprecated('Use Request.params instead')
String params(Request request, String name) {
Expand Down
1 change: 0 additions & 1 deletion pkgs/shelf_router/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ environment:
sdk: ^3.3.0

dependencies:
http_methods: ^1.1.0
meta: ^1.3.0
shelf: ^1.0.0

Expand Down
19 changes: 18 additions & 1 deletion pkgs/shelf_router_generator/lib/src/shelf_router_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,30 @@ import 'package:analyzer/dart/element/element.dart'
import 'package:analyzer/dart/element/type.dart' show ParameterizedType;
import 'package:build/build.dart' show BuildStep, log;
import 'package:code_builder/code_builder.dart' as code;
import 'package:http_methods/http_methods.dart' show isHttpMethod;
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf_router/shelf_router.dart' as shelf_router;
import 'package:shelf_router/src/router_entry.dart' // ignore: implementation_imports
show RouterEntry;
import 'package:source_gen/source_gen.dart' as g;

/// Returns true if [method] is a valid HTTP method.
bool isHttpMethod(String method) {
switch (method.toUpperCase()) {
case 'GET':
case 'HEAD':
case 'POST':
case 'PUT':
case 'DELETE':
case 'CONNECT':
case 'OPTIONS':
case 'TRACE':
case 'PATCH':
return true;
default:
return false;
}
}
Comment on lines +31 to +47

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid code duplication, you should import the isHttpMethod function from the shelf_router package instead of redefining it here. The shelf_router_generator package already has a dependency on shelf_router.

You can remove this function and add the following import at the top of the file:

import 'package:shelf_router/src/router.dart' show isHttpMethod; // ignore: implementation_imports

This follows the existing pattern in this file of importing from shelf_router's src directory.


// Type checkers that we need later
const _routeType = g.TypeChecker.typeNamed(
shelf_router.Route,
Expand Down
1 change: 0 additions & 1 deletion pkgs/shelf_router_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies:
build: ^4.0.0
build_config: ^1.2.0
code_builder: ^4.2.0
http_methods: ^1.1.0
shelf: ^1.1.0
shelf_router: ^1.1.0
source_gen: ^4.0.1
Expand Down