async.js 1.9 KB

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