@nan0web/core 1.0.1 → 1.0.3
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 +3 -3
- package/package.json +12 -12
- package/src/registry/ApiRegistry.js +7 -0
- package/src/registry/CommandRegistry.js +50 -0
- package/src/registry/ComponentRegistry.js +51 -0
- package/src/registry/RouteRegistry.js +50 -0
- package/src/registry/UiRegistry.js +7 -0
- package/src/system/Validator.js +55 -0
- package/src/system/integrator.js +84 -0
- package/src/system/main.js +82 -0
- package/types/registry/ApiRegistry.d.ts +3 -0
- package/types/registry/CommandRegistry.d.ts +42 -0
- package/types/registry/ComponentRegistry.d.ts +42 -0
- package/types/registry/RouteRegistry.d.ts +42 -0
- package/types/registry/UiRegistry.d.ts +3 -0
- package/types/system/Validator.d.ts +30 -0
- package/types/system/integrator.d.ts +30 -0
- package/types/system/main.d.ts +1 -0
- package/src/AppCore.test.js +0 -59
- package/src/AppResult.test.js +0 -45
package/README.md
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
Core application framework for nan0web providing a lightweight
|
|
4
4
|
DB‑backed state container with built‑in internationalisation.
|
|
5
5
|
|
|
6
|
-
|[Status](https://github.com/nan0web/monorepo/blob/main/system.md#написання-сценаріїв)|Documentation|Test coverage|Features|Npm version|
|
|
7
|
-
|
|
8
|
-
|🟢 `
|
|
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 |
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
package/package.json
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nan0web/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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",
|
|
7
7
|
"types": "types/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
|
-
"src
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"README.md"
|
|
9
|
+
"src/**/*.js",
|
|
10
|
+
"!src/**/*.test.js",
|
|
11
|
+
"types/**/*.d.ts"
|
|
13
12
|
],
|
|
14
13
|
"scripts": {
|
|
15
14
|
"build": "tsc",
|
|
@@ -19,14 +18,15 @@
|
|
|
19
18
|
"prepush": "npm test",
|
|
20
19
|
"prepare": "husky",
|
|
21
20
|
"prepublishOnly": "npm test && npm run build",
|
|
22
|
-
"
|
|
21
|
+
"play": "node play/main.js",
|
|
23
22
|
"release": "nan0release publish",
|
|
24
|
-
"test": "node --test",
|
|
23
|
+
"test": "node --test --test-timeout=3333 \"src/**/*.test.js\"",
|
|
25
24
|
"test:docs": "node --test src/README.md.js",
|
|
26
25
|
"test:release": "node --test \"releases/**/*.test.js\"",
|
|
27
26
|
"test:coverage": "node --experimental-test-coverage --test-coverage-include=\"src/**/*.js\" --test-coverage-exclude=\"src/**/*.test.js\" --test \"src/**/*.test.js\"",
|
|
28
27
|
"test:coverage:collect": "nan0test coverage",
|
|
29
|
-
"test:status": "nan0test status --hide-name"
|
|
28
|
+
"test:status": "nan0test status --hide-name",
|
|
29
|
+
"test:all": "npm run test && npm run test:docs && npm run build"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [
|
|
32
32
|
"nan0",
|
|
@@ -43,12 +43,12 @@
|
|
|
43
43
|
"license": "ISC",
|
|
44
44
|
"packageManager": "pnpm@10.11.0",
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@nan0web/release": "
|
|
47
|
-
"@nan0web/test": "
|
|
46
|
+
"@nan0web/release": "1.0.1",
|
|
47
|
+
"@nan0web/test": "1.1.3",
|
|
48
48
|
"husky": "^9.1.7"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@nan0web/interface": "
|
|
52
|
-
"@nan0web/protocol": "
|
|
51
|
+
"@nan0web/interface": "1.0.1",
|
|
52
|
+
"@nan0web/protocol": "2.0.0"
|
|
53
53
|
}
|
|
54
54
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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';
|
|
@@ -0,0 +1,30 @@
|
|
|
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';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/src/AppCore.test.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { test, describe } from 'node:test'
|
|
2
|
-
import { strictEqual, deepEqual, rejects } from 'node:assert'
|
|
3
|
-
import AppCore from './AppCore.js'
|
|
4
|
-
import DB from '@nan0web/db'
|
|
5
|
-
|
|
6
|
-
describe('AppCore', () => {
|
|
7
|
-
test('constructor initializes properties correctly', () => {
|
|
8
|
-
const db = new DB()
|
|
9
|
-
const app = new AppCore({ db, locale: 'en' })
|
|
10
|
-
|
|
11
|
-
strictEqual(app.db, db)
|
|
12
|
-
strictEqual(app.locale, 'en')
|
|
13
|
-
deepEqual(app.data, {})
|
|
14
|
-
deepEqual(app.actions, {})
|
|
15
|
-
deepEqual(app.meta, {})
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
test('constructor uses default locale when not provided', () => {
|
|
19
|
-
const db = new DB()
|
|
20
|
-
const app = new AppCore({ db })
|
|
21
|
-
|
|
22
|
-
strictEqual(app.locale, 'uk')
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test('bootstrapI18n loads translations from db', async () => {
|
|
26
|
-
const db = new DB()
|
|
27
|
-
const mockTranslations = { 'test.key': 'Test Value' }
|
|
28
|
-
db.fetch = async () => mockTranslations
|
|
29
|
-
|
|
30
|
-
const app = new AppCore({ db, locale: 'en' })
|
|
31
|
-
await app.bootstrapI18n('/custom/{{locale}}.json')
|
|
32
|
-
|
|
33
|
-
const result = app.t('test.key')
|
|
34
|
-
strictEqual(result, 'Test Value')
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test('state() returns current application state', () => {
|
|
38
|
-
const db = new DB()
|
|
39
|
-
const app = new AppCore({ db })
|
|
40
|
-
|
|
41
|
-
const state = app.state()
|
|
42
|
-
deepEqual(Object.keys(state), ['data', 'actions', 'meta', 't'])
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test('run() throws error when not implemented', async () => {
|
|
46
|
-
const db = new DB()
|
|
47
|
-
const app = new AppCore({ db })
|
|
48
|
-
|
|
49
|
-
await rejects(
|
|
50
|
-
async () => {
|
|
51
|
-
await app.run()
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
name: 'Error',
|
|
55
|
-
message: 'AppCore: run() must be implemented'
|
|
56
|
-
}
|
|
57
|
-
)
|
|
58
|
-
})
|
|
59
|
-
})
|
package/src/AppResult.test.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { test, describe } from 'node:test'
|
|
2
|
-
import { deepEqual, strictEqual, ok } from 'node:assert'
|
|
3
|
-
import AppResult from './AppResult.js'
|
|
4
|
-
|
|
5
|
-
describe('AppResult', () => {
|
|
6
|
-
test('constructor creates instance with string content', () => {
|
|
7
|
-
const result = new AppResult({
|
|
8
|
-
content: 'Test content',
|
|
9
|
-
priority: 1,
|
|
10
|
-
meta: { test: true }
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
deepEqual(result.content, ['Test content'])
|
|
14
|
-
strictEqual(result.priority, 1)
|
|
15
|
-
deepEqual(result.meta, { test: true })
|
|
16
|
-
strictEqual(result.error, null)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test('constructor creates instance with array content', () => {
|
|
20
|
-
const result = new AppResult({
|
|
21
|
-
content: ['Line 1', 'Line 2'],
|
|
22
|
-
priority: 2
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
deepEqual(result.content, ['Line 1', 'Line 2'])
|
|
26
|
-
strictEqual(result.priority, 2)
|
|
27
|
-
deepEqual(result.meta, {})
|
|
28
|
-
strictEqual(result.error, null)
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test('static from() returns same instance if AppResult provided', () => {
|
|
32
|
-
const original = new AppResult({ content: 'test' })
|
|
33
|
-
const result = AppResult.from(original)
|
|
34
|
-
|
|
35
|
-
strictEqual(result, original)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test('static from() creates new instance if plain object provided', () => {
|
|
39
|
-
const input = { content: 'test' }
|
|
40
|
-
const result = AppResult.from(input)
|
|
41
|
-
|
|
42
|
-
ok(result instanceof AppResult)
|
|
43
|
-
deepEqual(result.content, ['test'])
|
|
44
|
-
})
|
|
45
|
-
})
|