0.5.86 • Published 5 months ago

@shrub/core v0.5.86

Weekly downloads
155
License
MIT
Repository
github
Last release
5 months ago

Description

Shrub is a simple framework for building modular server-side applications and front-end components. The Core package provides a basic API for defining and loading modules.

Creating a Module

A module can be defined as a class that implements the IModule interface or as a JSON object that satisfies the IModule interface.

export class FooModule implements IModule {
    readonly name = "foo";
}
const fooModule: IModule = {
    name: "foo"
};

There are a few methods a module can define that allow it to configure functionality provided by the module and each method is invoked in the following order.

1. initialize(init: IModuleInitializer): void

Allows a module the ability to perform pre-initialization that needs to happen before any modules or services are configured. Such tasks include binding settings or registering a configuration interface that is exposed to other modules.

2. configureServices(registration: IServiceRegistration): void

Register services with a service collection.

3. configure(configurator: IModuleConfigurator): void | Promise<void>

Allows a module to perform additional configuration, such as configure a dependency module. This method supports async operations which can be useful if data needs to be fetched from a remote service during configuration.

Dependencies

Module depenedencies are specified by defining a dependencies property on a module and lifecycle methods get invoked on the dependency before they are invoked on the dependent.

export class FooModule implements IModule {
    readonly name = "foo";
    readonly dependencies = [BarModule];
}

Configuration

export const IBarModuleConfiguration = createConfig<IBarModuleConfiguration>();
export interface IBarModuleConfiguration {
    registerWidget(widget: IWidget): void;
}

export class BarModule implements IModule {
    readonly name = "bar";

    initialize({ config }: IModuleInitializer): void {
        config(IBarModuleConfiguration).register(() => ({
            registerWidget: widget => {}
        }));
    }
}

export class FooModule implements IModule {
    readonly name = "foo";
    readonly dependencies = [BarModule];

    configure({ config }: IModuleConfigurator): void {
        config.get(IBarModuleConfiguration).registerWidget({});
    }
}

Settings and Options

While Dependency Configuration is a way for one module to configure another at load time. Settings provide a way to provide settings/configuration externally, such as from a config file.

Module Settings are provided as a simple object keyed by a module's name; for example, the below is an example settings object that defines settings for the 'foo' module:

const settings = {
    foo: {
        key: value
    }
};

A module can access these settings directly from the configure method:

export class FooModule implements IModule {
    readonly name = "foo";

    configure({ settings }: IModuleConfigurator): void {
        const keyValue = settings.key;
    }
}

Sometimes it's useful to pass settings to service instances and this can be done via Service Options.

export const IFooOptions = createOptions<IFooOptions>("foo-options");
export interface IFooOptions {
    readonly value: string;
}

export class FooModule implements IModule {
    readonly name = "foo";

    initialize({ settings }: IModuleInitializer): void {
        settings.bindToOptions(IFooOptions);
    }

    configureServices(registration: IServiceRegistration): void {
        registration.registerTransient(IFooService, FooService);
    }
}

export class FooService implements IFooService {
    constructor(@IFooOptions private readonly options: IFooOptions) {
    }
}

Loading Modules

Modules are loaded using the ModuleLoader class by simply invoking ModuleLoader.load.

await ModuleLoader.load([
    FooModule,
    BarModule
]);

Note: If a module has any dependencies not specified when calling ModuleLoader.load those dependencies will automatically get loaded.

If you want a little more control the module loader provides additional methods for configuring the service collection or settings.

await ModuleLoader()
    .useModules([
        FooModule,
        BarModule
    ])
    .useSettings({
        foo: { value: "Hello!" }
    })
    .load();
0.5.85

5 months ago

0.5.86

5 months ago

0.5.83

6 months ago

0.5.84

6 months ago

0.5.81

10 months ago

0.5.82

6 months ago

0.5.80

11 months ago

0.5.77

11 months ago

0.5.78

11 months ago

0.5.79

11 months ago

0.5.76

2 years ago

0.5.74

2 years ago

0.5.75

2 years ago

0.5.72

2 years ago

0.5.73

2 years ago

0.5.70

2 years ago

0.5.71

2 years ago

0.5.69

2 years ago

0.5.67

2 years ago

0.5.68

2 years ago

0.5.65

2 years ago

0.5.66

2 years ago

0.5.63

2 years ago

