@quatrain/cloudwrapper-firebase 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,17 @@
1
1
  import { HttpsFunction, HttpsOptions } from 'firebase-functions/v2/https';
2
+ import { CloudFunction } from 'firebase-functions/v2';
2
3
  import { AbstractCloudWrapper, BackendAction } from '@quatrain/core';
3
4
  export type FirebaseParams = {
4
- region?: string;
5
+ region?: string[];
5
6
  useEmulator?: boolean;
7
+ projectId?: string;
8
+ serviceAccount?: string;
6
9
  };
7
10
  export declare class FirebaseCloudWrapper extends AbstractCloudWrapper {
8
11
  protected _isInitialized: boolean;
9
12
  constructor(params: FirebaseParams);
10
- httpsWrapper(func: any, params?: HttpsOptions): HttpsFunction;
11
- databaseWrapper(func: any, eventType: BackendAction, rule?: string): import("firebase-functions/v2").CloudFunction<import("firebase-functions/v2/firestore").FirestoreEvent<import("firebase-functions/v2/firestore").QueryDocumentSnapshot | undefined, Record<string, string>>> | import("firebase-functions/v2").CloudFunction<import("firebase-functions/v2/firestore").FirestoreEvent<import("firebase-functions/v2").Change<import("firebase-functions/v2/firestore").QueryDocumentSnapshot> | undefined, Record<string, string>>>;
13
+ httpsTrigger(func: any, params?: HttpsOptions): HttpsFunction;
14
+ databaseTrigger(func: any, eventType: BackendAction, rule?: string): CloudFunction<any>;
15
+ storageTrigger(func: any, eventType: BackendAction): CloudFunction<import("firebase-functions/v2/storage").StorageEvent>;
12
16
  protected _initialize(): void;
13
17
  }
@@ -1,10 +1,21 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.FirebaseCloudWrapper = void 0;
4
13
  const app_1 = require("firebase-admin/app");
14
+ const firebase_admin_1 = require("firebase-admin");
5
15
  const https_1 = require("firebase-functions/v2/https");
6
16
  const firestore_1 = require("firebase-functions/v2/firestore");
7
17
  const v2_1 = require("firebase-functions/v2");
18
+ const storage_1 = require("firebase-functions/v2/storage");
8
19
  const core_1 = require("@quatrain/core");
9
20
  class FirebaseCloudWrapper extends core_1.AbstractCloudWrapper {
10
21
  constructor(params) {
@@ -12,14 +23,14 @@ class FirebaseCloudWrapper extends core_1.AbstractCloudWrapper {
12
23
  this._isInitialized = false;
13
24
  const { useEmulator, region } = params;
14
25
  if (useEmulator === false && region) {
15
- (0, v2_1.setGlobalOptions)({ region });
26
+ (0, v2_1.setGlobalOptions)({ region: region.join(',') });
16
27
  }
17
28
  }
18
- httpsWrapper(func, params = { memory: '4GiB', timeoutSeconds: 500 }) {
29
+ httpsTrigger(func, params = { memory: '4GiB', timeoutSeconds: 500 }) {
19
30
  this._initialize();
20
31
  return (0, https_1.onRequest)(Object.assign({ concurrency: 500 }, params), func);
21
32
  }
22
- databaseWrapper(func, eventType, rule = 'videos/{vid}/vectos/{uid}') {
33
+ databaseTrigger(func, eventType, rule = '') {
23
34
  this._initialize();
24
35
  switch (eventType) {
25
36
  case core_1.BackendAction.CREATE:
@@ -34,23 +45,26 @@ class FirebaseCloudWrapper extends core_1.AbstractCloudWrapper {
34
45
  throw new Error(`Unknown event type '${eventType}'`);
35
46
  }
36
47
  }
37
- // getConfig(key?:string) {
38
- // const config = functions.config()
39
- // return key ? config[key] : config
40
- // }
48
+ storageTrigger(func, eventType) {
49
+ this._initialize();
50
+ switch (eventType) {
51
+ case core_1.BackendAction.CREATE:
52
+ return (0, storage_1.onObjectFinalized)((object) => __awaiter(this, void 0, void 0, function* () { return yield func(object); }));
53
+ default:
54
+ throw new Error(`Unknown event type '${eventType}'`);
55
+ }
56
+ }
41
57
  _initialize() {
42
58
  if (this._isInitialized === false) {
43
- if (this._params.useEmulator === true) {
44
- // if emulator is active, we need a service account to access storage functions
45
- // @see bin/setServiceAccountPath.sh
46
- (0, app_1.initializeApp)({
47
- databaseURL: 'https://totalymage-staging.firebaseio.com',
48
- storageBucket: 'totalymage-staging.appspot.com',
49
- });
50
- }
51
- else {
52
- (0, app_1.initializeApp)();
53
- }
59
+ const { projectId, serviceAccount } = this._params;
60
+ core_1.Core.log(`[FCW] Firebase App init for project ${projectId}`);
61
+ //if (this._params.useEmulator === true) {
62
+ // if emulator is active, we need a service account to access storage functions
63
+ // @see bin/setServiceAccountPath.sh
64
+ const config = Object.assign(Object.assign({}, (serviceAccount && {
65
+ credential: firebase_admin_1.credential.cert(require(serviceAccount)),
66
+ })), (projectId && { projectId }));
67
+ (0, app_1.initializeApp)(config);
54
68
  this._isInitialized = true;
55
69
  }
56
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quatrain/cloudwrapper-firebase",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "license": "MIT",
5
5
  "description": "Cloud Wrapper adapter for Firebase",
6
6
  "main": "lib/index.js",