tsoa
tsoa copied to clipboard
SST Support
If tsoa provided support for SST out-of-the-box, it would acquire even more relevance in the market!
Sorting
-
I'm submitting a ...
- [ ] bug report
- [X] feature request
- [ ] support request
-
I confirm that I
- [X] used the search to make sure that a similar issue hasn't already been submit
Expected Behavior
Generate routes for the SST framework to create our serverless functions for us!
Current Behavior
Instead I have to do all things manually - which is not bad but could be seamless.
Could you provide more info which steps do you do manually?
The way I'm doing is probably not ideal. I'm just creating one api+lambda that has a wildcard to receive all connections, putting a small koa server behind it, and then using tsoa with koa.
To elaborate.
It would be good if there was an adapter for API gateway. API gateway already parses events, headers, and query params sending request like below:
{
"resource": "/",
"path": "/",
"httpMethod": "GET",
"requestContext": {
"resourcePath": "/",
"httpMethod": "GET",
"path": "/Prod/",
...
},
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate, br",
"Host": "70ixmpl4fl.execute-api.us-east-2.amazonaws.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5e66d96f-7491f09xmpl79d18acf3d050",
...
},
"multiValueHeaders": {
"accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
],
"accept-encoding": [
"gzip, deflate, br"
],
...
},
"queryStringParameters": null,
"multiValueQueryStringParameters": null,
"pathParameters": null,
"stageVariables": null,
"body": null,
"isBase64Encoded": false
}
If tsoa could generate functions that perform schema validation and type coercion for the lambda that would be great.
I can provide further feedback, so please ask away 👍
Can I take a look into this, it doesn't look too insane to proof of concept something to support apigateway event format V2 👍
Hey @simonireilly I'm not sure if we are talking about the same thing. So, there's this example for an API in SST:
https://serverless-stack.com/examples/how-to-create-a-rest-api-with-serverless.html
Today, I have to set this manually:
// Create the HTTP API
const api = new sst.Api(this, "Api", {
routes: {
"GET /notes": "src/list.main",
"GET /notes/{id}": "src/get.main",
"PUT /notes/{id}": "src/update.main",
},
});
If tsoa supported SST, we could have something like:
import generatedRoutesFromTsoa from './routes'
const api = new sst.Api(this, "Api", {
routes: generatedRoutesFromTsoa,
});
Obviously, what you said about Api Gateway already parsing things would definitely play a part so that tsoa doesn't do that twice.
I will also provide a little bit of feedback.
Thanks to @simonireilly we've got a PoC that shows us how use TSOA and SST in the same project, and how to generate a rich OpenAPI spec by merging the ApiGateway and TSOA outputs.
There are, however, a couple of things that we're still looking forward to solve.
- Right now we don't have the capability of runtime type validations in the lambda code, because there's no generated 'routes' part. It's possible for ApiGateway itself to perform these, but in many cases one might want to opt out of this and rely on in-code validation instead.
- At the moment, we have to manually duplicate and keep in sync the route definitions in the TSOA Controller and in sst.Api construct, as mentioned above by @jpmtrabbold
- If these could be generated, we must be able to somehow override default sst.Function params (so instead of a string route, an sst FunctionDefinition object is passed), e.g. by passing custom security groups or vpc's.
- At the moment, for each controller method, we also have to manually create a separate exported function in the APIGatewayProxyHandlerV2 format (which has access to raw ApiGw event with pure string body, headers, etc) and call the controller from it, manually JSON.stringifying and parsing corresponding values.
There are, however, a couple of things that we're still looking forward to solve.
- Right now we don't have the capability of runtime type validations in the lambda code, because there's no generated 'routes' part. It possible for ApiGateway itself is able to perform these, but in many cases one might want to opt out of this and rely on in-code validation instead.
- At the moment, we have to manually duplicate and keep in sync the route definitions in the TSOA Controller and in sst.Api construct, as mentioned above by @jpmtrabbold
- If these could be generated, we must be able to somehow override default sst.Function params (so instead of a string route, an sst FunctionDefinition object is passed), e.g. by passing custom security groups or vpc's.
- At the moment, for each controller method, we also have to manually create a separate exported function in the APIGatewayProxyHandlerV2 format (which has access to raw ApiGw event with pure string body, headers, etc) and call the controller from it, manually JSON.stringifying and parsing corresponding values.
:+1:
Yeah, so I think all these issues would be solved if we had an interface direct to API Gateway like:
// Monolithic lambda code
export const handler = async (event, context, callback) =>
RegisterRoutes()[event.routeKey](event, context, callback);
- event.routeKey = 'GET /v1/GetTest/ClassModel'
- This would do runtime checking
- Oh, this one is SST specific :+1:
- I am not sure about this one, cause these security groups VPC's etc, are Infra concerns
- Yeah, so integrating with APIGateway directly would be the fix for this, set some basic helpers in the template
I have not been following this discussion, but maybe you can evaluate if #1342 could help with integration between SST and tsoa?
I believe the example I created in #1342 for an AWS CDK stack could be adapted for SST pretty easily
@pclements12 I must be blind, but exactly where is this example mentioned or documented?