@duongtrungnguyen/nestro
Version:
Service registry for Nest JS
103 lines • 2.9 kB
JavaScript
import { HttpProxyService, GatewayService, RouteHandleService, WsProxyService } from "./services";
import { GLOBAL_GUARDS, PROXY_ROUTES_CONFIG } from "./constants";
import { GatewayController } from "./controllers";
import { GatewayModule } from "./gateway.module";
import { DEFAULT_PROTOCOL } from "../../common";
class GatewayConfigBuilder {
constructor() {
this.globalMiddlewares = [];
this.globalGuards = [];
this.routes = [];
this.providers = [RouteHandleService, HttpProxyService, WsProxyService, GatewayService];
}
/**
* Adds a route configuration.
*
* @param config - The route configuration, including optional targetPath as string or function.
* @returns This builder instance.
*/
route(config) {
this.routes.push({
...config,
protocol: config.protocol || DEFAULT_PROTOCOL
// Default to HTTP
});
this.registerRequestHookProviders(config);
return this;
}
/**
* Adds an HTTP route configuration.
*
* @param config - The route configuration without protocol.
* @returns This builder instance.
*/
httpRoute(config) {
return this.route({ ...config, protocol: "http" });
}
/**
* Adds a WebSocket route configuration.
*
* @param config - The route configuration without protocol.
* @returns This builder instance.
*/
wsRoute(config) {
return this.route({ ...config, protocol: "ws" });
}
/**
* Adds global middlewares to be applied to all routes.
*
* @param middlewares - Middleware classes to apply.
* @returns This builder instance.
*/
useGlobalMiddleware(...middlewares) {
this.globalMiddlewares.push(...middlewares);
return this;
}
useGlobalGuard(...guards) {
this.globalGuards.push(...guards);
return this;
}
/**
* Register providers from request hooks
*/
registerRequestHookProviders(config) {
const extractProviders = (items = []) => {
return items.map((hook) => typeof hook === "function" ? hook : hook.instance);
};
if (config.guards) {
this.providers.push(...extractProviders(config.guards));
}
if (config.middlewares) {
this.providers.push(...extractProviders(config.middlewares));
}
}
/**
* Builds the proxy module with configured routes and middlewares.
*
* @returns A dynamic module configuration.
*/
build() {
GatewayModule.routes = this.routes;
GatewayModule.globalMiddlewares = this.globalMiddlewares;
this.providers.push(
{
provide: PROXY_ROUTES_CONFIG,
useValue: this.routes
},
{
provide: GLOBAL_GUARDS,
useValue: this.globalGuards
}
);
return {
module: GatewayModule,
controllers: [GatewayController],
providers: this.providers,
exports: this.providers
};
}
}
export {
GatewayConfigBuilder
};
//# sourceMappingURL=config-builder.js.map