Generate a TypeScript API client or GraphQL schema based on a Swagger JSON file
$ npm install swagger-client-codegen --save-dev
$ yarn add swagger-client-codegen --dev
$ npx swagger-client-codegen --typescript --pathToSwaggerJSON 'https://some.url/path.json' --outputPath 'some/file/path'
$ yarn swagger-client-codegen --graphql --pathToSwaggerJSON '../some/file/path.json' --outputPath './some/file/path/' --includeJSONOutput
NOTE: Specifying a recursive outputPath (nested non-existing directories) will only work on Node >= v10.12!
You can also use swagger-client-codegen
directly from your code, if that suits you, as mentioned in the following JavaScript and TypeScript sections.
const { generateTypeScriptClient, generateGraphQLClient } = require('swagger-client-codegen')
generateTypeScriptClient({
pathToSwaggerJSON: 'https://some.url/path.json',
outputPath: 'some/file/path'
})
generateGraphQLClient({
pathToSwaggerJSON: '../some/file/path.json',
outputPath: './some/file/path/',
includeJSONOutput: true
})
import { generateTypeScriptClient, generateGraphQLClient } from 'swagger-client-codegen'
generateTypeScriptClient({
pathToSwaggerJSON: 'https://some.url/path.json',
outputPath: 'some/file/path'
})
generateGraphQLClient({
pathToSwaggerJSON: '../some/file/path.json',
outputPath: './some/file/path/',
includeJSONOutput: true
})
$ npm run build # 1. Builds TypeScript to JavaScript
$ npm run start # 2. Runs actual codegen