Skip to content

Commit 56c62af

Browse files
committed
- Added documenation
- Bumped version
1 parent 73bcbca commit 56c62af

5 files changed

+115
-1
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.23.0] - 2022-06-02
5+
### Fixed
6+
- Upgraded dependencies
7+
- Added blank line at the end of generated files
8+
- Added support for Node.js v12
9+
### Added
10+
- Added `request` property inside `ApiError`
11+
- Added support for `@depricated` inside models and operations
12+
413
## [0.22.0] - 2022-04-26
514
### Fixed
615
- Upgraded dependencies

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Documentation
6969
- [Nullable props (OpenAPI v2)](docs/nullable-props.md)
7070
- [Authorization](docs/authorization.md)
7171
- [External references](docs/external-references.md)
72+
- [Canceling requests](docs/canceling-requests.md)
73+
- [Custom request file](docs/custom-request-file.md)
7274

7375
Support
7476
===

docs/canceling-requests.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Canceling requests
2+
3+
The generated clients support canceling of requests, this works by canceling the promise that
4+
is returned from the request. Each method inside a service (operation) returns a `CancelablePromise`
5+
object. This promise can be canceled by calling the `cancel()` method.
6+
7+
Below is an example of canceling the request after a certain timeout:
8+
9+
```typescript
10+
import { UserService } from './myClient';
11+
12+
const getAllUsers = async () => {
13+
14+
const request = UserService.getAllUsers();
15+
16+
setTimeout(() => {
17+
if (!request.isResolved() && !request.isRejected()) {
18+
console.warn('Canceling request due to timeout');
19+
request.cancel();
20+
}
21+
}, 1000);
22+
23+
await request;
24+
};
25+
```
26+
27+
The API of the `CancelablePromise` is similar to a regular `Promise`, but it adds the
28+
`cancel()` method and some additional properties:
29+
30+
```typescript
31+
interface CancelablePromise<TResult> extends Promise<TResult> {
32+
readonly isResolved: boolean;
33+
readonly isRejected: boolean;
34+
readonly isCancelled: boolean;
35+
cancel: () => void;
36+
}
37+
```
38+
39+
- `isResolved`: Indicates if the promise was resolved.
40+
- `isRejected`: Indicates if the promise was rejected.
41+
- `isCancelled`: Indicates if the promise was canceled.
42+
- `cancel()`: Cancels the promise (and request) and throws a rejection error: `Request aborted`.

docs/custom-request-file.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Custom request file
2+
3+
If you want to implement custom logic on the request level,
4+
or implement a client based on a different library, then
5+
one option is to write your own request file and tell
6+
the generator to use this.
7+
8+
The request file (`request.ts`) can be found inside the
9+
`/core` folder of the generated client. You can modify
10+
that file and use it, or alternatively, you can write
11+
your own. Below is a very simplified example of an Axios
12+
based request file:
13+
14+
```typescript
15+
import axios from 'axios';
16+
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
17+
18+
import type { ApiRequestOptions } from './ApiRequestOptions';
19+
import { CancelablePromise } from './CancelablePromise';
20+
import type { OpenAPIConfig } from './OpenAPI';
21+
22+
const axiosInstance = axios.create({
23+
// Your custom Axios instance config
24+
});
25+
26+
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
27+
return new CancelablePromise((resolve, reject, onCancel) => {
28+
// Get the request URL. Depending on your needs, this might need additional processing,
29+
// @see ./src/templates/core/functions/getUrl.hbs
30+
const url = `${config.BASE}${options.path}`;
31+
32+
// Optional: Get and link the cancelation token, so the request can be aborted.
33+
const source = axiosInstance.CancelToken.source();
34+
onCancel(() => source.cancel('The user aborted a request.'));
35+
36+
// Execute the request. This is a minimal example, in real world scenarios
37+
// you will need to add headers, process form data, etc.
38+
// @see ./src/templates/core/axios/request.hbs
39+
axiosInstance.request({
40+
url,
41+
data: options.body,
42+
method: options.method,
43+
cancelToken: source.token,
44+
}).then(data => {
45+
resolve(data);
46+
}).catch(error => {
47+
reject(error);
48+
});
49+
});
50+
};
51+
```
52+
53+
To use this request file in your generated code you can execute the
54+
following command:
55+
56+
```
57+
npx openapi-typescript-codegen --input ./spec.json --output ./generated --request ./request.ts
58+
```
59+
60+
The `--request` parameter will tell the generator to not generate the default
61+
`request.ts` file, but instead copy over the custom file that was specified.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.22.0",
3+
"version": "0.23.0",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

0 commit comments

Comments
 (0)