twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

promise.js (1941B)


      1 var Ajv = require('ajv')
      2 var HARError = require('./error')
      3 var schemas = require('har-schema')
      4 
      5 var ajv
      6 
      7 function createAjvInstance () {
      8   var ajv = new Ajv({
      9     allErrors: true
     10   })
     11   ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'))
     12   ajv.addSchema(schemas)
     13 
     14   return ajv
     15 }
     16 
     17 function validate (name, data) {
     18   data = data || {}
     19 
     20   // validator config
     21   ajv = ajv || createAjvInstance()
     22 
     23   var validate = ajv.getSchema(name + '.json')
     24 
     25   return new Promise(function (resolve, reject) {
     26     var valid = validate(data)
     27 
     28     !valid ? reject(new HARError(validate.errors)) : resolve(data)
     29   })
     30 }
     31 
     32 exports.afterRequest = function (data) {
     33   return validate('afterRequest', data)
     34 }
     35 
     36 exports.beforeRequest = function (data) {
     37   return validate('beforeRequest', data)
     38 }
     39 
     40 exports.browser = function (data) {
     41   return validate('browser', data)
     42 }
     43 
     44 exports.cache = function (data) {
     45   return validate('cache', data)
     46 }
     47 
     48 exports.content = function (data) {
     49   return validate('content', data)
     50 }
     51 
     52 exports.cookie = function (data) {
     53   return validate('cookie', data)
     54 }
     55 
     56 exports.creator = function (data) {
     57   return validate('creator', data)
     58 }
     59 
     60 exports.entry = function (data) {
     61   return validate('entry', data)
     62 }
     63 
     64 exports.har = function (data) {
     65   return validate('har', data)
     66 }
     67 
     68 exports.header = function (data) {
     69   return validate('header', data)
     70 }
     71 
     72 exports.log = function (data) {
     73   return validate('log', data)
     74 }
     75 
     76 exports.page = function (data) {
     77   return validate('page', data)
     78 }
     79 
     80 exports.pageTimings = function (data) {
     81   return validate('pageTimings', data)
     82 }
     83 
     84 exports.postData = function (data) {
     85   return validate('postData', data)
     86 }
     87 
     88 exports.query = function (data) {
     89   return validate('query', data)
     90 }
     91 
     92 exports.request = function (data) {
     93   return validate('request', data)
     94 }
     95 
     96 exports.response = function (data) {
     97   return validate('response', data)
     98 }
     99 
    100 exports.timings = function (data) {
    101   return validate('timings', data)
    102 }