Skip to content

Instantly share code, notes, and snippets.

@cmalard
Last active February 7, 2020 14:01
Show Gist options
  • Save cmalard/26417cf044764885099e2c6f2d927009 to your computer and use it in GitHub Desktop.
Save cmalard/26417cf044764885099e2c6f2d927009 to your computer and use it in GitHub Desktop.
Swagger - script to automate generation of client library
// For JavaScript project - `npm run update:api`
{
"scripts": {
"update:api": "./tools/update-api-swagger.sh"
}
}
#!/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"
@cmalard
Copy link
Author

cmalard commented Nov 20, 2019

Tested on Ubuntu/Mac with zsh installed... got some issues to make it work with sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment