@sentzunhat/zacatl
Version:
A modular, high-performance TypeScript microservice framework for Node.js, featuring layered architecture, dependency injection, and robust validation for building scalable APIs and distributed systems.
100 lines • 3.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiServer = exports.HandlersType = void 0;
const dependency_injection_1 = require("../../../../dependency-injection/index.js");
const error_1 = require("../../../../error/index.js");
const server_config_1 = require("../types/server-config");
var HandlersType;
(function (HandlersType) {
HandlersType["HOOK"] = "hook";
HandlersType["ROUTE"] = "route";
})(HandlersType || (exports.HandlersType = HandlersType = {}));
class ApiServer {
config;
adapter;
constructor(config, adapter) {
this.config = config;
this.adapter = adapter;
}
async registerEntrypoints(entryPoints) {
await this.registerAllRestHandlers(entryPoints);
if (this.config.type === server_config_1.ServerType.GATEWAY && this.config.gateway?.proxies != null) {
for (const proxyConf of this.config.gateway.proxies) {
this.registerProxy({
upstream: proxyConf.upstream,
prefix: proxyConf.prefix ?? '/',
http2: false,
});
}
}
}
async registerHandlers(input) {
const { handlers, handlersType } = input;
await Promise.all(handlers.map(async (handler) => {
try {
if (handlersType === HandlersType.ROUTE) {
this.adapter.registerRoute(handler);
}
else if (handlersType === HandlersType.HOOK) {
this.adapter.registerHook(handler);
}
else {
throw new error_1.InternalServerError({
message: `Handler type ${handlersType} is not supported`,
reason: 'Handler type must be ROUTE or HOOK',
component: 'ApiServer',
operation: 'registerHandlers',
metadata: { handlersType },
});
}
}
catch (error) {
throw new error_1.CustomError({
message: `failed to register ${handlersType}: ${handler.constructor.name}`,
code: 500,
reason: 'handler registration failed',
error: error,
metadata: { handler: handler.constructor.name },
});
}
}));
}
async registerAllHooks(restEntryPoints) {
if (restEntryPoints.hooks == null || restEntryPoints.hooks.length === 0) {
return;
}
const hooks = (0, dependency_injection_1.resolveDependencies)(restEntryPoints.hooks);
await this.registerHandlers({
handlers: hooks,
handlersType: HandlersType.HOOK,
});
}
async registerAllRoutes(restEntryPoints) {
if (restEntryPoints.routes == null || restEntryPoints.routes.length === 0) {
return;
}
const routes = (0, dependency_injection_1.resolveDependencies)(restEntryPoints.routes);
await this.registerHandlers({
handlers: routes,
handlersType: HandlersType.ROUTE,
});
}
async registerAllRestHandlers(restEntryPoints) {
await this.registerAllHooks(restEntryPoints);
await this.registerAllRoutes(restEntryPoints);
}
registerProxy(config) {
this.adapter.registerProxy(config);
}
async listen(port) {
await this.adapter.listen(port);
}
getRawServer() {
return this.adapter.getRawServer();
}
getAdapter() {
return this.adapter;
}
}
exports.ApiServer = ApiServer;
//# sourceMappingURL=api-server.js.map