Adam Fanello
01/17/2022, 7:48 PMRoss Gerbasi
01/17/2022, 8:05 PMuser.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.Adam Fanello
01/17/2022, 9:00 PMAdam Fanello
01/17/2022, 9:18 PM#!/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
Frank
Adam Fanello
01/18/2022, 7:01 PMthdxr
01/24/2022, 2:46 PM