Skip to content

Commit 71fd3ed

Browse files
committed
- Updated documentation
1 parent 392fb67 commit 71fd3ed

9 files changed

+129
-59
lines changed

Diff for: README.md

-5
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
- Supports aborting of requests (cancelable promise pattern)
2323
- Supports external references using [json-schema-ref-parser](https://github.com/APIDevTools/json-schema-ref-parser/)
2424

25-
2625
## Install
2726

2827
```
2928
npm install openapi-typescript-codegen --save-dev
3029
```
3130

32-
3331
## Usage
3432

3533
```
@@ -59,7 +57,6 @@ $ openapi --help
5957
$ openapi --input ./spec.json --output ./generated --client xhr
6058
```
6159

62-
6360
Documentation
6461
===
6562
- [Basic usage](docs/basic-usage.md)
@@ -73,15 +70,13 @@ Documentation
7370
- [Authorization](docs/authorization.md)
7471
- [External references](docs/external-references.md)
7572

76-
7773
Support
7874
===
7975
- [Babel support](docs/babel-support.md)
8076
- [Axios support](docs/axios-support.md)
8177
- [Angular support](docs/angular-support.md)
8278
- [Node-Fetch support](docs/node-fetch-support.md)
8379

84-
8580
[npm-url]: https://npmjs.org/package/openapi-typescript-codegen
8681
[npm-image]: https://img.shields.io/npm/v/openapi-typescript-codegen.svg
8782
[license-url]: LICENSE

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

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ There's no [named parameter](https://en.wikipedia.org/wiki/Named_parameter) in J
66
that, we offer the flag `--useOptions` to generate code in two different styles.
77

88
**Argument style:**
9+
910
```typescript
1011
const createUser = (name: string, password: string, type?: string, address?: string) => {
1112
// ...
@@ -16,6 +17,7 @@ createUser('Jack', '123456', undefined, 'NY US');
1617
```
1718

1819
**Object style:**
20+
1921
```typescript
2022
const createUser = ({ name, password, type, address }: {
2123
name: string,

Diff for: docs/basic-usage.md

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ $ openapi --help
2626
$ openapi --input ./spec.json --output ./generated
2727
```
2828

29-
3029
## Example
3130

3231
**package.json**

Diff for: docs/custom-enums.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
You can use `x-enum-varnames` and `x-enum-descriptions` in your spec to generate enum with custom names and descriptions.
44
It's not in official [spec](https://github.com/OAI/OpenAPI-Specification/issues/681) yet. But it's a supported extension
55
that can help developers use more meaningful enumerators.
6+
67
```json
78
{
89
"EnumWithStrings": {
@@ -27,6 +28,7 @@ that can help developers use more meaningful enumerators.
2728
```
2829

2930
Generated code:
31+
3032
```typescript
3133
enum EnumWithStrings {
3234
/*

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ we offer the flag `--useUnionTypes` to generate [union types](https://www.typesc
1010
instead of the traditional enums. The difference can be seen below:
1111

1212
**Enums:**
13+
1314
```typescript
1415
// Model
1516
export type Order = {
@@ -35,6 +36,7 @@ const order: Order = {
3536
```
3637

3738
**Union Types:**
39+
3840
```typescript
3941
// Model
4042
export type Order = {

Diff for: docs/external-references.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ down your openapi.yml into multiple sub-files, or incorporate third-party schema
88
as part of your types to ensure everything is able to be TypeScript generated.
99

1010
External references may be:
11+
1112
* *relative references* - references to other files at the same location e.g.
1213
`{ $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' }`
1514

16-
For remote references, both files (when the file is on the current filesystem)
17-
and http(s) URLs are supported.
15+
* *remote references* - fully qualified references to another remote location e.g.
16+
`{ $ref: 'https://myexampledomain.com/schemas/customer_schema.yml' }`
17+
18+
For remote references, both files (when the file is on the current filesystem)
19+
and http(s) URLs are supported.
1820

1921
External references may also contain internal paths in the external schema (e.g.
2022
`schemas/collection.yml#/definitions/schemas/Customer`) and back-references to

Diff for: docs/nullable-props.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ to generate nullable properties in OpenApi v2.
2929
```
3030

3131
Generated code:
32+
3233
```typescript
3334
export type ModelWithNullableString = {
3435
prop?: string | null;

Diff for: docs/openapi-object.md

+116-21
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ below you can find the properties and their usage.
77

88
```typescript
99
export const OpenAPI: OpenAPIConfig = {
10-
BASE: 'http://localhost:3000/my-api',
11-
VERSION: '1.0',
10+
BASE: 'http://localhost:3000/api',
11+
VERSION: '2.0',
1212
WITH_CREDENTIALS: false,
1313
CREDENTIALS: 'include',
1414
TOKEN: undefined,
@@ -19,31 +19,126 @@ export const OpenAPI: OpenAPIConfig = {
1919
};
2020
```
2121

22-
## Properties
22+
Properties
23+
===
2324

24-
`OpenAPI.BASE`
25-
Test
25+
### `OpenAPI.BASE`
2626

27-
`OpenAPI.VERSION`
28-
Test
27+
The base path of the OpenAPI server, this is generated from the spec,
28+
but can be overwritten to switch servers.
2929

30-
`OpenAPI.WITH_CREDENTIALS`
31-
Test
30+
```typescript
31+
if (process.env === 'development') {
32+
OpenAPI.BASE = 'http://staging.company.com:3000/api';
33+
}
34+
if (process.env === 'production') {
35+
OpenAPI.BASE = '/api';
36+
}
37+
```
38+
39+
### `OpenAPI.VERSION`
40+
41+
The version param in the OpenAPI paths `{api-version}`. The version is taken from the spec,
42+
but can be updated to call multiple versions of the same OpenAPI backend.
43+
44+
### `OpenAPI.WITH_CREDENTIALS`
45+
46+
Similar to the [withCredentials](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)
47+
property of the XHR specification. When set to true, cross-site requests should be made
48+
using credentials such as cookies, authorization headers, etc.
49+
50+
### `OpenAPI.CREDENTIALS`
51+
52+
Similar to the [credentials](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#sending_a_request_with_credentials_included)
53+
property of the Fetch specification. When `OpenAPI.WITH_CREDENTIALS` is set to true,
54+
this property controls the specific implementation for Fetch and Node-Fetch clients.
55+
Valid values are: `include`, `omit` and `same-origin`.
56+
57+
### `OpenAPI.TOKEN`
58+
59+
Set the Bearer authentication token to use for the requests. This needs to be a valid
60+
(non-expired) token, otherwise the request will fail. The property can be updated as often
61+
as you want, this is useful for scenario's where the token would automatically refresh
62+
after x minutes. This property also allows you to use an `async` method that will be resolved
63+
before requests are made.
64+
65+
```typescript
66+
OpenAPI.TOKEN = 'MY_TOKEN';
67+
68+
OpenAPI.TOKEN = async () => {
69+
// Note: loading this from a JSON file is not recommended ;-)
70+
const response = await fetch('configuration.json');
71+
const { token } = response.json();
72+
return token;
73+
};
74+
```
75+
76+
### `OpenAPI.USERNAME`
77+
78+
Set the basic authentication username, although not recommended, the basic authentication
79+
header is still supported. The username and password hash will be calculated by the client
80+
before sending the request. This property also allows you to use an `async` method that
81+
will be resolved before requests are made.
82+
83+
```typescript
84+
OpenAPI.USERNAME = 'john';
3285

33-
`OpenAPI.CREDENTIALS`
34-
Test
86+
OpenAPI.USERNAME = async () => {
87+
// Note: loading this from a JSON file is not recommended ;-)
88+
const response = await fetch('configuration.json');
89+
const { username } = response.json();
90+
return username;
91+
};
92+
```
93+
94+
### `OpenAPI.PASSWORD`
95+
96+
Set the basic authentication password. See `OpenAPI.USERNAME` for more info.
97+
98+
```typescript
99+
OpenAPI.PASSWORD = 'welcome123';
35100

36-
`OpenAPI.TOKEN`
37-
Test
101+
OpenAPI.PASSWORD = async () => {
102+
// Note: loading this from a JSON file is not recommended ;-)
103+
const response = await fetch('configuration.json');
104+
const { password } = response.json();
105+
return password;
106+
};
107+
```
38108

39-
`OpenAPI.USERNAME`
40-
Test
109+
### `OpenAPI.HEADERS`
41110

42-
`OpenAPI.PASSWORD`
43-
Test
111+
This property allows you to specify additional headers to send for each request. This can be useful
112+
for adding headers that are not generated through the spec. Or adding headers for tracking purposes.
113+
This property also allows you to use an `async` method that will be resolved before requests are made.
44114

45-
`OpenAPI.HEADERS`
46-
Test
115+
```typescript
116+
OpenAPI.HEADERS = {
117+
'x-navigator': window.navigator.appVersion,
118+
'x-environment': process.env,
119+
'last-modified': 'Wed, 21 Oct 2015 07:28:00 GMT',
120+
};
47121

48-
`OpenAPI.ENCODE_PATH`
49-
Test
122+
OpenAPI.HEADERS = async () => {
123+
// Note: loading this from a JSON file is not recommended ;-)
124+
const response = await fetch('configuration.json');
125+
const { headers } = response.json();
126+
return headers;
127+
};
128+
```
129+
130+
### `OpenAPI.ENCODE_PATH`
131+
132+
By default, all path parameters are encoded using the `encodeURI` method. This will convert invalid
133+
URL characters, for example spaces, backslashes, etc. However, you might want to make the encoding
134+
more strict due to security restrictions. So you can set this to `encodeURIComponent` to encode
135+
most non-alphanumerical characters to percentage encoding. Or set a customer encoder that just
136+
replaces some special characters.
137+
138+
```typescript
139+
OpenAPI.ENCODE_PATH = encodeURIComponent;
140+
141+
OpenAPI.ENCODE_PATH = (value: string) => {
142+
return value.replace(':', '_');
143+
};
144+
```

Diff for: test/spec/aap.json

-28
This file was deleted.

0 commit comments

Comments
 (0)