Skip to content

Commit

Permalink
fix(ES5): we just can't have nice things!
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Nassri committed Mar 14, 2017
1 parent 3ec971e commit be27f68
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 56 deletions.
103 changes: 77 additions & 26 deletions lib/async.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'
var Ajv = require('ajv')
var HARError = require('./error')
var schemas = require('har-schema')

const Ajv = require('ajv')
const HARError = require('./error')
const schemas = require('har-schema')

let ajv
var ajv

function validate (name, data, next) {
data = data || {}
Expand All @@ -15,9 +13,9 @@ function validate (name, data, next) {
schemas: schemas
})

let validate = ajv.getSchema(name + '.json')
var validate = ajv.getSchema(name + '.json')

let valid = validate(data)
var valid = validate(data)

// callback?
if (typeof next === 'function') {
Expand All @@ -27,21 +25,74 @@ function validate (name, data, next) {
return valid
}

exports.afterRequest = (data, next) => validate('afterRequest', data, next)
exports.beforeRequest = (data, next) => validate('beforeRequest', data, next)
exports.browser = (data, next) => validate('browser', data, next)
exports.cache = (data, next) => validate('cache', data, next)
exports.content = (data, next) => validate('content', data, next)
exports.cookie = (data, next) => validate('cookie', data, next)
exports.creator = (data, next) => validate('creator', data, next)
exports.entry = (data, next) => validate('entry', data, next)
exports.har = (data, next) => validate('har', data, next)
exports.header = (data, next) => validate('header', data, next)
exports.log = (data, next) => validate('log', data, next)
exports.page = (data, next) => validate('page', data, next)
exports.pageTimings = (data, next) => validate('pageTimings', data, next)
exports.postData = (data, next) => validate('postData', data, next)
exports.query = (data, next) => validate('query', data, next)
exports.request = (data, next) => validate('request', data, next)
exports.response = (data, next) => validate('response', data, next)
exports.timings = (data, next) => validate('timings', data, next)
exports.afterRequest = function (data, next) {
return validate('afterRequest', data, next)
}

exports.beforeRequest = function (data, next) {
return validate('beforeRequest', data, next)
}

exports.browser = function (data, next) {
return validate('browser', data, next)
}

exports.cache = function (data, next) {
return validate('cache', data, next)
}

exports.content = function (data, next) {
return validate('content', data, next)
}

exports.cookie = function (data, next) {
return validate('cookie', data, next)
}

exports.creator = function (data, next) {
return validate('creator', data, next)
}

exports.entry = function (data, next) {
return validate('entry', data, next)
}

exports.har = function (data, next) {
return validate('har', data, next)
}

exports.header = function (data, next) {
return validate('header', data, next)
}

exports.log = function (data, next) {
return validate('log', data, next)
}

exports.page = function (data, next) {
return validate('page', data, next)
}

exports.pageTimings = function (data, next) {
return validate('pageTimings', data, next)
}

exports.postData = function (data, next) {
return validate('postData', data, next)
}

exports.query = function (data, next) {
return validate('query', data, next)
}

exports.request = function (data, next) {
return validate('request', data, next)
}

exports.response = function (data, next) {
return validate('response', data, next)
}

exports.timings = function (data, next) {
return validate('timings', data, next)
}
4 changes: 1 addition & 3 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

function HARError (errors) {
let message = 'validation failed'
var message = 'validation failed'

this.name = 'HARError'
this.message = message
Expand Down
105 changes: 78 additions & 27 deletions lib/promise.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'
var Ajv = require('ajv')
var HARError = require('./error')
var schemas = require('har-schema')

const Ajv = require('ajv')
const HARError = require('./error')
const schemas = require('har-schema')

let ajv
var ajv

function validate (name, data) {
data = data || {}
Expand All @@ -15,30 +13,83 @@ function validate (name, data) {
schemas: schemas
})

let validate = ajv.getSchema(name + '.json')
var validate = ajv.getSchema(name + '.json')

return new Promise((resolve, reject) => {
let valid = validate(data)
return new Promise(function (resolve, reject) {
var valid = validate(data)

!valid ? reject(new HARError(validate.errors)) : resolve(data)
})
}

exports.afterRequest = (data) => validate('afterRequest', data)
exports.beforeRequest = (data) => validate('beforeRequest', data)
exports.browser = (data) => validate('browser', data)
exports.cache = (data) => validate('cache', data)
exports.content = (data) => validate('content', data)
exports.cookie = (data) => validate('cookie', data)
exports.creator = (data) => validate('creator', data)
exports.entry = (data) => validate('entry', data)
exports.har = (data) => validate('har', data)
exports.header = (data) => validate('header', data)
exports.log = (data) => validate('log', data)
exports.page = (data) => validate('page', data)
exports.pageTimings = (data) => validate('pageTimings', data)
exports.postData = (data) => validate('postData', data)
exports.query = (data) => validate('query', data)
exports.request = (data) => validate('request', data)
exports.response = (data) => validate('response', data)
exports.timings = (data) => validate('timings', data)
exports.afterRequest = function (data) {
return validate('afterRequest', data)
}

exports.beforeRequest = function (data) {
return validate('beforeRequest', data)
}

exports.browser = function (data) {
return validate('browser', data)
}

exports.cache = function (data) {
return validate('cache', data)
}

exports.content = function (data) {
return validate('content', data)
}

exports.cookie = function (data) {
return validate('cookie', data)
}

exports.creator = function (data) {
return validate('creator', data)
}

exports.entry = function (data) {
return validate('entry', data)
}

exports.har = function (data) {
return validate('har', data)
}

exports.header = function (data) {
return validate('header', data)
}

exports.log = function (data) {
return validate('log', data)
}

exports.page = function (data) {
return validate('page', data)
}

exports.pageTimings = function (data) {
return validate('pageTimings', data)
}

exports.postData = function (data) {
return validate('postData', data)
}

exports.query = function (data) {
return validate('query', data)
}

exports.request = function (data) {
return validate('request', data)
}

exports.response = function (data) {
return validate('response', data)
}

exports.timings = function (data) {
return validate('timings', data)
}

0 comments on commit be27f68

Please sign in to comment.