Skip to content

Conversation

@Ulydev
Copy link
Contributor

@Ulydev Ulydev commented Apr 28, 2017

Hello,
Here's a sample cloud function I'm using in my project. Thought I'd share it 😃

It replaces new links with shorter URLs generated with the Google URL Shortener API.

@nicolasgarnier
Copy link
Contributor

Thanks for the sample @Ulydev , Sorry for not reviewing it right now as I'm cranking on some stuff for Google IO. I'll review as soon as I can find some time :)

Copy link
Contributor

@nicolasgarnier nicolasgarnier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good! :)
Just one nit and also can you add an entry in the main README please? :)

function createShortenerPromise(snapshot) {
const key = snapshot.key;
const originalUrl = snapshot.val();
return request(createShortenerRequest(originalUrl)).then(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of prefer it this way, i.e. make the Promise return the shortURL then use it inside another then().

return request(createShortenerRequest(originalUrl)).then(response => { if (response.statusCode === 200) { return response.body.id; } throw response.body; }).then(shortUrl => { return admin.database().ref(`/links/${key}`).set({ original: originalUrl, short: shortUrl }); });
@Ulydev
Copy link
Contributor Author

Ulydev commented May 31, 2017

There you go!

@nicolasgarnier nicolasgarnier merged commit b075ec9 into firebase:master May 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants