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

inject request and response into controller #13

Merged
merged 1 commit into from
Dec 21, 2023
Merged
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
49 changes: 35 additions & 14 deletions yaroo/lib/http/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import 'dart:async';

import 'package:meta/meta_meta.dart';
import 'package:yaroo/src/_reflector/reflector.dart';
import 'package:yaroo/src/core.dart';

import 'http.dart';

export 'package:pharaoh/pharaoh.dart'
show
Request,
Expand All @@ -20,23 +21,43 @@
useShelfMiddleware;

@inject
abstract class BaseController extends AppInstance {}
abstract class ApplicationController extends AppInstance {
late final Request request;

@inject
abstract class ServiceProvider extends AppInstance {
static List<Type> get defaultProviders => [AppServiceProvider];
late final Response response;

FutureOr<void> boot();
}
Map<String, dynamic> get params => request.params;

Check warning on line 29 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L29

Added line #L29 was not covered by tests

Map<String, dynamic> get query => request.query;

Check warning on line 31 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L31

Added line #L31 was not covered by tests

Map<String, dynamic> get headers => request.headers;

Check warning on line 33 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L33

Added line #L33 was not covered by tests

@Target({TargetKind.parameter})
class Param {
const Param(String name);
get body => request.body;

Check warning on line 35 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L35

Added line #L35 was not covered by tests

Response badRequest([String? message]) {

Check warning on line 37 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L37

Added line #L37 was not covered by tests
const status = 422;
if (message == null) return response.status(status);
return response.json({'message': message}, statusCode: status);

Check warning on line 40 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L39-L40

Added lines #L39 - L40 were not covered by tests
}

Response notFound([String? message]) {

Check warning on line 43 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L43

Added line #L43 was not covered by tests
const status = 404;
if (message == null) return response.status(status);
return response.json({'message': message}, statusCode: status);

Check warning on line 46 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L45-L46

Added lines #L45 - L46 were not covered by tests
}

Response jsonResponse(data, {int statusCode = 200}) {
return response.json(data, statusCode: statusCode);

Check warning on line 50 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L49-L50

Added lines #L49 - L50 were not covered by tests
}

Response redirectTo(String url, {int statusCode = 302}) {
return response.redirect(url, statusCode);

Check warning on line 54 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L53-L54

Added lines #L53 - L54 were not covered by tests
}
}

@Target({TargetKind.parameter})
class Body {
final String? param;
@inject
abstract class ServiceProvider extends AppInstance {
static List<Type> get defaultProviders => [AppServiceProvider];

Check warning on line 60 in yaroo/lib/http/http.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/http/http.dart#L60

Added line #L60 was not covered by tests

const Body({this.param});
FutureOr<void> boot();
}
7 changes: 4 additions & 3 deletions yaroo/lib/src/_reflector/reflector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
final method = defn.$2;

final ctrlMirror = inject.reflectType(type) as r.ClassMirror;
if (ctrlMirror.superclass?.reflectedType != BaseController) {
if (ctrlMirror.superclass?.reflectedType != ApplicationController) {
throw ArgumentError('$type must extend BaseController');
}

Expand All @@ -74,8 +74,9 @@
throw ArgumentError('$type does not have method #${symbolToString(method)}');
}

// final parameters = actualMethod.parameters.map((e) => e.reflectedType);
// print(parameters);
if (actualMethod.parameters.isNotEmpty) {
throw ArgumentError.value('$type.${actualMethod.simpleName}', null, 'Controller methods cannot have parameters');

Check warning on line 78 in yaroo/lib/src/_reflector/reflector.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/_reflector/reflector.dart#L78

Added line #L78 was not covered by tests
}

return ControllerMethod(defn);
}
47 changes: 23 additions & 24 deletions yaroo/lib/src/_router/definition.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:meta/meta.dart';
import 'package:pharaoh/pharaoh.dart';

import '../../../http/http.dart';
import '../_reflector/reflector.dart';
import '../core.dart';
import 'utils.dart';

enum RouteDefinitionType { route, group, middleware }
Expand Down Expand Up @@ -46,10 +46,27 @@
typedef ControllerMethodDefinition = (Type controller, Symbol symbol);

class ControllerMethod {
final ControllerMethodDefinition classMethod;
final List<Type> parameters = [];
final ControllerMethodDefinition method;

ControllerMethod(this.classMethod);
String get methodName => symbolToString(method.$2);

Check warning on line 51 in yaroo/lib/src/_router/definition.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/_router/definition.dart#L51

Added line #L51 was not covered by tests

Type get controller => method.$1;

Check warning on line 53 in yaroo/lib/src/_router/definition.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/_router/definition.dart#L53

Added line #L53 was not covered by tests

ControllerMethod(this.method);
}

class ControllerMethodParam {
final String name;
final Type type;
final bool required;
final List<Object> metadata;

const ControllerMethodParam(

Check warning on line 64 in yaroo/lib/src/_router/definition.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/_router/definition.dart#L64

Added line #L64 was not covered by tests
this.name,
this.type, {
this.metadata = const [],
this.required = true,
});
}

class ControllerRouteMethodDefinition extends RouteDefinition {
Expand All @@ -63,12 +80,9 @@

@override
void commit(Spanner spanner) {
final handler = ApplicationFactory.buildControllerMethod(method);

Check warning on line 83 in yaroo/lib/src/_router/definition.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/_router/definition.dart#L83

Added line #L83 was not covered by tests
for (final routeMethod in route.methods) {
spanner.addRoute(
routeMethod,
route.path,
useRequestHandler(_controllerHandler(method)),
);
spanner.addRoute(routeMethod, route.path, useRequestHandler(handler));

Check warning on line 85 in yaroo/lib/src/_router/definition.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/_router/definition.dart#L85

Added line #L85 was not covered by tests
}
}
}
Expand Down Expand Up @@ -134,18 +148,3 @@
spanner.addRoute(method, path, useRequestHandler(handler));
}
}

RequestHandler _controllerHandler(ControllerMethod method) {
final defn = method.classMethod;

return (req, res) async {
final instance = createNewInstance<BaseController>(defn.$1);
final mirror = inject.reflect(instance);

final result = await Future.sync(
() => mirror.invoke(symbolToString(defn.$2), [req, res]),
);

return result;
};
}
24 changes: 20 additions & 4 deletions yaroo/lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,15 @@

List<Middleware> get globalMiddlewares => [bodyParser];

Future<void> bootstrap({
bool start_server = true,
}) async {
Future<void> bootstrap({bool listen = true}) async {

Check warning on line 90 in yaroo/lib/src/core.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/core.dart#L90

Added line #L90 was not covered by tests
if (dbConfig != null) {
DB.init(dbConfig!);
await DB.defaultDriver.connect();
}

await _bootstrapComponents(appConfig);

if (start_server) await startServer();
if (listen) await startServer();

Check warning on line 98 in yaroo/lib/src/core.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/core.dart#L98

Added line #L98 was not covered by tests
}

Future<void> startServer() async {
Expand Down Expand Up @@ -128,4 +126,22 @@
final application = (instanceFromRegistry<Application>() as _YarooAppImpl);
return request(application._createPharaohInstance());
}

static RequestHandler buildControllerMethod(ControllerMethod method) {
return (req, res) async {
final methodName = method.methodName;
final instance = createNewInstance<ApplicationController>(method.controller);
final mirror = inject.reflect(instance);

Check warning on line 134 in yaroo/lib/src/core.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/core.dart#L130-L134

Added lines #L130 - L134 were not covered by tests

mirror
..invokeSetter('request', req)
..invokeSetter('response', res);

Check warning on line 138 in yaroo/lib/src/core.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/core.dart#L137-L138

Added lines #L137 - L138 were not covered by tests

methodCall() => mirror.invoke(methodName, []);

Check warning on line 140 in yaroo/lib/src/core.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/core.dart#L140

Added line #L140 was not covered by tests

final result = await Future.sync(methodCall);

Check warning on line 142 in yaroo/lib/src/core.dart

View check run for this annotation

Codecov / codecov/patch

yaroo/lib/src/core.dart#L142

Added line #L142 was not covered by tests

return result;
};
}
}
2 changes: 1 addition & 1 deletion yaroo/test/router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:yaroo/yaroo.dart';

import './router_test.reflectable.dart';

class TestController extends BaseController {
class TestController extends ApplicationController {
void create() {}

void index() {}
Expand Down