The Hapiness object is used to bootstrap a module as Web Server.
Bootstrap a module and start the web server.
Declare an Hapiness module with the providers, routes and libs.
@HapinessModule({metadata})
class MyClass {}
-
metadata
version
- module versionoptions
- bootstrapped module :
host
- server hostport
- http server portsocketPort
- websocket server port
- bootstrapped module :
declarations
- Routes | Libs to declare in the moduleproviders
- Providers to add in the DIimports
- Modules to importexports
- Providers to export and will be available in the module that import it
-
interfaces
OnStart
- Only for the bootstrapped module, called when the web server is started.OnError
- Only for the bootstrapped module, it is the error handler.- arguments: (error: Error)
OnRegister
- Called when the module is registeredOnModuleResolved
- Called when imported module is resolved- arguments: (module: string)
When you import a module, you can provide data.
// external-module.ts
import {
HapinessModule,
CoreModuleWithProviders,
InjectionToken,
Inject,
Optional,
} from '@hapiness/core';
const CONFIG = new InjectionToken('config');
interface Config {
baseUrl: string;
}
@HapinessModule({
...
})
export class ExternalModule {
static setConfig(config: Config): CoreModuleWithProviders {
return {
module: ExternalModule,
providers: [{ provide: CONFIG, useValue: config }]
};
}
}
export class Service {
constructor(@Optional() @Inject(CONFIG) config) { // @Optional to not throw errors if config is not passed
...
}
}
// main-module.ts
import {
HapinessModule,
} from '@hapiness/core';
import { ExternalModule } from 'external-module';
@HapinessModule({
...
imports: [ ExternalModule.setConfig({ hello: 'world!' }) ]
})
...
Declare an injectable provider
@Injectable()
class MyService {}
Declare an empty component for any use
@Lib()
class MyLib {}
When you ask for a dependency, Optional tell to the DI to not throw an error if the dependency is not available.
...
constructor(@Optional() dep: MyDep) {
if (dep) {
...
}
}
...
Create custom token for the DI
const MY_CUSTOM_TOKEN = new InjectionToken('my-token');
@HapinessModule({
...
imports: [{ provide: MY_CUSTOM_TOKEN, useValue: 'abcdef' }]
...
})
class MyModule {
constructor(@Inject(MY_CUSTOM_TOKEN) myValue) {
console.log(myValue) // ouput: 'abcdef'
}
}
An extension is a class provided to the boostrap.
class ExampleExt implements OnExtensionLoad {
public static setConfig(config: any): ExtensionWithConfig {
return {
token: HttpServerExt,
config
};
}
onExtensionLoad(module: CoreModule, config: any) {
...
}
}
Methods to implement:
onExtensionLoad
- Called while bootstrapping- arguments: (module: CoreModule, config: any)
- returns: Observable
OnModuleInstantiated
- (Optional) Called once the bootstrapped module is instanciated- arguments: (module: CoreModule, extension value)
- returns: void
OnShutdown
- (Optional) Called once the process catch aSIGTERM
or aSIGINT
event- arguments: (module: CoreModule, extension value)
- returns: ExtensionShutdown { priority: ExtensionShutdownPriority.IMPORTANT | ExtensionShutdownPriority.NORMAL, resolver: Observable }
The shutdown feature allow to stop gracefully the service.
the IMPORTANT
priority is for the extensions that needs to process ongoing requests.
The NORMAL
priority is for extensions that can be stopped directly.
Extension:
{
value: any;
instance: any;
token: Type<any>;
}
value
- It's the value provided through the DIinstance
- Extension instance,this
token
- Class token (ex: ExampleExt)
See the documentation: here
See the documentation: here
See the documentation: here