Skip to content

Latest commit

 

History

History
299 lines (231 loc) · 10.4 KB

api.md

File metadata and controls

299 lines (231 loc) · 10.4 KB

Classes

ConsulConfigSyncJsonConfig
JsonConfig
Parser

The class of Validator

Functions

CustomParseFunction(option)ElementParsedResult
AfterParseFunction(newValue, isFromWatch)
WatchFunction(error, key, newValue)
getConfigFromConsulSync(option)

Typedefs

ConsulOption
SchemaElement
Schema : Object.<string, SchemaElement>
ElementParsedResult

ConsulConfigSync ⇐ JsonConfig

Kind: global class
Extends: JsonConfig

new ConsulConfigSync(option)

Param Type Default Description
option Object
option.consulAddr String The consul address, in the format of ip:port.
option.pathPrefix String The prefix of consul key element.
option.schema Schema The schema object.
[option.waitTimeBeforeThrowErrorMs] Number 0 The wait time before trigger error when the value returned is not expected. The default value is 0, but it will throw the error in async way even if you set the parameter to 0.
[option.timeout4RequestConsulMs] Number 5000 The timeout milliseconds for consul request.
[option.retryLimit] Number 0 The retry times before getting data from consul failed. The default is 0, which means no limit.
[option.consulOption] ConsulOption {} The consul option used to init a consul client.

consulConfigSync.params

Get All the value.

Kind: instance property of ConsulConfigSync
Overrides: params

consulConfigSync.getValue(key) ⇒ *

Get the value via key name.

Kind: instance method of ConsulConfigSync
Overrides: getValue

Param Type
key String

JsonConfig

Kind: global class

new JsonConfig(option)

Param Type Default Description
option Object
option.configObject Object The Json object.
option.schema Schema The schema object.
[option.waitTimeBeforeThrowErrorMs] Number 0 The wait time before trigger error when the value returned is not expected. The default value is 0, but it will throw the error in async way even if you set the parameter to 0.

jsonConfig.params

Get All the value.

Kind: instance property of JsonConfig

jsonConfig.getValue(key) ⇒ *

Get the value via key name.

Kind: instance method of JsonConfig

Param Type
key String

Parser

The class of Validator

Kind: global class

new Parser(option)

Creates an instance of Validator.

Param Type
option Object
option.schema Schema

parser.doParseElement(option) ⇒ ElementParsedResult

Parse one config.

Kind: instance method of Parser

Param Type Default Description
option Object
option.key String
option.value *
[option.isFromWatch] Boolean false
[option.oldValue] * pass to when isFromWatch is true

parser.doParse(params) ⇒ Object

Parse all the config.

Kind: instance method of Parser

Param Type
params Object

CustomParseFunction(option) ⇒ ElementParsedResult

Kind: global function
Returns: ElementParsedResult - the validate result.

Param Type Description
option Object
option.key String the key
option.value * the value you want to validated
option.schemaElement SchemaElement the element of schema.

AfterParseFunction(newValue, isFromWatch)

Kind: global function

Param Type
newValue *
isFromWatch Boolean

WatchFunction(error, key, newValue)

Kind: global function

Param Type
error Error | String
key String
newValue *

getConfigFromConsulSync(option)

Kind: global function

Param Type Default Description
option Object
option.consulAddr String The consul address, in the format of ip:port.
option.pathPrefix String The prefix of consul key element.
option.savePath String The full path name of the generated config file.
option.keys Array.<String> The consul keys' name.
[option.timeoutMs] Number 5000 The timeout milliseconds for consul request.
[option.retryLimit] Number 0 The retry times before getting data from consul failed. The default is 0, which means no limit.
[option.consulOption] ConsulOption {} The consul option used to init a consul client.

ConsulOption

Kind: global typedef
Properties

Name Type Default Description
[secure] Boolean false enable HTTPS
[ca] Array.<String> array of strings or Buffers of trusted certificates in PEM format
[defaults] Object common method call options that will be included with every call (ex: set default token)

SchemaElement

Kind: global typedef
Properties

Name Type Default Description
[preferred] * | function when it not empty , it will been used instead of the value get from consul or json object. It can be an function or a value of type of String/Number/Object etc.
[required] Boolean | Array false indicate whether the current field necessary
type Number | JSON | Date | String | Object declare the current field's type, it can be Number JSON String Parser.TYPE_FILE Parser.TYPE_DIRECTORY Parser.TYPE_URL or an Object with properties. see
[custom] CustomParseFunction the custom validate function
[options] Object the extended options used to check validate.
[afterParse] AfterParseFunction the function to do after parse.
[watch] WatchFunction the function used in consul watch callback
[isWatchDisabled] Boolean false whether disable watching the key's changes, default is false.

Schema : Object.<string, SchemaElement>

Kind: global typedef
Example

{
  numberFiled : {
    required:true,
    type : Number,
    options: {
     gte: 10,
     lte: 1
    },
    afterParse: function(value) {}
  }
}

Example

{
 dateField:{
     required:[true,'the dateField can not be empty']
     type:[Date,'the dataField must be a Date']
 }
}

Example

{
  objectField: {
     type: {
         field1: {type: [Number, 'field must be a Number']},
         field2: {type: String, required: true}
     }
 }
}

ElementParsedResult

Kind: global typedef
Properties

Name Type Default Description
[error] String | Error when the value is invalid, the error is not empty.
value * when the value is valid, return the value , it may be transfromed to the type you want.
changed Boolean whether the value is changed, it may be true when the key changed from consul.