{"size": 1902, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "normankong/stock-api", "max_stars_repo_stars_event_min_datetime": "2021-06-19T08:01:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-29T12:36:55.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "normankong/stock-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "normankong/stock-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.9705882353, "max_line_length": 92, "alphanum_fraction": 0.6535226078}

const ENDPOINT = "https://financialmodelingprep.com/api/v3/quote/%SYMBOL%?apikey=%API_KEY%";
const VALID_SYMBOL = /^[A-Z|0-9]{1,5}$/;
const moment = require("moment-timezone");

// Main Handler
addEventListener('fetch', (event) => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const { pathname } = new URL(request.url)
  const uriList = pathname.split('/');

  // Get the first param as stock symbol
  let symbol = uriList[1];

  if (!VALID_SYMBOL.test(symbol)) {
    let error = { "error": `Invalid or Empty Symbol ${symbol}` };
    return new Response(JSON.stringify(error, null, ''), {
      headers: { 'content-type': 'text/json; charset=utf-8' },
    })
  }

  // Get API Key from KV Store
  const API_KEY = await NAMESPACE.get("API_KEY");

  // Replace Symobl
  let url = ENDPOINT.replace("%SYMBOL%", symbol).replace("%API_KEY%", API_KEY);

  // Fetch Stock Quote
  const response = await fetch(url, REQUEST_HEADER);

  // Gather Response
  const results = await gatherResponse(symbol, response);

  // Return to Caller
  return new Response(JSON.stringify(results, null, ''), {
    headers: { 'content-type': 'text/json; charset=utf-8' },
  })
}

async function gatherResponse(symbol, response) {
  const { headers } = response;
  const contentType = headers.get("content-type") || ""
  if (contentType.includes("application/json")) {

    // Encrich the timestamp
    let data = await response.json();
    if (data.length == 0) {
      return { "error": `Invalid Symbol ${symbol}` };
    }
    let timeStamp = new Date(data[0].timestamp * 1000);
    let timeString = moment(timeStamp).tz("Asia/Hong_Kong").format();

    data[0].timeString = timeString;
    return data;
  }
  else {
    return { "error": `Invalid Symbol ${symbol}` };
  }
}

const REQUEST_HEADER = {
  headers: {
    "content-type": "application/json;charset=UTF-8",
  },
}