Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
81c3796
.
romantech48 Jun 3, 2022
5599532
feat: add custom generator to generate only required url paths
romantech48 Jun 8, 2022
b56e7fd
feat: add custom generator to generate only required models
romantech48 Jun 9, 2022
f552dd2
fix: include to required models which reference from models
romantech48 Jun 9, 2022
42307bb
feat: add .models extension to file names
romantech48 Jun 9, 2022
c1fa4d0
feat: add .models extension to file names
romantech48 Jun 9, 2022
8e4d97c
feat: add pascalCase helper
romantech48 Jun 14, 2022
e2a606e
feat: add the saddleback layout for generate custom services
romantech48 Jun 14, 2022
2541445
feat: add building process to npm install from git
romantech48 Jun 15, 2022
dda85bd
feat: add scripts to run generator with custom config.json file
romantech48 Jun 16, 2022
737e6e5
feat: add different services & refactor ability to add .service .mode…
romantech48 Jun 17, 2022
78cdd78
feat: remove unused filters
romantech48 Jun 17, 2022
30c4006
feat: rename custom to saddleback
romantech48 Jun 17, 2022
941596a
feat: remove lodash prefixes
romantech48 Jun 20, 2022
8fc10ec
fix: index file and config priority
romantech48 Jun 20, 2022
b7eb86d
feat: add new README
romantech48 Jun 21, 2022
cee9a2f
feat: add functions to fetch swagger.json from saddleback server
romantech48 Jun 23, 2022
789d3a3
wip
romantech48 Jun 23, 2022
a5e1dfd
wip
romantech48 Jun 23, 2022
2285d36
Merge remote-tracking branch 'origin/features/auto-fetch-swagger' int…
romantech48 Jul 11, 2022
d0c1f37
fix: add functions to fetch swagger.json from saddleback server
romantech48 Jul 11, 2022
7752008
update README
romantech48 Jul 11, 2022
578371a
feat: change file names from PascalCase to camelCase
romantech48 Jul 12, 2022
49d5223
feat: add index.ts to models and service directory
romantech48 Jul 12, 2022
c31661b
feat: remove global generated index.ts
romantech48 Jul 12, 2022
2ecc049
feat: add include\exclude filters
romantech48 Sep 6, 2022
8e9830e
feat: handle empty filter array
romantech48 Sep 6, 2022
8478c91
fix: cli argument order
romantech48 Sep 6, 2022
70c097a
update README
romantech48 Sep 7, 2022
accba3f
update package name
romantech48 Sep 7, 2022
1eba12a
update package name
romantech48 Sep 7, 2022
f00a293
remove pass and config
romantech48 Sep 7, 2022
fdb887a
fix cli params
romantech48 Sep 7, 2022
469a52d
update version
romantech48 Sep 7, 2022
1ca0649
add service to getBaseConfigParams
romantech48 Sep 9, 2022
a139593
change serviceError props to axiosError
romantech48 Sep 9, 2022
30a739a
update version
romantech48 Sep 9, 2022
c2a0072
change getBaseConfig & serviceError to relative location
romantech48 Sep 9, 2022
3cb1acb
update version
romantech48 Sep 9, 2022
b0cee88
fix service name
romantech48 Sep 9, 2022
c455671
add new services
romantech48 Sep 9, 2022
3e51ace
update version
romantech48 Sep 9, 2022
068c3ce
add await for getBaseConfig
romantech48 Sep 9, 2022
7250032
update version
romantech48 Sep 9, 2022
319f8f1
fix: remove only first prefix
romantech48 Sep 19, 2022
34bcfa9
fix: remove all lodash symbols
romantech48 Sep 21, 2022
ba52be6
fix: add camelCase in import/export file paths
romantech48 Sep 26, 2022
4ed7ec1
feat: add ability to pass custom axios instance throw services
romantech48 Oct 10, 2022
42f56ac
update version
romantech48 Oct 10, 2022
62c5173
change default config location
romantech48 Oct 10, 2022
c43473a
update README
romantech48 Oct 10, 2022
33025ad
update README
romantech48 Oct 10, 2022
061f3fc
remove model index file & update version
romantech48 Dec 15, 2022
64e7630
Merge branch 'master' into features/saddleback-version
romantech48 Jan 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 122 additions & 77 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,140 @@
# OpenAPI Typescript Codegen

[![NPM][npm-image]][npm-url]
[![License][license-image]][license-url]
[![Coverage][coverage-image]][coverage-url]
[![Quality][quality-image]][quality-url]
[![Code Climate][climate-image]][climate-url]
[![Downloads][downloads-image]][downloads-url]
[![Build][build-image]][build-url]

