harno-kernel-web

0.0.1 • Public • Published

Harno-kernel-web

Npm module that helps you to build your web-like js projects. This module is basically the same as harno-kernel, but it also contains a lot of usefull services for web

Install

npm install harno-kernel-web

Usage (SORRY, IT IS NOT QUITE RIGHT, WILL BE UPDATE SOON)

Basic

main.js
// you can export WebKernel instance
const { YourService } = require('./your.file.js')
const webKernel = require('harno-kernel-web').default
 
export default class App {
  constructor () {
    // you can init all default services and it's dependencies
    webKernel.registerDefaultServices()
    webKernel.initDefaultServicesDependencies()
 
    // some services have logger service as their dependencies, so before initing them, it would be great to configure logger
    webKernel.getService('logger').config({ ... })
 
    // call init methods of some services
    webKernel.initDefaultServices()
 
    // you can register your services like this
    webKernel.registerService('yourAnyName', YourService)
 
    // you can init dependencies for your service like this
    webKernel.initDependencies({
      yourAnyName: ['express', 'config']
    })
 
    // you can get any service by its name like this
    const yourAnyName = webKernel.getService('yourAnyName')
    // or
    const yourAnyName = webKernel.s.yourAnyName
  }
}

Advanced

main.js
// you can export WebKernel class, or any other available service
const { YourService } = require('./your.file.js')
const { WebKernel, ConfigService, ExpressService, ... } = require('harno-kernel-web').default
 
export default class App {
  constructor () {
    this.kernel = new WebKernel()
 
    // you can register services
    this.kernel.registerService('express', ExpressService)
    this.kernel.registerService('config', ConfigService)
    this.kernel.registerService('yourAnyName', YourService)
 
    // you can init dependencies for any service (for some default services you must do that)
    this.kernel.initDependencies({
      yourAnyName: ['express', 'config']
    })
 
    // you need to call init function of some default services
    this.kernel.s.express.init()
 
    // in this example YourService would have property _dependencies or _d with links to express and config services
    const yourAnyName = this.kernel.getService('yourAnyName')
 
    // look at the your.file.js bellow
    console.log(yourAnyName.afterInitDependencies())
    // output: My dependencies is: express and config
  }
 
  async start () {
    // you can get services like this
    this.kernel.getService('configService').readJson('./path.to.json')
 
    // or like this
    const port = this.kernel.s.configService.c.port
    await this.kernel.s.expressService.start(port)
  }
}
your.file.js
class YourService {
  afterInitDependencies () {
    // after init your service will have property _dependencies or _d, that will contain all inited dependencies
    const express = this._d.express
    const config = this._d.config
 
    if (express && config) {
      console.log('My dependencies is: express and config')
    }
  }
}
 
export { YourService }

API

new WebKernel()

Creates new server kernel instance

webKernel.initDependencies(dependenciesConfig)

  • dependenciesConfig example:
  const dependenciesConfig = {
    serviceName1: ['serviceName2', 'serviceName3'],
    serviceName2: ['serviceName4'],
    ...
  }

Inits/sets services dependencies, so that in our example service serviceName1 will have property _dependencies or _d that would contain serviceName2, serviceName3 services

webKernel.registerService(name, Service)

  • name - service name (can be any string)
  • Service - any js class

Register/saves Service class instance under the name in the server kernel

webKernel.getService(name)

  • name - service name

Gets service that was created under that name

webKernel.s

Kernel property that contains all registered services

webKernel.registerDefaultServices()

Registers all default services under default names (look at Defaut services for more info)

webKernel.initDefaultServicesDependencies()

Inits all default services dependencies

webKernel.initDefaultServices()

Inits all default services. Calls init method of default services

Default services

TestService

  • Default name: testService
  • Dependencies: ['testService']
  • Description: Test class would be deleted
  • API: Work in progress, so more reliable api would be announced later

License

License

Package Sidebar

Install

npm i harno-kernel-web

Weekly Downloads

0

Version

0.0.1

License

SEE LICENSE IN LICENSE

Unpacked Size

10.8 kB

Total Files

9

Last publish

Collaborators

  • harnosoft