From 7f13dfa9a2f6bf303bff5faad0fa7ed190f6ce38 Mon Sep 17 00:00:00 2001 From: taylorromero Date: Tue, 7 Nov 2017 10:31:42 -0700 Subject: [PATCH] listeners --- factories/listeners.js | 21 +++++++++++++++++++++ https/index.js | 22 ++++++++-------------- index.js | 24 +++++++++++++----------- index.test.js | 2 +- package.json | 2 +- 5 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 factories/listeners.js diff --git a/factories/listeners.js b/factories/listeners.js new file mode 100644 index 0000000..e9bd6c3 --- /dev/null +++ b/factories/listeners.js @@ -0,0 +1,21 @@ +const glob = require('glob') +const path = require('path') +const { camelCase } = require('lodash') + +module.exports = (dir, router) => { + const matches = glob.sync(path.join(dir, '/**/*.js')) + return matches.reduce((ctx, match) => { + const m = require(match) + const eventName = camelCase( + match + .split('/events/') + .pop() + .split('.') + .shift() + ) + + ctx[eventName] = require(match) + + return ctx + }, {}) +} diff --git a/https/index.js b/https/index.js index 36585e6..1b1e404 100644 --- a/https/index.js +++ b/https/index.js @@ -2,21 +2,15 @@ const url = require('../utilities/url') const https = require('https') module.exports = class Https { - constructor({ - host, - apiKey, - skillId, - version, - allowSelfSignedCerts = false - }) { - if (!host || !apiKey || !skillId || !version) { + constructor({ host, apiKey, id, version, allowSelfSignedCerts = false }) { + if (!host || !apiKey || !id || !version) { throw new Error( - 'You gotta pass host, apiKey, skillId, and version to the Http constructor' + 'You gotta pass host, apiKey, id, and version to the Http constructor' ) } this.host = host this.apiKey = apiKey - this.skillId = skillId + this.id = id this.version = version this.allowSelfSignedCerts = allowSelfSignedCerts } @@ -24,7 +18,7 @@ module.exports = class Https { /** * GET an endpoint. * - * @param {String} url Path to the endpoint you want to hit. Do NOT include /api/${version}/skills/${skillId} + * @param {String} url Path to the endpoint you want to hit. Do NOT include /api/${version}/skills/${id} * @param {Object} query Vanilla object that is converted into a query string * @returns {Promise} */ @@ -39,7 +33,7 @@ module.exports = class Https { const request = https.request( { host: this.host, - path: url.build(path, query, this.version, this.skillId), + path: url.build(path, query, this.version, this.id), rejectUnauthorized: !this.allowSelfSignedCerts, headers }, @@ -80,7 +74,7 @@ module.exports = class Https { host: this.host, headers, rejectUnauthorized: !this.allowSelfSignedCerts, - path: url.build(path, query, this.version, this.skillId) + path: url.build(path, query, this.version, this.id) }, response => { this.handleResponse(request, response, resolve, reject) @@ -126,7 +120,7 @@ module.exports = class Https { host: this.host, headers, rejectUnauthorized: !this.allowSelfSignedCerts, - path: url.build(path, query, this.version, this.skillId) + path: url.build(path, query, this.version, this.id) }, response => { this.handleResponse(request, response, resolve, reject) diff --git a/index.js b/index.js index 1fcbffd..a46409d 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,14 @@ const context = require('./factories/context') const routes = require('./factories/routes') const wares = require('./factories/wares') +const listeners = require('./factories/listeners') const Https = require('./https') /** * Politely tell someone they didn't define an arg * @param {string} name */ -function argError(name) { +function required(name) { throw new Error( `You are missing some params! Make sure you set ${name} properly` ) @@ -15,13 +16,13 @@ function argError(name) { class Sprucebot { constructor({ - apiKey = argError('apiKey'), - skillId = argError('apiKey'), - host = argError('apiKey'), - name = argError('apiKey'), - description = argError('apiKey'), - skillUrl = argError('apiKey'), - svgIcon = argError('apiKey'), + apiKey = required('apiKey'), + id = required('id'), + host = required('host'), + name = required('name'), + description = required('description'), + skillUrl = required('skillUrl'), + svgIcon = required('svgIcon'), allowSelfSignedCerts = false }) { // Setup http(s) class with everything it needs to talk to api @@ -36,14 +37,14 @@ class Sprucebot { this.https = new Https({ host, apiKey, - skillId, + id, version: this.version, allowSelfSignedCerts }) console.log( `🌲 Sprucebot🌲 Skills Kit API ${this - .version}\n\napiKey : ${apiKey}, \nhost : ${host}, \nskillId : ${skillId} \nname : ${name}\n---------------------------------` + .version}\n\napiKey : ${apiKey}, \nhost : ${host}, \nid : ${id} \nname : ${name}\n---------------------------------` ) // Setup skillskit helpers @@ -51,7 +52,8 @@ class Sprucebot { factories: { context, routes, - wares + wares, + listeners } } } diff --git a/index.test.js b/index.test.js index 4bdda7b..f9939ac 100644 --- a/index.test.js +++ b/index.test.js @@ -7,7 +7,7 @@ const TIMESTAMP = new Date().getTime() const SPRUCE_ID = '1975559c-e071-4198-8ab3-eccbeb00e1d0' const TAYLOR_ID = '78245981-5022-49a7-b2f2-6ac687e0f3d1' const SKILL = { - skillId: '482D8B56-5223-43BF-8E7F-011509B9968A', + id: '482D8B56-5223-43BF-8E7F-011509B9968A', apiKey: 'DD16373A-9482-4E27-A4A3-77B2664F6C82', host: 'dev-api.sprucebot.com', name: `Unit Test Skill - ${TIMESTAMP}`, diff --git a/package.json b/package.json index 4867184..6e4b073 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sprucebot-node", - "version": "0.0.1", + "version": "0.0.2", "scripts": { "lint": "eslint '**/**/*{.js}'", "test": "jest --coverage",