serverstruct 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Eric Afes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Commandstruct
2
+
3
+ Type safe and modular servers with [Hono](https://github.com/honojs/hono).
@@ -0,0 +1,57 @@
1
+ import { InitFactory, InstantiableConstructor, RegisterTokens, inferContainer, Hollywood, AnyHollywood } from 'hollywood-di';
2
+ import { Env, Hono } from 'hono';
3
+
4
+ type IsAny<T> = unknown extends T & string ? true : false;
5
+ type WithoutTrailingSlash<T extends string> = T extends `${infer P}/` ? P : T;
6
+ type WithoutLeadingSlash<T extends string> = T extends `/${infer P}` ? P : T;
7
+ type WithMinimumSlash<T extends string> = T extends "" ? "/" : T;
8
+ type MergePath<P extends string, T extends string> = IsAny<T> extends true ? any : WithMinimumSlash<WithoutTrailingSlash<`${WithoutTrailingSlash<P>}/${WithoutLeadingSlash<T>}`>>;
9
+
10
+ type Router<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}> = Hono<E, S, MergePath<BasePath, Path>>;
11
+ declare class ControllerDef<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}, Out = S> {
12
+ path: Path;
13
+ route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>;
14
+ constructor(path: Path, route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>);
15
+ mountTo(parent: Hono<any, any, BasePath>): void;
16
+ }
17
+ type Controller<BasePath extends string, Path extends string, T extends Record<string, any> = {}, E extends Env = Env, S = {}, Out = S> = InitFactory<T, ControllerDef<BasePath, Path, E, S, Out>>;
18
+ type inferController<T extends Controller<any, any, any, any, any>> = T extends Controller<infer BasePath, infer Path, any, infer E, any, infer Out> ? Router<BasePath, Path, E, Out> : never;
19
+ type ControllerInit<T> = <U extends Record<string, any>>(constructor: InstantiableConstructor<T, U>) => U;
20
+ type ControllerContext<TContainer, TRouter> = {
21
+ router: TRouter;
22
+ container: TContainer;
23
+ /**
24
+ * Initialize an instantiable constructor with the controller's container.
25
+ *
26
+ * Also automatically binds class methods to their instance using `auto-bind`.
27
+ */
28
+ init: ControllerInit<TContainer>;
29
+ };
30
+ type ControllerBuilder<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}> = {
31
+ build<Out>(config: (ctx: ControllerContext<T, Router<BasePath, Path, E, S>>) => Router<BasePath, Path, E, Out>): Controller<BasePath, Path, T, E, S, Out>;
32
+ };
33
+ declare function createController<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}>(path: Path): ControllerBuilder<BasePath, Path, T, E, S>;
34
+
35
+ interface Module<BasePath extends string, Path extends string, P extends Record<string, any>> {
36
+ path: Path;
37
+ tokens: RegisterTokens<any, P>;
38
+ controllers: Controller<MergePath<BasePath, Path>, any, P, any, any, any>[];
39
+ submodules?: Module<MergePath<BasePath, Path>, any, P>[];
40
+ }
41
+ interface ModuleConfig<BasePath extends string, Path extends string, T extends Record<string, any>, P extends Record<string, any>> {
42
+ tokens: RegisterTokens<T, P>;
43
+ controllers: Controller<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>, any, any, any>[];
44
+ submodules?: Module<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>>[];
45
+ }
46
+ type ModuleBuilder<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}> = {
47
+ build<T extends Record<string, any> = {}>(config: ModuleConfig<BasePath, Path, T, P>): Module<BasePath, Path, P>;
48
+ };
49
+ declare function createModule<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}>(path: Path): ModuleBuilder<BasePath, Path, P>;
50
+
51
+ interface Serverstruct<Container extends AnyHollywood, BasePath extends string> {
52
+ modules(...modules: Module<BasePath, any, inferContainer<Container>>[]): this;
53
+ controllers(...controllers: Controller<BasePath, any, inferContainer<Container>, any, any, any>[]): this;
54
+ }
55
+ declare function serverstruct<App extends Hono<any, any, BasePath>, Container extends AnyHollywood, BasePath extends string = "/">(app: App, container: Container, _base?: BasePath): Serverstruct<Container, BasePath>;
56
+
57
+ export { Controller, ControllerContext, Module, ModuleConfig, Router, Serverstruct, createController, createModule, inferController, serverstruct };
@@ -0,0 +1,57 @@
1
+ import { InitFactory, InstantiableConstructor, RegisterTokens, inferContainer, Hollywood, AnyHollywood } from 'hollywood-di';
2
+ import { Env, Hono } from 'hono';
3
+
4
+ type IsAny<T> = unknown extends T & string ? true : false;
5
+ type WithoutTrailingSlash<T extends string> = T extends `${infer P}/` ? P : T;
6
+ type WithoutLeadingSlash<T extends string> = T extends `/${infer P}` ? P : T;
7
+ type WithMinimumSlash<T extends string> = T extends "" ? "/" : T;
8
+ type MergePath<P extends string, T extends string> = IsAny<T> extends true ? any : WithMinimumSlash<WithoutTrailingSlash<`${WithoutTrailingSlash<P>}/${WithoutLeadingSlash<T>}`>>;
9
+
10
+ type Router<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}> = Hono<E, S, MergePath<BasePath, Path>>;
11
+ declare class ControllerDef<BasePath extends string = "/", Path extends string = "/", E extends Env = Env, S = {}, Out = S> {
12
+ path: Path;
13
+ route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>;
14
+ constructor(path: Path, route: (router: Router<BasePath, Path, E, S>) => Router<BasePath, Path, E, Out>);
15
+ mountTo(parent: Hono<any, any, BasePath>): void;
16
+ }
17
+ type Controller<BasePath extends string, Path extends string, T extends Record<string, any> = {}, E extends Env = Env, S = {}, Out = S> = InitFactory<T, ControllerDef<BasePath, Path, E, S, Out>>;
18
+ type inferController<T extends Controller<any, any, any, any, any>> = T extends Controller<infer BasePath, infer Path, any, infer E, any, infer Out> ? Router<BasePath, Path, E, Out> : never;
19
+ type ControllerInit<T> = <U extends Record<string, any>>(constructor: InstantiableConstructor<T, U>) => U;
20
+ type ControllerContext<TContainer, TRouter> = {
21
+ router: TRouter;
22
+ container: TContainer;
23
+ /**
24
+ * Initialize an instantiable constructor with the controller's container.
25
+ *
26
+ * Also automatically binds class methods to their instance using `auto-bind`.
27
+ */
28
+ init: ControllerInit<TContainer>;
29
+ };
30
+ type ControllerBuilder<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}> = {
31
+ build<Out>(config: (ctx: ControllerContext<T, Router<BasePath, Path, E, S>>) => Router<BasePath, Path, E, Out>): Controller<BasePath, Path, T, E, S, Out>;
32
+ };
33
+ declare function createController<BasePath extends string = "/", Path extends string = "/", T extends Record<string, any> = {}, E extends Env = Env, S = {}>(path: Path): ControllerBuilder<BasePath, Path, T, E, S>;
34
+
35
+ interface Module<BasePath extends string, Path extends string, P extends Record<string, any>> {
36
+ path: Path;
37
+ tokens: RegisterTokens<any, P>;
38
+ controllers: Controller<MergePath<BasePath, Path>, any, P, any, any, any>[];
39
+ submodules?: Module<MergePath<BasePath, Path>, any, P>[];
40
+ }
41
+ interface ModuleConfig<BasePath extends string, Path extends string, T extends Record<string, any>, P extends Record<string, any>> {
42
+ tokens: RegisterTokens<T, P>;
43
+ controllers: Controller<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>, any, any, any>[];
44
+ submodules?: Module<MergePath<BasePath, Path>, any, inferContainer<Hollywood<T, Hollywood<P, any>>>>[];
45
+ }
46
+ type ModuleBuilder<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}> = {
47
+ build<T extends Record<string, any> = {}>(config: ModuleConfig<BasePath, Path, T, P>): Module<BasePath, Path, P>;
48
+ };
49
+ declare function createModule<BasePath extends string = "/", Path extends string = "/", P extends Record<string, any> = {}>(path: Path): ModuleBuilder<BasePath, Path, P>;
50
+
51
+ interface Serverstruct<Container extends AnyHollywood, BasePath extends string> {
52
+ modules(...modules: Module<BasePath, any, inferContainer<Container>>[]): this;
53
+ controllers(...controllers: Controller<BasePath, any, inferContainer<Container>, any, any, any>[]): this;
54
+ }
55
+ declare function serverstruct<App extends Hono<any, any, BasePath>, Container extends AnyHollywood, BasePath extends string = "/">(app: App, container: Container, _base?: BasePath): Serverstruct<Container, BasePath>;
56
+
57
+ export { Controller, ControllerContext, Module, ModuleConfig, Router, Serverstruct, createController, createModule, inferController, serverstruct };
package/dist/index.js ADDED
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __export = (target, all) => {
26
+ for (var name in all)
27
+ __defProp(target, name, { get: all[name], enumerable: true });
28
+ };
29
+ var __copyProps = (to, from, except, desc) => {
30
+ if (from && typeof from === "object" || typeof from === "function") {
31
+ for (let key of __getOwnPropNames(from))
32
+ if (!__hasOwnProp.call(to, key) && key !== except)
33
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
34
+ }
35
+ return to;
36
+ };
37
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
+ // If the importer is in node compatibility mode or this is not an ESM
39
+ // file that has been converted to a CommonJS file using a Babel-
40
+ // compatible transform (i.e. "__esModule" has not been set), then set
41
+ // "default" to the CommonJS "module.exports" for node compatibility.
42
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
+ mod
44
+ ));
45
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
+
47
+ // src/index.ts
48
+ var src_exports = {};
49
+ __export(src_exports, {
50
+ createController: () => createController,
51
+ createModule: () => createModule,
52
+ serverstruct: () => serverstruct
53
+ });
54
+ module.exports = __toCommonJS(src_exports);
55
+ var import_hono2 = require("hono");
56
+
57
+ // src/controller.ts
58
+ var import_auto_bind = __toESM(require("auto-bind"));
59
+ var import_hollywood_di = require("hollywood-di");
60
+ var import_hono = require("hono");
61
+ var ControllerDef = class {
62
+ constructor(path, route) {
63
+ this.path = path;
64
+ this.route = route;
65
+ }
66
+ mountTo(parent) {
67
+ const router = new import_hono.Hono();
68
+ const mountedRouter = this.route(router);
69
+ parent.route(this.path, mountedRouter);
70
+ }
71
+ };
72
+ function createController(path) {
73
+ return {
74
+ build(config) {
75
+ return {
76
+ init(container) {
77
+ const init = (constructor) => {
78
+ const instance = import_hollywood_di.Hollywood.initConstructor(constructor, container);
79
+ return (0, import_auto_bind.default)(instance);
80
+ };
81
+ return new ControllerDef(path, (router) => config({ router, container, init }));
82
+ }
83
+ };
84
+ }
85
+ };
86
+ }
87
+
88
+ // src/module.ts
89
+ function createModule(path) {
90
+ return {
91
+ build(config) {
92
+ return __spreadProps(__spreadValues({}, config), { path });
93
+ }
94
+ };
95
+ }
96
+
97
+ // src/index.ts
98
+ function serverstruct(app, container, _base) {
99
+ return {
100
+ controllers(...controllers) {
101
+ registerControllers(app, container, controllers);
102
+ return this;
103
+ },
104
+ modules(...modules) {
105
+ for (const module2 of modules) {
106
+ registerModule(app, container, module2);
107
+ }
108
+ return this;
109
+ }
110
+ };
111
+ }
112
+ function registerControllers(app, container, controllers) {
113
+ for (const controllerInit of controllers) {
114
+ const controller = container.resolve(controllerInit);
115
+ controller.mountTo(app);
116
+ }
117
+ }
118
+ function registerModule(app, container, module2) {
119
+ var _a;
120
+ const moduleContainer = container.createChild(module2.tokens);
121
+ const subapp = new import_hono2.Hono();
122
+ registerControllers(subapp, moduleContainer, module2.controllers);
123
+ for (const submodule of (_a = module2.submodules) != null ? _a : []) {
124
+ registerModule(subapp, moduleContainer, submodule);
125
+ }
126
+ app.route(module2.path, subapp);
127
+ }
128
+ // Annotate the CommonJS export names for ESM import in node:
129
+ 0 && (module.exports = {
130
+ createController,
131
+ createModule,
132
+ serverstruct
133
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,99 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+
21
+ // src/index.ts
22
+ import { Hono as Hono2 } from "hono";
23
+
24
+ // src/controller.ts
25
+ import autoBind from "auto-bind";
26
+ import { Hollywood } from "hollywood-di";
27
+ import { Hono } from "hono";
28
+ var ControllerDef = class {
29
+ constructor(path, route) {
30
+ this.path = path;
31
+ this.route = route;
32
+ }
33
+ mountTo(parent) {
34
+ const router = new Hono();
35
+ const mountedRouter = this.route(router);
36
+ parent.route(this.path, mountedRouter);
37
+ }
38
+ };
39
+ function createController(path) {
40
+ return {
41
+ build(config) {
42
+ return {
43
+ init(container) {
44
+ const init = (constructor) => {
45
+ const instance = Hollywood.initConstructor(constructor, container);
46
+ return autoBind(instance);
47
+ };
48
+ return new ControllerDef(path, (router) => config({ router, container, init }));
49
+ }
50
+ };
51
+ }
52
+ };
53
+ }
54
+
55
+ // src/module.ts
56
+ function createModule(path) {
57
+ return {
58
+ build(config) {
59
+ return __spreadProps(__spreadValues({}, config), { path });
60
+ }
61
+ };
62
+ }
63
+
64
+ // src/index.ts
65
+ function serverstruct(app, container, _base) {
66
+ return {
67
+ controllers(...controllers) {
68
+ registerControllers(app, container, controllers);
69
+ return this;
70
+ },
71
+ modules(...modules) {
72
+ for (const module of modules) {
73
+ registerModule(app, container, module);
74
+ }
75
+ return this;
76
+ }
77
+ };
78
+ }
79
+ function registerControllers(app, container, controllers) {
80
+ for (const controllerInit of controllers) {
81
+ const controller = container.resolve(controllerInit);
82
+ controller.mountTo(app);
83
+ }
84
+ }
85
+ function registerModule(app, container, module) {
86
+ var _a;
87
+ const moduleContainer = container.createChild(module.tokens);
88
+ const subapp = new Hono2();
89
+ registerControllers(subapp, moduleContainer, module.controllers);
90
+ for (const submodule of (_a = module.submodules) != null ? _a : []) {
91
+ registerModule(subapp, moduleContainer, submodule);
92
+ }
93
+ app.route(module.path, subapp);
94
+ }
95
+ export {
96
+ createController,
97
+ createModule,
98
+ serverstruct
99
+ };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "serverstruct",
3
+ "version": "0.1.0",
4
+ "description": "Type safe and modular servers with Hono",
5
+ "private": false,
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.mjs",
13
+ "default": "./dist/index.js"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "prebuild": "shx rm -rf dist",
18
+ "build": "tsc --noEmit && tsup src/index.ts --format esm,cjs --dts",
19
+ "release": "pnpm run build && changeset publish",
20
+ "watch": "vitest",
21
+ "test": "vitest run"
22
+ },
23
+ "keywords": [
24
+ "command",
25
+ "cli",
26
+ "sade",
27
+ "hollywood-di",
28
+ "typescript"
29
+ ],
30
+ "author": "Eric Afes <eriicafes@gmail.com>",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/eriicafes/serverstruct.git"
35
+ },
36
+ "bugs": {
37
+ "url": "https://github.com/eriicafes/serverstruct/issues"
38
+ },
39
+ "homepage": "https://github.com/eriicafes/serverstruct#readme",
40
+ "dependencies": {
41
+ "auto-bind": "^5.0.1"
42
+ },
43
+ "devDependencies": {
44
+ "@changesets/cli": "^2.26.2",
45
+ "@hono/node-server": "^1.1.0",
46
+ "@types/node": "^20.4.2",
47
+ "@types/supertest": "^2.0.12",
48
+ "shx": "^0.3.4",
49
+ "supertest": "^6.3.3",
50
+ "tsup": "^7.1.0",
51
+ "typescript": "^5.1.6",
52
+ "vitest": "^0.33.0"
53
+ },
54
+ "peerDependencies": {
55
+ "hollywood-di": ">= 0.2.1",
56
+ "hono": ">= 3.3.0"
57
+ }
58
+ }