Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/swaggest/swac

🤖 OpenAPI/Swagger client generator for PHP, Go and JavaScript (ES5)
https://github.com/swaggest/swac

client-lib codegen go javascript openapi php rest-api swagger

Last synced: 7 days ago
JSON representation

🤖 OpenAPI/Swagger client generator for PHP, Go and JavaScript (ES5)

Awesome Lists containing this project

README

        

# OpenAPI 3.0 / Swagger 2.0 compiler

A tool to render API spec as code.

[![Build Status](https://travis-ci.org/swaggest/swac.svg?branch=master)](https://travis-ci.org/swaggest/swac)
[![codecov](https://codecov.io/gh/swaggest/swac/branch/master/graph/badge.svg)](https://codecov.io/gh/swaggest/swac)
[![Image Size](https://images.microbadger.com/badges/image/swaggest/swac.svg)](https://microbadger.com/images/swaggest/swac)
![Code lines](https://sloc.xyz/github/swaggest/swac/?category=code)
![Comments](https://sloc.xyz/github/swaggest/swac/?category=comments)

## Installation

### Phar

Download `swac` from [releases](https://github.com/swaggest/swac/releases) page.

### Docker

```bash
docker run --rm swaggest/swac swac --help
```

```
v0.1.28 swac
OpenAPI/Swagger compiler, https://github.com/swaggest/swac
Usage:
swac
action Action name
Allowed values: php-guzzle-client, go-client, js-client, markdown
...
```

Example

```bash
mkdir petstore && cd petstore
docker run -v $(pwd):/code -u 1000:1000 swaggest/swac swac php-guzzle-client https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v2.0/json/petstore.json --namespace MyApp\\Petstore
```

Schema can be passed by STDIN using `-` as file path.

```bash
cat ./openapi.json | docker run -i --rm swaggest/swac swac markdown -
```

### Composer

[Install PHP Composer](https://getcomposer.org/doc/00-intro.md)

```bash
composer global require swaggest/swac
```

## Usage

### PHP Client

```
swac php-guzzle-client --help
```

```
v0.1.0 swac php-guzzle-client
OpenAPI/Swagger compiler, https://github.com/swaggest/swac
Usage:
swac php-guzzle-client --namespace
schemaPath Path/URL to OpenAPI/Swagger schema

Options:
--operations Operations filter in form of comma-separated list of method/path, default empty
--project-path Path to project root, default ./
--namespace Project namespace
```

The generated client depends on [`swaggest/rest-client`](https://github.com/swaggest/php-rest-client),
`guzzlehttp/guzzle` 6, and [`swaggest/json-schema`](https://github.com/swaggest/php-json-schema).

[Examples](/examples/php-guzzle-client).

### Go Client

```
swac go-client --help
```

```
v0.1.14 swac go-client
OpenAPI/Swagger compiler, https://github.com/swaggest/swac
Usage:
swac go-client
schemaPath Path/URL to OpenAPI/Swagger schema

Options:
--operations Operations filter in form of comma-separated list of method/path, default empty
--out Path to output package, default ./client
--pkg-name Output package name, default "client"
--skip-default-additional-properties Do not add field property for undefined `additionalProperties`
--skip-do-not-edit Skip adding "DO NOT EDIT" comments
--add-request-tags Add field tags with name and location to request structure properties, e.g. 'ID int `query:"id"`'
--show-const-properties Show properties with constant values, hidden by default
--keep-parent-in-property-names Keep parent prefix in property name, removed by default
--ignore-nullable Add `omitempty` to nullable properties, removed by default
--ignore-xgo-type Ignore `x-go-type` in schema to skip generation
--with-zero-values Use pointer types to avoid zero value ambiguity
--fluent-setters Add fluent setters to struct fields
--ignore-required Ignore if property is required when deciding on pointer type or omitempty
--renames Map of exported symbol renames, example From:To
--with-tests Generate (un)marshaling tests for entities (experimental feature)
--require-xgenerate Generate properties with `x-generate: true` only
--validate-required Generate validation code to check required properties during unmarshal
```

[Examples](/examples/go-client).

The generated client is a single package without external dependencies.

### JavaScript Client

```
swac js-client --help
```

```
v0.1.21 swac js-client
OpenAPI/Swagger compiler, https://github.com/swaggest/swac
Usage:
swac js-client
schema Path/URL to OpenAPI/Swagger schema

Options:
--operations Operations filter in form of comma-separated list of method/path, default empty
--ignore-operation-id Ignore operationId and always name operations using method and path
--client-name Name of generated client class, default APIClient
--types-prefix Prefix generated jsdoc class names
--out Path to output files, default ./client
--patches JSON patches to apply to schema file before processing, merge patches are also supported
```

[Examples](/examples/js-client).

```
swac js-client openapi.json --out ./ --client-name Backend --types-prefix xh
```

The generated client is a ES5 class using `XMLHttpRequest` and `jsdoc` type definitions without external dependencies
suitable for direct usage in browsers.

### Markdown

```
swac markdown --help
```
```
v0.1.24 swac markdown
OpenAPI/Swagger compiler, https://github.com/swaggest/swac
Usage:
swac markdown
schema Path/URL to OpenAPI/Swagger schema

Options:
--operations Operations filter in form of comma-separated list of method/path, default empty
--ignore-operation-id Ignore operationId and always name operations using method and path
--client-name Name of generated client class, default APIClient
--add-schema-url Add schema link to the document
--types-prefix Prefix generated type names
--out Path to output files, default ./client
--patches JSON patches to apply to schema file before processing, merge patches are also supported

```

[Examples](/examples/).

```
swac markdown openapi.json --out ./API-Docs.md --types-prefix xh
```