hey, I was wondering maybe this could be a feature...
# general
g
hey, I was wondering maybe this could be a feature for the API constructor. What if API constructor could have defaultRoutesFolder which would just point to a folder and generate the routes from the path+filenames. So
Copy code
new Api(this, "Api", {
    defaultRoutesFolder: { folder: "src/", defaultHandler: "main" },

    routes: { 
        //override custom functions
        "GET /notes/{id}/count": {
	        function: 'src/notes/{id}/count.get.handler',
		    authorizationType: sst.ApiAuthorizationType.NONE,
	    },
    }
}
which then generates the paths from the folder which contains:
Copy code
src/notes/default.get.js
src/notes/create.post.js
src/notes/{id}/default.get.js
src/notes/{id}/note.put.js
src/notes/{id}/note.delete.js
Which then would be the same as
Copy code
routes : {
    "GET /notes" : "src/notes/default.get.main",
    "POST /notes/create" : "src/notes/create.post.main",
    "GET /notes/{id}" : "src/notes/{id}/default.get.main",
    "PUT /notes/{id}/note" : "src/notes/{id}/note.put.main",
    "DELETE /notes/{id}/note" : "src/notes/{id}/note.delete.main"
}
or something similar maybe?
t
We are interested in file based definitions. You can actually implement this yourself and pass it into the routes object
If you can implement something that works for you we'd love to see it
g
But I guess the current function won't allow me to do filename.get.js as atleast in node handler it's hardcoded to search for base + ".js" https://github.com/serverless-stack/serverless-stack/blob/master/packages/core/src/runtime/handler/node.ts#L53
t
You can iterate over the files in the directory and build the routes object automatically instead of hand writing each route
g
yeah I understand. but currently if I have a file src/invite.post.js and definition:
Copy code
routes: {
    "POST /invite": "src/invite.post.handler"
}
it will throw an error on yarn sst deploy saying :
Copy code
Error: Cannot find a handler file for "invite.post.handler"

at NodeHandler (/node_modules/@serverless-stack/core/dist/runtime/handler/node.js:98:15)
    at instructions (/node_modules/@serverless-stack/core/dist/runtime/handler/handler.js:36:12)
    at Object.bundle (/node_modules/@serverless-stack/core/dist/runtime/handler/handler.js:16:17)
    at new Function (/node_modules/@serverless-stack/resources/src/Function.ts:362:39)
    at Function.fromDefinition (/node_modules/@serverless-stack/resources/src/Function.ts:531:14)
    at Api.createFunctionIntegration (/node_modules/@serverless-stack/resources/src/Api.ts:579:23)
    at Api.addRoute (/node_modules/@serverless-stack/resources/src/Api.ts:466:26)
    at /node_modules/@serverless-stack/resources/src/Api.ts:303:12
    at Array.forEach (<anonymous>)
    at Api.addRoutes (/node_modules/@serverless-stack/resources/src/Api.ts:302:25)
t
Ah yeah right now you can't specify more than one period in the file name
Try hyphen?
g
sure
t
I ran into this the other day doing the exact same file based function thing haha