1.3.0 • Published 5 months ago

service-startup v1.3.0

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

Wait for server ready before start.

npm

Installation

  • npm: npm i -S service-startup
  • yarn: yarn add service-startup

Usage

You can use it like on example below. App will exit if there at least on step is failed. Check all connections before starting server:

You can create file for your services like startup.ts. Note that this file will be important automatically, so you don't need to import it. But you can create own separate files

import serviceStartup from 'service-startup'
// databases and others...

serviceStartup.addPrioritizedSteps([
  {
    name: 'PostgreSQL',
    onRun:  () => client.connect(),
  },
  {
    name: 'MongoDB',
    onRun: () => connectMongo(),
  },
])

Also you can add steps from inside single library files to support dynamic import:

// lib/media.ts
import serviceStartup from 'service-startup'

serviceStartup.addStep({
  name: 'Media Service',
  envBlackList: ['test'],
  onRun: async () => {
    mediaApi.setCredentials(config.media)
    await mediaApi.me.info()
  },
})

You can use it to wait for services start at server.ts:

import serviceStartup from 'service-startup'

serviceStartup.start().then(() => {
  server.listen(3000, () => {
    console.log(`Listening port: 3000`)
  })
})

Api

Step interface:

interface StarterStep {
  name: string
  onRun: Function
  isRequired?: boolean
  envWhiteList?: string | string[]
  envBlackList?: string | string[]
}

Config interface:

interface StarterConfig {
  successSymbol?: string
  errorSymbol?: string
  env?: string
}
1.3.0

5 months ago

1.2.0

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

0.1.0

3 years ago

0.1.1

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.0.1-rc.1

3 years ago