@nan0web/core 1.0.3 → 1.0.5

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/README.md CHANGED
@@ -3,9 +3,7 @@
3
3
  Core application framework for nan0web providing a lightweight
4
4
  DB‑backed state container with built‑in internationalisation.
5
5
 
6
- |Package name|[Status](https://github.com/nan0web/monorepo/blob/main/system.md#написання-сценаріїв)|Documentation|Test coverage|Features|Npm version|
7
- |---|---|---|---|---|---|
8
- |[@nan0web/core](https://github.com/nan0web/core/) |🟢 `99.1%` |🧪 [English 🏴󠁧󠁢󠁥󠁮󠁧󠁿](https://github.com/nan0web/core/blob/main/README.md)<br />[Українською 🇺🇦](https://github.com/nan0web/core/blob/main/docs/uk/README.md) |🟢 `95.3%` |✅ d.ts 📜 system.md 🕹️ playground |1.0.1 |
6
+ <!-- %PACKAGE_STATUS% -->
9
7
 
10
8
  ## Installation
11
9
 
@@ -32,7 +30,7 @@ How to instantiate AppCore?
32
30
  ```js
33
31
  import { AppCore } from "@nan0web/core"
34
32
  const db = new DB()
35
- const core = new AppCore({ db, title: "Demo", uri: "/demo", locale: "en" })
33
+ const core = new AppCore({ db, title: 'Demo', uri: '/demo', locale: 'en' })
36
34
  console.info(core.title) // ← Demo
37
35
  console.info(core.uri) // ← /demo
38
36
  ```
@@ -43,14 +41,12 @@ Load translation JSON from the DB and obtain a translation function.
43
41
  How does bootstrapI18n load translations?
44
42
  ```js
45
43
  const db = new DB({
46
- predefined: [
47
- ["i18n/uk.json", { "hello": "Вітаю!" }]
48
- ]
44
+ predefined: [['i18n/uk.json', { hello: 'Вітаю!' }]],
49
45
  })
50
46
  await db.connect()
51
- const core = new AppCore({ db, locale: "uk" })
47
+ const core = new AppCore({ db, locale: 'uk' })
52
48
  await core.init()
53
- const result = core.t("hello")
49
+ const result = core.t('hello')
54
50
  console.info(result) // ← Вітаю!
55
51
  ```
56
52
  ## State inspection
@@ -66,7 +62,7 @@ const state = core.state()
66
62
  How to create an AppResult instance?
67
63
  ```js
68
64
  import { AppResult } from "@nan0web/core"
69
- const res = new AppResult({ content: "Done", priority: 2, meta: { ok: true } })
65
+ const res = new AppResult({ content: 'Done', priority: 2, meta: { ok: true } })
70
66
  console.info(res.content[0]) // ← Done
71
67
  ```
72
68
  ## run() contract
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nan0web/core",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Core application framework for nan0web with database handling, internationalization, and interface connectivity",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -8,6 +8,7 @@
8
8
  "files": [
9
9
  "src/**/*.js",
10
10
  "!src/**/*.test.js",
11
+ "!src/README.md.js",
11
12
  "types/**/*.d.ts"
12
13
  ],
13
14
  "scripts": {
@@ -20,13 +21,14 @@
20
21
  "prepublishOnly": "npm test && npm run build",
21
22
  "play": "node play/main.js",
22
23
  "release": "nan0release publish",
24
+ "knip": "knip --production",
23
25
  "test": "node --test --test-timeout=3333 \"src/**/*.test.js\"",
24
26
  "test:docs": "node --test src/README.md.js",
25
27
  "test:release": "node --test \"releases/**/*.test.js\"",
26
28
  "test:coverage": "node --experimental-test-coverage --test-coverage-include=\"src/**/*.js\" --test-coverage-exclude=\"src/**/*.test.js\" --test \"src/**/*.test.js\"",
27
29
  "test:coverage:collect": "nan0test coverage",
28
30
  "test:status": "nan0test status --hide-name",
29
- "test:all": "npm run test && npm run test:docs && npm run build"
31
+ "test:all": "npm run test && npm run test:docs && npm run build && npm run knip"
30
32
  },
31
33
  "keywords": [
32
34
  "nan0",
@@ -43,12 +45,13 @@
43
45
  "license": "ISC",
44
46
  "packageManager": "pnpm@10.11.0",
45
47
  "devDependencies": {
46
- "@nan0web/release": "1.0.1",
47
- "@nan0web/test": "1.1.3",
48
- "husky": "^9.1.7"
48
+ "@nan0web/release": "*",
49
+ "@nan0web/test": "*",
50
+ "husky": "^9.1.7",
51
+ "knip": "^5.82.1"
49
52
  },
50
53
  "dependencies": {
51
- "@nan0web/interface": "1.0.1",
52
- "@nan0web/protocol": "2.0.0"
54
+ "@nan0web/db": "*",
55
+ "@nan0web/protocol": "*"
53
56
  }
54
57
  }
package/src/AppCore.js CHANGED
@@ -1,6 +1,6 @@
1
- import DB from "@nan0web/db"
2
- import { createT } from "@nan0web/i18n"
3
- import AppResult from "./AppResult.js"
1
+ import DB from '@nan0web/db'
2
+ import { createT } from '@nan0web/i18n'
3
+ import AppResult from './AppResult.js'
4
4
 
5
5
  /** @typedef {{id: string, icon?: string, locale?: string}} Language */
6
6
 
@@ -16,15 +16,16 @@ import AppResult from "./AppResult.js"
16
16
  * @property {Function} t - Translation function
17
17
  */
18
18
  export default class AppCore {
19
+ static DB = DB
19
20
  #initialized = false
20
21
  /** @type {DB} */
21
22
  db
22
23
  /** @type {string} */
23
- title = ""
24
+ title = ''
24
25
  /** @type {string} */
25
- uri = ""
26
+ uri = ''
26
27
  /** @type {string} */
27
- locale = "uk"
28
+ locale = 'uk'
28
29
  /** @type {object} */
29
30
  data = {}
30
31
  /** @type {Record<string, Function>} */
@@ -34,18 +35,16 @@ export default class AppCore {
34
35
  /** @type {Record<string, Language>} */
35
36
  langs = {
36
37
  en: {
37
- id: "en",
38
- icon: "🇬🇧",
39
- locale: "en-GB"
40
- }
38
+ id: 'en',
39
+ icon: '🇬🇧',
40
+ locale: 'en-GB',
41
+ },
41
42
  }
42
- /** @type {(key: string, replacements: Record<string, string>) => string} */
43
- t = (key, replacements) => key
43
+ /** @type {(key: string, replacements?: Record<string, string>) => string} */
44
+ t = (key, replacements = {}) => key
44
45
  /**
45
46
  * Create an AppCore instance
46
- * @param {object} config - Application configuration
47
- * @param {DB} config.db - Database instance
48
- * @param {string} [config.locale='uk'] - Locale identifier 2 or 5 chars: "uk" | "uk-UA"
47
+ * @param {object} input - Application configuration
49
48
  */
50
49
  constructor(input = {}) {
51
50
  const {
@@ -59,8 +58,8 @@ export default class AppCore {
59
58
  langs = this.langs,
60
59
  t = this.t,
61
60
  } = input
62
- if (!(db instanceof DB)) {
63
- throw new Error("Database must be an instance of @nan0web/db.DB")
61
+ if (db && typeof db.fetch !== 'function') {
62
+ throw new Error('Database must be an instance of @nan0web/db.DB')
64
63
  }
65
64
  this.db = db
66
65
  this.title = String(title)
@@ -79,7 +78,7 @@ export default class AppCore {
79
78
  * @returns {Promise<void>}
80
79
  */
81
80
  async bootstrapI18n(path = `/i18n/{{locale}}.json`) {
82
- const [code, country] = this.locale.split("-")
81
+ const [code, country] = this.locale.split('-')
83
82
  let uri = path.replace('{{locale}}', this.locale)
84
83
  let i18n = await this.db.fetch(uri, { defaultValue: null })
85
84
  if (null === i18n && country) {
@@ -109,7 +108,7 @@ export default class AppCore {
109
108
  data: this.data,
110
109
  actions: this.actions,
111
110
  meta: this.meta,
112
- t: this.t
111
+ t: this.t,
113
112
  }
114
113
  }
115
114
 
package/src/index.js CHANGED
@@ -1,13 +1,9 @@
1
- import { ExecutableCommand } from "@nan0web/protocol"
2
- import AppCore from "./AppCore.js"
3
- import AppResult from "./AppResult.js"
1
+ import { ExecutableCommand } from '@nan0web/protocol'
2
+ import AppCore from './AppCore.js'
3
+ import AppResult from './AppResult.js'
4
4
 
5
5
  /** @typedef {import("./AppCore.js").Language} Language */
6
6
 
7
- export {
8
- AppCore,
9
- AppResult,
10
- ExecutableCommand,
11
- }
7
+ export { AppCore, AppResult, ExecutableCommand }
12
8
 
13
9
  export default AppCore
@@ -11,14 +11,13 @@
11
11
  * @property {Function} t - Translation function
12
12
  */
13
13
  export default class AppCore {
14
+ static DB: typeof DB;
14
15
  static from(input: any): AppCore;
15
16
  /**
16
17
  * Create an AppCore instance
17
- * @param {object} config - Application configuration
18
- * @param {DB} config.db - Database instance
19
- * @param {string} [config.locale='uk'] - Locale identifier 2 or 5 chars: "uk" | "uk-UA"
18
+ * @param {object} input - Application configuration
20
19
  */
21
- constructor(input?: {});
20
+ constructor(input?: object);
22
21
  /** @type {DB} */
23
22
  db: DB;
24
23
  /** @type {string} */
@@ -35,8 +34,8 @@ export default class AppCore {
35
34
  meta: object;
36
35
  /** @type {Record<string, Language>} */
37
36
  langs: Record<string, Language>;
38
- /** @type {(key: string, replacements: Record<string, string>) => string} */
39
- t: (key: string, replacements: Record<string, string>) => string;
37
+ /** @type {(key: string, replacements?: Record<string, string>) => string} */
38
+ t: (key: string, replacements?: Record<string, string>) => string;
40
39
  /**
41
40
  * Bootstrap internationalization by loading translations from database
42
41
  * @param {string} path - Path to i18n file with locale placeholder
@@ -71,5 +70,5 @@ export type Language = {
71
70
  icon?: string;
72
71
  locale?: string;
73
72
  };
74
- import DB from "@nan0web/db";
75
- import AppResult from "./AppResult.js";
73
+ import DB from '@nan0web/db';
74
+ import AppResult from './AppResult.js';
package/types/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export default AppCore;
2
2
  export type Language = import("./AppCore.js").Language;
3
- import AppCore from "./AppCore.js";
4
- import AppResult from "./AppResult.js";
5
- import { ExecutableCommand } from "@nan0web/protocol";
3
+ import AppCore from './AppCore.js';
4
+ import AppResult from './AppResult.js';
5
+ import { ExecutableCommand } from '@nan0web/protocol';
6
6
  export { AppCore, AppResult, ExecutableCommand };
package/src/README.md.js DELETED
@@ -1,219 +0,0 @@
1
- import { describe, it, before, beforeEach } from "node:test"
2
- import assert from "node:assert/strict"
3
- import FS from "@nan0web/db-fs"
4
- import { NoConsole } from "@nan0web/log"
5
- import { DocsParser, DatasetParser } from "@nan0web/test"
6
- import {
7
- AppCore,
8
- AppResult,
9
- ExecutableCommand,
10
- } from "./index.js"
11
- import DB from "@nan0web/db"
12
-
13
- /**
14
- * Load package.json once before any test.
15
- */
16
- let pkg
17
- const fs = new FS()
18
-
19
- before(async () => {
20
- const doc = await fs.loadDocument("package.json", {})
21
- pkg = doc || {}
22
- })
23
-
24
- let console = new NoConsole()
25
-
26
- beforeEach(() => {
27
- console = new NoConsole()
28
- })
29
-
30
- /**
31
- * Core test suite that also serves as source for README generation.
32
- *
33
- * Block comments inside each `it` are extracted by DocsParser to build the final README.
34
- */
35
- function testRender() {
36
- /**
37
- * @docs
38
- * # @nan0web/core
39
- *
40
- * Core application framework for nan0web providing a lightweight
41
- * DB‑backed state container with built‑in internationalisation.
42
- *
43
- * <!-- %PACKAGE_STATUS% -->
44
- *
45
- * ## Installation
46
- */
47
- it("How to install with npm?", () => {
48
- /**
49
- * ```bash
50
- * npm install @nan0web/core
51
- * ```
52
- */
53
- assert.equal(pkg.name, "@nan0web/core")
54
- })
55
-
56
- /**
57
- * @docs
58
- */
59
- it("How to install with pnpm?", () => {
60
- /**
61
- * ```bash
62
- * pnpm add @nan0web/core
63
- * ```
64
- */
65
- assert.equal(pkg.name, "@nan0web/core")
66
- })
67
-
68
- /**
69
- * @docs
70
- */
71
- it("How to install with yarn?", () => {
72
- /**
73
- * ```bash
74
- * yarn add @nan0web/core
75
- * ```
76
- */
77
- assert.equal(pkg.name, "@nan0web/core")
78
- })
79
-
80
- /**
81
- * @docs
82
- * ## Basic usage – AppCore
83
- *
84
- * Create an `AppCore` instance with a mock DB and inspect its state.
85
- */
86
- it("How to instantiate AppCore?", () => {
87
- //import { AppCore } from "@nan0web/core"
88
- const db = new DB()
89
- const core = new AppCore({ db, title: "Demo", uri: "/demo", locale: "en" })
90
- console.info(core.title) // ← Demo
91
- console.info(core.uri) // ← /demo
92
- assert.equal(core.title, "Demo")
93
- assert.equal(core.uri, "/demo")
94
- assert.equal(core.locale, "en")
95
- })
96
-
97
- /**
98
- * @docs
99
- * ## Internationalisation bootstrap
100
- *
101
- * Load translation JSON from the DB and obtain a translation function.
102
- */
103
- it("How does bootstrapI18n load translations?", async () => {
104
- const db = new DB({
105
- predefined: [
106
- ["i18n/uk.json", { "hello": "Вітаю!" }]
107
- ]
108
- })
109
- await db.connect()
110
- const core = new AppCore({ db, locale: "uk" })
111
- await core.init()
112
- const result = core.t("hello")
113
- console.info(result) // ← Вітаю!
114
- assert.equal(result, "Вітаю!")
115
- })
116
-
117
- /**
118
- * @docs
119
- * ## State inspection
120
- */
121
- it("How to retrieve current state?", () => {
122
- const db = new DB()
123
- const core = new AppCore({ db })
124
- const state = core.state()
125
- assert.deepEqual(Object.keys(state), ["data", "actions", "meta", "t"])
126
- assert.ok(typeof state.t === "function")
127
- })
128
-
129
- /**
130
- * @docs
131
- * ## AppResult helper
132
- */
133
- it("How to create an AppResult instance?", () => {
134
- //import { AppResult } from "@nan0web/core"
135
- const res = new AppResult({ content: "Done", priority: 2, meta: { ok: true } })
136
- console.info(res.content[0]) // ← Done
137
- assert.equal(res.content[0], "Done")
138
- assert.equal(res.priority, 2)
139
- assert.deepEqual(res.meta, { ok: true })
140
- assert.equal(res.error, null)
141
- })
142
-
143
- /**
144
- * @docs
145
- * ## run() contract
146
- *
147
- * The base `run` method throws an error – subclasses must implement it.
148
- */
149
- it("How does run() behave when not overridden?", async () => {
150
- const db = new DB()
151
- const core = new AppCore({ db })
152
- //await core.run() // ← throws an error → AppCore: run() must be implemented
153
- assert.ok(core)
154
- await assert.rejects(
155
- async () => {
156
- await core.run()
157
- },
158
- {
159
- name: "Error",
160
- message: "AppCore: run() must be implemented"
161
- }
162
- )
163
- })
164
-
165
- /**
166
- * ## ExecutableCommand exposure
167
- */
168
- it("Are exported commands available?", () => {
169
- assert.ok(ExecutableCommand)
170
- })
171
-
172
- /**
173
- * ## TypeScript declarations
174
- */
175
- it("Does package expose declaration files?", () => {
176
- assert.equal(pkg.types, "types/index.d.ts")
177
- })
178
-
179
- /**
180
- * @docs
181
- * ## Contributing
182
- */
183
- it("How to contribute? - [check here](./CONTRIBUTING.md)", async () => {
184
- assert.equal(pkg.scripts?.precommit, "npm test")
185
- assert.equal(pkg.scripts?.prepush, "npm test")
186
- assert.equal(pkg.scripts?.prepare, "husky")
187
- const text = await fs.loadDocument("CONTRIBUTING.md")
188
- const str = String(text)
189
- assert.ok(str.includes("# Contributing"))
190
- })
191
-
192
- /**
193
- * @docs
194
- * ## License
195
- */
196
- it("How to license ISC? - [check here](./LICENSE)", async () => {
197
- /** @docs */
198
- const text = await fs.loadDocument("LICENSE")
199
- assert.ok(String(text).includes("ISC"))
200
- })
201
- }
202
-
203
- /* Run the test suite that also generates the README */
204
- describe("README.md testing", testRender)
205
-
206
- describe("Rendering README.md", async () => {
207
- let text = ""
208
- const format = new Intl.NumberFormat("en-US").format
209
- const parser = new DocsParser()
210
- text = String(parser.decode(testRender))
211
- await fs.saveDocument("README.md", text)
212
- const dataset = DatasetParser.parse(text, pkg.name)
213
- await fs.saveDocument(".datasets/README.dataset.jsonl", dataset)
214
-
215
- it(`document is rendered in README.md [${format(Buffer.byteLength(text))}b]`, async () => {
216
- const readme = await fs.loadDocument("README.md")
217
- assert.ok(readme.includes("## Installation"))
218
- })
219
- })
@@ -1,7 +0,0 @@
1
- /**
2
- * Simple wrapper for API route registration.
3
- * Extends the generic RouteRegistry to keep a semantic name.
4
- */
5
- import RouteRegistry from './RouteRegistry.js'
6
-
7
- export default class ApiRegistry extends RouteRegistry { }
@@ -1,50 +0,0 @@
1
- /**
2
- * Registry for CLI command namespaces
3
- * Tracks registered commands and their owners
4
- *
5
- * @example
6
- * const registry = new CommandRegistry()
7
- * registry.register('auth', { setup: () => { ... }, owner: '@nan0web/auth.app' })
8
- * registry.has('auth') // true
9
- */
10
- export default class CommandRegistry {
11
- /** @type {Map<string, { setup: Function, owner: string }>} */
12
- #commands = new Map()
13
-
14
- /**
15
- * Register a CLI command namespace
16
- * @param {string} command - Command name (e.g. 'auth')
17
- * @param {{ setup: Function, owner: string }} config - Setup function and package name
18
- * @returns {this}
19
- */
20
- register(command, config) {
21
- this.#commands.set(command, config)
22
- return this
23
- }
24
-
25
- /**
26
- * Get all registered commands
27
- * @returns {Map<string, { setup: Function, owner: string }>}
28
- */
29
- getCommands() {
30
- return new Map(this.#commands)
31
- }
32
-
33
- /**
34
- * Check if command exists
35
- * @param {string} command
36
- * @returns {boolean}
37
- */
38
- has(command) {
39
- return this.#commands.has(command)
40
- }
41
-
42
- /**
43
- * Get owner of command
44
- * @param {string} command
45
- * @returns {string | null}
46
- */
47
- getOwner(command) {
48
- return this.#commands.get(command)?.owner || null
49
- }
50
- }
@@ -1,51 +0,0 @@
1
- /**
2
- * Registry for UI components
3
- * Tracks registered component namespaces
4
- *
5
- * @example
6
- * const registry = new ComponentRegistry()
7
- * registry.register('Auth.Login', { component: () => import('./Auth/Login.jsx'), owner: '@nan0web/auth.app' })
8
- * registry.getComponent('Auth.Login') // Returns component loader
9
- */
10
- export default class ComponentRegistry {
11
- /** @type {Map<string, { component: Function, owner: string }>} */
12
- #components = new Map()
13
-
14
- /**
15
- * Register a component namespace
16
- * @param {string} name - Component path (e.g. 'Auth.Login')
17
- * @param {{ component: Function, owner: string }} config - Component loader and package name
18
- * @returns {this}
19
- */
20
- register(name, config) {
21
- this.#components.set(name, config)
22
- return this
23
- }
24
-
25
- /**
26
- * Get all registered components
27
- * @returns {Map<string, { component: Function, owner: string }>}
28
- */
29
- getComponents() {
30
- return new Map(this.#components)
31
- }
32
-
33
- /**
34
- * Get component loader by name
35
- * @param {string} name
36
- * @returns {Function | null}
37
- */
38
- getComponent(name) {
39
- return this.#components.get(name)?.component || null
40
- }
41
-
42
- /**
43
- * Find components by namespace prefix
44
- * @param {string} namespace - E.g., 'Auth' to find all Auth.* components
45
- * @returns {Array<string>} Component names
46
- */
47
- findComponents(namespace) {
48
- return Array.from(this.#components.keys())
49
- .filter(name => name.startsWith(namespace))
50
- }
51
- }
@@ -1,50 +0,0 @@
1
- /**
2
- * Registry for API route namespaces
3
- * Tracks registered prefixes and their owners
4
- *
5
- * @example
6
- * const registry = new RouteRegistry()
7
- * registry.register('auth', { setup: (router) => { ... }, owner: '@nan0web/auth.app' })
8
- * registry.has('auth') // true
9
- */
10
- export default class RouteRegistry {
11
- /** @type {Map<string, { setup: Function, owner: string }>} */
12
- #routes = new Map()
13
-
14
- /**
15
- * Register a namespace prefix
16
- * @param {string} prefix - URL prefix (e.g. 'auth')
17
- * @param {{ setup: Function, owner: string }} config - Setup function and package name
18
- * @returns {this}
19
- */
20
- register(prefix, config) {
21
- this.#routes.set(prefix, config)
22
- return this
23
- }
24
-
25
- /**
26
- * Get all registered routes
27
- * @returns {Map<string, { setup: Function, owner: string }>}
28
- */
29
- getRoutes() {
30
- return new Map(this.#routes)
31
- }
32
-
33
- /**
34
- * Check if prefix exists
35
- * @param {string} prefix
36
- * @returns {boolean}
37
- */
38
- has(prefix) {
39
- return this.#routes.has(prefix)
40
- }
41
-
42
- /**
43
- * Get owner of prefix
44
- * @param {string} prefix
45
- * @returns {string | null}
46
- */
47
- getOwner(prefix) {
48
- return this.#routes.get(prefix)?.owner || null
49
- }
50
- }
@@ -1,7 +0,0 @@
1
- /**
2
- * Wrapper around ComponentRegistry for UI components.
3
- * Provides the same API surface (register, getComponent, etc.).
4
- */
5
- import ComponentRegistry from './ComponentRegistry.js'
6
-
7
- export default class UiRegistry extends ComponentRegistry { }
@@ -1,55 +0,0 @@
1
- import RouteRegistry from '../registry/RouteRegistry.js'
2
- import CommandRegistry from '../registry/CommandRegistry.js'
3
- import ComponentRegistry from '../registry/ComponentRegistry.js'
4
-
5
- /**
6
- * Validates application integration before registration
7
- * Checks for namespace collisions across all registries
8
- */
9
- export default class SystemValidator {
10
- /**
11
- * @param {Object} registries - System-wide registries
12
- * @param {RouteRegistry} registries.apiRegistry - Manages API route prefixes
13
- * @param {CommandRegistry} registries.cliRegistry - Manages CLI commands
14
- * @param {ComponentRegistry} [registries.uiRegistry] - Manages UI components
15
- */
16
- constructor({ apiRegistry, cliRegistry, uiRegistry }) {
17
- if (!(apiRegistry instanceof RouteRegistry)) {
18
- throw new Error('apiRegistry must be an instance of RouteRegistry')
19
- }
20
-
21
- if (!(cliRegistry instanceof CommandRegistry)) {
22
- throw new Error('cliRegistry must be an instance of CommandRegistry')
23
- }
24
-
25
- this.apiRegistry = apiRegistry
26
- this.cliRegistry = cliRegistry
27
- this.uiRegistry = uiRegistry || new ComponentRegistry()
28
- }
29
-
30
- /**
31
- * Validate application configuration for potential conflicts
32
- * @param {Object} application - Must have register() method
33
- * @param {Object} [configuration] - Custom integration settings
34
- * @returns {Array<string>} List of collision errors
35
- */
36
- validate(application, configuration = {}) {
37
- const { api, cli, react } = application.register(configuration)
38
- const errors = []
39
-
40
- if (this.apiRegistry.has(api.prefix)) {
41
- errors.push(`API prefix "${api.prefix}" used by ${this.apiRegistry.getOwner(api.prefix)}`)
42
- }
43
-
44
- if (this.cliRegistry.has(cli.command)) {
45
- errors.push(`CLI command "${cli.command}" used by ${this.cliRegistry.getOwner(cli.command)}`)
46
- }
47
-
48
- // Check UI namespace collision (simplified)
49
- if (react.namespace && this.uiRegistry.findComponents(react.namespace).length > 0) {
50
- errors.push(`UI namespace "${react.namespace}.*" already has registered components`)
51
- }
52
-
53
- return errors
54
- }
55
- }
@@ -1,84 +0,0 @@
1
- import ApiRegistry from '../registry/ApiRegistry.js'
2
- import CliRegistry from '../registry/CommandRegistry.js'
3
- import UiRegistry from '../registry/UiRegistry.js'
4
- import SystemValidator from './Validator.js'
5
-
6
- /**
7
- * Integrates applications based on configuration file.
8
- * The config is loaded lazily to avoid TS JSON‑module issues.
9
- */
10
- export class AppIntegrator {
11
- constructor() {
12
- this.apiRegistry = new ApiRegistry()
13
- this.cliRegistry = new CliRegistry()
14
- this.uiRegistry = new UiRegistry()
15
- this.validator = new SystemValidator({
16
- apiRegistry: this.apiRegistry,
17
- cliRegistry: this.cliRegistry,
18
- uiRegistry: this.uiRegistry,
19
- })
20
- }
21
-
22
- /**
23
- * Load the apps configuration (JSON) at runtime.
24
- * @returns {Promise<Record<string, any>>}
25
- */
26
- async _loadConfig() {
27
- // @ts-ignore Fix the load
28
- const { default: cfg } = await import('../config/apps.json')
29
- return cfg
30
- }
31
-
32
- /**
33
- * Register all configured applications.
34
- */
35
- async registerAll() {
36
- const config = await this._loadConfig()
37
-
38
- for (const [packageName, appConfig] of Object.entries(config.apps)) {
39
- if (!appConfig.enabled) continue
40
-
41
- const { default: app } = await import(`${packageName}/src/register.js`)
42
- const validationErrors = this.validator.validate(app, appConfig)
43
-
44
- if (validationErrors.length > 0) {
45
- console.error(`❌ ${packageName} conflicts:`, validationErrors)
46
- continue
47
- }
48
-
49
- this._registerApp(packageName, app, appConfig)
50
- }
51
- }
52
-
53
- /**
54
- * Internal registration logic.
55
- * @param {string} packageName
56
- * @param {object} app
57
- * @param {object} cfg
58
- */
59
- _registerApp(packageName, app, cfg) {
60
- const registration = app.register(cfg)
61
-
62
- if (registration.api) {
63
- this.apiRegistry.register(registration.api.prefix, {
64
- setup: registration.api.setup,
65
- owner: packageName,
66
- })
67
- }
68
-
69
- if (registration.cli) {
70
- this.cliRegistry.register(registration.cli.command, {
71
- setup: registration.cli.setup,
72
- owner: packageName,
73
- })
74
- }
75
-
76
- if (registration.ui) {
77
- // ComponentRegistry expects a `register` method; we store the loader function.
78
- this.uiRegistry.register(registration.ui.namespace, {
79
- component: registration.ui.loader,
80
- owner: packageName,
81
- })
82
- }
83
- }
84
- }
@@ -1,82 +0,0 @@
1
- import RouteRegistry from '../registry/RouteRegistry.js'
2
- import CommandRegistry from '../registry/CommandRegistry.js'
3
- import SystemValidator from './Validator.js'
4
-
5
- /**
6
- * System bootstrapper
7
- * Integrates all applications into shared infrastructure
8
- *
9
- * Flow:
10
- * 1. Create registries
11
- * 2. Validate integrations
12
- * 3. Register conflict‑free apps
13
- * 4. Start services
14
- */
15
- async function bootstrapSystem() {
16
- // Create shared registries
17
- const apiRegistry = new RouteRegistry()
18
- const cliRegistry = new CommandRegistry()
19
-
20
- // Setup validator with all registries
21
- const validator = new SystemValidator({
22
- apiRegistry,
23
- cliRegistry,
24
- })
25
-
26
- // Import and validate auth application
27
- // @ts-ignore Fix the load of aith.app
28
- const { default: authApp } = await import('@nan0web/auth.app/src/register.js')
29
- const validationErrors = validator.validate(authApp, {
30
- api: { prefix: 'auth' },
31
- cli: { command: 'auth' },
32
- })
33
-
34
- // Handle potential conflicts
35
- if (validationErrors.length > 0) {
36
- console.error('❌ Integration conflicts detected:', validationErrors)
37
- console.log('Resolve with: authApp.register({ api: { prefix: "auth2" } })')
38
- process.exit(1)
39
- }
40
-
41
- // Register validated application
42
- const { api, cli, react } = authApp.register({
43
- api: { prefix: 'auth' },
44
- cli: { command: 'auth' },
45
- })
46
-
47
- api.setup(apiRegistry)
48
- cliRegistry.register(cli.command, cli.setup)
49
- if (react && typeof react.setup === 'function') {
50
- react.setup(/* component registry placeholder */)
51
- }
52
-
53
- // Start infrastructure services
54
- startApiServer(apiRegistry)
55
- startCliProcessor(cliRegistry)
56
- }
57
-
58
- /* ---------- Helper stubs for build (replace with real implementations) ---------- */
59
- function startCliProcessor(registry) {
60
- console.log('⚙️ CLI processor started with', registry.getCommands().size, 'commands')
61
- }
62
- function createHttpServer() {
63
- return {
64
- use: (path, handler) => console.log(`🔌 Route mounted: ${path}`),
65
- listen: (port) => console.log(`🚀 HTTP server listening on port ${port}`),
66
- }
67
- }
68
- /* --------------------------------------------------------------------------- */
69
-
70
- bootstrapSystem().catch(console.error)
71
-
72
- /**
73
- * Start API server with registered routes
74
- * @param {RouteRegistry} registry
75
- */
76
- function startApiServer(registry) {
77
- const server = createHttpServer()
78
- for (const [prefix, { setup }] of registry.getRoutes()) {
79
- server.use(`/${prefix}`, setup)
80
- }
81
- server.listen(420)
82
- }
@@ -1,3 +0,0 @@
1
- export default class ApiRegistry extends RouteRegistry {
2
- }
3
- import RouteRegistry from './RouteRegistry.js';
@@ -1,42 +0,0 @@
1
- /**
2
- * Registry for CLI command namespaces
3
- * Tracks registered commands and their owners
4
- *
5
- * @example
6
- * const registry = new CommandRegistry()
7
- * registry.register('auth', { setup: () => { ... }, owner: '@nan0web/auth.app' })
8
- * registry.has('auth') // true
9
- */
10
- export default class CommandRegistry {
11
- /**
12
- * Register a CLI command namespace
13
- * @param {string} command - Command name (e.g. 'auth')
14
- * @param {{ setup: Function, owner: string }} config - Setup function and package name
15
- * @returns {this}
16
- */
17
- register(command: string, config: {
18
- setup: Function;
19
- owner: string;
20
- }): this;
21
- /**
22
- * Get all registered commands
23
- * @returns {Map<string, { setup: Function, owner: string }>}
24
- */
25
- getCommands(): Map<string, {
26
- setup: Function;
27
- owner: string;
28
- }>;
29
- /**
30
- * Check if command exists
31
- * @param {string} command
32
- * @returns {boolean}
33
- */
34
- has(command: string): boolean;
35
- /**
36
- * Get owner of command
37
- * @param {string} command
38
- * @returns {string | null}
39
- */
40
- getOwner(command: string): string | null;
41
- #private;
42
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * Registry for UI components
3
- * Tracks registered component namespaces
4
- *
5
- * @example
6
- * const registry = new ComponentRegistry()
7
- * registry.register('Auth.Login', { component: () => import('./Auth/Login.jsx'), owner: '@nan0web/auth.app' })
8
- * registry.getComponent('Auth.Login') // Returns component loader
9
- */
10
- export default class ComponentRegistry {
11
- /**
12
- * Register a component namespace
13
- * @param {string} name - Component path (e.g. 'Auth.Login')
14
- * @param {{ component: Function, owner: string }} config - Component loader and package name
15
- * @returns {this}
16
- */
17
- register(name: string, config: {
18
- component: Function;
19
- owner: string;
20
- }): this;
21
- /**
22
- * Get all registered components
23
- * @returns {Map<string, { component: Function, owner: string }>}
24
- */
25
- getComponents(): Map<string, {
26
- component: Function;
27
- owner: string;
28
- }>;
29
- /**
30
- * Get component loader by name
31
- * @param {string} name
32
- * @returns {Function | null}
33
- */
34
- getComponent(name: string): Function | null;
35
- /**
36
- * Find components by namespace prefix
37
- * @param {string} namespace - E.g., 'Auth' to find all Auth.* components
38
- * @returns {Array<string>} Component names
39
- */
40
- findComponents(namespace: string): Array<string>;
41
- #private;
42
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * Registry for API route namespaces
3
- * Tracks registered prefixes and their owners
4
- *
5
- * @example
6
- * const registry = new RouteRegistry()
7
- * registry.register('auth', { setup: (router) => { ... }, owner: '@nan0web/auth.app' })
8
- * registry.has('auth') // true
9
- */
10
- export default class RouteRegistry {
11
- /**
12
- * Register a namespace prefix
13
- * @param {string} prefix - URL prefix (e.g. 'auth')
14
- * @param {{ setup: Function, owner: string }} config - Setup function and package name
15
- * @returns {this}
16
- */
17
- register(prefix: string, config: {
18
- setup: Function;
19
- owner: string;
20
- }): this;
21
- /**
22
- * Get all registered routes
23
- * @returns {Map<string, { setup: Function, owner: string }>}
24
- */
25
- getRoutes(): Map<string, {
26
- setup: Function;
27
- owner: string;
28
- }>;
29
- /**
30
- * Check if prefix exists
31
- * @param {string} prefix
32
- * @returns {boolean}
33
- */
34
- has(prefix: string): boolean;
35
- /**
36
- * Get owner of prefix
37
- * @param {string} prefix
38
- * @returns {string | null}
39
- */
40
- getOwner(prefix: string): string | null;
41
- #private;
42
- }
@@ -1,3 +0,0 @@
1
- export default class UiRegistry extends ComponentRegistry {
2
- }
3
- import ComponentRegistry from './ComponentRegistry.js';
@@ -1,30 +0,0 @@
1
- /**
2
- * Validates application integration before registration
3
- * Checks for namespace collisions across all registries
4
- */
5
- export default class SystemValidator {
6
- /**
7
- * @param {Object} registries - System-wide registries
8
- * @param {RouteRegistry} registries.apiRegistry - Manages API route prefixes
9
- * @param {CommandRegistry} registries.cliRegistry - Manages CLI commands
10
- * @param {ComponentRegistry} [registries.uiRegistry] - Manages UI components
11
- */
12
- constructor({ apiRegistry, cliRegistry, uiRegistry }: {
13
- apiRegistry: RouteRegistry;
14
- cliRegistry: CommandRegistry;
15
- uiRegistry?: ComponentRegistry | undefined;
16
- });
17
- apiRegistry: RouteRegistry;
18
- cliRegistry: CommandRegistry;
19
- uiRegistry: ComponentRegistry;
20
- /**
21
- * Validate application configuration for potential conflicts
22
- * @param {Object} application - Must have register() method
23
- * @param {Object} [configuration] - Custom integration settings
24
- * @returns {Array<string>} List of collision errors
25
- */
26
- validate(application: any, configuration?: any): Array<string>;
27
- }
28
- import RouteRegistry from '../registry/RouteRegistry.js';
29
- import CommandRegistry from '../registry/CommandRegistry.js';
30
- import ComponentRegistry from '../registry/ComponentRegistry.js';
@@ -1,30 +0,0 @@
1
- /**
2
- * Integrates applications based on configuration file.
3
- * The config is loaded lazily to avoid TS JSON‑module issues.
4
- */
5
- export class AppIntegrator {
6
- apiRegistry: ApiRegistry;
7
- cliRegistry: CliRegistry;
8
- uiRegistry: UiRegistry;
9
- validator: SystemValidator;
10
- /**
11
- * Load the apps configuration (JSON) at runtime.
12
- * @returns {Promise<Record<string, any>>}
13
- */
14
- _loadConfig(): Promise<Record<string, any>>;
15
- /**
16
- * Register all configured applications.
17
- */
18
- registerAll(): Promise<void>;
19
- /**
20
- * Internal registration logic.
21
- * @param {string} packageName
22
- * @param {object} app
23
- * @param {object} cfg
24
- */
25
- _registerApp(packageName: string, app: object, cfg: object): void;
26
- }
27
- import ApiRegistry from '../registry/ApiRegistry.js';
28
- import CliRegistry from '../registry/CommandRegistry.js';
29
- import UiRegistry from '../registry/UiRegistry.js';
30
- import SystemValidator from './Validator.js';
@@ -1 +0,0 @@
1
- export {};