Skip to content

Commit

Permalink
Cors support for badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Jan 23, 2025
1 parent 704f135 commit f79351e
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions functions/src/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void main() {
);

exports['badges'] = FirebaseFunctions.https.onRequest(
HttpsOptions(cors: true.toJS),
(Request request, express.Response response) {
final match = packageBadgeRegex.firstMatch(request.path);
final packageName = match?[1];
Expand All @@ -57,24 +58,25 @@ void main() {
final message = switch (badgeType) {
BadgeType.popularity || BadgeType.dependents => data,
BadgeType.rank => (data as JSNumber).toDartInt + 1
}
.toString();
};

return response.send(
badgeMaker
.makeBadge(
BadgeFormat(
label: badgeType.name,
message: message,
color: '#007ec6',
logoBase64: logoData,
links: [
'https://pubstats.dev/packages/$packageName'.toJS,
].toJS,
),
)
.toJS,
);
final badge = badgeMaker
.makeBadge(
BadgeFormat(
label: badgeType.name,
message: '$message',
color: '#007ec6',
logoBase64: logoData,
links: [
'https://pubstats.dev/packages/$packageName'.toJS,
].toJS,
),
)
.toJS;

return response
.setHeader('Content-Type', 'image/svg+xml')
.send(badge);
});
}.toJS,
);
Expand Down

0 comments on commit f79351e

Please sign in to comment.