Skip to content

Commit

Permalink
Finalizing HTTP clients refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
etki committed Aug 11, 2017
1 parent b1aa74a commit f8b19f7
Show file tree
Hide file tree
Showing 20 changed files with 852 additions and 580 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ npm-debug.log
# Artifacts
build
tmp
test/metadata
test/report
package-lock.json

# IDE Files
.idea
.idea
76 changes: 59 additions & 17 deletions Jakefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
var Mocha = require('mocha')
var glob = require('glob')
var FileSystem = require('fs-extra')
var Path = require('path')

var root = __dirname
var artifactDirectory = __dirname + '/tmp'
var libraryDirectory = root + '/lib'
var artifactDirectory = root + '/tmp'
var minificationDirectory = artifactDirectory + '/minified'
var reportDirectory = artifactDirectory + '/report'
var allureReportDirectory = reportDirectory + '/allure'
var coverageReportDirectory = reportDirectory + '/coverage'
var metadataDirectory = artifactDirectory + '/metadata'
var allureMetadataDirectory = metadataDirectory + '/allure'
var coverageMetadataDirectory = metadataDirectory + '/coverage'
var suites = ['unit']

var suites = ['unit', 'integration']

var execute = function (command, options) {
return new Promise(function (resolve, reject) {
if (command.join) {
command = command.join(' ')
}
jake.exec(command, options || {printStdout: true}, function (error, value) {
error ? reject(error) : resolve(value);
})
})
}

var exec = function (command, options) {
if (command.join) {
command = command.join(' ')
}
var callback = function (error, value) {
error ? fail(error) : complete(value)
}
return jake.exec(command, options || {printStdout: true}, callback)
return execute(command, options).then(complete, fail)
}

var chain = function (tasks, ignoreErrors) {
Expand Down Expand Up @@ -99,22 +109,28 @@ namespace('test', function () {
})
})

task('clean', {async: true}, function () {
FileSystem.emptyDir(metadataDirectory).then(complete, fail)
})

task('report', {async: true}, function () {
chain(['test:report:allure', 'test:report:coverage'])
})

task('coverage', {async: true}, function () {
chain(suites.map(function (suite) {
var tasks = suites.map(function (suite) {
return jake.Task['test:' + suite + ':coverage']
}))
})
tasks.unshift(jake.Task['test:clean'])
chain(tasks)
})

task('with-report', {async: true}, function () {
chain(['test:coverage', 'test:report'], true)
})