> Node.js library that generates Typescript clients based on the OpenAPI specification.

## Why?
- Frontend ❤️ OpenAPI, but we do not want to use JAVA codegen in our builds
- Quick, lightweight, robust and framework-agnostic 🚀
- Supports generation of TypeScript clients
- Supports generations of Fetch, [Node-Fetch](#node-fetch-support), [Axios](#axios-support), [Angular](#angular-support) and XHR http clients
- Supports OpenAPI specification v2.0 and v3.0
- Supports JSON and YAML files for input
- Supports generation through CLI, Node.js and NPX
- Supports tsc and @babel/plugin-transform-typescript
- Supports aborting of requests (cancelable promise pattern)
- Supports external references using [json-schema-ref-parser](https://github.com/APIDevTools/json-schema-ref-parser/)
# Custom Saddleback OpenAPI Typescript Codegen

> For original usage read - [original README](docs/original-readme.md)

## Install

```
npm install openapi-typescript-codegen --save-dev
npm install saddleback-openapi-typescript-codegen --save-dev
```

## Step-by-step guide based on ME app
### auto fetch
1. install the package
2. create config files for every microservice that you need (put it in the root project folder for example openapiEvents.config.json)
3. inside the config file you need to specify
1. output folder (for Events it would be ./src/shared/api/events)
2. microservice that you're specifying ("Events")
3. environment that using for fetch ("feature")
4. if you don't want to generate whole microservices, you can specify filterMethod and filterArray
4. create utility files that used in services
1. 'baseAxios' should export axios instance that be used for fetch
2. 'getBaseConfig' and 'serviceError' described here https://github.com/saddlebackdev/church-management/wiki/Services
5. run the command where you should pass your login and pass from saddleback identity server `saddlebackApi --config openapiEvents.config.json --login login --password password`
### local swagger
1. same as above
2. same as above
3. addition specify the input path to the swagger.json file
4. same as above
5. run the command `saddlebackApi --config openapiEvents.config.json`

## Usage

Generated folders should be untouchable. Because every generate action will delete and put generated files into the output folder.

```
$ openapi --help
$ saddlebackOpenapi --help

Usage: openapi [options]
Usage: saddlebackOpenapi [options]

Options:
-V, --version output the version number
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
-o, --output <value> Output directory (required)
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
--name <value> Custom client class name
--useOptions Use options instead of arguments
--useUnionTypes Use union types instead of enums
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk (default: true)
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfix <value> Service name postfix (default: "Service")
--request <value> Path to custom request file
-i, --input <value> OpenAPI specification, can be a path, url or string content
-o, --output <value> Output directory should end with service name workflows | events | notifications | core | journey | giving | smallGroup
-c, --config <value> Path to the config file
-l, --login <value> Login
-p, --password <value> Password
-e, --environment <value> Environment dev | stage | stage2
-s, --service <value> Service Service Workflows | Events | Notifications | Core | Journey | Giving | SmallGroup
-h, --help display help for command
-m, --filterMethod Filter method include(default) | exclude')
-f, --filterArray Filter array

Examples
$ openapi --input ./spec.json --output ./generated
$ openapi --input ./spec.json --output ./generated --client xhr
$ saddlebackOpenApi --input ./spec.json --output ./generated
$ saddlebackOpenApi --config ./openapi.config.json
$ saddlebackOpenApi -o "./folderPath" -l "Login" -p "Password" -e "dev" -s "core"
```

## Config file
*extends original OPTIONS*
```
input required in the config or cmd arguments
output required in the config or cmd arguments

additionalModelFileExtension optional
additionalServiceFileExtension optional
removeLodashPrefixes optional
```
### Settings:
### `input`
- Default: `undefined`
- Type: `string`

OpenAPI specification, can be a path, url or string content (required in the config or cmd arguments)

### `output`
- Default: `undefined`
- Type: `string`

Output directory (required in the config or cmd arguments)

### `login` (autofetch)
- Default: `undefined`
- Type: `string`

Login to saddleback identity server

### `password` (autofetch)
- Default: `undefined`
- Type: `string`

Password to saddleback identity server

### `environment` (autofetch)
- Default: `undefined`
- Type: `'dev' | 'stage' | 'stage2'`

Which Environment should be used for swagger.json

### `service` (autofetch)
- Default: `undefined`
- Type: `'workflows' | 'event' | 'notifications' | 'core'`

Which service should be fetched

### `filterMethod` (autofetch)
- Default: `include`
- Type: `'include' | 'exclude'`

Which method of sort should be applied to the filter array

### `filterArray` (autofetch)
- Default: `undefined`
- Type: `string[]`

Which services should be *included* or *excluded* to/from generated list

### `additionalModelFileExtension`
- Default: `true`
- Type: `boolean`

Apply `*.models.*` extension to model files.

For example (myModel.ts -> myModel.models.ts)

### `additionalServiceFileExtension`
- Default: `true`
- Type: `boolean`

Apply `*.service.*` extension to service files.

For example (myService.ts -> myService.service.ts)

### `removeLodashPrefixes`
- Default: `true`
- Type: `boolean`

Remove special prefixes that are separated by `_` at the start of names.

Documentation
===
- [Basic usage](docs/basic-usage.md)
- [OpenAPI object](docs/openapi-object.md)
- [Client instances](docs/client-instances.md) `--name`
- [Argument vs. Object style](docs/arguments-vs-object-style.md) `--useOptions`
- [Enums vs. Union types](docs/enum-vs-union-types.md) `--useUnionTypes`
- [Runtime schemas](docs/runtime-schemas.md) `--exportSchemas`
- [Enum with custom names and descriptions](docs/custom-enums.md)
- [Nullable props (OpenAPI v2)](docs/nullable-props.md)
- [Authorization](docs/authorization.md)
- [External references](docs/external-references.md)
- [Canceling requests](docs/canceling-requests.md)
- [Custom request file](docs/custom-request-file.md)

Support
===
- [Babel support](docs/babel-support.md)
- [Axios support](docs/axios-support.md)
- [Angular support](docs/angular-support.md)
- [Node-Fetch support](docs/node-fetch-support.md)

[npm-url]: https://npmjs.org/package/openapi-typescript-codegen
[npm-image]: https://img.shields.io/npm/v/openapi-typescript-codegen.svg
[license-url]: LICENSE
[license-image]: http://img.shields.io/npm/l/openapi-typescript-codegen.svg
[coverage-url]: https://codecov.io/gh/ferdikoomen/openapi-typescript-codegen
[coverage-image]: https://img.shields.io/codecov/c/github/ferdikoomen/openapi-typescript-codegen.svg
[quality-url]: https://lgtm.com/projects/g/ferdikoomen/openapi-typescript-codegen
[quality-image]: https://img.shields.io/lgtm/grade/javascript/g/ferdikoomen/openapi-typescript-codegen.svg
[climate-url]: https://codeclimate.com/github/ferdikoomen/openapi-typescript-codegen
[climate-image]: https://img.shields.io/codeclimate/maintainability/ferdikoomen/openapi-typescript-codegen.svg
[downloads-url]: http://npm-stat.com/charts.html?package=openapi-typescript-codegen
[downloads-image]: http://img.shields.io/npm/dm/openapi-typescript-codegen.svg
[build-url]: https://circleci.com/gh/ferdikoomen/openapi-typescript-codegen/tree/master
[build-image]: https://circleci.com/gh/ferdikoomen/openapi-typescript-codegen/tree/master.svg?style=svg
For example (Custom_Prefix_Name -> Name)
65 changes: 65 additions & 0 deletions bin/saddleback.cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env node

'use strict';

const path = require('path');
const { program } = require('commander');
const pkg = require('../package.json');

const params = program
.name('saddlebackOpenApi')
.usage('[options]')
.version(pkg.version)
.option('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
.option('-o, --output <value>', 'Output directory (required)')
.option('-c, --config <value>', 'Path to the config file')
.option('-l, --login <value>', 'Login')
.option('-p, --password <value>', 'Password')
.option('-e, --environment <value>', 'Environment dev | stage | stage2')
.option(
'-s, --service <value>',
'Service Workflows | Events | Notifications | Core | Journey | Giving | SmallGroup'
)
.option('-m, --filterMethod <value>', 'Filter method include(default) | exclude')
.option('-f, --filterArray <value>', 'Filter array')
.parse(process.argv)
.opts();

const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
const config = require(path.resolve(params.config || `openapi.config.json`));

if (OpenAPI) {
OpenAPI.generateSaddlebackSpec({
httpClient: 'saddleback',
clientName: '',
useOptions: true,
useUnionTypes: false,
exportCore: false,
exportServices: true,
exportModels: true,
exportSchemas: false,
indent: '4',
postfix: '',
request: '',
write: true,
additionalModelFileExtension: true,
additionalServiceFileExtension: true,
removeLodashPrefixes: true,
...config,
input: params.input || config.input,
output: params.output || config.output,
username: params.login || config.login,
password: params.password || config.password,
useEnvironment: params.environment || config.environment,
useService: params.service || config.service,
filterMethod: params.filterMethod || config.filterMethod || 'include',
filterArray: params.filterArray || config.filterArray || [],
})
.then(() => {
process.exit(0);
})
.catch(error => {
console.error(error);
process.exit(1);
});
}
2 changes: 1 addition & 1 deletion docs/basic-usage.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Basic usage
# Basic usage

```
$ openapi --help
Expand Down
95 changes: 95 additions & 0 deletions docs/original-readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# OpenAPI Typescript Codegen

[![NPM][npm-image]][npm-url]
[![License][license-image]][license-url]
[![Coverage][coverage-image]][coverage-url]
[![Quality][quality-image]][quality-url]
[![Code Climate][climate-image]][climate-url]
[![Downloads][downloads-image]][downloads-url]
[![Build][build-image]][build-url]

> Node.js library that generates Typescript clients based on the OpenAPI specification.

## Why?
- Frontend ❤️ OpenAPI, but we do not want to use JAVA codegen in our builds
- Quick, lightweight, robust and framework-agnostic 🚀
- Supports generation of TypeScript clients
- Supports generations of Fetch, [Node-Fetch](#node-fetch-support), [Axios](#axios-support), [Angular](#angular-support) and XHR http clients
- Supports OpenAPI specification v2.0 and v3.0
- Supports JSON and YAML files for input
- Supports generation through CLI, Node.js and NPX
- Supports tsc and @babel/plugin-transform-typescript
- Supports aborting of requests (cancelable promise pattern)
- Supports external references using [json-schema-ref-parser](https://github.com/APIDevTools/json-schema-ref-parser/)

## Install

```
npm install openapi-typescript-codegen --save-dev
```

## Usage

```
$ openapi --help

Usage: openapi [options]

Options:
-V, --version output the version number
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
-o, --output <value> Output directory (required)
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
--name <value> Custom client class name
--useOptions Use options instead of arguments
--useUnionTypes Use union types instead of enums
--exportCore <value> Write core files to disk (default: true)
--exportServices <value> Write services to disk (default: true)
--exportModels <value> Write models to disk (default: true)
--exportSchemas <value> Write schemas to disk (default: false)
--indent <value> Indentation options [4, 2, tab] (default: "4")
--postfix <value> Service name postfix (default: "Service")
--request <value> Path to custom request file
-h, --help display help for command

Examples
$ openapi --input ./spec.json --output ./generated
$ openapi --input ./spec.json --output ./generated --client xhr
```

Documentation
===
- [Basic usage](basic-usage.md)
- [OpenAPI object](openapi-object.md)
- [Client instances](client-instances.md) `--name`
- [Argument vs. Object style](arguments-vs-object-style.md) `--useOptions`
- [Enums vs. Union types](enum-vs-union-types.md) `--useUnionTypes`
- [Runtime schemas](runtime-schemas.md) `--exportSchemas`
- [Enum with custom names and descriptions](custom-enums.md)
- [Nullable props (OpenAPI v2)](nullable-props.md)
- [Authorization](authorization.md)
- [External references](external-references.md)
- [Canceling requests](canceling-requests.md)
- [Custom request file](custom-request-file.md)

Support
===
- [Babel support](babel-support.md)
- [Axios support](axios-support.md)
- [Angular support](angular-support.md)
- [Node-Fetch support](node-fetch-support.md)

[npm-url]: https://npmjs.org/package/openapi-typescript-codegen
[npm-image]: https://img.shields.io/npm/v/openapi-typescript-codegen.svg
[license-url]: ../LICENSE
[license-image]: http://img.shields.io/npm/l/openapi-typescript-codegen.svg
[coverage-url]: https://codecov.io/gh/ferdikoomen/openapi-typescript-codegen
[coverage-image]: https://img.shields.io/codecov/c/github/ferdikoomen/openapi-typescript-codegen.svg
[quality-url]: https://lgtm.com/projects/g/ferdikoomen/openapi-typescript-codegen
[quality-image]: https://img.shields.io/lgtm/grade/javascript/g/ferdikoomen/openapi-typescript-codegen.svg
[climate-url]: https://codeclimate.com/github/ferdikoomen/openapi-typescript-codegen
[climate-image]: https://img.shields.io/codeclimate/maintainability/ferdikoomen/openapi-typescript-codegen.svg
[downloads-url]: http://npm-stat.com/charts.html?package=openapi-typescript-codegen
[downloads-image]: http://img.shields.io/npm/dm/openapi-typescript-codegen.svg
[build-url]: https://circleci.com/gh/ferdikoomen/openapi-typescript-codegen/tree/master
[build-image]: https://circleci.com/gh/ferdikoomen/openapi-typescript-codegen/tree/master.svg?style=svg
Loading