mirror of
https://github.com/sern-handler/handler
synced 2026-06-27 18:22:14 +00:00
feat: globalize dependencies type
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import * as assert from 'assert';
|
||||
import { composeRoot, useContainer } from './dependency-injection';
|
||||
import { Dependencies, DependencyConfiguration } from './types';
|
||||
import { DependencyConfiguration } from './types';
|
||||
import { CoreContainer } from '../structures/container';
|
||||
|
||||
//SIDE EFFECT: GLOBAL DI
|
||||
|
||||
10
src/core/ioc/dependencies.d.ts
vendored
Normal file
10
src/core/ioc/dependencies.d.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
import { CoreDependencies } from './types'
|
||||
|
||||
declare global {
|
||||
|
||||
interface Dependencies extends CoreDependencies {}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import type {
|
||||
DependencyConfiguration,
|
||||
MapDeps,
|
||||
IntoDependencies,
|
||||
Dependencies,
|
||||
CoreDependencies,
|
||||
DependencyConfiguration,
|
||||
IntoDependencies,
|
||||
} from './types';
|
||||
import { DefaultLogging } from '../structures';
|
||||
import { SernError } from '../structures/errors';
|
||||
@@ -57,7 +55,7 @@ export async function composeRoot(
|
||||
});
|
||||
}
|
||||
//Build the container based on the callback provided by the user
|
||||
conf.build(container as CoreContainer<CoreDependencies>);
|
||||
conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>);
|
||||
try {
|
||||
container.get('@sern/client');
|
||||
} catch {
|
||||
@@ -78,5 +76,5 @@ export function useContainer<const T extends Dependencies>() {
|
||||
Use the new Service(s) api function instead.
|
||||
`);
|
||||
return <V extends (keyof T)[]>(...keys: [...V]) =>
|
||||
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as MapDeps<T, V>;
|
||||
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies<V>;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export { useContainerRaw, makeDependencies } from './base';
|
||||
export { Service, Services, single, transient } from './dependency-injection';
|
||||
export type { Singleton, Transient } from './types';
|
||||
export type { Singleton, Transient, CoreDependencies } from './types';
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { Container, UnpackFunction } from 'iti';
|
||||
import * as Contract from '../contracts';
|
||||
export type Singleton<T> = () => T;
|
||||
export type Transient<T> = () => () => T;
|
||||
|
||||
export interface CoreDependencies {
|
||||
'@sern/logger'?: Singleton<Contract.Logging>;
|
||||
'@sern/emitter': Singleton<import('../structures/sern-emitter').SernEmitter>;
|
||||
'@sern/store': Singleton<Contract.CoreModuleStore>;
|
||||
'@sern/modules': Singleton<Contract.ModuleManager>;
|
||||
'@sern/errors': Singleton<Contract.ErrorHandling>;
|
||||
'@sern/client': () => EventEmitter
|
||||
'@sern/logger'?: () => import('../contracts').Logging;
|
||||
'@sern/emitter': () => import('../structures/sern-emitter').SernEmitter;
|
||||
'@sern/store': () => import('../contracts').CoreModuleStore;
|
||||
'@sern/modules': () => import('../contracts').ModuleManager;
|
||||
'@sern/errors': () => import('../contracts').ErrorHandling;
|
||||
}
|
||||
|
||||
export interface Dependencies extends CoreDependencies {
|
||||
'@sern/client': Singleton<import('node:events').EventEmitter>;
|
||||
}
|
||||
export type DependencyFromKey<T extends keyof Dependencies> = Dependencies[T];
|
||||
|
||||
export type IntoDependencies<Tuple extends [...any[]]> = {
|
||||
@@ -23,17 +21,6 @@ export type IntoDependencies<Tuple extends [...any[]]> = {
|
||||
export interface DependencyConfiguration {
|
||||
//@deprecated. Loggers will always be included in the future
|
||||
exclude?: Set<'@sern/logger'>;
|
||||
build: (root: Container<CoreDependencies, {}>) => Container<Dependencies, {}>;
|
||||
build: (root: Container<Omit<CoreDependencies, '@sern/client'>, {}>) => Container<Dependencies, {}>;
|
||||
}
|
||||
|
||||
//To be removed in future
|
||||
//prettier-ignore
|
||||
export type MapDeps<Deps extends Dependencies, T extends readonly unknown[]> = T extends [
|
||||
infer First extends keyof Deps,
|
||||
...infer Rest extends readonly unknown[],
|
||||
]
|
||||
? [
|
||||
UnpackFunction<Deps[First]>,
|
||||
...(MapDeps<Deps, Rest> extends [never] ? [] : MapDeps<Deps, Rest>),
|
||||
]
|
||||
: [never];
|
||||
|
||||
@@ -4,7 +4,6 @@ import { isAsyncFunction } from 'node:util/types';
|
||||
import * as assert from 'node:assert';
|
||||
import { Subject } from 'rxjs';
|
||||
import { ModuleStore } from './module-store';
|
||||
import { Dependencies } from '../ioc/types';
|
||||
|
||||
/**
|
||||
* Provides all the defaults for sern to function properly.
|
||||
@@ -41,7 +40,8 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
|
||||
}
|
||||
|
||||
private async callInitHooks(e: { key: keyof T; newContainer: T[keyof T] | null }) {
|
||||
const dep = e.newContainer;
|
||||
const dep = e.newContainer ;
|
||||
|
||||
assert.ok(dep);
|
||||
//Ignore any dependencies that are not objects or array
|
||||
if (typeof dep !== 'object' || Array.isArray(dep)) {
|
||||
|
||||
Reference in New Issue
Block a user