aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-05-15 06:22:56 +0000
committerFuwn <[email protected]>2022-05-15 06:22:56 +0000
commit3dc5aaa698d9065ccb955fee881c49c02574f670 (patch)
treed453d3644e8c2d61f9527913ca3ee280d4b8a3b1
parentfeat(0.1.0): initial release (diff)
downloadlaurali-3dc5aaa698d9065ccb955fee881c49c02574f670.tar.xz
laurali-3dc5aaa698d9065ccb955fee881c49c02574f670.zip
feat(decorators): auto deduce callback type
-rw-r--r--README.md2
-rw-r--r--examples/my_cool_server.ts8
-rw-r--r--laurali/decorators.ts36
3 files changed, 38 insertions, 8 deletions
diff --git a/README.md b/README.md
index 2367dae..6416fb3 100644
--- a/README.md
+++ b/README.md
@@ -43,7 +43,7 @@ import {
} from "https://deno.land/x/laurali/mod.ts";
class MyCoolServer extends Server {
- /** Visit /hi */
+ /** Visit `/hi` */
@route()
hi() {
return "Hello, World!";
diff --git a/examples/my_cool_server.ts b/examples/my_cool_server.ts
index a2fb731..76657a0 100644
--- a/examples/my_cool_server.ts
+++ b/examples/my_cool_server.ts
@@ -16,7 +16,7 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
-import { Callback, callback, route, Server } from "../mod.ts";
+import { callback, route, Server } from "../mod.ts";
import * as optic from "https://deno.land/x/[email protected]/mod.ts";
/** Implement a new Laurali server */
@@ -50,7 +50,7 @@ class MyCoolServer extends Server {
return MyCoolServer.clicks;
}
- @callback(Callback.ON_PRE_ROUTE)
+ @callback()
override onPreRoute(ctx: Deno.TlsConn) {
MyCoolServer.clicks += 1;
@@ -60,12 +60,12 @@ class MyCoolServer extends Server {
);
}
- @callback(Callback.ON_POST_ROUTE)
+ @callback()
override onPostRoute() {
MyCoolServer.logger.info("Closed connection.");
}
- @callback(Callback.ON_ERROR)
+ @callback()
override onError() {
return "hi";
}
diff --git a/laurali/decorators.ts b/laurali/decorators.ts
index 1d43e9d..b1936c1 100644
--- a/laurali/decorators.ts
+++ b/laurali/decorators.ts
@@ -39,14 +39,44 @@ export const route = (path?: string) => {
* Mark the function as a callback and register it to the `Server`.
* @param callback The type of callback which the function will be called for.
*/
-export const callback = (callback: Callback) => {
+export const callback = (callback?: Callback) => {
return (
// deno-lint-ignore no-explicit-any
target: any,
- _key: string | symbol,
+ key: string | symbol,
descriptor: PropertyDescriptor,
) => {
- target.addCallback(callback, descriptor.value);
+ let type;
+
+ if (callback) {
+ type = callback;
+ } else {
+ switch (key) {
+ case "onPreRoute":
+ {
+ type = Callback.ON_PRE_ROUTE;
+ }
+ break;
+ case "onPostRoute":
+ {
+ type = Callback.ON_POST_ROUTE;
+ }
+ break;
+ case "onError":
+ {
+ type = Callback.ON_ERROR;
+ }
+ break;
+ default: {
+ throw new Error(
+ `Unknown callback type: '${key.toString()}'. Did you forget to ` +
+ "specify the callback type?`",
+ );
+ }
+ }
+ }
+
+ target.addCallback(type, descriptor.value);
return descriptor;
};