Okay... has anyone solved for this scenario yet? I...
# help
a
Okay... has anyone solved for this scenario yet? I have my domain model defined in one (or more) OpenAPI spec files with just schema. I have SST creating my Lambdas. I want to generate a new complete OpenAPI spec for each API Gateway, pulling in any referenced domain schema, with the enclosed paths to actual API Gateway endpoints.
r
I know we have already talked about this but that could be a very valuable tool to build 😉. OpenAPI to SST. I ended up going with sidecar yml files for each handler. so
user.ts
next to
user.yml
as well as common
.yml
files for schemas. Then a watcher on yml files that runs a script to generate TS types, validates and docs.
MyProjectOpenAPISchema.ts|yml|json
Not exactly controller generation and stubs, but the handler uses the
UserRequestBody
and
UserResponse200Body
types that were generated from my schema. At least gives me a single source to start with.
a
I'm generating code and type guards now from my domain model schema. It's tying that into the APIs that I'm trying to figure out now.
FYI: Here's what I ended up with as far as the domain schema
Copy code
#!/bin/sh
# After changing the OpenAPI schemas, run this to generate the types, interfaces, and type guards.

# The swagger-codegen generates the types and interfaces - the angular version of typescript
# language generates the cleanest models and the rest of Angular specific code is thrown out.
# Install it locally: <https://github.com/swagger-api/swagger-codegen>
echo "Generating Typescript from OpenAPI"
rm dist/swagger/models/*.ts 2>/dev/null
echo '{"sortParamsByRequiredFlag":false, "modelPropertyNaming":"original", "supportsES6":true}' > dist/swagger-codegen.json
swagger-codegen generate -i src/schemas.yaml -l typescript-angular -o dist/swagger -c dist/swagger-codegen.json || exit 1
rm src/generated-api/* 2>/dev/null
mv dist/swagger/model/*.ts src/generated-api/ || exit 2
mv src/generated-api/models.ts src/generated-api/index.ts

# This creates type guards to then use for input validation and safe casting.
echo "Generating type guards"
node ~/dev/kernwig/ts-auto-guard/lib/cli.js --export-all src/generated-api/index.ts || exit 4
npx ts-auto-guard --export-all src/generated-api/index.ts || exit 4

echo "Formatting code"
npx prettier -w src/generated-api/ || exit 5
f
Hey @Adam Fanello if we had a way to generate OpenAPI spec discussed here, does it work for your use case https://github.com/serverless-stack/serverless-stack/issues/810
a
I wanted the spec as the single source of truth, and a solution that can work for event driven architectures (i.e. no API Gateway). I had looked at TSOA too, but didn't care for it.
t
I've thought about this and I'm not sure if SST should be doing anything here - it doesn't seem to be the place to define the source of truth. We have a similar situation in GQL where the source of truth of the schema is defined externally and used by many tools