how can I set a function base path for a bunch of ...
# help
s
how can I set a function base path for a bunch of routes in a
sst.Api
? so I don’t have to do:
Copy code
'GET /folders': 'lambda/rest/functions/get-folders.main',
  'GET /global-search': 'lambda/rest/functions/global-search.main',
  'GET /tags': 'lambda/rest/functions/get-tags.main',
  'GET /trash': 'lambda/rest/functions/get-deleted-items.main',
way back in the early days of SST, I wrote a messy helper function to handle this for me. but now I need to get rid of that as it’s.. well, a mess, and there’s probably a better way now 🙂
r
String template literal 😁
s
hmm. yeah, I mean it’s not ideal, but that’ll work of course
r
I know, was being cheeky. 😄
o
haha I read that as "have you tried...variables?"
r
I think that’s fair 😄
o
tbf variables are pretty good and sometimes the simplest answer is the best
r
Being serious for a moment, it is something I’ve thought about and anything more than specifying the base path as a string template literal just gets complex quickly and there’s just not the ROI for something that doesn’t change (at least for us) very often
s
yeah I suppose so. not a huge deal.. doing a mass search/replace then 🙂
'([\w-]+)\.main'
-> ``${restFuncBasePath}/$1.main``
k
You can define one in the
defaultFunctionProps
object of the Api construct like this
Copy code
const myApi = new sst.Api(this, "myApi", {
  defaultFunctionProps: {
    srcPath: "lambda/rest/functions/",
  },
  routes: {
    "GET /folders": "get-folders.main",
    "GET /global-search": "global-search.main",
    "GET /tags": "get-tags.main",
    "GET /trash": "get-deleted-items.main",
  },
});
f
Thanks for the feedback guys. The only reason hold us back adding a
basePath
field to the Function is we messed up the naming for
srcPath
.
srcPath
really meant “path to your package.json”, but ppl mis-use it as “base path”.
We want to rename
srcPath
to ie.
packageJsonPath
and then add
basePath
.
Let me talk to @thdxr to prioritze this.
k
Ohh indeed the
package.json
is referenced under
FunctionProps
. I saw examples of relative paths scattered in the Function docs and sort of ran with it 😅
I suppose this is also suggestive of a
basePath
Copy code
If the srcPath is set, then the path to the handler is relative to it. So if the srcPath is set to src. Then lambda.main as the handler would mean that the file is in src/lambda.js (or the other extensions).
f
Yeah, it is a bit confusing rn. Will fix!