interface ApiRouteSchema {
    aliases?: {
        [k: string]: string | AliasFunction | (AliasFunction | string)[] | AliasRouteSchema;
    };
    authentication?: string | boolean;
    authorization?: string | boolean;
    autoAliases?: boolean;
    bodyParsers?: boolean | bodyParserOptions;
    busboyConfig?: BusboyConfig<onEventBusboyConfig<Alias>>;
    callOptions?: CallingOptions;
    camelCaseNames?: boolean;
    cors?: CorsOptions;
    debounceTime?: number;
    etag?: boolean | "weak" | "strong" | ETagFunction;
    logging?: boolean;
    mappingPolicy?: "all" | "restrict";
    mergeParams?: boolean;
    name?: string;
    onAfterCall?: onAfterCall;
    onBeforeCall?: onBeforeCall;
    onError?: ((req, res, error) => void);
    openapi?: false | ApiRouteOpenApi;
    path: string;
    rateLimit?: RateLimitSettings;
    use?: (routeMiddleware | routeMiddlewareError)[];
    whitelist?: (string | RegExp)[];
}

Hierarchy

  • CommonSettingSchema
    • ApiRouteSchema

Properties

aliases?: {
    [k: string]: string | AliasFunction | (AliasFunction | string)[] | AliasRouteSchema;
}

You can use alias names instead of action names. You can also specify the method. Otherwise it will handle every method types.
Using named parameters in aliases is possible. Named parameters are defined by prefixing a colon to the parameter name (:name).

Type declaration

  • [k: string]: string | AliasFunction | (AliasFunction | string)[] | AliasRouteSchema
authentication?: string | boolean

To enable the support for authentication, you need to do something similar to what is describe in the Authorization paragraph.
Also in this case you have to:

  1. Set authentication: true in your routes
  2. Define your custom authenticate method in your service
  3. The returned value will be set to the ctx.meta.user property. You can use it in your actions to get the logged in user entity.
    From v0.10.3: You can define custom authentication and authorization methods for every routes. In this case you should set the method name instead of true value.
authorization?: string | boolean

You can implement authorization. Do 2 things to enable it.

  1. Set authorization: true in your routes.
  2. Define the authorize method in service.
    From v0.10.3: You can define custom authentication and authorization methods for every routes. In this case you should set the method name instead of true value.
autoAliases?: boolean

The auto-alias feature allows you to declare your route alias directly in your services.
The gateway will dynamically build the full routes from service schema. Gateway will regenerate the routes every time a service joins or leaves the network.
Use whitelist parameter to specify services that the Gateway should track and build the routes.

bodyParsers?: boolean | bodyParserOptions

Parse incoming request bodies, available under the ctx.params property

busboyConfig?: BusboyConfig<onEventBusboyConfig<Alias>>

API Gateway has implemented file uploads.
You can upload files as a multipart form data (thanks to busboy library) or as a raw request body.
In both cases, the file is transferred to an action as a Stream.
In multipart form data mode you can upload multiple files, as well.
Please note: you have to disable other body parsers in order to accept files.

callOptions?: CallingOptions

The route has a callOptions property which is passed to broker.call. So you can set timeout, retries or fallbackResponse options for routes.

camelCaseNames?: boolean

If alias handler not found, api will try to call service by action name
This option will convert request url to camelCase before call action

Example

`/math/sum-all` => `math.sumAll`
@default: null
cors?: CorsOptions

Cross-origin resource sharing configuration (using module cors)

Example

{
// Configures the Access-Control-Allow-Origin CORS header.
origin: "*", // ["http://localhost:3000", "https://localhost:4000"],
// Configures the Access-Control-Allow-Methods CORS header.
methods: ["GET", "OPTIONS", "POST", "PUT", "DELETE"],
// Configures the Access-Control-Allow-Headers CORS header.
allowedHeaders: [],
// Configures the Access-Control-Expose-Headers CORS header.
exposedHeaders: [],
// Configures the Access-Control-Allow-Credentials CORS header.
credentials: false,
// Configures the Access-Control-Max-Age CORS header.
maxAge: 3600
}

See

https://moleculer.services/docs/0.14/moleculer-web.html#CORS-headers

debounceTime?: number

Debounce wait time before call to regenerated aliases when got event "$services.changed"

Default

500
etag?: boolean | "weak" | "strong" | ETagFunction

The etag option value can be false, true, weak, strong, or a custom Function

logging?: boolean

Enable/disable logging

Default

true
mappingPolicy?: "all" | "restrict"

The route has a mappingPolicy property to handle routes without aliases.
Available options:
all - enable to request all routes with or without aliases (default)
restrict - enable to request only the routes with aliases.

mergeParams?: boolean

To disable parameter merging set mergeParams: false in route settings.
Default is true

name?: string

From v0.10.2
Support multiple routes with the same path.
You should give a unique name for the routes if they have same path.

onAfterCall?: onAfterCall

You could manipulate the data in onAfterCall.
Must always return the new or original data.

onBeforeCall?: onBeforeCall

The route has before & after call hooks. You can use it to set ctx.meta, access req.headers or modify the response data.

onError?: ((req, res, error) => void)

You can add route-level & global-level custom error handlers.
In handlers, you must call the res.end. Otherwise, the request is unhandled.

Type declaration

    • (req, res, error): void
    • Parameters

      • req: IncomingMessage
      • res: ServerResponse<IncomingMessage>
      • error: Error

      Returns void

openapi?: false | ApiRouteOpenApi
path: string

Path prefix to this route

rateLimit?: RateLimitSettings

The Moleculer-Web has a built-in rate limiter with a memory store.

use?: (routeMiddleware | routeMiddlewareError)[]

It supports Connect-like middlewares in global-level, route-level & alias-level.
Signature: function (req, res, next) {...}.
Signature: function (err, req, res, next) {...}.
For more info check express middleware

whitelist?: (string | RegExp)[]

If you don’t want to publish all actions, you can filter them with whitelist option.
Use match strings or regexp in list. To enable all actions, use "**" item.
"posts.*": Access any actions in 'posts' service
"users.list": Access call only the 'users.list' action
/^math.\w+$/: Access any actions in 'math' service