suites.forEach(function (suite) {
task(suite, function (regexp) {
task(suite, {async: true}, function (regexp) {
var options = {
reporter: 'mocha-multi-reporters',
reporterOptions: {
Expand Down Expand Up @@ -142,9 +158,7 @@ namespace('test', function () {
mocha.addFile(files[i])
}
mocha.run(function (failures) {
process.on('exit', function () {
process.exit(failures > 0 ? 1 : 0)
})
failures === 0 ? complete(0) : fail(failures)
})
})
})
Expand All @@ -169,11 +183,39 @@ namespace('test', function () {
})

task('test', {async: true}, function () {
chain(suites.map(function (suite) {
var tasks = suites.map(function (suite) {
return jake.Task['test:' + suite]
}))
})
tasks.unshift(jake.Task['test:clean'])
chain(tasks)
})

task('lint', {async: true}, function () {
exec('node_modules/.bin/standard')
})

task('minify', {async: true}, function () {
glob(libraryDirectory + '/**/*.js', function (error, files) {
if (error) {
return fail(error);
}
Promise.all(files.map(function (file) {
var relative = file.substr(libraryDirectory.length + 1)
var target = minificationDirectory + '/' + relative
var parentDirectory = Path.resolve(target, '..')
return FileSystem
.mkdirs(parentDirectory)
.then(function () {
var command = [
root + '/node_modules/.bin/uglifyjs',
file,
'-m',
'-c',
'-o',
target
]
return execute(command)
})
}))
})
})
45 changes: 29 additions & 16 deletions lib/http/_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ function normalize (bag) {
})
return bag
}

/** @deprecated */
exports.Params = {
normalize: normalize
}

exports.Query = {
/**
* Encodes provided query to a url-safe string
*
* @param {Query} query
* @returns {string}
*/
encode: function (query) {
query = normalize(query || {})
return Object.keys(query).reduce(function (carrier, key) {
Expand All @@ -177,6 +185,12 @@ exports.Query = {
}

exports.Headers = {
/**
* Encodes headers object to VoxEngine / protocol presentation
*
* @param {Headers} headers
* @returns {string[]}
*/
encode: function (headers) {
headers = normalize(headers || {})
return Object.keys(headers).reduce(function (_, k) {
Expand All @@ -188,7 +202,6 @@ exports.Headers = {
/**
* Headers in VoxImplant come in key-value pairs:
*
* ,{"key":"Link","value":"<https://voximplant.com/wp-json/>; rel=\"https://api.w.org/\""},{"key":"Link","value":"<https://voximplant.com/>; rel=shortlink"}
* - key: Server
* value: nginx/1.9.9
* - key: Link
Expand All @@ -209,8 +222,8 @@ exports.Headers = {
* - <https://voximplant.com/>; rel="home"
* - <https://voximplant.com/docs>; rel="documentation"
*
* @param {Object} headers
* @return {Object}
* @param {Object[]} headers
* @return {Headers}
*/
decode: function (headers) {
return (headers || []).reduce(function (carrier, item) {
Expand All @@ -219,7 +232,15 @@ exports.Headers = {
return carrier
}, {})
},
override: function (a, b) {

/**
* Merges two or more headers definitions, using latter's values on
* intersecting keys.
*
* @param {...Headers} args
* @returns {Headers}
*/
override: function (args) {
return []
.filter.call(arguments, function (_) { return _ })
.reduce(function (carrier, _) {
Expand All @@ -230,20 +251,12 @@ exports.Headers = {
normalize: normalize
}

/**
* @deprecated
*/
/** @deprecated */
exports.Headers.merge = exports.Headers.override
/**
* @deprecated
*/
/** @deprecated */
exports.headers = exports.Headers
/**
* @deprecated
*/
/** @deprecated */
exports.query = exports.Query
/**
* @deprecated
*/
/** @deprecated */
exports.params = exports.Params
module.exports = exports
25 changes: 13 additions & 12 deletions lib/http/_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @property {Method} method HTTP method
* @property {Query|null} query Request query
* @property {Headers|null} headers Request headers
* @property {String} payload Serialized request payload
* @property {string} payload Serialized request payload
*/

/**
Expand All @@ -43,11 +43,12 @@
*/

/**
* Promise that will resolve with {@link HttpResponse} with any HTTP code, or reject with {@link HttpException},
* {@link NetworkException} or any of their children. Please note that {@link HttpResponse} is virtual class and can't be
* `instanceof`-d.
* Promise that will resolve with {@link HttpResponse} with any HTTP
* code, or reject with {@link HttpException}, {@link NetworkException}
* or any of their children. Please note that {@link HttpResponse} is
* virtual class and can't be `instanceof`-d.
*
* @typedef {Promise.<HttpResponse>} HttpResponsePromise
* @typedef {Promise.<(HttpResponse|Error)>} HttpResponsePromise
*/

/**
Expand Down Expand Up @@ -185,7 +186,7 @@ var Method = {
* @function IRestClient#request
*
* @param {Method|string} method
* @param {string} route
* @param {string} resource
* @param {Query|undefined} [query]
* @param {*|undefined} [payload]
* @param {Headers|undefined} [headers]
Expand All @@ -200,7 +201,7 @@ var Method = {
* @param {Query} [query]
* @param {Headers} [headers]
*
* @return {Promise.<boolean>}
* @return {Promise.<boolean|Error>}
*/

/**
Expand All @@ -210,7 +211,7 @@ var Method = {
* @param {Query} [query]
* @param {Headers} [headers]
*
* @return {Promise.<*>}
* @return {Promise.<*|Error>}
*/

/**
Expand All @@ -220,7 +221,7 @@ var Method = {
* @param {*} [payload]
* @param {Headers} [headers]
*
* @return {Promise.<*>}
* @return {Promise.<*|Error>}
*/

/**
Expand All @@ -230,7 +231,7 @@ var Method = {
* @param {*} [payload]
* @param {Headers} [headers]
*
* @return {Promise.<*>}
* @return {Promise.<*|Error>}
*/

/**
Expand All @@ -240,7 +241,7 @@ var Method = {
* @param {*} [payload]
* @param {Headers} [headers]
*
* @return {Promise.<*>}
* @return {Promise.<*|Error>}
*/

/**
Expand All @@ -250,7 +251,7 @@ var Method = {
* @param {*} [payload]
* @param {Headers} [headers]
*
* @return {Promise.<*>}
* @return {Promise.<*|Error>}
*/

/**
Expand Down
Loading

0 comments on commit f8b19f7

Please sign in to comment.