functions.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * @license
  5. * Copyright 2021 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.TaskQueue = exports.Functions = void 0;
  21. const functions_api_client_internal_1 = require("./functions-api-client-internal");
  22. const validator = require("../utils/validator");
  23. /**
  24. * The Firebase `Functions` service interface.
  25. */
  26. class Functions {
  27. /**
  28. * @param app - The app for this `Functions` service.
  29. * @constructor
  30. * @internal
  31. */
  32. constructor(app) {
  33. this.app = app;
  34. this.client = new functions_api_client_internal_1.FunctionsApiClient(app);
  35. }
  36. /**
  37. * Creates a reference to a {@link TaskQueue} for a given function name.
  38. * The function name can be either:
  39. *
  40. * 1) A fully qualified function resource name:
  41. * `projects/{project}/locations/{location}/functions/{functionName}`
  42. *
  43. * 2) A partial resource name with location and function name, in which case
  44. * the runtime project ID is used:
  45. * `locations/{location}/functions/{functionName}`
  46. *
  47. * 3) A partial function name, in which case the runtime project ID and the default location,
  48. * `us-central1`, is used:
  49. * `{functionName}`
  50. *
  51. * @param functionName - The name of the function.
  52. * @param extensionId - Optional Firebase extension ID.
  53. * @returns A promise that fulfills with a `TaskQueue`.
  54. */
  55. taskQueue(functionName, extensionId) {
  56. return new TaskQueue(functionName, this.client, extensionId);
  57. }
  58. }
  59. exports.Functions = Functions;
  60. /**
  61. * The `TaskQueue` interface.
  62. */
  63. class TaskQueue {
  64. /**
  65. * @param functionName - The name of the function.
  66. * @param client - The `FunctionsApiClient` instance.
  67. * @param extensionId - Optional canonical ID of the extension.
  68. * @constructor
  69. * @internal
  70. */
  71. constructor(functionName, client, extensionId) {
  72. this.functionName = functionName;
  73. this.client = client;
  74. this.extensionId = extensionId;
  75. if (!validator.isNonEmptyString(functionName)) {
  76. throw new functions_api_client_internal_1.FirebaseFunctionsError('invalid-argument', '`functionName` must be a non-empty string.');
  77. }
  78. if (!validator.isNonNullObject(client) || !('enqueue' in client)) {
  79. throw new functions_api_client_internal_1.FirebaseFunctionsError('invalid-argument', 'Must provide a valid FunctionsApiClient instance to create a new TaskQueue.');
  80. }
  81. if (typeof extensionId !== 'undefined' && !validator.isString(extensionId)) {
  82. throw new functions_api_client_internal_1.FirebaseFunctionsError('invalid-argument', '`extensionId` must be a string.');
  83. }
  84. }
  85. /**
  86. * Creates a task and adds it to the queue. Tasks cannot be updated after creation.
  87. * This action requires `cloudtasks.tasks.create` IAM permission on the service account.
  88. *
  89. * @param data - The data payload of the task.
  90. * @param opts - Optional options when enqueuing a new task.
  91. * @returns A promise that resolves when the task has successfully been added to the queue.
  92. */
  93. enqueue(data, opts) {
  94. return this.client.enqueue(data, this.functionName, this.extensionId, opts);
  95. }
  96. /**
  97. * Deletes an enqueued task if it has not yet completed.
  98. * @param id - the ID of the task, relative to this queue.
  99. * @returns A promise that resolves when the task has been deleted.
  100. */
  101. delete(id) {
  102. return this.client.delete(id, this.functionName, this.extensionId);
  103. }
  104. }
  105. exports.TaskQueue = TaskQueue;