Hyounoo Sung
Hyounoo Sung

Reputation: 59

Add github webhook to generate client sdk with openapi-generator

Hi I wonder if this scenario is even possible.

I am using openapi-generator with typescript-axios generator option and want to know if it can be automated as below.

This is my initial thought.

  1. Create a project and add script to generate code using openapi-generator and compile. ex) "build": "openapi-generator-cli generate -g typescript-axios -o src -i https://myapi.com/swagger.json && tsc"
  2. Upload to github and add a webhook so it gets notifications when our api project gets PR or commit.
  3. Once this webhook gets notification, Github action triggers and generates new client sdk and compiled lib with script at no.1

I tired this with below work flow but published package doesn't contain generated code. Any help would be appreciated.

name: Node.js Package

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm ci      

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/
      - run: npm ci
      - run: npm run-script build      
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}

and my package.json

{
  "name": "my-api-client",
  "version": "1.0.0",
  "description": "Openapi generated typescript-axios client",
  "main": "index.js",
  "scripts": {
    "test": "echo \\\"Error: no test specified\\\" && exit 1",
    "build": "openapi-generator-cli generate -g typescript-axios -o src -i https://myapi.com/swagger.json --type-mappings=DateTime=Date && tsc"
  },
  "keywords": [
    "typescript-axios",
    "client"
  ],
  "license": "ISC",
  "dependencies": {
    "axios": "^0.21.0",
    "typescript": "^3.8.3"
  },
  "devDependencies": {
    "@openapitools/openapi-generator-cli": "^2.0.3",
    "@types/node": "^13.9.0"
  }
}```


Thank you


Upvotes: 1

Views: 762

Answers (1)

Hyounoo Sung
Hyounoo Sung

Reputation: 59

I accidently add src and lib folder and that was the reason my published package didn't contain those folders. After fixing .gitignore everything works fine.

So basically, my project will only contain package.json and whenever I create a release tag, it will trigger above Github workflow and publish a new openapi-generated client sdk to npm.

Upvotes: 1

Related Questions