Geen omschrijving
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.

metadata.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.setRulesetLabels = exports.createRuleset = exports.getRulesetLabels = exports.getRuleset = exports.listAllRulesets = void 0;
  4. const api_1 = require("../api");
  5. const apiv2_1 = require("../apiv2");
  6. const logger_1 = require("../logger");
  7. const utils = require("../utils");
  8. function handleErrorResponse(response) {
  9. if (response.body && response.body.error) {
  10. return utils.reject(response.body.error, { code: 2 });
  11. }
  12. logger_1.logger.debug("[rules] error:", response.status, response.body);
  13. return utils.reject("Unexpected error encountered with database.", {
  14. code: 2,
  15. });
  16. }
  17. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.rtdbMetadataOrigin });
  18. async function listAllRulesets(databaseName) {
  19. const response = await apiClient.get(`/namespaces/${databaseName}/rulesets`, { resolveOnHTTPError: true });
  20. if (response.status === 200) {
  21. return response.body.rulesets;
  22. }
  23. return handleErrorResponse(response);
  24. }
  25. exports.listAllRulesets = listAllRulesets;
  26. async function getRuleset(databaseName, rulesetId) {
  27. const response = await apiClient.get(`/namespaces/${databaseName}/rulesets/${rulesetId}`, { resolveOnHTTPError: true });
  28. if (response.status === 200) {
  29. return response.body;
  30. }
  31. return handleErrorResponse(response);
  32. }
  33. exports.getRuleset = getRuleset;
  34. async function getRulesetLabels(databaseName) {
  35. const response = await apiClient.get(`/namespaces/${databaseName}/ruleset_labels`, {
  36. resolveOnHTTPError: true,
  37. });
  38. if (response.status === 200) {
  39. return response.body;
  40. }
  41. return handleErrorResponse(response);
  42. }
  43. exports.getRulesetLabels = getRulesetLabels;
  44. async function createRuleset(databaseName, source) {
  45. const localApiClient = new apiv2_1.Client({
  46. urlPrefix: utils.addSubdomain(api_1.realtimeOrigin, databaseName),
  47. });
  48. const response = await localApiClient.post(`/.settings/rulesets.json`, source, { resolveOnHTTPError: true });
  49. if (response.status === 200) {
  50. return response.body.id;
  51. }
  52. return handleErrorResponse(response);
  53. }
  54. exports.createRuleset = createRuleset;
  55. async function setRulesetLabels(databaseName, labels) {
  56. const localApiClient = new apiv2_1.Client({
  57. urlPrefix: utils.addSubdomain(api_1.realtimeOrigin, databaseName),
  58. });
  59. const response = await localApiClient.put(`/.settings/ruleset_labels.json`, labels, {
  60. resolveOnHTTPError: true,
  61. });
  62. if (response.status === 200) {
  63. return response.body;
  64. }
  65. return handleErrorResponse(response);
  66. }
  67. exports.setRulesetLabels = setRulesetLabels;