bb-service
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 |
Promise
abstractService.validateParamsAsync(params, schema, [strict]) ⇒ 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
-
.paramsAsync(params, schema, [strict]) ⇒
Promise
-
.options(options, schema, [strict]) ⇒
Object
-
.api(api, schema, [desc]) ⇒
*
-
.params(params, schema, [strict]) ⇒
Object
Validator.params(params, schema, [strict]) ⇒ 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 |
Promise
Validator.paramsAsync(params, schema, [strict]) ⇒ Async version of Validator#params
Kind: static method of Validator
Param | Type | Default | Description |
---|---|---|---|
params | Object |
||
schema | Joi |
||
[strict] | boolean |
true |
Allows unknown parameters |
Object
Validator.options(options, schema, [strict]) ⇒ 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.