Skip to main content

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

ParameterTypeDescription
callable(...args) => any: Function which will be called if user is authorized.

Returns

Function

Parameters
ParameterType
...argsany[]
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

ParameterTypeDescription
optionsIAdminForthEndpointOptions: Object with method, path and handler properties.

Returns

void

Inherited from

IHttpServer . endpoint


listen()

listen(port, callback)

listen(port, callback): void

Method to start listening on port.

Parameters
ParameterType
portnumber
callbackFunction
Returns

void

listen(port, host, callback)

listen(port, host, callback): void

Parameters
ParameterType
portnumber
hoststring
callbackFunction
Returns

void


serve()

serve(app): void

Call this method to serve AdminForth SPA from Express instance.

Parameters

ParameterTypeDescription
appExpress: 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

IHttpServer . setupSpaServer


translatable()

translatable(callable): (...args) => any

Method (middleware) to inject translation helper into Express request object.

Parameters

ParameterType
callable(...args) => any

Returns

Function

Parameters
ParameterType
...argsany[]
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

ParameterType
schemaIAdminForthExpressRouteSchema
callable(...args) => any

Returns

Function

Parameters
ParameterType
...argsany[]
Returns

any