Create npm packages of swagger generated code via the swagger cli

First download swagger codegen version 2 as version 3 seems to produce code with lots of errors. Build the cli

cd swagger-codegen-2.4.0
mvn package -DskipTests

Then get your swagger json definition from your application endpoint in spring boot this is normally /api/v2/api-docs and save it yo a file api-docs.json or use the url directly if you can, then with swagger cli built run the following command pointing to the json provided by the swagger endpoint

java -jar ./swagger-codegen-2.4.0/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i api-docs.json -l typescript-angular -o ./client/ --additional-properties ngVersion=6.0,npmName=<PACKAGE_NAME>,npmVersion=0.0.1

This puts the generated code in the client directory mkdir client. Then to verify that it worked properly do the following

cd client
npm run build

this will run ng-packgr which should build a dist directory which can be published to npm

installing the package locally

Go to the distfolder that was created by building and run npm link . This will create a symlink to you system node modules install which you can then use in your consuming project by doing npm link <package_name> this installs it locally for development with out the need to publish first.

Publishing the package to an internal npm repo

First you’ll need to update your package.json in the generated client with your npm repo like so


{
 ...
    "publishConfig": {
    	"registry": "http://myrepo/nexus/repository/npm-private/"
    }
 ...
}

then to actually publish do

npm publish dist

If you get an error about having to login or user not authorised this is because the npm cli has to login/authorise a user with the remote, if we were using the npm registry provided by npm we would use the credentials we used to make an account on npm.com. However because we are using an internal repository we must tell npm to authorise our user for a specific registry with npm adduser however running this on its own from the project does not read the publishConfig section from the package.json so we have to run.

npm adduser --registry http://myrepo/nexus/repository/npm-private/

Troubleshooting

if when building you get typescript compile errors involving rxjs and your global ng version is 7 and the local angular version is 6, remove the ^ from the version of rxjs in the dev dependencies of the generated package.json