Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

extensionsApi.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getExtension = exports.deleteExtension = exports.unpublishExtension = exports.publishExtensionVersion = exports.undeprecateExtensionVersion = exports.deprecateExtensionVersion = exports.registerPublisherProfile = exports.getPublisherProfile = exports.listExtensionVersions = exports.listExtensions = exports.getExtensionVersion = exports.getSource = exports.createSource = exports.updateInstanceFromRegistry = exports.updateInstance = exports.configureInstance = exports.listInstances = exports.getInstance = exports.deleteInstance = exports.createInstance = void 0;
  4. const yaml = require("js-yaml");
  5. const clc = require("colorette");
  6. const { marked } = require("marked");
  7. const apiv2_1 = require("../apiv2");
  8. const api_1 = require("../api");
  9. const error_1 = require("../error");
  10. const logger_1 = require("../logger");
  11. const operationPoller = require("../operation-poller");
  12. const refs = require("./refs");
  13. const VERSION = "v1beta";
  14. const PAGE_SIZE_MAX = 100;
  15. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.extensionsOrigin, apiVersion: VERSION });
  16. async function createInstanceHelper(projectId, instanceId, config, validateOnly = false) {
  17. const createRes = await apiClient.post(`/projects/${projectId}/instances/`, {
  18. name: `projects/${projectId}/instances/${instanceId}`,
  19. config,
  20. }, {
  21. queryParams: {
  22. validateOnly: validateOnly ? "true" : "false",
  23. },
  24. });
  25. if (validateOnly) {
  26. return createRes.body;
  27. }
  28. const pollRes = await operationPoller.pollOperation({
  29. apiOrigin: api_1.extensionsOrigin,
  30. apiVersion: VERSION,
  31. operationResourceName: createRes.body.name,
  32. masterTimeout: 600000,
  33. });
  34. return pollRes;
  35. }
  36. async function createInstance(args) {
  37. var _a, _b, _c;
  38. const config = {
  39. params: args.params,
  40. systemParams: (_a = args.systemParams) !== null && _a !== void 0 ? _a : {},
  41. allowedEventTypes: args.allowedEventTypes,
  42. eventarcChannel: args.eventarcChannel,
  43. };
  44. if (args.extensionSource && args.extensionVersionRef) {
  45. throw new error_1.FirebaseError("ExtensionSource and ExtensionVersion both provided, but only one should be.");
  46. }
  47. else if (args.extensionSource) {
  48. config.source = { name: (_b = args.extensionSource) === null || _b === void 0 ? void 0 : _b.name };
  49. }
  50. else if (args.extensionVersionRef) {
  51. const ref = refs.parse(args.extensionVersionRef);
  52. config.extensionRef = refs.toExtensionRef(ref);
  53. config.extensionVersion = (_c = ref.version) !== null && _c !== void 0 ? _c : "";
  54. }
  55. else {
  56. throw new error_1.FirebaseError("No ExtensionVersion or ExtensionSource provided but one is required.");
  57. }
  58. if (args.allowedEventTypes) {
  59. config.allowedEventTypes = args.allowedEventTypes;
  60. }
  61. if (args.eventarcChannel) {
  62. config.eventarcChannel = args.eventarcChannel;
  63. }
  64. return createInstanceHelper(args.projectId, args.instanceId, config, args.validateOnly);
  65. }
  66. exports.createInstance = createInstance;
  67. async function deleteInstance(projectId, instanceId) {
  68. const deleteRes = await apiClient.delete(`/projects/${projectId}/instances/${instanceId}`);
  69. const pollRes = await operationPoller.pollOperation({
  70. apiOrigin: api_1.extensionsOrigin,
  71. apiVersion: VERSION,
  72. operationResourceName: deleteRes.body.name,
  73. masterTimeout: 600000,
  74. });
  75. return pollRes;
  76. }
  77. exports.deleteInstance = deleteInstance;
  78. async function getInstance(projectId, instanceId) {
  79. try {
  80. const res = await apiClient.get(`/projects/${projectId}/instances/${instanceId}`);
  81. return res.body;
  82. }
  83. catch (err) {
  84. if (err.status === 404) {
  85. throw new error_1.FirebaseError(`Extension instance '${clc.bold(instanceId)}' not found in project '${clc.bold(projectId)}'.`, { status: 404 });
  86. }
  87. throw err;
  88. }
  89. }
  90. exports.getInstance = getInstance;
  91. async function listInstances(projectId) {
  92. const instances = [];
  93. const getNextPage = async (pageToken = "") => {
  94. const res = await apiClient.get(`/projects/${projectId}/instances`, {
  95. queryParams: {
  96. pageSize: PAGE_SIZE_MAX,
  97. pageToken,
  98. },
  99. });
  100. if (Array.isArray(res.body.instances)) {
  101. instances.push(...res.body.instances);
  102. }
  103. if (res.body.nextPageToken) {
  104. await getNextPage(res.body.nextPageToken);
  105. }
  106. };
  107. await getNextPage();
  108. return instances;
  109. }
  110. exports.listInstances = listInstances;
  111. async function configureInstance(args) {
  112. var _a;
  113. const reqBody = {
  114. projectId: args.projectId,
  115. instanceId: args.instanceId,
  116. updateMask: "config.params",
  117. validateOnly: (_a = args.validateOnly) !== null && _a !== void 0 ? _a : false,
  118. data: {
  119. config: {
  120. params: args.params,
  121. },
  122. },
  123. };
  124. if (args.canEmitEvents) {
  125. if (args.allowedEventTypes === undefined || args.eventarcChannel === undefined) {
  126. throw new error_1.FirebaseError(`This instance is configured to emit events, but either allowed event types or eventarc channel is undefined.`);
  127. }
  128. reqBody.data.config.allowedEventTypes = args.allowedEventTypes;
  129. reqBody.data.config.eventarcChannel = args.eventarcChannel;
  130. }
  131. reqBody.updateMask += ",config.allowed_event_types,config.eventarc_channel";
  132. if (args.systemParams) {
  133. reqBody.data.config.systemParams = args.systemParams;
  134. reqBody.updateMask += ",config.system_params";
  135. }
  136. return patchInstance(reqBody);
  137. }
  138. exports.configureInstance = configureInstance;
  139. async function updateInstance(args) {
  140. var _a;
  141. const body = {
  142. config: {
  143. source: { name: args.extensionSource.name },
  144. },
  145. };
  146. let updateMask = "config.source.name";
  147. if (args.params) {
  148. body.config.params = args.params;
  149. updateMask += ",config.params";
  150. }
  151. if (args.systemParams) {
  152. body.config.systemParams = args.systemParams;
  153. updateMask += ",config.system_params";
  154. }
  155. if (args.canEmitEvents) {
  156. if (args.allowedEventTypes === undefined || args.eventarcChannel === undefined) {
  157. throw new error_1.FirebaseError(`This instance is configured to emit events, but either allowed event types or eventarc channel is undefined.`);
  158. }
  159. body.config.allowedEventTypes = args.allowedEventTypes;
  160. body.config.eventarcChannel = args.eventarcChannel;
  161. }
  162. updateMask += ",config.allowed_event_types,config.eventarc_channel";
  163. return patchInstance({
  164. projectId: args.projectId,
  165. instanceId: args.instanceId,
  166. updateMask,
  167. validateOnly: (_a = args.validateOnly) !== null && _a !== void 0 ? _a : false,
  168. data: body,
  169. });
  170. }
  171. exports.updateInstance = updateInstance;
  172. async function updateInstanceFromRegistry(args) {
  173. var _a;
  174. const ref = refs.parse(args.extRef);
  175. const body = {
  176. config: {
  177. extensionRef: refs.toExtensionRef(ref),
  178. extensionVersion: ref.version,
  179. },
  180. };
  181. let updateMask = "config.extension_ref,config.extension_version";
  182. if (args.params) {
  183. body.config.params = args.params;
  184. updateMask += ",config.params";
  185. }
  186. if (args.systemParams) {
  187. body.config.systemParams = args.systemParams;
  188. updateMask += ",config.system_params";
  189. }
  190. if (args.canEmitEvents) {
  191. if (args.allowedEventTypes === undefined || args.eventarcChannel === undefined) {
  192. throw new error_1.FirebaseError(`This instance is configured to emit events, but either allowed event types or eventarc channel is undefined.`);
  193. }
  194. body.config.allowedEventTypes = args.allowedEventTypes;
  195. body.config.eventarcChannel = args.eventarcChannel;
  196. }
  197. updateMask += ",config.allowed_event_types,config.eventarc_channel";
  198. return patchInstance({
  199. projectId: args.projectId,
  200. instanceId: args.instanceId,
  201. updateMask,
  202. validateOnly: (_a = args.validateOnly) !== null && _a !== void 0 ? _a : false,
  203. data: body,
  204. });
  205. }
  206. exports.updateInstanceFromRegistry = updateInstanceFromRegistry;
  207. async function patchInstance(args) {
  208. const updateRes = await apiClient.patch(`/projects/${args.projectId}/instances/${args.instanceId}`, args.data, {
  209. queryParams: {
  210. updateMask: args.updateMask,
  211. validateOnly: args.validateOnly ? "true" : "false",
  212. },
  213. });
  214. if (args.validateOnly) {
  215. return updateRes;
  216. }
  217. const pollRes = await operationPoller.pollOperation({
  218. apiOrigin: api_1.extensionsOrigin,
  219. apiVersion: VERSION,
  220. operationResourceName: updateRes.body.name,
  221. masterTimeout: 600000,
  222. });
  223. return pollRes;
  224. }
  225. function populateResourceProperties(spec) {
  226. if (spec) {
  227. spec.resources.forEach((r) => {
  228. try {
  229. if (r.propertiesYaml) {
  230. r.properties = yaml.safeLoad(r.propertiesYaml);
  231. }
  232. }
  233. catch (err) {
  234. logger_1.logger.debug(`[ext] failed to parse resource properties yaml: ${err}`);
  235. }
  236. });
  237. }
  238. }
  239. async function createSource(projectId, packageUri, extensionRoot) {
  240. const createRes = await apiClient.post(`/projects/${projectId}/sources/`, {
  241. packageUri,
  242. extensionRoot,
  243. });
  244. const pollRes = await operationPoller.pollOperation({
  245. apiOrigin: api_1.extensionsOrigin,
  246. apiVersion: VERSION,
  247. operationResourceName: createRes.body.name,
  248. masterTimeout: 600000,
  249. });
  250. if (pollRes.spec) {
  251. populateResourceProperties(pollRes.spec);
  252. }
  253. return pollRes;
  254. }
  255. exports.createSource = createSource;
  256. async function getSource(sourceName) {
  257. const res = await apiClient.get(`/${sourceName}`);
  258. if (res.body.spec) {
  259. populateResourceProperties(res.body.spec);
  260. }
  261. return res.body;
  262. }
  263. exports.getSource = getSource;
  264. async function getExtensionVersion(extensionVersionRef) {
  265. const ref = refs.parse(extensionVersionRef);
  266. if (!ref.version) {
  267. throw new error_1.FirebaseError(`ExtensionVersion ref "${extensionVersionRef}" must supply a version.`);
  268. }
  269. try {
  270. const res = await apiClient.get(`/${refs.toExtensionVersionName(ref)}`);
  271. if (res.body.spec) {
  272. populateResourceProperties(res.body.spec);
  273. }
  274. return res.body;
  275. }
  276. catch (err) {
  277. if (err.status === 404) {
  278. throw refNotFoundError(ref);
  279. }
  280. else if (err instanceof error_1.FirebaseError) {
  281. throw err;
  282. }
  283. throw new error_1.FirebaseError(`Failed to query the extension version '${clc.bold(extensionVersionRef)}': ${err}`);
  284. }
  285. }
  286. exports.getExtensionVersion = getExtensionVersion;
  287. async function listExtensions(publisherId) {
  288. const extensions = [];
  289. const getNextPage = async (pageToken = "") => {
  290. const res = await apiClient.get(`/publishers/${publisherId}/extensions`, {
  291. queryParams: {
  292. pageSize: PAGE_SIZE_MAX,
  293. pageToken,
  294. },
  295. });
  296. if (Array.isArray(res.body.extensions)) {
  297. extensions.push(...res.body.extensions);
  298. }
  299. if (res.body.nextPageToken) {
  300. await getNextPage(res.body.nextPageToken);
  301. }
  302. };
  303. await getNextPage();
  304. return extensions;
  305. }
  306. exports.listExtensions = listExtensions;
  307. async function listExtensionVersions(ref, filter = "", showPrereleases = false) {
  308. const { publisherId, extensionId } = refs.parse(ref);
  309. const extensionVersions = [];
  310. const getNextPage = async (pageToken = "") => {
  311. const res = await apiClient.get(`/publishers/${publisherId}/extensions/${extensionId}/versions`, {
  312. queryParams: {
  313. filter,
  314. showPrereleases: String(showPrereleases),
  315. pageSize: PAGE_SIZE_MAX,
  316. pageToken,
  317. },
  318. });
  319. if (Array.isArray(res.body.extensionVersions)) {
  320. extensionVersions.push(...res.body.extensionVersions);
  321. }
  322. if (res.body.nextPageToken) {
  323. await getNextPage(res.body.nextPageToken);
  324. }
  325. };
  326. await getNextPage();
  327. return extensionVersions;
  328. }
  329. exports.listExtensionVersions = listExtensionVersions;
  330. async function getPublisherProfile(projectId, publisherId) {
  331. const res = await apiClient.get(`/projects/${projectId}/publisherProfile`, {
  332. queryParams: publisherId === undefined
  333. ? undefined
  334. : {
  335. publisherId,
  336. },
  337. });
  338. return res.body;
  339. }
  340. exports.getPublisherProfile = getPublisherProfile;
  341. async function registerPublisherProfile(projectId, publisherId) {
  342. const res = await apiClient.post(`/projects/${projectId}/publisherProfile:register`, {
  343. publisherId,
  344. });
  345. return res.body;
  346. }
  347. exports.registerPublisherProfile = registerPublisherProfile;
  348. async function deprecateExtensionVersion(extensionRef, deprecationMessage) {
  349. const ref = refs.parse(extensionRef);
  350. try {
  351. const res = await apiClient.post(`/${refs.toExtensionVersionName(ref)}:deprecate`, {
  352. deprecationMessage,
  353. });
  354. return res.body;
  355. }
  356. catch (err) {
  357. if (err.status === 403) {
  358. throw new error_1.FirebaseError(`You are not the owner of extension '${clc.bold(extensionRef)}' and don’t have the correct permissions to deprecate this extension version.` + err, { status: err.status });
  359. }
  360. else if (err.status === 404) {
  361. throw new error_1.FirebaseError(`Extension version ${clc.bold(extensionRef)} was not found.`);
  362. }
  363. else if (err instanceof error_1.FirebaseError) {
  364. throw err;
  365. }
  366. throw new error_1.FirebaseError(`Error occurred deprecating extension version '${extensionRef}': ${err}`, {
  367. status: err.status,
  368. });
  369. }
  370. }
  371. exports.deprecateExtensionVersion = deprecateExtensionVersion;
  372. async function undeprecateExtensionVersion(extensionRef) {
  373. const ref = refs.parse(extensionRef);
  374. try {
  375. const res = await apiClient.post(`/${refs.toExtensionVersionName(ref)}:undeprecate`);
  376. return res.body;
  377. }
  378. catch (err) {
  379. if (err.status === 403) {
  380. throw new error_1.FirebaseError(`You are not the owner of extension '${clc.bold(extensionRef)}' and don’t have the correct permissions to undeprecate this extension version.`, { status: err.status });
  381. }
  382. else if (err.status === 404) {
  383. throw new error_1.FirebaseError(`Extension version ${clc.bold(extensionRef)} was not found.`);
  384. }
  385. else if (err instanceof error_1.FirebaseError) {
  386. throw err;
  387. }
  388. throw new error_1.FirebaseError(`Error occurred undeprecating extension version '${extensionRef}': ${err}`, {
  389. status: err.status,
  390. });
  391. }
  392. }
  393. exports.undeprecateExtensionVersion = undeprecateExtensionVersion;
  394. async function publishExtensionVersion(extensionVersionRef, packageUri, extensionRoot) {
  395. const ref = refs.parse(extensionVersionRef);
  396. if (!ref.version) {
  397. throw new error_1.FirebaseError(`ExtensionVersion ref "${extensionVersionRef}" must supply a version.`);
  398. }
  399. const publishRes = await apiClient.post(`/${refs.toExtensionName(ref)}/versions:publish`, {
  400. versionId: ref.version,
  401. packageUri,
  402. extensionRoot: extensionRoot !== null && extensionRoot !== void 0 ? extensionRoot : "/",
  403. });
  404. const pollRes = await operationPoller.pollOperation({
  405. apiOrigin: api_1.extensionsOrigin,
  406. apiVersion: VERSION,
  407. operationResourceName: publishRes.body.name,
  408. masterTimeout: 600000,
  409. });
  410. return pollRes;
  411. }
  412. exports.publishExtensionVersion = publishExtensionVersion;
  413. async function unpublishExtension(extensionRef) {
  414. const ref = refs.parse(extensionRef);
  415. if (ref.version) {
  416. throw new error_1.FirebaseError(`Extension reference "${extensionRef}" must not contain a version.`);
  417. }
  418. try {
  419. await apiClient.post(`/${refs.toExtensionName(ref)}:unpublish`);
  420. }
  421. catch (err) {
  422. if (err.status === 403) {
  423. throw new error_1.FirebaseError(`You are not the owner of extension '${clc.bold(extensionRef)}' and don’t have the correct permissions to unpublish this extension.`, { status: err.status });
  424. }
  425. else if (err instanceof error_1.FirebaseError) {
  426. throw err;
  427. }
  428. throw new error_1.FirebaseError(`Error occurred unpublishing extension '${extensionRef}': ${err}`, {
  429. status: err.status,
  430. });
  431. }
  432. }
  433. exports.unpublishExtension = unpublishExtension;
  434. async function deleteExtension(extensionRef) {
  435. const ref = refs.parse(extensionRef);
  436. if (ref.version) {
  437. throw new error_1.FirebaseError(`Extension reference "${extensionRef}" must not contain a version.`);
  438. }
  439. try {
  440. await apiClient.delete(`/${refs.toExtensionName(ref)}`);
  441. }
  442. catch (err) {
  443. if (err.status === 403) {
  444. throw new error_1.FirebaseError(`You are not the owner of extension '${clc.bold(extensionRef)}' and don’t have the correct permissions to delete this extension.`, { status: err.status });
  445. }
  446. else if (err.status === 404) {
  447. throw new error_1.FirebaseError(`Extension ${clc.bold(extensionRef)} was not found.`);
  448. }
  449. else if (err instanceof error_1.FirebaseError) {
  450. throw err;
  451. }
  452. throw new error_1.FirebaseError(`Error occurred delete extension '${extensionRef}': ${err}`, {
  453. status: err.status,
  454. });
  455. }
  456. }
  457. exports.deleteExtension = deleteExtension;
  458. async function getExtension(extensionRef) {
  459. const ref = refs.parse(extensionRef);
  460. try {
  461. const res = await apiClient.get(`/${refs.toExtensionName(ref)}`);
  462. return res.body;
  463. }
  464. catch (err) {
  465. if (err.status === 404) {
  466. throw refNotFoundError(ref);
  467. }
  468. else if (err instanceof error_1.FirebaseError) {
  469. throw err;
  470. }
  471. throw new error_1.FirebaseError(`Failed to query the extension '${clc.bold(extensionRef)}': ${err}`, {
  472. status: err.status,
  473. });
  474. }
  475. }
  476. exports.getExtension = getExtension;
  477. function refNotFoundError(ref) {
  478. return new error_1.FirebaseError(`The extension reference '${clc.bold(ref.version ? refs.toExtensionVersionRef(ref) : refs.toExtensionRef(ref))}' doesn't exist. This could happen for two reasons:\n` +
  479. ` -The publisher ID '${clc.bold(ref.publisherId)}' doesn't exist or could be misspelled\n` +
  480. ` -The name of the ${ref.version ? "extension version" : "extension"} '${clc.bold(ref.version ? `${ref.extensionId}@${ref.version}` : ref.extensionId)}' doesn't exist or could be misspelled\n\n` +
  481. `Please correct the extension reference and try again. If you meant to install an extension from a local source, please provide a relative path prefixed with '${clc.bold("./")}', '${clc.bold("../")}', or '${clc.bold("~/")}'. Learn more about local extension installation at ${marked("[https://firebase.google.com/docs/extensions/alpha/install-extensions_community#install](https://firebase.google.com/docs/extensions/alpha/install-extensions_community#install).")}`, { status: 404 });
  482. }