Writing a Plugin
​Guidelines​
Writing a plug-in is easy. You can provide plug-ins as either functions, or classes:
1
AngelConfigurer awesomeify({String message = 'This request was intercepted by an awesome plug-in.'}) {
2
return (Angel app) async {
3
app.fallback((req, res) async => res.write(message));
4
};
5
}
6
​
7
class MyAwesomePlugin {
8
@override
9
Future<void> configureServer(Angel app) async {
10
app.responseFinalizers.add((req, res) async {
11
res.headers['x-be-awesome'] = 'All the time :)';
12
});
13
}
14
}
15
​
16
await app.configure(MyAwesomePlugin().configureServer);
Copied!

Guidelines

  • Plugins should only do one thing, or serve one purpose.
  • Functions are preferred to classes.
  • Always need to be well-documented and thoroughly tested.
  • Make sure no other plugin already serves the purpose.
  • Use the provided Angel3 API's whenever possible. This will help your plugin resist breaking change in the future.
  • Plugins should generally be small, as they usually serve just one purpose.
  • Plugins are allowed to modify app configuration.
  • Stay away from req.rawRequest and res.rawResponse if possible. This can restrict people from using your plugin on multiple platforms.
  • Avoid checking app.isProduction; leave that to user instead.
  • Always use req.parseBody() before accessing the request body.
Finally, your plugin should expose common options in a simple way. For example, the (deprecated) compress plugin has a shortcut function, gzip, to set up GZIP compression, whereas for any other codec, you would manually have to specify additional options.
This can greatly aid readability, as there is simply less text to read in the most common cases.
1
void main() {
2
var app = Angel();
3
​
4
// Calling gzip()
5
app.responseFinalizers.add(gzip());
6
​
7
// Easier than:
8
app.responseFinalizers.add(compress('lzma', lzma));
9
}
Copied!
Last modified 1mo ago
Copy link
Contents
Guidelines