promise.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. var ajv = void 0;
  33. function validate(name) {
  34. var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  35. // validator config
  36. ajv = ajv || new _ajv2.default({
  37. allErrors: true,
  38. schemas: schemas
  39. });
  40. var validate = ajv.getSchema(name + '.json');
  41. return new Promise(function (resolve, reject) {
  42. var valid = validate(data);
  43. !valid ? reject(new _error2.default(validate.errors)) : resolve(data);
  44. });
  45. }
  46. function afterRequest(data) {
  47. return validate('afterRequest', data);
  48. }
  49. function beforeRequest(data) {
  50. return validate('beforeRequest', data);
  51. }
  52. function browser(data) {
  53. return validate('browser', data);
  54. }
  55. function cache(data) {
  56. return validate('cache', data);
  57. }
  58. function content(data) {
  59. return validate('content', data);
  60. }
  61. function cookie(data) {
  62. return validate('cookie', data);
  63. }
  64. function creator(data) {
  65. return validate('creator', data);
  66. }
  67. function entry(data) {
  68. return validate('entry', data);
  69. }
  70. function har(data) {
  71. return validate('har', data);
  72. }
  73. function header(data) {
  74. return validate('header', data);
  75. }
  76. function log(data) {
  77. return validate('log', data);
  78. }
  79. function page(data) {
  80. return validate('page', data);
  81. }
  82. function pageTimings(data) {
  83. return validate('pageTimings', data);
  84. }
  85. function postData(data) {
  86. return validate('postData', data);
  87. }
  88. function query(data) {
  89. return validate('query', data);
  90. }
  91. function request(data) {
  92. return validate('request', data);
  93. }
  94. function response(data) {
  95. return validate('response', data);
  96. }
  97. function timings(data) {
  98. return validate('timings', data);
  99. }