This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Plugin API
Luiz Henrique Farcic Mineo edited this page Jul 2, 2014
·
2 revisions
Redstone plugins can dynamically create new routes, interceptors, error handlers, parameter providers and response processors.
Note: See redstone_mapper for a more complete serialization plugin
For example, if your app often needs to convert from json data to Dart objects, like this:
@app.Route("/user", methods: const[app.POST])
printUser(@app.Body() Map json) {
User user = new User();
user.fromJson(json);
...
}
You can build a plugin to do this job for you. Example:
class FromJson {
const FromJson();
}
FromJsonPlugin(app.Manager manager) {
manager.addParameterProvider(FromJson, (metadata, type, handlerName, paramName, req, injector) {
if (req.bodyType != app.JSON) {
throw new app.RequestException(
"FromJson plugin - $handlerName", "content-type must be 'application/json'");
}
ClassMirror clazz = reflectClass(type);
InstanceMirror obj = clazz.newInstance(const Symbol(""), const []);
obj.invoke(#fromJson, [req.body]);
return obj.reflectee;
});
}
Now, if you install FromJsonPlugin
, you can use the @FromJson
annotation:
@app.Route("/user", methods: const[app.POST])
printUser(@FromJson() User user) {
...
}
main() {
app.addPlugin(FromJsonPlugin);
app.setupConsoleLog();
app.start();
}
Besides, you can also build a plugin to convert from dart objects to json data:
class ToJson {
const ToJson();
}
ToJsonPlugin(app.Manager manager) {
manager.addResponseProcessor(ToJson, (metadata, handlerName, value, injector) {
if (value == null) {
return value;
}
return value.toJson();
});
}
@app.Route("/user/find")
@ToJson()
findUser() {
return new Future(() {
...
return user;
});
}
main() {
app.addPlugin(ToJsonPlugin);
app.setupConsoleLog();
app.start();
}
-
Quick Start Guide
-
Reference
-
Serialization
-
Databases
-
MVC
-
Web Sockets