Skip to content

Commit e36adba

Browse files
authored
Merge pull request #2162 from firebase/next
Release firestore-shorten-urls-bitly
2 parents 6f15c2d + 3e9b635 commit e36adba

File tree

5 files changed

+63
-123
lines changed

5 files changed

+63
-123
lines changed

firestore-shorten-urls-bitly/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 0.1.17
2+
3+
fixed - bump dependencies, fix vulnerabilities
4+
15
## Version 0.1.16
26

37
fixed - bump dependencies, fix vulnerabilities (#2061)

firestore-shorten-urls-bitly/extension.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
name: firestore-shorten-urls-bitly
16-
version: 0.1.16
16+
version: 0.1.17
1717
specVersion: v1beta
1818

1919
displayName: Shorten URLs in Firestore

firestore-shorten-urls-bitly/functions/package-lock.json

Lines changed: 31 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

firestore-shorten-urls-bitly/functions/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
"generate-readme": "firebase ext:info .. --markdown > ../README.md"
1414
},
1515
"dependencies": {
16-
"axios": "^1.6.0",
16+
"@types/express-serve-static-core": "4.17.30",
17+
"@types/node": "^20.10.3",
1718
"firebase-admin": "^12.1.0",
1819
"firebase-functions": "^4.9.0",
1920
"rimraf": "^2.6.3",
20-
"typescript": "^4.8.4",
21-
"@types/express-serve-static-core": "4.17.30",
22-
"@types/node": "^20.10.3"
21+
"typescript": "^4.8.4"
2322
},
2423
"private": true
2524
}

firestore-shorten-urls-bitly/functions/src/index.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,26 @@
1616

1717
import * as admin from "firebase-admin";
1818
import * as functions from "firebase-functions";
19-
import axios, { AxiosInstance } from "axios";
2019

2120
import { FirestoreUrlShortener } from "./abstract-shortener";
2221
import config from "./config";
2322
import * as logs from "./logs";
2423
import * as events from "./events";
24+
25+
interface BitlyResponse {
26+
link?: string;
27+
}
28+
2529
class FirestoreBitlyUrlShortener extends FirestoreUrlShortener {
26-
private instance: AxiosInstance;
30+
private bitlyAccessToken: string;
2731

2832
constructor(
2933
urlFieldName: string,
3034
shortUrlFieldName: string,
3135
bitlyAccessToken: string
3236
) {
3337
super(urlFieldName, shortUrlFieldName);
34-
this.instance = axios.create({
35-
headers: {
36-
Authorization: `Bearer ${bitlyAccessToken}`,
37-
"Content-Type": "application/json",
38-
},
39-
baseURL: "https://api-ssl.bitly.com/v4/",
40-
});
41-
38+
this.bitlyAccessToken = bitlyAccessToken;
4239
logs.init();
4340
}
4441

@@ -49,15 +46,27 @@ class FirestoreBitlyUrlShortener extends FirestoreUrlShortener {
4946
logs.shortenUrl(url);
5047

5148
try {
52-
const response: any = await this.instance.post("bitlinks", {
53-
long_url: url,
49+
const response = await fetch("https://api-ssl.bitly.com/v4/bitlinks", {
50+
method: "POST",
51+
headers: {
52+
Authorization: `Bearer ${this.bitlyAccessToken}`,
53+
"Content-Type": "application/json",
54+
},
55+
body: JSON.stringify({ long_url: url }),
5456
});
5557

56-
const { link } = response.data;
58+
if (!response.ok) {
59+
throw new Error(`Error shortening URL: ${response.statusText}`);
60+
}
5761

58-
logs.shortenUrlComplete(link);
62+
const data: BitlyResponse = await response.json();
5963

60-
await this.updateShortUrl(snapshot, link);
64+
if (data.link) {
65+
logs.shortenUrlComplete(data.link);
66+
await this.updateShortUrl(snapshot, data.link);
67+
} else {
68+
throw new Error("Bitly response did not contain a link.");
69+
}
6170
} catch (err) {
6271
logs.error(err);
6372
}

0 commit comments

Comments
 (0)