@duongtrungnguyen/nestro
Version:
Service registry for Nest JS
126 lines • 4.04 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var config_builder_exports = {};
__export(config_builder_exports, {
GatewayConfigBuilder: () => GatewayConfigBuilder
});
module.exports = __toCommonJS(config_builder_exports);
var import_services = require("./services");
var import_constants = require("./constants");
var import_controllers = require("./controllers");
var import_gateway = require("./gateway.module");
var import_common2 = require("../../common");
class GatewayConfigBuilder {
constructor() {
this.globalMiddlewares = [];
this.globalGuards = [];
this.routes = [];
this.providers = [import_services.RouteHandleService, import_services.HttpProxyService, import_services.WsProxyService, import_services.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 || import_common2.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() {
import_gateway.GatewayModule.routes = this.routes;
import_gateway.GatewayModule.globalMiddlewares = this.globalMiddlewares;
this.providers.push(
{
provide: import_constants.PROXY_ROUTES_CONFIG,
useValue: this.routes
},
{
provide: import_constants.GLOBAL_GUARDS,
useValue: this.globalGuards
}
);
return {
module: import_gateway.GatewayModule,
controllers: [import_controllers.GatewayController],
providers: this.providers,
exports: this.providers
};
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
GatewayConfigBuilder
});
//# sourceMappingURL=config-builder.js.map