async.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.validate = validate;
  6. exports.afterRequest = afterRequest;
  7. exports.beforeRequest = beforeRequest;
  8. exports.browser = browser;
  9. exports.cache = cache;
  10. exports.content = content;
  11. exports.cookie = cookie;
  12. exports.creator = creator;
  13. exports.entry = entry;
  14. exports.har = har;
  15. exports.header = header;
  16. exports.log = log;
  17. exports.page = page;
  18. exports.pageTimings = pageTimings;
  19. exports.postData = postData;
  20. exports.query = query;
  21. exports.request = request;
  22. exports.response = response;
  23. exports.timings = timings;
  24. var _harSchema = require('har-schema');
  25. var schemas = _interopRequireWildcard(_harSchema);
  26. var _ajv = require('ajv');
  27. var _ajv2 = _interopRequireDefault(_ajv);
  28. var _error = require('./error');
  29. var _error2 = _interopRequireDefault(_error);
  30. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  31. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  32. let ajv;
  33. function validate(name, data = {}, next) {
  34. // validator config
  35. ajv = ajv || new _ajv2.default({
  36. allErrors: true,
  37. schemas: schemas
  38. });
  39. let validate = ajv.getSchema(name + '.json');
  40. let valid = validate(data);
  41. // callback?
  42. if (typeof next === 'function') {
  43. return next(!valid ? new _error2.default(validate.errors) : null, valid);
  44. }
  45. return valid;
  46. }
  47. function afterRequest(data, next) {
  48. return validate('afterRequest', data, next);
  49. }
  50. function beforeRequest(data, next) {
  51. return validate('beforeRequest', data, next);
  52. }
  53. function browser(data, next) {
  54. return validate('browser', data, next);
  55. }
  56. function cache(data, next) {
  57. return validate('cache', data, next);
  58. }
  59. function content(data, next) {
  60. return validate('content', data, next);
  61. }
  62. function cookie(data, next) {
  63. return validate('cookie', data, next);
  64. }
  65. function creator(data, next) {
  66. return validate('creator', data, next);
  67. }
  68. function entry(data, next) {
  69. return validate('entry', data, next);
  70. }
  71. function har(data, next) {
  72. return validate('har', data, next);
  73. }
  74. function header(data, next) {
  75. return validate('header', data, next);
  76. }
  77. function log(data, next) {
  78. return validate('log', data, next);
  79. }
  80. function page(data, next) {
  81. return validate('page', data, next);
  82. }
  83. function pageTimings(data, next) {
  84. return validate('pageTimings', data, next);
  85. }
  86. function postData(data, next) {
  87. return validate('postData', data, next);
  88. }
  89. function query(data, next) {
  90. return validate('query', data, next);
  91. }
  92. function request(data, next) {
  93. return validate('request', data, next);
  94. }
  95. function response(data, next) {
  96. return validate('response', data, next);
  97. }
  98. function timings(data, next) {
  99. return validate('timings', data, next);
  100. }