Skip to content

Commit

Permalink
Merge pull request #1 from jeromemacias/update-deps
Browse files Browse the repository at this point in the history
Update deps
  • Loading branch information
jeromemacias authored Sep 22, 2020
2 parents 0b410bf + af023d2 commit 31cfe52
Show file tree
Hide file tree
Showing 5 changed files with 5,857 additions and 4,080 deletions.
12 changes: 7 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
language: node_js
node_js:
- "8"
sudo: false
cache:
directories:
- node_modules
- "10"
- "12"
- "node"
cache: npm
branches:
only:
- master
40 changes: 10 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,16 @@

const fp = require('fastify-plugin')

const Ajv = require('ajv')
const ajv = new Ajv({
removeAdditional: true,
useDefaults: true,
coerceTypes: true
})

const optsSchema = {
type: 'object',
required: [],
properties: {
confKey: { type: 'string', default: 'config' },
asProperties: { type: 'boolean', default: false },
config: { type: 'object', default: null }
}
}
const optsSchemaValidator = ajv.compile(optsSchema)

function loadAndValidateConfig(fastify, options, next) {
const isOptionsValid = optsSchemaValidator(options)
if (!isOptionsValid) {
return next(new Error(optsSchemaValidator.errors.map(e => e.message)))
}

const confKey = options.confKey
function fastifyRobConfig(fastify, options, next) {
const { confKey, asProperties } = options

let config

if (options.config) {
config = options.config
} else {
try {
config = require('rob-config')
config = require('rob-config') // eslint-disable-line global-require
} catch (e) {
return next(e)
}
Expand All @@ -43,17 +20,20 @@ function loadAndValidateConfig(fastify, options, next) {
try {
config.validate()
} catch (e) {
e.message = '[rob-config] ' + e.message
e.message = `[rob-config] ${e.message}`

return next(e)
}

fastify.decorate(confKey, options.asProperties ? config.properties() : config)
fastify.decorate(
confKey || 'config',
asProperties ? config.properties() : config
)

next()
}

module.exports = fp(loadAndValidateConfig, {
fastify: '>=1.0.0-rc.1',
module.exports = fp(fastifyRobConfig, {
fastify: '>=2.0.0',
name: 'fastify-rob-config'
})
Loading

0 comments on commit 31cfe52

Please # to comment.