No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eventarcEmulatorUtils.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.cloudEventFromProtoToJson = void 0;
  4. const error_1 = require("../error");
  5. const BUILT_IN_ATTRS = ["time", "datacontenttype", "subject"];
  6. function cloudEventFromProtoToJson(ce) {
  7. if (ce["id"] === undefined) {
  8. throw new error_1.FirebaseError("CloudEvent 'id' is required.");
  9. }
  10. if (ce["type"] === undefined) {
  11. throw new error_1.FirebaseError("CloudEvent 'type' is required.");
  12. }
  13. if (ce["specVersion"] === undefined) {
  14. throw new error_1.FirebaseError("CloudEvent 'specVersion' is required.");
  15. }
  16. if (ce["source"] === undefined) {
  17. throw new error_1.FirebaseError("CloudEvent 'source' is required.");
  18. }
  19. const out = {
  20. id: ce["id"],
  21. type: ce["type"],
  22. specversion: ce["specVersion"],
  23. source: ce["source"],
  24. subject: getOptionalAttribute(ce, "subject", "ceString"),
  25. time: getRequiredAttribute(ce, "time", "ceTimestamp"),
  26. data: getData(ce),
  27. datacontenttype: getRequiredAttribute(ce, "datacontenttype", "ceString"),
  28. };
  29. for (const attr in ce["attributes"]) {
  30. if (BUILT_IN_ATTRS.includes(attr)) {
  31. continue;
  32. }
  33. out[attr] = getRequiredAttribute(ce, attr, "ceString");
  34. }
  35. return out;
  36. }
  37. exports.cloudEventFromProtoToJson = cloudEventFromProtoToJson;
  38. function getOptionalAttribute(ce, attr, type) {
  39. return ce["attributes"][attr][type];
  40. }
  41. function getRequiredAttribute(ce, attr, type) {
  42. const val = ce["attributes"][attr][type];
  43. if (val === undefined) {
  44. throw new error_1.FirebaseError("CloudEvent must contain " + attr + " attribute");
  45. }
  46. return val;
  47. }
  48. function getData(ce) {
  49. const contentType = getRequiredAttribute(ce, "datacontenttype", "ceString");
  50. switch (contentType) {
  51. case "application/json":
  52. return JSON.parse(ce["textData"]);
  53. case "text/plain":
  54. return ce["textData"];
  55. case undefined:
  56. return undefined;
  57. default:
  58. throw new error_1.FirebaseError("Unsupported content type: " + contentType);
  59. }
  60. }