Refactor units to be generic; start bundles; start app index.ts
This commit is contained in:
@@ -8,11 +8,11 @@ export interface CanonicalDefinition {
|
||||
imported: any,
|
||||
}
|
||||
|
||||
export class Canonical extends LifecycleUnit {
|
||||
export class Canonical<T> extends LifecycleUnit {
|
||||
protected base_path: string = '.'
|
||||
protected suffix: string = '.ts'
|
||||
protected canonical_item: string = ''
|
||||
protected _items: { [key: string]: any } = {}
|
||||
protected _items: { [key: string]: T } = {}
|
||||
|
||||
public get path(): string {
|
||||
return path.resolve(this.base_path)
|
||||
@@ -32,7 +32,7 @@ export class Canonical extends LifecycleUnit {
|
||||
this.make(Canon).register_resource(this.canonical_items, (key: string) => this.get(key))
|
||||
}
|
||||
|
||||
public async init_canonical_item(definition: CanonicalDefinition): Promise<any> {
|
||||
public async init_canonical_item(definition: CanonicalDefinition): Promise<T> {
|
||||
return definition.imported.default
|
||||
}
|
||||
|
||||
@@ -47,12 +47,7 @@ export class Canonical extends LifecycleUnit {
|
||||
return { canonical_name, original_name, imported }
|
||||
}
|
||||
|
||||
public get(key: string): any {
|
||||
const key_parts = key.split('.')
|
||||
let desc_value = this._items
|
||||
key_parts.forEach(part => {
|
||||
desc_value = desc_value[part]
|
||||
})
|
||||
return desc_value
|
||||
public get(key: string): T | undefined {
|
||||
return this._items[key]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {Canonical} from './Canonical.ts'
|
||||
import { Unit } from '../lifecycle/decorators.ts'
|
||||
import {RecursiveCanonical} from './RecursiveCanonical.ts'
|
||||
|
||||
@Unit()
|
||||
export default class Config extends Canonical {
|
||||
export default class Config extends RecursiveCanonical {
|
||||
protected base_path = './app/configs'
|
||||
protected suffix = '.config.ts'
|
||||
protected canonical_item = 'config'
|
||||
|
||||
@@ -4,7 +4,7 @@ import Controller from '../http/Controller.ts'
|
||||
import { Unit } from '../lifecycle/decorators.ts'
|
||||
|
||||
@Unit()
|
||||
export default class Controllers extends InstantiableCanonical {
|
||||
export default class Controllers extends InstantiableCanonical<Controller> {
|
||||
protected base_path = './app/http/controllers'
|
||||
protected canonical_item = 'controller'
|
||||
protected suffix = '.controller.ts'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {Canonical, CanonicalDefinition} from './Canonical.ts'
|
||||
import {isInstantiable} from '../../../di/src/type/Instantiable.ts'
|
||||
import Instantiable, {isInstantiable} from '../../../di/src/type/Instantiable.ts'
|
||||
|
||||
export class InvalidCanonicalExportError extends Error {
|
||||
constructor(name: string) {
|
||||
@@ -7,8 +7,8 @@ export class InvalidCanonicalExportError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class InstantiableCanonical extends Canonical {
|
||||
public async init_canonical_item(def: CanonicalDefinition) {
|
||||
export class InstantiableCanonical<T> extends Canonical<Instantiable<T>> {
|
||||
public async init_canonical_item(def: CanonicalDefinition): Promise<Instantiable<T>> {
|
||||
if ( isInstantiable(def.imported.default) ) {
|
||||
return this.make(def.imported.default)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Middleware } from '../http/Middleware.ts'
|
||||
import { Unit } from '../lifecycle/decorators.ts'
|
||||
|
||||
@Unit()
|
||||
export default class Middlewares extends InstantiableCanonical {
|
||||
export default class Middlewares extends InstantiableCanonical<Middleware> {
|
||||
protected base_path = './app/http/middleware'
|
||||
protected canonical_item = 'middleware'
|
||||
protected suffix = '.middleware.ts'
|
||||
|
||||
12
lib/src/unit/RecursiveCanonical.ts
Normal file
12
lib/src/unit/RecursiveCanonical.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import {Canonical} from './Canonical.ts'
|
||||
|
||||
export class RecursiveCanonical extends Canonical<any> {
|
||||
public get(key: string): any | undefined {
|
||||
const parts = key.split('.')
|
||||
let current_value = this._items
|
||||
for ( const part of parts ) {
|
||||
current_value = current_value?.[part]
|
||||
}
|
||||
return current_value
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import { Container } from '../../../di/src/Container.ts'
|
||||
import { Inject } from '../../../di/src/decorator/Injection.ts'
|
||||
import CacheFactory from "../support/CacheFactory.ts";
|
||||
|
||||
const env = (name: string, fallback: any) => {
|
||||
const env = (name: string, fallback?: any) => {
|
||||
const scaffolding = make(Scaffolding)
|
||||
return scaffolding.env(name) ?? fallback
|
||||
}
|
||||
@@ -31,6 +31,9 @@ export default class Scaffolding extends LifecycleUnit {
|
||||
|
||||
public async up() {
|
||||
this.setup_logging()
|
||||
|
||||
this.logger.verbose('Adding the cache production factory to the container...')
|
||||
this.injector.register_factory(new CacheFactory())
|
||||
}
|
||||
|
||||
public setup_logging() {
|
||||
@@ -48,8 +51,5 @@ export default class Scaffolding extends LifecycleUnit {
|
||||
} catch (e) {}
|
||||
|
||||
this.logger.info('Logging initialized.', true)
|
||||
|
||||
this.logger.verbose('Adding the cache production factory to the container...')
|
||||
this.injector.register_factory(new CacheFactory())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user