0

I am trying to figure out the best approach to coding my firebase cloud function.

My requirement includes 3 nested http requests. The 2nd request is made with data from the 1st request. And the 3rd request is with data from the 2nd.

I have started my project in Typescript but I have read in this answer Cloud Functions for Firebase Async Await style that typescript doesn't support async/ await (which I think I would like to use).

I could switch my project back to Javascript, or as the previous answer suggested, I could use the plugin here https://github.com/babel/kneden to transpile but I am still confused about how to include that in my typescript project (and how to write the async/await code after I do).

I found another answer here Nested HTTP requests in Firebase cloud function that is similar but uses promise chaining.

My first problem is that I cannot decide which of the 3 approaches to take (Start over with javascript, transpile, or use promises). All 3 seem to have a downside (except maybe transpiling if I can figure out how).

Here is a bit of my code using Cheerio with request-promise-native and Typescript.

Inside the .each loop there is some commented code which calls a second function to make the next request. This is the code I would like to refactor to use a better method.

import * as functions from 'firebase-functions';
import * as cheerio from 'cheerio';
import * as moment from 'moment';
import * as http from 'http';
import * as admin from 'firebase-admin';
import * as request from "request-promise-native";

var urlDate = moment().format('YYYYMMDD');



export const getPlayers = functions.https.onRequest((req, response) => {

var options = {
    uri: 'https://www.cbssports.com/nba/scoreboard/',
    transform: function (body) {
        return cheerio.load(body);
    }
};

request(options)
    .then(($) => {

$('.live-update').each((i, element) => {

        var homeTeamAbbr = $(element).find('tbody').children('tr').eq(0).find('a').html().split("alt/").pop().split('.svg')[0];
        var awayTeamAbbr = $(element).find('tbody').children('tr').eq(1).find('a').html().split("alt/").pop().split('.svg')[0];
        var homeTeam = $(element).find('tbody').children('tr').eq(0).find('a.team').text().trim();
        var awayTeam = $(element).find('tbody').children('tr').eq(1).find('a.team').text().trim();
        var homeTeamStatsURL = $(element).find('tbody').children('tr').eq(0).find('td').html();
        var awayTeamStatsURL = $(element).find('tbody').children('tr').eq(1).find('td').html();
        var gameTime = $(element).find('.pregame-date').text().trim();

        homeTeamStatsURL = homeTeamStatsURL.match(/href="([^"]*)/)[1] + "roster";
        awayTeamStatsURL = awayTeamStatsURL.match(/href="([^"]*)/)[1] + "roster";

        var matchupString = awayTeamAbbr + "@" + homeTeamAbbr;
        var URLString = "NBA_" + urlDate + "_" + matchupString;

        // var docRef = database.collection('NBASchedule').doc("UpcommingSchedule");
        // var boxScoreURL = "www.cbssports.com/nba/gametracker/boxscore/" + URLString;

        // var setAda = docRef.set({[URLString]:{
        //   homeTeam: homeTeam,
        //   awayTeam: awayTeam,
        //   date: gameTime,
        //   homeTeamAbbr: homeTeamAbbr,
        //   awayTeamAbbr: awayTeamAbbr,
        //   homeTeamStatsURL: homeTeamStatsURL,
        //   awayTeamStatsURL: awayTeamStatsURL,
        //   boxScoreURL: boxScoreURL
        // }}, { merge: true });

        // getTeamPlayers(homeTeamStatsURL, matchupString);
        // getTeamPlayers(awayTeamStatsURL, matchupString);

        console.log("retrieved schedule for "+ matchupString + " on " + urlDate)

});

 response.send("retrieved schedule");
})
.catch(function (err) {
   console.log("error " + err);
});



});

If anyone could advise me on the best solution and how to implement it would be appreciated. Thanks

Louis Sankey
  • 481
  • 8
  • 26
  • Typescript not supporting async-await? It certainly does: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-7.html However, if it would not - typescript is javascript, thus I have not encounteted frameworks, where you could not mix them (on file level at least), if you need. Practically you could write a simple class to wrap your chaining and use it on ts side if you want - but just to make it cleaner. – ZorgoZ Dec 16 '18 at 19:21
  • My mistake, it looks like it is google cloud functions that do not support async-await, unless it is transpiled. I still do not know for sure. – Louis Sankey Dec 16 '18 at 21:41

0 Answers0