Taxum / @taxum/core / extract / extension
Function: extension()
extension<
T
,B
>(key
,required?
): (req
) =>Promise
<B
extendstrue
?T
:undefined
|T
>
Defined in: extract/extension.ts:30
Extractor that extracts a raw extension from the request.
If the required
flag is not set or false, returns either the extension or undefined.
Otherwise, it throws an error if the extension is missing.
Type Parameters
T
T
B
B
extends boolean
= false
Parameters
key
ExtensionKey
<T
>
required?
B
Returns
(
req
):Promise
<B
extendstrue
?T
:undefined
|T
>
Parameters
req
Returns
Promise
<B
extends true
? T
: undefined
| T
>
Example
ts
import { extension } from "@taxum/core/extract";
import { ExtensionKey } from "@taxum/core/http";
import { m, Router } from "@taxum/core/routing";
const MY_EXTENSION = new ExtensionKey<string>("My extension");
const handler = handler([extension(MY_EXTENSION)], (extension) => {
// ...
});
const router = new Router()
.route("/users", m.get(handler));
Throws
Error if the extension is missing and required
is true.