IExpressHttpServer
Implement this interface to create custom HTTP server adapter for AdminForth.
Extends
Methods
authorize()
authorize(
callable): (...args) =>any
Method (middleware) to wrap express endpoints with authorization check. Adds adminUser to request object if user is authorized. Drops request with 401 status if user is not authorized.
Parameters
| Parameter | Type | Description |
|---|---|---|
callable | (...args) => any | : Function which will be called if user is authorized. |
Returns
Function
Parameters
| Parameter | Type |
|---|---|
...args | any[] |
Returns
any
Example
expressApp.get('/myApi', authorize((req, res) => {
console.log('User is authorized', req.adminUser);
res.json({ message: 'Hello World' });
}));
endpoint()
endpoint(
options):void
Method which should register endpoint in HTTP server.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | IAdminForthEndpointOptions | : Object with method, path and handler properties. |
Returns
void
Inherited from
listen()
listen(port, callback)
listen(
port,callback):void
Method to start listening on port.
Parameters
| Parameter | Type |
|---|---|
port | number |
callback | Function |
Returns
void
listen(port, host, callback)
listen(
port,host,callback):void
Parameters
| Parameter | Type |
|---|---|
port | number |
host | string |
callback | Function |
Returns
void
serve()
serve(
app):void
Call this method to serve AdminForth SPA from Express instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
app | Express | : Express instance |
Returns
void
setupSpaServer()
setupSpaServer():
void
Sets up HTTP server to serve AdminForth SPA.
if hotReload is true, it should proxy all requests and headers to Vite dev server at http://localhost:5173$\{req.url\}
otherwise it should serve AdminForth SPA from dist folder. See Express for example.
Returns
void
Inherited from
translatable()
translatable(
callable): (...args) =>any
Method (middleware) to inject translation helper into Express request object.
Parameters
| Parameter | Type |
|---|---|
callable | (...args) => any |
Returns
Function
Parameters
| Parameter | Type |
|---|---|
...args | any[] |
Returns
any
withSchema()
withSchema(
schema,callable): (...args) =>any
Registers OpenAPI schemas for a custom Express route.
Wrap this around the handler passed to app.get/post/....
If you also need authorization, make withSchema the outer wrapper:
import * as z from 'zod';
app.get('/myApi', admin.express.withSchema({
description: 'Returns current user profile',
response: z.object({ user: z.unknown() }),
}, admin.express.authorize((req, res) => {
res.json({ user: req.adminUser });
})));
Parameters
| Parameter | Type |
|---|---|
schema | IAdminForthExpressRouteSchema |
callable | (...args) => any |
Returns
Function
Parameters
| Parameter | Type |
|---|---|
...args | any[] |
Returns
any