Skip to content

Commit 392fb67

Browse files
committed
- Updating documentation
1 parent ac62141 commit 392fb67

17 files changed

+597
-461
lines changed

Diff for: README.md

+24-459
Large diffs are not rendered by default.

Diff for: docs/angular-support.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Angular support
2+
3+
Lorem
4+
5+
`openapi --input ./spec.json --output ./generated --client angular`
6+
7+
This has been tested with the following versions:
8+
9+
```
10+
"@angular/common": "13.1.3",
11+
"@angular/core": "13.1.3",
12+
"rxjs": "7.5.2",
13+
```

Diff for: docs/arguments-vs-object-style.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Arguments vs. Object style
2+
3+
**Flag:** `--useOptions`
4+
5+
There's no [named parameter](https://en.wikipedia.org/wiki/Named_parameter) in JavaScript or TypeScript, because of
6+
that, we offer the flag `--useOptions` to generate code in two different styles.
7+
8+
**Argument style:**
9+
```typescript
10+
const createUser = (name: string, password: string, type?: string, address?: string) => {
11+
// ...
12+
};
13+
14+
// Usage
15+
createUser('Jack', '123456', undefined, 'NY US');
16+
```
17+
18+
**Object style:**
19+
```typescript
20+
const createUser = ({ name, password, type, address }: {
21+
name: string,
22+
password: string,
23+
type?: string
24+
address?: string
25+
}) => {
26+
// ...
27+
};
28+
29+
// Usage
30+
createUser({
31+
name: 'Jack',
32+
password: '123456',
33+
address: 'NY US'
34+
});
35+
```

Diff for: docs/authorization.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Authorization
2+
3+
The OpenAPI generator supports Bearer Token authorization. In order to enable the sending
4+
of tokens in each request you can set the token using the global OpenAPI configuration:
5+
6+
```typescript
7+
import { OpenAPI } from './generated';
8+
9+
OpenAPI.TOKEN = 'some-bearer-token';
10+
```
11+
12+
Alternatively, we also support an async method that provides the token for each request.
13+
You can simply assign this method to the same `TOKEN `property in the global OpenAPI object.
14+
15+
```typescript
16+
import { OpenAPI } from './generated';
17+
18+
const getToken = async () => {
19+
// Some code that requests a token...
20+
return 'SOME_TOKEN';
21+
};
22+
23+
OpenAPI.TOKEN = getToken;
24+
```

