transforms.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var __spreadArrays = (this && this.__spreadArrays) || function () {
  2. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  3. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  4. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  5. r[k] = a[j];
  6. return r;
  7. };
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. function applySchemaTransforms(originalSchema, transforms) {
  10. return transforms.reduce(function (schema, transform) {
  11. return transform.transformSchema ? transform.transformSchema(schema) : schema;
  12. }, originalSchema);
  13. }
  14. exports.applySchemaTransforms = applySchemaTransforms;
  15. function applyRequestTransforms(originalRequest, transforms) {
  16. return transforms.reduce(function (request, transform) {
  17. return transform.transformRequest
  18. ? transform.transformRequest(request)
  19. : request;
  20. }, originalRequest);
  21. }
  22. exports.applyRequestTransforms = applyRequestTransforms;
  23. function applyResultTransforms(originalResult, transforms) {
  24. return transforms.reduce(function (result, transform) {
  25. return transform.transformResult ? transform.transformResult(result) : result;
  26. }, originalResult);
  27. }
  28. exports.applyResultTransforms = applyResultTransforms;
  29. function composeTransforms() {
  30. var transforms = [];
  31. for (var _i = 0; _i < arguments.length; _i++) {
  32. transforms[_i] = arguments[_i];
  33. }
  34. var reverseTransforms = __spreadArrays(transforms).reverse();
  35. return {
  36. transformSchema: function (originalSchema) {
  37. return applySchemaTransforms(originalSchema, transforms);
  38. },
  39. transformRequest: function (originalRequest) {
  40. return applyRequestTransforms(originalRequest, reverseTransforms);
  41. },
  42. transformResult: function (result) {
  43. return applyResultTransforms(result, reverseTransforms);
  44. },
  45. };
  46. }
  47. exports.composeTransforms = composeTransforms;
  48. //# sourceMappingURL=transforms.js.map