promise.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var Ajv = require('ajv')
  2. var HARError = require('./error')
  3. var schemas = require('har-schema')
  4. var ajv
  5. function validate (name, data) {
  6. data = data || {}
  7. // validator config
  8. ajv = ajv || new Ajv({
  9. allErrors: true,
  10. schemas: schemas
  11. })
  12. var validate = ajv.getSchema(name + '.json')
  13. return new Promise(function (resolve, reject) {
  14. var valid = validate(data)
  15. !valid ? reject(new HARError(validate.errors)) : resolve(data)
  16. })
  17. }
  18. exports.afterRequest = function (data) {
  19. return validate('afterRequest', data)
  20. }
  21. exports.beforeRequest = function (data) {
  22. return validate('beforeRequest', data)
  23. }
  24. exports.browser = function (data) {
  25. return validate('browser', data)
  26. }
  27. exports.cache = function (data) {
  28. return validate('cache', data)
  29. }
  30. exports.content = function (data) {
  31. return validate('content', data)
  32. }
  33. exports.cookie = function (data) {
  34. return validate('cookie', data)
  35. }
  36. exports.creator = function (data) {
  37. return validate('creator', data)
  38. }
  39. exports.entry = function (data) {
  40. return validate('entry', data)
  41. }
  42. exports.har = function (data) {
  43. return validate('har', data)
  44. }
  45. exports.header = function (data) {
  46. return validate('header', data)
  47. }
  48. exports.log = function (data) {
  49. return validate('log', data)
  50. }
  51. exports.page = function (data) {
  52. return validate('page', data)
  53. }
  54. exports.pageTimings = function (data) {
  55. return validate('pageTimings', data)
  56. }
  57. exports.postData = function (data) {
  58. return validate('postData', data)
  59. }
  60. exports.query = function (data) {
  61. return validate('query', data)
  62. }
  63. exports.request = function (data) {
  64. return validate('request', data)
  65. }
  66. exports.response = function (data) {
  67. return validate('response', data)
  68. }
  69. exports.timings = function (data) {
  70. return validate('timings', data)
  71. }