|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
| 18 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
18 | 19 | return new (P || (P = Promise))(function (resolve, reject) {
|
19 | 20 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
20 | 21 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
21 |
| - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 22 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
22 | 23 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
23 | 24 | });
|
24 | 25 | };
|
25 | 26 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 |
| -const admin = require("firebase-admin"); |
27 | 27 | const functions = require("firebase-functions");
|
28 | 28 | const bitly_1 = require("bitly");
|
| 29 | +const abstract_shortener_1 = require("./abstract-shortener"); |
29 | 30 | const config_1 = require("./config");
|
30 | 31 | const logs = require("./logs");
|
31 |
| -var ChangeType; |
32 |
| -(function (ChangeType) { |
33 |
| - ChangeType[ChangeType["CREATE"] = 0] = "CREATE"; |
34 |
| - ChangeType[ChangeType["DELETE"] = 1] = "DELETE"; |
35 |
| - ChangeType[ChangeType["UPDATE"] = 2] = "UPDATE"; |
36 |
| -})(ChangeType || (ChangeType = {})); |
37 |
| -const bitly = new bitly_1.BitlyClient(config_1.default.bitlyAccessToken); |
38 |
| -// Initialize the Firebase Admin SDK |
39 |
| -admin.initializeApp(); |
40 |
| -logs.init(); |
41 |
| -exports.fsurlshortener = functions.handler.firestore.document.onWrite((change) => __awaiter(this, void 0, void 0, function* () { |
42 |
| - logs.start(); |
43 |
| - if (config_1.default.urlFieldName === config_1.default.shortUrlFieldName) { |
44 |
| - logs.fieldNamesNotDifferent(); |
45 |
| - return; |
46 |
| - } |
47 |
| - const changeType = getChangeType(change); |
48 |
| - switch (changeType) { |
49 |
| - case ChangeType.CREATE: |
50 |
| - yield handleCreateDocument(change.after); |
51 |
| - break; |
52 |
| - case ChangeType.DELETE: |
53 |
| - handleDeleteDocument(); |
54 |
| - break; |
55 |
| - case ChangeType.UPDATE: |
56 |
| - yield handleUpdateDocument(change.before, change.after); |
57 |
| - break; |
58 |
| - default: { |
59 |
| - throw new Error(`Invalid change type: ${changeType}`); |
60 |
| - } |
61 |
| - } |
62 |
| - logs.complete(); |
| 32 | +class FirestoreBitlyUrlShortener extends abstract_shortener_1.FirestoreUrlShortener { |
| 33 | + constructor(urlFieldName, shortUrlFieldName, bitlyAccessToken) { |
| 34 | + super(urlFieldName, shortUrlFieldName); |
| 35 | + this.bitly = new bitly_1.BitlyClient(bitlyAccessToken); |
| 36 | + logs.init(); |
| 37 | + } |
| 38 | + shortenUrl(snapshot) { |
| 39 | + return __awaiter(this, void 0, void 0, function* () { |
| 40 | + const url = this.extractUrl(snapshot); |
| 41 | + logs.shortenUrl(url); |
| 42 | + try { |
| 43 | + const response = yield this.bitly.shorten(url); |
| 44 | + const { url: shortUrl } = response; |
| 45 | + logs.shortenUrlComplete(shortUrl); |
| 46 | + yield this.updateShortUrl(snapshot, shortUrl); |
| 47 | + } |
| 48 | + catch (err) { |
| 49 | + logs.error(err); |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | +} |
| 54 | +const urlShortener = new FirestoreBitlyUrlShortener(config_1.default.urlFieldName, config_1.default.shortUrlFieldName, config_1.default.bitlyAccessToken); |
| 55 | +exports.fsurlshortener = functions.handler.firestore.document.onWrite((change) => __awaiter(void 0, void 0, void 0, function* () { |
| 56 | + return urlShortener.onDocumentWrite(change); |
63 | 57 | }));
|
64 |
| -const extractUrl = (snapshot) => { |
65 |
| - return snapshot.get(config_1.default.urlFieldName); |
66 |
| -}; |
67 |
| -const getChangeType = (change) => { |
68 |
| - if (!change.after.exists) { |
69 |
| - return ChangeType.DELETE; |
70 |
| - } |
71 |
| - if (!change.before.exists) { |
72 |
| - return ChangeType.CREATE; |
73 |
| - } |
74 |
| - return ChangeType.UPDATE; |
75 |
| -}; |
76 |
| -const handleCreateDocument = (snapshot) => __awaiter(this, void 0, void 0, function* () { |
77 |
| - const url = extractUrl(snapshot); |
78 |
| - if (url) { |
79 |
| - logs.documentCreatedWithUrl(); |
80 |
| - yield shortenUrl(snapshot); |
81 |
| - } |
82 |
| - else { |
83 |
| - logs.documentCreatedNoUrl(); |
84 |
| - } |
85 |
| -}); |
86 |
| -const handleDeleteDocument = () => { |
87 |
| - logs.documentDeleted(); |
88 |
| -}; |
89 |
| -const handleUpdateDocument = (before, after) => __awaiter(this, void 0, void 0, function* () { |
90 |
| - const urlAfter = extractUrl(after); |
91 |
| - const urlBefore = extractUrl(before); |
92 |
| - if (urlAfter === urlBefore) { |
93 |
| - logs.documentUpdatedUnchangedUrl(); |
94 |
| - } |
95 |
| - else if (urlAfter) { |
96 |
| - logs.documentUpdatedChangedUrl(); |
97 |
| - yield shortenUrl(after); |
98 |
| - } |
99 |
| - else if (urlBefore) { |
100 |
| - logs.documentUpdatedDeletedUrl(); |
101 |
| - yield updateShortUrl(after, admin.firestore.FieldValue.delete()); |
102 |
| - } |
103 |
| - else { |
104 |
| - logs.documentUpdatedNoUrl(); |
105 |
| - } |
106 |
| -}); |
107 |
| -const shortenUrl = (snapshot) => __awaiter(this, void 0, void 0, function* () { |
108 |
| - const url = extractUrl(snapshot); |
109 |
| - logs.shortenUrl(url); |
110 |
| - try { |
111 |
| - const response = yield bitly.shorten(url); |
112 |
| - const { url: shortUrl } = response; |
113 |
| - logs.shortenUrlComplete(shortUrl); |
114 |
| - yield updateShortUrl(snapshot, shortUrl); |
115 |
| - } |
116 |
| - catch (err) { |
117 |
| - logs.error(err); |
118 |
| - } |
119 |
| -}); |
120 |
| -const updateShortUrl = (snapshot, url) => __awaiter(this, void 0, void 0, function* () { |
121 |
| - logs.updateDocument(snapshot.ref.path); |
122 |
| - // Wrapping in transaction to allow for automatic retries (#48) |
123 |
| - yield admin.firestore().runTransaction((transaction => { |
124 |
| - transaction.update(snapshot.ref, config_1.default.shortUrlFieldName, url); |
125 |
| - return Promise.resolve(); |
126 |
| - })); |
127 |
| - logs.updateDocumentComplete(snapshot.ref.path); |
128 |
| -}); |
0 commit comments