Add more to parseId
This commit is contained in:
parent
41e70787bc
commit
d211804cd8
6 changed files with 30 additions and 149 deletions
|
@ -10,4 +10,5 @@
|
|||
* Remove `contentType` argument in `ResponseContext.serialize`.
|
||||
* Moved `lib/hooks.dart` into `package:angel_hooks`.
|
||||
* Moved `TypedService` into `package:angel_typed_service`.
|
||||
* Completely removed the `AngelBase` class.
|
||||
* Completely removed the `AngelBase` class.
|
||||
* Removed all `@deprecated` symbols.
|
|
@ -553,6 +553,7 @@ class HookedServiceEventDispatcher {
|
|||
/// *NOTE*: Callbacks on the returned [Stream] cannot be guaranteed to run before other [listeners].
|
||||
/// Use this only if you need a read-only stream of events.
|
||||
Stream<HookedServiceEvent> asStream() {
|
||||
// TODO: Close StreamController
|
||||
var ctrl = new StreamController<HookedServiceEvent>();
|
||||
_ctrl.add(ctrl);
|
||||
listen(ctrl.add);
|
||||
|
|
|
@ -17,12 +17,6 @@ import 'server.dart' show Angel;
|
|||
|
||||
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
||||
|
||||
/// Serializes response data into a String.
|
||||
///
|
||||
/// Prefer the String Function(dynamic) syntax.
|
||||
@deprecated
|
||||
typedef String ResponseSerializer(data);
|
||||
|
||||
/// A convenience wrapper around an outgoing HTTP request.
|
||||
abstract class ResponseContext implements StreamSink<List<int>>, StringSink {
|
||||
final Map properties = {};
|
||||
|
|
|
@ -71,14 +71,6 @@ class Routable extends Router {
|
|||
/// **NOTE**: This is a broadcast stream.
|
||||
Stream<Service> get onService => _onService.stream;
|
||||
|
||||
/// Assigns a middleware to a name for convenience.
|
||||
@override
|
||||
registerMiddleware(String name, middleware) {
|
||||
assert(middleware is RequestMiddleware);
|
||||
// ignore: deprecated_member_use
|
||||
super.registerMiddleware(name, middleware);
|
||||
}
|
||||
|
||||
/// Retrieves the service assigned to the given path.
|
||||
Service service(Pattern path) =>
|
||||
_services[path] ??
|
||||
|
|
|
@ -6,7 +6,6 @@ import 'dart:convert';
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:angel_container/angel_container.dart';
|
||||
import 'package:angel_container/mirrors.dart';
|
||||
import 'package:angel_http_exception/angel_http_exception.dart';
|
||||
import 'package:angel_route/angel_route.dart';
|
||||
import 'package:combinator/combinator.dart';
|
||||
|
@ -22,12 +21,6 @@ import 'service.dart';
|
|||
|
||||
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
||||
|
||||
/// A function that binds an [Angel] server to an Internet address and port.
|
||||
///
|
||||
/// Prefer the Future<HttpServer> Function(dynamic, int) syntax.
|
||||
@deprecated
|
||||
typedef Future<HttpServer> ServerGenerator(address, int port);
|
||||
|
||||
/// A function that configures an [Angel] server in some way.
|
||||
typedef FutureOr AngelConfigurer(Angel app);
|
||||
|
||||
|
@ -54,7 +47,11 @@ class Angel extends Routable {
|
|||
|
||||
final Map _injections = {};
|
||||
|
||||
@deprecated
|
||||
/// A function that is called on every request to create a [Zone], A.K.A an asynchronous
|
||||
/// execution context.
|
||||
///
|
||||
/// The utility of zones in Angel is to safely catch errors without crashing the application;
|
||||
/// this also lets the driver wrap failures in instances of [AngelHttpException].
|
||||
Future<ZoneSpecification> Function(
|
||||
HttpRequest request, RequestContext req, ResponseContext res)
|
||||
createZoneForRequest;
|
||||
|
@ -95,11 +92,6 @@ class Angel extends Routable {
|
|||
(Platform.environment['ANGEL_ENV'] == 'production');
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
ServerGenerator get serverGenerator =>
|
||||
(_http ??= new AngelHttp(this)).serverGenerator;
|
||||
|
||||
/// Returns the parent instance of this application, if any.
|
||||
Angel get parent => _parent;
|
||||
|
||||
|
@ -150,22 +142,6 @@ class Angel extends Routable {
|
|||
/// Called by [ResponseContext]@`render`.
|
||||
ViewGenerator viewGenerator = noViewEngineConfigured;
|
||||
|
||||
/// //
|
||||
/// Use [configuration] instead.
|
||||
@deprecated
|
||||
Map get properties {
|
||||
try {
|
||||
throw new Error();
|
||||
} catch (e, st) {
|
||||
logger?.warning(
|
||||
'`properties` is deprecated, and should not be used.',
|
||||
new UnsupportedError('`properties` is deprecated.'),
|
||||
st,
|
||||
);
|
||||
}
|
||||
return configuration;
|
||||
}
|
||||
|
||||
/// The handler currently configured to run on [AngelHttpException]s.
|
||||
Function(AngelHttpException e, RequestContext req, ResponseContext res)
|
||||
errorHandler =
|
||||
|
@ -190,17 +166,6 @@ class Angel extends Routable {
|
|||
res.end();
|
||||
};
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
HttpServer get httpServer => _http?.httpServer;
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
Future<HttpServer> startServer([address, int port]) {
|
||||
_http ??= new AngelHttp(this);
|
||||
return _http.startServer(address, port);
|
||||
}
|
||||
|
||||
@override
|
||||
Route addRoute(String method, Pattern path, Object handler,
|
||||
{List middleware: const []}) {
|
||||
|
@ -301,12 +266,6 @@ class Angel extends Routable {
|
|||
this.encoders.addAll(encoders);
|
||||
}
|
||||
|
||||
/// Prefer directly setting [serializer].
|
||||
@deprecated
|
||||
void injectSerializer(String serializer(x)) {
|
||||
this.serializer = serializer;
|
||||
}
|
||||
|
||||
Future getHandlerResult(handler, RequestContext req, ResponseContext res) {
|
||||
if (handler is RequestHandler) {
|
||||
var result = handler(req, res);
|
||||
|
@ -349,22 +308,6 @@ class Angel extends Routable {
|
|||
});
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
Future<RequestContext> createRequestContext(HttpRequest request) {
|
||||
_http ??= new AngelHttp(this);
|
||||
return _http.createRequestContext(request);
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
Future<ResponseContext> createResponseContext(HttpResponse response,
|
||||
[RequestContext correspondingRequest]) {
|
||||
_http ??= new AngelHttp(this);
|
||||
|
||||
return _http.createResponseContext(response, correspondingRequest);
|
||||
}
|
||||
|
||||
/// Attempts to find a middleware by the given name within this application.
|
||||
findMiddleware(key) {
|
||||
if (requestMiddleware.containsKey(key)) return requestMiddleware[key];
|
||||
|
@ -377,23 +320,6 @@ class Angel extends Routable {
|
|||
return parent != null ? parent.findProperty(key) : null;
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
Future handleAngelHttpException(AngelHttpException e, StackTrace st,
|
||||
RequestContext req, ResponseContext res, HttpRequest request,
|
||||
{bool ignoreFinalizers: false}) {
|
||||
_http ??= new AngelHttp(this);
|
||||
return _http.handleAngelHttpException(e, st, req, res, request,
|
||||
ignoreFinalizers: ignoreFinalizers == true);
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
Future handleRequest(HttpRequest request) {
|
||||
_http ??= new AngelHttp(this);
|
||||
return _http.handleRequest(request);
|
||||
}
|
||||
|
||||
/// Runs several optimizations, *if* [isProduction] is `true`.
|
||||
///
|
||||
/// * Preprocesses all dependency injection, and eliminates the burden of reflecting handlers
|
||||
|
@ -450,21 +376,6 @@ class Angel extends Routable {
|
|||
// return closureMirror.apply(args).reflectee;
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
Future sendResponse(
|
||||
HttpRequest request, RequestContext req, ResponseContext res,
|
||||
{bool ignoreFinalizers: false}) {
|
||||
_http ??= new AngelHttp(this);
|
||||
return _http.sendResponse(request, req, res);
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
void throttle(int maxConcurrentRequests, {Duration timeout}) {
|
||||
_http?.throttle(maxConcurrentRequests, timeout: timeout);
|
||||
}
|
||||
|
||||
/// Applies an [AngelConfigurer] to this instance.
|
||||
Future configure(AngelConfigurer configurer) {
|
||||
return new Future.sync(() => configurer(this));
|
||||
|
@ -540,34 +451,4 @@ class Angel extends Routable {
|
|||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
factory Angel.custom(
|
||||
Future<HttpServer> Function(dynamic, int) serverGenerator) {
|
||||
var app = new Angel(reflector: MirrorsReflector());
|
||||
return app.._http = new AngelHttp.custom(app, serverGenerator);
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
factory Angel.fromSecurityContext(SecurityContext context) {
|
||||
var app = new Angel(reflector: MirrorsReflector());
|
||||
|
||||
app._http = new AngelHttp.custom(app, (address, int port) {
|
||||
return HttpServer.bindSecure(address, port, context);
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
/// Use the serving methods in [AngelHttp] instead.
|
||||
@deprecated
|
||||
factory Angel.secure(String certificateChainPath, String serverKeyPath,
|
||||
{String password}) {
|
||||
var app = new Angel(reflector: MirrorsReflector());
|
||||
return app
|
||||
.._http = new AngelHttp.secure(app, certificateChainPath, serverKeyPath,
|
||||
password: password);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
library angel_framework.http.service;
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:angel_http_exception/angel_http_exception.dart';
|
||||
import 'package:merge_map/merge_map.dart';
|
||||
|
||||
import '../util.dart';
|
||||
import 'hooked_service.dart' show HookedService;
|
||||
import 'metadata.dart';
|
||||
|
@ -29,7 +31,7 @@ class Providers {
|
|||
static const Providers rest = Providers(viaRest);
|
||||
|
||||
/// Represents a request over WebSockets.
|
||||
static const Providers websocket = Providers(viaWebsocket);
|
||||
static const Providers websocket = Providers(viaWebsocket);
|
||||
|
||||
/// Represents a request parsed from GraphQL.
|
||||
static const Providers graphql = Providers(viaGraphQL);
|
||||
|
@ -48,7 +50,7 @@ class Providers {
|
|||
/// Heavily inspired by FeathersJS. <3
|
||||
class Service extends Routable {
|
||||
/// A [List] of keys that services should ignore, should they see them in the query.
|
||||
static const List<String> specialQueryKeys = <String> [
|
||||
static const List<String> specialQueryKeys = <String>[
|
||||
r'$limit',
|
||||
r'$sort',
|
||||
'page',
|
||||
|
@ -121,12 +123,22 @@ class Service extends Routable {
|
|||
throw new AngelHttpException.methodNotAllowed();
|
||||
}
|
||||
|
||||
/// Transforms an [id] string into one acceptable by a service.
|
||||
toId<T>(T id) {
|
||||
/// Transforms an [id] (whether it is a String, num, etc.) into one acceptable by a service.
|
||||
T parseId<T>(id) {
|
||||
if (id == 'null' || id == null)
|
||||
return null;
|
||||
else if (T == String)
|
||||
return id.toString() as T;
|
||||
else if (T == int)
|
||||
return int.parse(id.toString()) as T;
|
||||
else if (T == bool)
|
||||
return (id == true || id?.toString() == 'true') as T;
|
||||
else if (T == double)
|
||||
return int.parse(id.toString()) as T;
|
||||
else if (T == num)
|
||||
return num.parse(id.toString()) as T;
|
||||
else
|
||||
return id;
|
||||
return id as T;
|
||||
}
|
||||
|
||||
/// Generates RESTful routes pointing to this class's methods.
|
||||
|
@ -181,7 +193,7 @@ class Service extends Routable {
|
|||
get(
|
||||
'/:id',
|
||||
(RequestContext req, res) => this.read(
|
||||
toId(req.params['id']),
|
||||
parseId(req.params['id']),
|
||||
mergeMap([
|
||||
{'query': req.query},
|
||||
restProvider,
|
||||
|
@ -195,7 +207,7 @@ class Service extends Routable {
|
|||
patch(
|
||||
'/:id',
|
||||
(RequestContext req, res) => req.lazyBody().then((body) => this.modify(
|
||||
toId(req.params['id']),
|
||||
parseId(req.params['id']),
|
||||
body,
|
||||
mergeMap([
|
||||
{'query': req.query},
|
||||
|
@ -211,7 +223,7 @@ class Service extends Routable {
|
|||
post(
|
||||
'/:id',
|
||||
(RequestContext req, res) => req.lazyBody().then((body) => this.update(
|
||||
toId(req.params['id']),
|
||||
parseId(req.params['id']),
|
||||
body,
|
||||
mergeMap([
|
||||
{'query': req.query},
|
||||
|
@ -225,7 +237,7 @@ class Service extends Routable {
|
|||
put(
|
||||
'/:id',
|
||||
(RequestContext req, res) => req.lazyBody().then((body) => this.update(
|
||||
toId(req.params['id']),
|
||||
parseId(req.params['id']),
|
||||
body,
|
||||
mergeMap([
|
||||
{'query': req.query},
|
||||
|
@ -254,7 +266,7 @@ class Service extends Routable {
|
|||
delete(
|
||||
'/:id',
|
||||
(RequestContext req, res) => this.remove(
|
||||
toId(req.params['id']),
|
||||
parseId(req.params['id']),
|
||||
mergeMap([
|
||||
{'query': req.query},
|
||||
restProvider,
|
||||
|
|
Loading…
Reference in a new issue