-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typescript-angular2: Make the api suffix configurable #8281
Comments
For anyone coming across these old issues (that may never be resolved), I created the following script to:
NOTE: The script uses docker and is written for MacOS (which has some #!/bin/bash
TEMP_SWAGGER_LOCATION=./order-api/
TEMP_SWAGGER_NAME=swagger-temp.yaml
TEMP_SWAGGER_FILE=$TEMP_SWAGGER_LOCATION$TEMP_SWAGGER_NAME
TEMP_SWAGGER_SUFFIX=api
echo "-----------------------------------------------"
echo "Clean up source folder..."
rm -r ./order-api/src/
echo " ...done"
echo ""
echo "-----------------------------------------------"
echo "pre-processing swagger to add an 'Api' suffix to all service names for code generation"
cp ./order-api/swagger.yaml "$TEMP_SWAGGER_FILE"
echo "searching and replacing all 'tags:' in "$TEMP_SWAGGER_NAME""
TAGS=$(grep -e '- name:' "$TEMP_SWAGGER_FILE" | sed 's/^.*: //')
# replace "tags:" declaration names with -api suffix
sed -i '' "s/- name: .*/&-$TEMP_SWAGGER_SUFFIX/g" "$TEMP_SWAGGER_FILE"
# loop over tags and add '-api' suffix to each
for TAG in ${TAGS}; do
echo "$TAG"
sed -i '' "s/- $TAG$/&-$TEMP_SWAGGER_SUFFIX/g" "$TEMP_SWAGGER_FILE"
done
echo "-----------------------------------------------"
echo "Generating code..."
docker run --rm --net=host -u="$(id -u)" -v ${PWD}/order-api:/local \
swaggerapi/swagger-codegen-cli generate \
-i /local/$TEMP_SWAGGER_NAME \
-l typescript-angular \
-o /local/src \
--additional-properties ngVersion=9.0.1 \
--model-name-prefix=$TEMP_SWAGGER_SUFFIX
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's/rxjs\//rxjs/' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's/formParams = formParams/formParams/' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's/ || formParams//' {} \;
# fix longstanding model prefix generation issue
# https://github.com/swagger-api/swagger-codegen/issues/7866 and https://github.com/swagger-api/swagger-codegen/issues/8281
find ./order-api/src/model/*.ts -type f -exec sed -i '' -e 's|/apiApi|/api|' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's|/apiApi|/api|' {} \;
find ./order-api/src/api/*.service.ts -type f -exec sed -i '' -e 's|ApiApi|Api|' {} \;
echo " ...done"
echo ""
echo "-----------------------------------------------"
echo "Building Library"
(cd ./order-api; ng build order-api --prod)
echo " ...done"
echo ""
echo "Compressing"
tar -zcvf order-api.tar.gz ./dist/order-api
echo "Cleaning up temp swagger file..."
rm -f "$TEMP_SWAGGER_FILE"
echo "generate.sh library generation complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd be happy to have a look if I'm able to provide a pull request for this feature request, if nobody is already working on this. I had a look in the issue tracker but there was only a request for changing it from Api to Service and another unrelated request for adding a possiblity to define a suffix in the JAXRs generator.
Description
In earlier versions the generated service classes were suffixed with "Api". Recently this has changed to be "Service". While this adheres to angular standards it sometimes can cause collisions with existing services. For me it would make sense to use the api suffix because of multiple reasons.
We avoid collisions. If I have a UserService in my client and somebody introduces a UserApi, suddenly I need to alias my imports in order to use both services in one place.
Having the .api file extensions and the Api suffix in the class name, it makes it more clear to the developer, that this is not a normal service. If you work in our project then you know that apis are always generated.
Suggest a fix/enhancement
Introduce a configuration for typescript-angular2 which lets you specify a different suffix.
The text was updated successfully, but these errors were encountered: