There is an undocumented feature where you can add custom paths to a web app url for routing (and you can reference that path via the 'pathInfo' property on the event object). But you'll have to pass a user access token with the request (for both GET and POST) regardless of the settings used to deploy the web app.
Thursday, January 23, 2020 Alex wrote:
You can pass it as a url variable ("access_token=[ACCESS_TOKEN_VALUE]") when making requests to the web url or in the Authorization header as a Bearer token. I'm not sure, but the last time I played with it, it seemed that only the owner of the script could access routing this way, so its probably not something you can scale to multiple users.
function doGet(e) { var parameters = e.parameters; return Router(parameters); }
function Router(parameters) { var view, id;
view = 'v' in parameters ? parameters.v[0] : null ; id = 'id' in parameters ? parameters.id[0] : null;
switch (view) { case 'orders': return loadOrders(); case 'view_order': return loadOrder(id);
default: return loadOrders(); }}
function loadOrders() {
var orderData = getUpcomingOrders(); var orders = orderData.orders;
return render("Views/Pages/Orders/index", { orders: orders });
}
function loadOrder(id) {
var orderData = getOrder(id); var order = createOrderObj(orderData);
return render("Views/Pages/View_Order/index", { order: order, });}
function render(file, argsObject){ var html = HtmlService.createTemplateFromFile(file);
if(argsObject) { var keys = Object.keys(argsObject); keys.forEach(function(key){ html[key] = argsObject[key]; }); } return html.evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);}
Do you mean having the code do something different depending upon some value being passed to it?
There is an undocumented feature where you can add custom paths to a web app url for routing
I'm not sure what Alex meant by "use a router?"
function doPost(e){
var router = new Route(e);
router.matches(); // <== the rules for checkers
return router
.on(checker, callback)
.on(checker, callback)
.resolve();
}
Hi Alex,
I've been working on a library called Boot for Google Apps Script Projects that makes this process much cleaner and more scalable.
To use this library, you'll need a modern development workflow: it's designed to work in a local development environment with clasp and a project bundler. It also has full TypeScript support.
It uses decorators to simplify REST API routing. Here's a quick example of how you can set up a controller and a GET endpoint:
Define a REST controller that will handle HTTP requests to your Apps Script web application:import {Get, RestController} from "appsscript-boot";
@RestController("api/sheet")
export class Sheet {
@Get("active-range")
getActiveRange(): string {
return "This action return active range.";
}
}
This code creates a REST controller at /api/sheet and defines a GET endpoint at /active-range. It shows how you can easily structure your web app without complex manual routing.
You can find the full library and documentation on GitHub:
https://github.com/MaksymStoianov/appsscript-boot
I hope this helps! I'd love to hear your thoughts on it.
Best regards,
Max
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/77180ee5-63e3-4c7e-9a71-08d9fdb27835n%40googlegroups.com.