Advertisement
IAmMoonie

Framework for Webapp routing

Sep 7th, 2024
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.14 KB | Source Code | 0 0
  1. /**
  2.  * Handle HTTP GET requests by routing to appropriate callback functions based on the requested route.
  3.  *
  4.  * @description
  5.  * This function serves as the main entry point for handling HTTP GET requests in the WebApp. It extracts the requested
  6.  * route from the query parameters and routes it to the corresponding callback function. If the route is not specified,
  7.  * it defaults to a predefined route. If the requested route is not found, it generates an error message.
  8.  * This function makes use of the path mappings set by the `path` function.
  9.  *
  10.  * @function
  11.  * @param {GoogleAppsScript.Events.HttpEvent} e - The event parameter for HTTP GET requests.
  12.  * @returns {GoogleAppsScript.HTML.HtmlOutput} The rendered HTML template output.
  13.  */
  14. function doGet(e) {
  15.   // Extract the route parameter from the query string
  16.   const route = e.parameter.route;
  17.  
  18.   // Define routes with their corresponding callback functions
  19.   path("home", loadHome);
  20.   path("pageTwo", loadPageTwo);
  21.   path("pageThree", loadPageThree);
  22.  
  23.   // Check if a route parameter is specified
  24.   if (route) {
  25.     // If the route exists in the Route object, call the associated callback function
  26.     if (Route[route]) {
  27.       return Route[route]();
  28.     } else {
  29.       // Handle case where the route is not found
  30.       console.error("Page Not Found - Requested Route:", route);
  31.       console.error("Request Details:", e);
  32.       const errorMessage = createGenericMessage(`
  33.         The requested page "${route}" does not exist. Please check the URL and try again.
  34.       `);
  35.       return errorMessage;
  36.     }
  37.   }
  38.  
  39.   // Default route if no route parameter is specified
  40.   return loadHome();
  41. }
  42.  
  43. /** Generic Content */
  44. // Define callback functions for rendering HTML templates
  45. const loadHome = () => render("Home.html");
  46. const loadPageTwo = () => render("pageTwo.html");
  47. const loadPageThree = () => render("pageThree.html");
  48.  
  49. /**
  50.  * RouteCallbacks object for associating route paths with callback functions.
  51.  *
  52.  * This object maintains a collection of route paths and their corresponding callback functions.
  53.  * It serves as a fundamental mechanism to establish seamless routing within a web application.
  54.  *
  55.  * @typedef {Object.<string, Function>} RouteCallbacks
  56.  */
  57. const Route = {};
  58.  
  59. /**
  60.  * Defines a route and associates it with a callback function.
  61.  *
  62.  * @param {string} route - The route to define.
  63.  * @param {function} callback - The callback function to associate with the route.
  64.  * @throws {TypeError} Throws a TypeError if the route is not a string or the callback is not a function.
  65.  * @example
  66.  * // Define a route "/home" and associate it with a callback function.
  67.  * path("/home", () => {
  68.  *   // Your route handling logic here
  69.  * });
  70.  */
  71. const path = (route, callback) => {
  72.   Route[route] = callback; // Associate the route with its callback function
  73. };
  74.  
  75. /**
  76.  * Renders an HTML template file with optional argument object.
  77.  *
  78.  * This function creates an HTML template from the specified file and
  79.  * evaluates it. It can also inject dynamic data into the template if
  80.  * an argument object is provided.
  81.  *
  82.  * @param {string} file - The name of the HTML file to be rendered.
  83.  * @param {Object} argsObject - (Optional) An object containing dynamic data
  84.  *                             to inject into the template.
  85.  * @returns {GoogleAppsScript.HTML.HtmlOutput} The evaluated HTML content.
  86.  * @throws {Error} If an error occurs during rendering.
  87.  *
  88.  * @example
  89.  * // Render an HTML file without arguments
  90.  * const htmlOutput = render('my_template.html');
  91.  *
  92.  * // Render an HTML file with arguments
  93.  * const data = { name: 'John', age: 30 };
  94.  * const htmlOutputWithData = render('my_template.html', data);
  95.  */
  96. const render = (file, argsObject) => {
  97.   try {
  98.     const temp = HtmlService.createTemplateFromFile(file); // Create a template from the file
  99.     if (argsObject) {
  100.       Object.assign(temp, argsObject); // Inject dynamic data into the template
  101.     }
  102.     return temp.evaluate(); // Evaluate the template and return the HTML content
  103.   } catch (error) {
  104.     // Handle rendering errors by creating a generic error message
  105.     return createGenericMessage(file);
  106.   }
  107. };
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement