Skip to content

📦 Publish API Client SDK Package #40

📦 Publish API Client SDK Package

📦 Publish API Client SDK Package #40

Workflow file for this run

name: 📦 Publish API Client SDK Package
on:
workflow_dispatch:
inputs:
api-name:
type: choice
description: 🧳 Select an API to generate and publish client SDK package
options:
- products-api
- users-api
required: true
api-version:
description: Package Version
required: true
default: '1.0.0'
env:
SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
USERNAME: ${{ secrets.SWAGGERHUB_USERNAME }}
jobs:
publish-sdk:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: 'https://registry.npmjs.org'
- name: Set variables
id: vars
run: |
API_NAME=${{ github.event.inputs['api-name'] }}
API_VERSION=${{ github.event.inputs['api-version'] }}
API_DISPLAY_NAME="$(echo ${API_NAME//-/ } | sed 's/\b\(.\)/\u\1/g')"
if [ -z "$API_NAME" ] || [ -z "$API_VERSION" ]; then
echo "Error: Required variables not set"
exit 1
fi
echo "API_NAME=$API_NAME" >> $GITHUB_ENV
echo "API_VERSION=$API_VERSION" >> $GITHUB_ENV
echo "API_DISPLAY_NAME=$API_DISPLAY_NAME" >> $GITHUB_ENV
echo "PACKAGE_NAME=@api-client-sdk-streamline-sample/${API_NAME}-client" >> $GITHUB_ENV
echo "Using API: $API_NAME version: $API_VERSION"
- name: Install OpenAPI Generator CLI
run: npm install -g @openapitools/[email protected]
- name: Build Runtime Package
run: |
echo "🏗️ Building openapi-fetch-runtime package..."
cd packages/openapi-fetch-runtime
npm install
npm run build
cd ../..
- name: Pull Spec from SwaggerHub
run: |
API_NAME_WITH_PREFIX="sample-${{ env.API_NAME }}"
curl -s -H "Authorization: ${SWAGGERHUB_API_KEY}" \
"https://api.swaggerhub.com/apis/${USERNAME}/${API_NAME_WITH_PREFIX}/${{ env.API_VERSION }}/swagger.json" \
-o spec.json
echo "📄 Preview of pulled OpenAPI spec:"
cat spec.json
- name: Generate SDK
run: |
BASE_NAME="$(echo ${API_NAME%-api} | sed 's/./\u&/1')"
openapi-generator-cli generate \
-i spec.json \
-g typescript-fetch \
-o packages/api-client-sdk \
--additional-properties=npmName=${{ env.PACKAGE_NAME }} \
--additional-properties=npmVersion=${{ env.API_VERSION }} \
--additional-properties=withInterfaces=true \
--additional-properties=modelPropertyNaming=original \
--additional-properties=supportsES6=true \
--additional-properties=description='OpenAPI client for @api-client-sdk-streamline-sample | ${BASE_NAME}API. Part of api-client-sdk-streamline-sample project.'
- name: Configure package.json
working-directory: ./packages/api-client-sdk
run: |
REPO_URL="https://github.com/${{ github.repository }}"
jq --arg url "${REPO_URL}.git" \
--arg homepage "${REPO_URL}#readme" \
'.repository.url = $url
| .homepage = $homepage
| .publishConfig.access = "public"
| .files = ["dist", "src", "*.json", "*.md"]' \
package.json > tmp.json && mv tmp.json package.json
- name: Add SwaggerHub link to README.md
working-directory: ./packages/api-client-sdk
run: |
API_NAME_WITH_PREFIX="sample-${{ env.API_NAME }}"
SWAGGER_URL="https://app.swaggerhub.com/apis/${USERNAME}/${API_NAME_WITH_PREFIX}/${{ env.API_VERSION }}"
REPO_URL="https://github.com/${{ github.repository }}"
echo "Part of [api-client-sdk-streamline-sample](${REPO_URL}), a project demonstrating streamlined API client SDK development workflow.\n" > temp_readme
echo "The OpenAPI specification for this client SDK is available on SwaggerHub: [@api-client-sdk-streamline-sample | ${{env.API_DISPLAY_NAME}}](${SWAGGER_URL}).\n" >> temp_readme
echo "---\n" >> temp_readme
cat README.md >> temp_readme
mv temp_readme README.md
- name: Configure runtime and clean up
working-directory: ./packages/api-client-sdk
run: |
echo "📂 Initial project structure:"
ls -R
jq '.dependencies["@api-client-sdk-streamline-sample/openapi-fetch-runtime"] = "^1.0.0"' \
package.json > tmp.json && mv tmp.json package.json
find ./src/apis -type f -exec sed -i "s|from '../runtime';|from '@api-client-sdk-streamline-sample/openapi-fetch-runtime';|g" {} +
find ./src/models -type f -exec sed -i "s|from '../runtime';|from '@api-client-sdk-streamline-sample/openapi-fetch-runtime';|g" {} +
sed -i "/export \* from '\.\/runtime';/d" ./src/index.ts
echo "🗑️ Removing generated runtime..."
rm -rf ./src/runtime.ts
echo "📂 Final project structure after cleanup:"
ls -R
- name: Build SDK
working-directory: ./packages/api-client-sdk
run: |
npm install
npm run build
- name: SDK Package Preview
working-directory: ./packages/api-client-sdk
run: |
echo "📂 Generated directory structure:"
tree -a || ls -la
echo
echo "📦 Generated package.json:"
cat package.json | jq .
echo
echo "⚙️ Generated tsconfig.json:"
cat tsconfig.json | jq .
echo
echo "📝 Generated tsconfig.esm.json (if exists):"
[ -f tsconfig.esm.json ] && cat tsconfig.esm.json | jq . || echo "No tsconfig.esm.json found"
echo
echo "📄 Current .npmignore content:"
cat .npmignore
echo
sed -i '/^dist/d' .npmignore
echo "📄 Updated .npmignore content:"
cat .npmignore
echo
echo "📦 Files to be published:"
npm pack --dry-run
- name: Create SDK Release
uses: ./.github/workflows/release-template.yml
with:
package-name: ${{ env.PACKAGE_NAME }}
version: ${{ env.API_VERSION }}
type: sdk
- name: Publish as NPM Package
working-directory: ./packages/api-client-sdk
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
if [ -z "$(npm view ${{ env.PACKAGE_NAME }}@${{ env.API_VERSION }} version 2>/dev/null)" ]; then
if npm publish; then
echo "✨ Successfully published ${{ env.PACKAGE_NAME }}@${{ env.API_VERSION }}"
else
echo "Failed to publish package ${{ env.PACKAGE_NAME }}@${{ env.API_VERSION }}"
exit 1
fi
else
echo "Version ${{ env.API_VERSION }} already exists for ${{ env.PACKAGE_NAME }}"
exit 1
fi