@kapitchi/bb-service
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

bb-service

Build Status

TODO

Abstract service implementation with the following spec:

  • service options: constructor first parameter is immutable plain object
  • service public methods: always async so they can be called either locally (when installed as npm package) or remotely (e.g. using seneca) without a consumer refactoring, accepts only one parameter plain object and always return a plain object.
  • private methods: should be prefixed with "_"
  • implements setLogger method - logger API: { log: Function }

Installation

npm install @kapitchi/bb-service

Usage

TODO

For ES5 compatible implementation use require('@kapitchi/bb-service/es5').

API

Classes

AbstractService

Abstract service.

Provides generic param validation methods. Uses Joi for validation. Constructor accepts options parameter with Joi schema to validate the options against.

Validator

Validation helpers

AbstractService

Abstract service.

Provides generic param validation methods. Uses Joi for validation. Constructor accepts options parameter with Joi schema to validate the options against.

Kind: global class

new AbstractService(options, optionsSchema)

Param Type
options Object
optionsSchema Joi

Example

const {AbstractService, Joi} = require('@kapitchi/bb-service');

class MyService extends AbstractService {
  constructor(myServiceOpts) {
    super(myServiceOpts, {
      param1: Joi.string(),
      param2: Joi.number().optional().default(10)
    })
  }

  ping(params) {
    params = this.params(params, {
      value: Joi.string()
    });

    //service options
    this.options.param1;
    this.options.param2 === 10;

    //method param
    params.value;

    return {
      pong: params.value
    }
  }
}

abstractService.setLogger(logger)

Set service logger

Kind: instance method of AbstractService

Param Type Description
logger Object
logger.log function Log function

abstractService.validateParamsAsync(params, schema, [strict]) ⇒ Promise

Async validates params using Joi schema provided.

Kind: instance method of AbstractService

Param Type Default Description
params Object
schema Joi
[strict] boolean true false - allows unknown parameters

abstractService.paramsAsync()

Alias of validateParamsAsync

Kind: instance method of AbstractService

abstractService.validateParams(params, schema, [strict]) ⇒ *

Validates params using Joi schema provided.

Kind: instance method of AbstractService

Param Type Default Description
params Object
schema Joi
[strict] boolean true false - allows unknown parameters

abstractService.params()

Alias of validateParams

Kind: instance method of AbstractService

Validator

Validation helpers

Kind: global class

Validator.params(params, schema, [strict]) ⇒ Object

Validates params using Joi schema provided. All parameters defined in Joi schema are required.

Kind: static method of Validator
Returns: Object - Validated params

Param Type Default Description
params Object
schema Joi
[strict] boolean true Allows unknown parameters

Validator.paramsAsync(params, schema, [strict]) ⇒ Promise

Async version of Validator#params

Kind: static method of Validator

Param Type Default Description
params Object
schema Joi
[strict] boolean true Allows unknown parameters

Validator.options(options, schema, [strict]) ⇒ Object

Validates service options

Kind: static method of Validator
Returns: Object - Validated options

Param Type Default
options Object
schema Joi
[strict] Joi true

Validator.api(api, schema, [desc]) ⇒ *

Validates object's API

Kind: static method of Validator

Param Type
api Object
schema Joi
[desc] String

Development

Run the command below to builds es5 folder and README.md.

npm run build

Tests

npm test

Contribute

Please feel free to submit an issue/PR or contact me at matus.zeman@gmail.com.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @kapitchi/bb-service

Weekly Downloads

1

Version

1.5.0

License

MIT

Unpacked Size

24.1 kB

Total Files

12

Last publish

Collaborators

  • matuszeman