Skip to content

📦 Publish API Client SDK Package #8

📦 Publish API Client SDK Package

📦 Publish API Client SDK Package #8

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 }}
PACKAGE_NAME: "@api-client-sdk-streamline-sample/${{ github.event.inputs.api-name }}-client"
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: 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-${{ github.event.inputs.api-name }}"
curl -s -H "Authorization: ${SWAGGERHUB_API_KEY}" \
"https://api.swaggerhub.com/apis/${USERNAME}/${API_NAME_WITH_PREFIX}/${{ github.event.inputs.api-version }}/swagger.json" \
-o spec.json
echo "📄 Preview of pulled OpenAPI spec:"
cat spec.json
- name: Generate SDK
run: |
openapi-generator-cli generate \
-i spec.json \
-g typescript-fetch \
-o packages/api-client-sdk \
--additional-properties=npmName=${{ env.PACKAGE_NAME }} \
--additional-properties=npmVersion=${{ github.event.inputs.api-version }} \
--additional-properties=withInterfaces=true \
--additional-properties=modelPropertyNaming=original \
--additional-properties=supportsES6=true
- 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"' \
package.json > tmp.json && mv tmp.json package.json
- 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: Display package info
working-directory: ./packages/api-client-sdk
run: |
echo "📦 Generated package.json:"
cat package.json | jq .
echo "⚙️ Generated tsconfig.json:"
cat tsconfig.json | jq .
- name: Build SDK
working-directory: ./packages/api-client-sdk
run: |
npm install
npm run build
- name: Publish as NPM Package
working-directory: ./packages/api-client-sdk
env:
NODE_AUTH_TOKEN: ${{ env.NPM_TOKEN }}
run: |
if [ -z "$(npm view ${{ env.PACKAGE_NAME }}@${{ github.event.inputs.api-version }} version 2>/dev/null)" ]; then
if npm publish; then
echo "✨ Successfully published ${{ env.PACKAGE_NAME }}@${{ github.event.inputs.api-version }}"
else
echo "Failed to publish package ${{ env.PACKAGE_NAME }}@${{ github.event.inputs.api-version }}"
exit 1
fi
else
echo "Version ${{ github.event.inputs.api-version }} already exists for ${{ env.PACKAGE_NAME }}"
exit 1
fi