Skip to content

Commit

Permalink
Merge pull request #2162 from firebase/next
Browse files Browse the repository at this point in the history
Release firestore-shorten-urls-bitly
  • Loading branch information
cabljac authored Aug 16, 2024
2 parents 6f15c2d + 3e9b635 commit e36adba
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 123 deletions.
4 changes: 4 additions & 0 deletions firestore-shorten-urls-bitly/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 0.1.17

fixed - bump dependencies, fix vulnerabilities

## Version 0.1.16

fixed - bump dependencies, fix vulnerabilities (#2061)
Expand Down
2 changes: 1 addition & 1 deletion firestore-shorten-urls-bitly/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

name: firestore-shorten-urls-bitly
version: 0.1.16
version: 0.1.17
specVersion: v1beta

displayName: Shorten URLs in Firestore
Expand Down
134 changes: 31 additions & 103 deletions firestore-shorten-urls-bitly/functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions firestore-shorten-urls-bitly/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
"generate-readme": "firebase ext:info .. --markdown > ../README.md"
},
"dependencies": {
"axios": "^1.6.0",
"@types/express-serve-static-core": "4.17.30",
"@types/node": "^20.10.3",
"firebase-admin": "^12.1.0",
"firebase-functions": "^4.9.0",
"rimraf": "^2.6.3",
"typescript": "^4.8.4",
"@types/express-serve-static-core": "4.17.30",
"@types/node": "^20.10.3"
"typescript": "^4.8.4"
},
"private": true
}
39 changes: 24 additions & 15 deletions firestore-shorten-urls-bitly/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,26 @@

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import axios, { AxiosInstance } from "axios";

import { FirestoreUrlShortener } from "./abstract-shortener";
import config from "./config";
import * as logs from "./logs";
import * as events from "./events";

interface BitlyResponse {
link?: string;
}

class FirestoreBitlyUrlShortener extends FirestoreUrlShortener {
private instance: AxiosInstance;
private bitlyAccessToken: string;

constructor(
urlFieldName: string,
shortUrlFieldName: string,
bitlyAccessToken: string
) {
super(urlFieldName, shortUrlFieldName);
this.instance = axios.create({
headers: {
Authorization: `Bearer ${bitlyAccessToken}`,
"Content-Type": "application/json",
},
baseURL: "https://api-ssl.bitly.com/v4/",
});

this.bitlyAccessToken = bitlyAccessToken;
logs.init();
}

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

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

const { link } = response.data;
if (!response.ok) {
throw new Error(`Error shortening URL: ${response.statusText}`);
}

logs.shortenUrlComplete(link);
const data: BitlyResponse = await response.json();

await this.updateShortUrl(snapshot, link);
if (data.link) {
logs.shortenUrlComplete(data.link);
await this.updateShortUrl(snapshot, data.link);
} else {
throw new Error("Bitly response did not contain a link.");
}
} catch (err) {
logs.error(err);
}
Expand Down

0 comments on commit e36adba

Please sign in to comment.