0.5.64

2 years ago

0.5.61

2 years ago

0.5.62

2 years ago

0.5.60

2 years ago

0.5.54

2 years ago

0.5.55

2 years ago

0.5.52

2 years ago

0.5.53

2 years ago

0.5.50

2 years ago

0.5.51

2 years ago

0.5.58

2 years ago

0.5.59

2 years ago

0.5.56

2 years ago

0.5.57

2 years ago

0.5.49

2 years ago

0.5.47

2 years ago

0.5.48

2 years ago

0.5.43

3 years ago

0.5.44

2 years ago

0.5.45

2 years ago

0.5.46

2 years ago

0.5.41

3 years ago

0.5.42

3 years ago

0.5.40

3 years ago

0.5.32

3 years ago

0.5.33

3 years ago

0.5.38

3 years ago

0.5.39

3 years ago

0.5.36

3 years ago

0.5.37

3 years ago

0.5.34

3 years ago

0.5.35

3 years ago

0.5.30

3 years ago

0.5.31

3 years ago

0.5.29

3 years ago

0.5.28

3 years ago

0.5.27

3 years ago

0.5.26

3 years ago

0.5.25

3 years ago

0.5.22

3 years ago

0.5.23

3 years ago

0.5.24

3 years ago

0.5.21

3 years ago

0.5.20

3 years ago

0.5.18

3 years ago

0.5.19

3 years ago

0.5.16

3 years ago

0.5.17

3 years ago

0.5.14

3 years ago

0.5.15

3 years ago

0.5.13

3 years ago

0.5.12

3 years ago

0.5.11

3 years ago

0.5.10

3 years ago

0.5.8

3 years ago

0.5.7

3 years ago

0.5.9

3 years ago

0.5.4

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.95

3 years ago

0.4.94

3 years ago

0.4.93

3 years ago

0.4.92

3 years ago

0.4.91

4 years ago

0.4.90

4 years ago

0.4.89

4 years ago

0.4.86

4 years ago

0.4.87

4 years ago

0.4.88

4 years ago

0.4.84

4 years ago

0.4.85

4 years ago

0.4.83

4 years ago

0.4.82

4 years ago

0.4.81

4 years ago

0.4.80

4 years ago

0.4.79

4 years ago

0.4.77

4 years ago

0.4.78

4 years ago

0.4.76

4 years ago

0.4.75

4 years ago

0.4.73

4 years ago

0.4.74

4 years ago

0.4.72

4 years ago

0.4.71

4 years ago

0.4.70

4 years ago

0.4.69

4 years ago

0.4.68

4 years ago

0.4.67

4 years ago

0.4.66

4 years ago

0.4.64

4 years ago

0.4.65

4 years ago

0.4.62

4 years ago

0.4.63

4 years ago

0.4.60

4 years ago

0.4.61

4 years ago

0.4.59

4 years ago

0.4.58

4 years ago

0.4.57

4 years ago

0.4.56

4 years ago

0.4.55

4 years ago

0.4.53

4 years ago

0.4.54

4 years ago

0.4.52

4 years ago

0.4.51

4 years ago

0.4.50

4 years ago

0.4.49

4 years ago

0.4.48

4 years ago

0.4.47

4 years ago

0.4.46

4 years ago

0.4.44

4 years ago

0.4.45

4 years ago

0.4.42

4 years ago

0.4.43

4 years ago

0.4.40

4 years ago

0.4.41

4 years ago

0.4.39

4 years ago

0.4.38

4 years ago

0.4.37

4 years ago

0.4.36

4 years ago

0.4.35

4 years ago

0.4.34

4 years ago

0.4.32

4 years ago

0.4.33

4 years ago

0.4.31

4 years ago

0.4.30

4 years ago

0.4.29

4 years ago

0.4.28

4 years ago

0.4.26

4 years ago

0.4.27

4 years ago

0.4.24

4 years ago

0.4.25

4 years ago

0.4.23

4 years ago

0.4.22

4 years ago

0.4.21

4 years ago

0.4.20

4 years ago

0.4.19

4 years ago

0.4.18

4 years ago

0.4.17

4 years ago

0.4.16

4 years ago

0.4.15

4 years ago

0.4.13

4 years ago

0.4.14

4 years ago

0.4.12

4 years ago

0.4.11

4 years ago

0.4.10

4 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.11

5 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago