Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Handle HTTP GET requests by routing to appropriate callback functions based on the requested route.
- *
- * @description
- * This function serves as the main entry point for handling HTTP GET requests in the WebApp. It extracts the requested
- * route from the query parameters and routes it to the corresponding callback function. If the route is not specified,
- * it defaults to a predefined route. If the requested route is not found, it generates an error message.
- * This function makes use of the path mappings set by the `path` function.
- *
- * @function
- * @param {GoogleAppsScript.Events.HttpEvent} e - The event parameter for HTTP GET requests.
- * @returns {GoogleAppsScript.HTML.HtmlOutput} The rendered HTML template output.
- */
- function doGet(e) {
- // Extract the route parameter from the query string
- const route = e.parameter.route;
- // Define routes with their corresponding callback functions
- path("home", loadHome);
- path("pageTwo", loadPageTwo);
- path("pageThree", loadPageThree);
- // Check if a route parameter is specified
- if (route) {
- // If the route exists in the Route object, call the associated callback function
- if (Route[route]) {
- return Route[route]();
- } else {
- // Handle case where the route is not found
- console.error("Page Not Found - Requested Route:", route);
- console.error("Request Details:", e);
- const errorMessage = createGenericMessage(`
- The requested page "${route}" does not exist. Please check the URL and try again.
- `);
- return errorMessage;
- }
- }
- // Default route if no route parameter is specified
- return loadHome();
- }
- /** Generic Content */
- // Define callback functions for rendering HTML templates
- const loadHome = () => render("Home.html");
- const loadPageTwo = () => render("pageTwo.html");
- const loadPageThree = () => render("pageThree.html");
- /**
- * RouteCallbacks object for associating route paths with callback functions.
- *
- * This object maintains a collection of route paths and their corresponding callback functions.
- * It serves as a fundamental mechanism to establish seamless routing within a web application.
- *
- * @typedef {Object.<string, Function>} RouteCallbacks
- */
- const Route = {};
- /**
- * Defines a route and associates it with a callback function.
- *
- * @param {string} route - The route to define.
- * @param {function} callback - The callback function to associate with the route.
- * @throws {TypeError} Throws a TypeError if the route is not a string or the callback is not a function.
- * @example
- * // Define a route "/home" and associate it with a callback function.
- * path("/home", () => {
- * // Your route handling logic here
- * });
- */
- const path = (route, callback) => {
- Route[route] = callback; // Associate the route with its callback function
- };
- /**
- * Renders an HTML template file with optional argument object.
- *
- * This function creates an HTML template from the specified file and
- * evaluates it. It can also inject dynamic data into the template if
- * an argument object is provided.
- *
- * @param {string} file - The name of the HTML file to be rendered.
- * @param {Object} argsObject - (Optional) An object containing dynamic data
- * to inject into the template.
- * @returns {GoogleAppsScript.HTML.HtmlOutput} The evaluated HTML content.
- * @throws {Error} If an error occurs during rendering.
- *
- * @example
- * // Render an HTML file without arguments
- * const htmlOutput = render('my_template.html');
- *
- * // Render an HTML file with arguments
- * const data = { name: 'John', age: 30 };
- * const htmlOutputWithData = render('my_template.html', data);
- */
- const render = (file, argsObject) => {
- try {
- const temp = HtmlService.createTemplateFromFile(file); // Create a template from the file
- if (argsObject) {
- Object.assign(temp, argsObject); // Inject dynamic data into the template
- }
- return temp.evaluate(); // Evaluate the template and return the HTML content
- } catch (error) {
- // Handle rendering errors by creating a generic error message
- return createGenericMessage(file);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement