Hi! When adding routes to an API, I'm thinking abo...
# help
p
Hi! When adding routes to an API, I'm thinking about two cases: 1. Is it possible to use a variable route? Instead of
'GET /root/sub'
could I do:
'GET /${myvar}/sub'
? 2. I would like to use a previously defined Function as the handler for a route, but can't seem to figure out the syntax. Is this possible?
Answering #1 myself: Yes, it's possible. Just create the object first and then pass into
addRoutes
.
Copy code
this.routes = {}
this.routes[`GET /${id}/ping`] = { authorizationType: sst.ApiAuthorizationType.NONE, function: `services/${id}/handler.ping` }
this.routes[`GET /${id}/secure-ping`] = `services/${id}/handler.securePing`
props.api.addRoutes(this, this.routes)
Bonus of doing it this way, you can now list all routes in an output section easily 😃
f
For #2
Copy code
this.routes[`GET /${id}/ping`] = { authorizationType: sst.ApiAuthorizationType.NONE, function: myFunction }
Or
Copy code
this.routes[`GET /${id}/secure-ping`] = myFunction;
Just curious what’s ur use case for using a pre-existing function?
p
My case was having a function defined in order to use it for scheduled invocation of having the same function invoked both by http and eventbridge.
Thank btw! Also, as I used
defaultFunctionProps
on my API this was a no-go:
Error: Cannot define defaultFunctionProps when a Function is passed in to the routes
f
Right. Hmm.. @Akos also ran into this. Maybe we should just ignore the
defaultFunctionProps
in this case instead of throwing the error. What do you think?
p
Or merge the
defaultFunctionProps
with whatever is defined on the function if that's possible? But even if it's not, perhaps a notice/warning that
defaultFunctionProps
will not affect routes defined with a full function? Would it be possible to merge the props?
f
Some could (ie. environment variable), but most
Function
props can’t be updated once created. So probably can’t merge if a
Function
is passed in.
p
Ah. Yeah, makes sense. Then the current solution is likely best TBH. If I had configured for example a default timeout or some default memory allocation, not having that applied might be quite unintuitive. Perhaps just stating in the error message that the reason for this behaviour is due to the fact that it's not possible to override most function properties after creation, and possibly allow an override with a flag of some sort (if that doesn't pollute the sst api).
It's also not a big inconvenience, just was a bit unexpected
f
Yeah that makes sense.
Hey @Pål Brattberg  Just a heads up, starting v0.10.8 onward, 
environment
 inside 
defaultFunctionProps
 will be merged with the ones defined at the function level.
You can now also do
app.setDefaultFunctionProps
to configure all functions in the app. Example here - https://docs.serverless-stack.com/constructs/App#specifying-default-function-props
p
Very nice! I think this makes a lot of sense, and will make adoption even easier! Good job!