Last active
February 7, 2020 14:01
-
-
Save cmalard/26417cf044764885099e2c6f2d927009 to your computer and use it in GitHub Desktop.
Swagger - script to automate generation of client library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For JavaScript project - `npm run update:api` | |
{ | |
"scripts": { | |
"update:api": "./tools/update-api-swagger.sh" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
SWAGGER_JSON_URL='https://.../api-docs' | |
CLIENT_LIB_DIR='libs/api-swagger-generated' | |
# Get json spec | |
SPEC=`curl $SWAGGER_JSON_URL --silent` # add --insecure to make it work whith some VPNs | |
# Empty client lib dir | |
rm -rf $CLIENT_LIB_DIR | |
# Get generated client as a zip, extract & delete it | |
# See https://github.com/swagger-api/swagger-codegen/tree/3.0.0#online-generators | |
TMPZIP=`mktemp` | |
curl -X POST \ | |
https://generator3.swagger.io/api/generate \ | |
-H 'content-type: application/json' \ | |
-d '{ | |
"lang": "typescript-angular", | |
"type": "CLIENT", | |
"codegenVersion": "V3", | |
"spec" : '$SPEC' | |
}' --silent > $TMPZIP | |
unzip -q $TMPZIP -d $CLIENT_LIB_DIR | |
rm $TMPZIP | |
# Git add & commit | |
## If you use NX & Prettier, uncomment the following if: | |
# npx prettier --write $CLIENT_LIB_DIR/**/*.ts --loglevel warn | |
# if [ -z "$(git status --porcelain $CLIENT_LIB_DIR)" ]; then | |
# echo '\n🧙 No changes' | |
# exit | |
# fi | |
git add $CLIENT_LIB_DIR | |
git commit -m "refactor(api-swagger-generated): update" | |
echo "\n🧙 Commit created, restart \`ng serve\` and \`ng test\` to check if everything is ok" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on Ubuntu/Mac with zsh installed... got some issues to make it work with sh