serverstruct
Version:
Type safe and modular servers with Hono
98 lines (95 loc) • 2.82 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);
// src/index.ts
var index_exports = {};
__export(index_exports, {
createModule: () => createModule
});
module.exports = __toCommonJS(index_exports);
// src/module.ts
var import_hollywood_di = require("hollywood-di");
var import_hono = require("hono");
var Module = class {
constructor(_context, _route) {
this._context = _context;
this._route = _route;
}
app(container) {
let currentContainer = container;
const resolvedModules = {};
for (const [tokens, submodules] of this._context) {
let subContainer = currentContainer;
if (tokens && subContainer) {
subContainer = import_hollywood_di.Hollywood.createWithParent(
subContainer,
tokens.tokens,
tokens.options
);
} else if (tokens) {
subContainer = import_hollywood_di.Hollywood.create(tokens.tokens, tokens.options);
}
currentContainer = subContainer;
for (const [key, submodule] of Object.entries(submodules)) {
resolvedModules[key] = submodule.app(subContainer);
}
}
const instances = currentContainer?.instances ?? {};
return this._route(
instances,
resolvedModules
);
}
};
var ModuleBuilder = class {
constructor(app) {
this.app = app;
this.context = [];
}
use() {
return this;
}
provide(tokens, options) {
this.context.push([{ tokens, options }, {}]);
return this;
}
submodules(modules) {
if (!this.context.length) this.context.push([void 0, modules]);
else {
const [, submodules] = this.context[this.context.length - 1];
Object.assign(submodules, modules);
}
return this;
}
route(fn) {
return new Module(this.context, (container, modules) => {
return fn(
this.app,
container,
modules
);
});
}
};
function createModule(app) {
return new ModuleBuilder(app ?? new import_hono.Hono());
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createModule
});