Diff for: docs/axios-support.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Axios support
2+
3+
This tool allows you to generate a client based on the [`Axios`](https://www.npmjs.com/package/axios) client.
4+
The advantage of the Axios client is that it works in both Node.js and Browser based environments.
5+
If you want to generate the Axios based client then you can specify `--client axios` in the openapi call:
6+
7+
`openapi --input ./spec.json --output ./generated --client axios`
8+
9+
The only downside is that this client needs some additional dependencies to work (due to the missing FormData
10+
classes in Node.js).
11+
12+
```
13+
npm install axios --save-dev
14+
npm install [email protected] --save-dev
15+
```
16+
17+
In order to compile the project and resolve the imports, you will need to enable the `allowSyntheticDefaultImports`
18+
in your `tsconfig.json` file.

Diff for: docs/babel-support.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Babel support
2+
3+
If you use enums inside your models / definitions then those enums are by default inside a namespace with the same name
4+
as your model. This is called declaration merging. However, the [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript)
5+
does not support these namespaces, so if you are using babel in your project please use the `--useUnionTypes` flag
6+
to generate union types instead of traditional enums. More info can be found here: [Enums vs. Union Types](#enums-vs-union-types---useuniontypes).
7+
8+
**Note:** If you are using Babel 7 and Typescript 3.8 (or higher) then you should enable the `onlyRemoveTypeImports` to
9+
ignore any 'type only' imports, see https://babeljs.io/docs/en/babel-preset-typescript#onlyremovetypeimports for more info
10+
11+
```javascript
12+
module.exports = {
13+
presets: [
14+
['@babel/preset-typescript', {
15+
onlyRemoveTypeImports: true,
16+
}],
17+
],
18+
};
19+
```

Diff for: docs/basic-usage.md

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Basic usage
2+
3+
```
4+
$ openapi --help
5+
6+
Usage: openapi [options]
7+
8+
Options:
9+
-V, --version output the version number
10+
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
11+
-o, --output <value> Output directory (required)
12+
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
13+
--name <value> Custom client class name
14+
--useOptions Use options instead of arguments
15+
--useUnionTypes Use union types instead of enums
16+
--exportCore <value> Write core files to disk (default: true)
17+
--exportServices <value> Write services to disk (default: true)
18+
--exportModels <value> Write models to disk (default: true)
19+
--exportSchemas <value> Write schemas to disk (default: false)
20+
--indent <value> Indentation options [4, 2, tab] (default: "5")
21+
--postfix <value> Service name postfix (default: "Service")
22+
--request <value> Path to custom request file
23+
-h, --help display help for command
24+
25+
Examples
26+
$ openapi --input ./spec.json --output ./generated
27+
```
28+
29+
30+
## Example
31+
32+
**package.json**
33+
```json
34+
{
35+
"scripts": {
36+
"generate": "openapi --input ./spec.json --output ./generated"
37+
}
38+
}
39+
```
40+
41+
**NPX**
42+
43+
```
44+
npx openapi-typescript-codegen --input ./spec.json --output ./generated
45+
```
46+
47+
**Node.js**
48+
49+
```javascript
50+
const OpenAPI = require('openapi-typescript-codegen');
51+
52+
OpenAPI.generate({
53+
input: './spec.json',
54+
output: './generated',
55+
});
56+
57+
// Or by providing the content of the spec directly 🚀
58+
OpenAPI.generate({
59+
input: require('./spec.json'),
60+
output: './generated',
61+
});
62+
```

Diff for: docs/client-instances.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Client instances
2+
3+
**Flag:** `--name`
4+
5+
The OpenAPI generator allows creation of client instances to support the multiple backend services use case.
6+
The generated client uses an instance of the server configuration and not the global `OpenAPI` constant.
7+
To generate a client instance, set a custom name to the client class, use `--name` option.
8+
9+
```
10+
openapi --input ./spec.json --output ./generated ---name AppClient
11+
```
12+
13+
The generated client will be exported from the `index` file and can be used as shown below:
14+
15+
```typescript
16+
// Create the client instance with server and authentication details
17+
const appClient = new AppClient({
18+
BASE: 'http://server-host.com',
19+
TOKEN: '1234',
20+
});
21+
22+
// Use the client instance to make the API call
23+
const response = await appClient.organizations.createOrganization({
24+
name: 'OrgName',
25+
description: 'OrgDescription',
26+
});
27+
```

Diff for: docs/custom-enums.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Enum with custom names and descriptions
2+
3+
You can use `x-enum-varnames` and `x-enum-descriptions` in your spec to generate enum with custom names and descriptions.
4+
It's not in official [spec](https://github.com/OAI/OpenAPI-Specification/issues/681) yet. But it's a supported extension
5+
that can help developers use more meaningful enumerators.
6+
```json
7+
{
8+
"EnumWithStrings": {
9+
"description": "This is a simple enum with strings",
10+
"enum": [
11+
0,
12+
1,
13+
2
14+
],
15+
"x-enum-varnames": [
16+
"Success",
17+
"Warning",
18+
"Error"
19+
],
20+
"x-enum-descriptions": [
21+
"Used when the status of something is successful",
22+
"Used when the status of something has a warning",
23+
"Used when the status of something has an error"
24+
]
25+
}
26+
}
27+
```
28+
29+
Generated code:
30+
```typescript
31+
enum EnumWithStrings {
32+
/*
33+
* Used when the status of something is successful
34+
*/
35+
Success = 0,
36+
/*
37+
* Used when the status of something has a warning
38+
*/
39+
Waring = 1,
40+
/*
41+
* Used when the status of something has an error
42+
*/
43+
Error = 2,
44+
}
45+
```

Diff for: docs/enum-vs-union-types.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Enums vs. Union types
2+
3+
**Flag:** `--useUnionTypes`
4+
5+
The OpenAPI spec allows you to define [enums](https://swagger.io/docs/specification/data-models/enums/) inside the
6+
data model. By default, we convert these enums definitions to [TypeScript enums](https://www.typescriptlang.org/docs/handbook/enums.html).
7+
However, these enums are merged inside the namespace of the model, this is unsupported by Babel, [see docs](https://babeljs.io/docs/en/babel-plugin-transform-typescript#impartial-namespace-support).
8+
Because we also want to support projects that use Babel [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript),
9+
we offer the flag `--useUnionTypes` to generate [union types](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-types)
10+
instead of the traditional enums. The difference can be seen below:
11+
12+
**Enums:**
13+
```typescript
14+
// Model
15+
export type Order = {
16+
id?: number;
17+
quantity?: number;
18+
status?: Order.status;
19+
};
20+
21+
export namespace Order {
22+
export enum status {
23+
PLACED = 'placed',
24+
APPROVED = 'approved',
25+
DELIVERED = 'delivered',
26+
}
27+
}
28+
29+
// Usage
30+
const order: Order = {
31+
id: 1,
32+
quantity: 40,
33+
status: Order.status.PLACED,
34+
};
35+
```
36+
37+
**Union Types:**
38+
```typescript
39+
// Model
40+
export type Order = {
41+
id?: number;
42+
quantity?: number;
43+
status?: 'placed' | 'approved' | 'delivered';
44+
};
45+
46+
// Usage
47+
const order: Order = {
48+
id: 1,
49+
quantity: 40,
50+
status: 'placed',
51+
};
52+
```

Diff for: docs/external-references.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# External references
2+
3+
Local references to schema definitions (those beginning with `#/definitions/schemas/`)
4+
will be converted to type references to the equivalent, generated top-level type.
5+
6+
The OpenAPI generator also supports external references, which allows you to break
7+
down your openapi.yml into multiple sub-files, or incorporate third-party schemas
8+
as part of your types to ensure everything is able to be TypeScript generated.
9+
10+
External references may be:
11+
* *relative references* - references to other files at the same location e.g.
12+
`{ $ref: 'schemas/customer.yml' }`
13+
* *remote references* - fully qualified references to another remote location
14+
e.g. `{ $ref: 'https://myexampledomain.com/schemas/customer_schema.yml' }`
15+
16+
For remote references, both files (when the file is on the current filesystem)
17+
and http(s) URLs are supported.
18+
19+
External references may also contain internal paths in the external schema (e.g.
20+
`schemas/collection.yml#/definitions/schemas/Customer`) and back-references to
21+
the base openapi file or between files (so that you can reference another
22+
schema in the main file as a type of an object or array property, for example).
23+
24+
At start-up, an OpenAPI or Swagger file with external references will be "bundled",
25+
so that all external references and back-references will be resolved (but local
26+
references preserved).

Diff for: docs/node-fetch-support.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Node-Fetch support
2+
3+
By default, this tool will generate a client that is compatible with the (browser based) Fetch API.
4+
However, this client will not work inside the Node.js environment. If you want to generate the Node.js compatible
5+
client then you can specify `--client node` in the openapi call:
6+
7+
`openapi --input ./spec.json --output ./generated --client node`
8+
9+
This will generate a client that uses [`node-fetch`](https://www.npmjs.com/package/node-fetch) internally. However,
10+
in order to compile and run this client, you might need to install the `[email protected]` dependencies.
11+
12+
> Since version 3.x [`node-fetch`](https://www.npmjs.com/package/node-fetch) switched to ESM only,
13+
> breaking many CommonJS based toolchains (like Jest). Right now we do not support this new version!
14+
15+
```
16+
npm install @types/[email protected] --save-dev
17+
npm install [email protected] --save-dev
18+
npm install [email protected] --save-dev
19+
npm install [email protected] --save-dev
20+
```
21+
22+
In order to compile the project and resolve the imports, you will need to enable the `allowSyntheticDefaultImports`
23+
in your `tsconfig.json` file.

Diff for: docs/nullable-props.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Nullable props (OpenAPI v2)
2+
3+
In the OpenAPI v3 spec you can create properties that can be `NULL`, by providing a `nullable: true` in your schema.
4+
However, the v2 spec does not allow you to do this. You can use the unofficial `x-nullable` in your specification
5+
to generate nullable properties in OpenApi v2.
6+
7+
```json
8+
{
9+
"ModelWithNullableString": {
10+
"required": [
11+
"requiredProp"
12+
],
13+
"description": "This is a model with one string property",
14+
"type": "object",
15+
"properties": {
16+
"prop": {
17+
"description": "This is a simple string property",
18+
"type": "string",
19+
"x-nullable": true
20+
},
21+
"requiredProp": {
22+
"description": "This is a simple string property",
23+
"type": "string",
24+
"x-nullable": true
25+
}
26+
}
27+
}
28+
}
29+
```
30+
31+
Generated code:
32+
```typescript
33+
export type ModelWithNullableString = {
34+
prop?: string | null;
35+
requiredProp: string | null;
36+
};
37+
```

0 commit comments

Comments
 (0)