0.5.88 • Published 4 months ago

@shrub/core v0.5.88

Weekly downloads
155
License
MIT
Repository
github
Last release
4 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.87

4 months ago

0.5.88

4 months ago

0.5.85

2 years ago

0.5.86

2 years ago

0.5.83

2 years ago

0.5.84

2 years ago

0.5.81

2 years ago

0.5.82

2 years ago

0.5.80

2 years ago

0.5.77

2 years ago

0.5.78

2 years ago

0.5.79

2 years ago

0.5.76

3 years ago

0.5.74

3 years ago

0.5.75

3 years ago

0.5.72

3 years ago

0.5.73

3 years ago

0.5.70

3 years ago

0.5.71

3 years ago

0.5.69

3 years ago

0.5.67

3 years ago

0.5.68

3 years ago

0.5.65

3 years ago

0.5.66

3 years ago

0.5.63

3 years ago

0.5.64

3 years ago

0.5.61

3 years ago

0.5.62

3 years ago

0.5.60

3 years ago

0.5.54

3 years ago

0.5.55

3 years ago

0.5.52

3 years ago

0.5.53

3 years ago

0.5.50

3 years ago

0.5.51

3 years ago

0.5.58

3 years ago

0.5.59

3 years ago

0.5.56

3 years ago

0.5.57

3 years ago

0.5.49

4 years ago

0.5.47

4 years ago

0.5.48

4 years ago

0.5.43

4 years ago

0.5.44

4 years ago

0.5.45

4 years ago

0.5.46

4 years ago

0.5.41

4 years ago

0.5.42

4 years ago

0.5.40

4 years ago

0.5.32

4 years ago

0.5.33

4 years ago

0.5.38

4 years ago

0.5.39

4 years ago

0.5.36

4 years ago

0.5.37

4 years ago

0.5.34

4 years ago

0.5.35

4 years ago

0.5.30

4 years ago

0.5.31

4 years ago

0.5.29

4 years ago

0.5.28

4 years ago

0.5.27

4 years ago

0.5.26

4 years ago

0.5.25

4 years ago

0.5.22

4 years ago

0.5.23

4 years ago

0.5.24

4 years ago

0.5.21

4 years ago

0.5.20

4 years ago

0.5.18

4 years ago

0.5.19

4 years ago

0.5.16

4 years ago

0.5.17

4 years ago

0.5.14

4 years ago

0.5.15

4 years ago

0.5.13

4 years ago

0.5.12

4 years ago

0.5.11

4 years ago

0.5.10

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.9

4 years ago

0.5.4

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.95

4 years ago

0.4.94

4 years ago

0.4.93

4 years ago

0.4.92

4 years ago

0.4.91

5 years ago

0.4.90

5 years ago

0.4.89

5 years ago

0.4.86

5 years ago

0.4.87

5 years ago

0.4.88

5 years ago

0.4.84

5 years ago

0.4.85

5 years ago

0.4.83

5 years ago

0.4.82

5 years ago

0.4.81

5 years ago

0.4.80

5 years ago

0.4.79

5 years ago

0.4.77

5 years ago

0.4.78

5 years ago

0.4.76

5 years ago

0.4.75

5 years ago

0.4.73

5 years ago

0.4.74

5 years ago

0.4.72

5 years ago

0.4.71

5 years ago

0.4.70

5 years ago

0.4.69

5 years ago

0.4.68

5 years ago

0.4.67

5 years ago

0.4.66

5 years ago

0.4.64

5 years ago

0.4.65

5 years ago

0.4.62

5 years ago

0.4.63

5 years ago

0.4.60

5 years ago

0.4.61

5 years ago

0.4.59

5 years ago

0.4.58

5 years ago

0.4.57

5 years ago

0.4.56

5 years ago

0.4.55

5 years ago

0.4.53

5 years ago

0.4.54

5 years ago

0.4.52

5 years ago

0.4.51

5 years ago

0.4.50

5 years ago

0.4.49

5 years ago

0.4.48

5 years ago

0.4.47

5 years ago

0.4.46

5 years ago

0.4.44

5 years ago

0.4.45

5 years ago

0.4.42

5 years ago

0.4.43

5 years ago

0.4.40

5 years ago

0.4.41

5 years ago

0.4.39

5 years ago

0.4.38

5 years ago

0.4.37

5 years ago

0.4.36

5 years ago

0.4.35

5 years ago

0.4.34

5 years ago

0.4.32

5 years ago

0.4.33

5 years ago

0.4.31

5 years ago

0.4.30

5 years ago

0.4.29

5 years ago

0.4.28

5 years ago

0.4.26

5 years ago

0.4.27

5 years ago

0.4.24

5 years ago

0.4.25

5 years ago

0.4.23

5 years ago

0.4.22

5 years ago

0.4.21

5 years ago

0.4.20

5 years ago

0.4.19

5 years ago

0.4.18

5 years ago

0.4.17

5 years ago

0.4.16

5 years ago

0.4.15

6 years ago

0.4.13

6 years ago

0.4.14

6 years ago

0.4.12

6 years ago

0.4.11

6 years ago

0.4.10

6 years ago

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.11

6 years ago

0.3.10

6 years ago

0.3.9

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago