@samitouri / QOSami-HFS / commits / be6a4ade

doc: improved middleware section

Massimo Melina committed Aug 31, 2025 at 10:47 UTC be6a4ade04acd55994d583ffdc9cfff76b2a7a91
1 file changed +6 -7
dev-plugins.md
+6 -7
@@ -55,6 +55,7 @@ In this document we define some types using pseudo-typescript syntax.
55 We use some predefined types for brevity:
56
57 `Promisable<Type> = Type | Promise<Type>` where Type can be wrapped in a promise or not (direct).
58 +When this is used for the return type, the function *can* be async.
59
60 `Functionable<Type, Arguments> = Type | ((...args: Arguments) => Type)` where Type can be returned by a function or not (direct).
61
@@ -136,20 +137,18 @@ used must be strictly JSON (thus, no single quotes, only double quotes for strin
137 You can also include external files, by entering a full URL. Multiple files can be specified as `['file1.css', 'file2.css']`.
138 - `frontend_js: string | string[]` path to one or more js files that you want the frontend to load. These are to be placed in the `public` folder (refer below).
139 You can also include external files, by entering a full URL.
139 -- `middleware: (Context) => Promisable<void | function>` a function that will be used as a middleware: use this to interfere with http activity.
140 -
140 +- `middleware: (Context) => Promisable<void | function>` a function that will be used as middleware: use this to interfere with http activity.
141 + E.g.:
142 ```js
143 exports.middleware = ctx => {
144 ctx.body = "You are in the wrong place"
145 ctx.status = 404
146 }
147 ```
147 - You'll find more examples by studying plugins like `antidos` or `antibrute`.
148 To interrupt other middlewares on this http request, call `ctx.stop()`.
149 - In past versions stop() was not available, and to get the same effect you'd `return true`, therefore a possible way
150 - to be compatible with older versions is to `return ctx.stop?.() || true`.
151 -
152 - If you want to execute something in the "upstream" of middlewares, return a function. This function can be async.
149 + If you want to execute something in the "upstream" of middlewares, return a function.
150 + Upstream you can access the response calculated by HFS and other middlewares, so you'll find both the status and body set.
151 + See more at https://github.com/rejetto/hfs/wiki/Middlewares .
152 You can read more in [the ctx object](#the-ctx-object) section.
153
154 - `unload: function` called when unloading a plugin. This is a good place for example to clearInterval().