-
-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Canceling requests | ||
|
||
The generated clients support canceling of requests, this works by canceling the promise that | ||
is returned from the request. Each method inside a service (operation) returns a `CancelablePromise` | ||
object. This promise can be canceled by calling the `cancel()` method. | ||
|
||
Below is an example of canceling the request after a certain timeout: | ||
|
||
```typescript | ||
import { UserService } from './myClient'; | ||
|
||
const getAllUsers = async () => { | ||
|
||
const request = UserService.getAllUsers(); | ||
|
||
setTimeout(() => { | ||
if (!request.isResolved() && !request.isRejected()) { | ||
console.warn('Canceling request due to timeout'); | ||
request.cancel(); | ||
} | ||
}, 1000); | ||
|
||
await request; | ||
}; | ||
``` | ||
|
||
The API of the `CancelablePromise` is similar to a regular `Promise`, but it adds the | ||
`cancel()` method and some additional properties: | ||
|
||
```typescript | ||
interface CancelablePromise<TResult> extends Promise<TResult> { | ||
readonly isResolved: boolean; | ||
readonly isRejected: boolean; | ||
readonly isCancelled: boolean; | ||
cancel: () => void; | ||
} | ||
``` | ||
|
||
- `isResolved`: Indicates if the promise was resolved. | ||
- `isRejected`: Indicates if the promise was rejected. | ||
- `isCancelled`: Indicates if the promise was canceled. | ||
- `cancel()`: Cancels the promise (and request) and throws a rejection error: `Request aborted`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Custom request file | ||
|
||
If you want to implement custom logic on the request level, | ||
or implement a client based on a different library, then | ||
one option is to write your own request file and tell | ||
the generator to use this. | ||
|
||
The request file (`request.ts`) can be found inside the | ||
`/core` folder of the generated client. You can modify | ||
that file and use it, or alternatively, you can write | ||
your own. Below is a very simplified example of an Axios | ||
based request file: | ||
|
||
```typescript | ||
import axios from 'axios'; | ||
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
|
||
import type { ApiRequestOptions } from './ApiRequestOptions'; | ||
import { CancelablePromise } from './CancelablePromise'; | ||
import type { OpenAPIConfig } from './OpenAPI'; | ||
|
||
const axiosInstance = axios.create({ | ||
// Your custom Axios instance config | ||
}); | ||
|
||
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => { | ||
return new CancelablePromise((resolve, reject, onCancel) => { | ||
// Get the request URL. Depending on your needs, this might need additional processing, | ||
// @see ./src/templates/core/functions/getUrl.hbs | ||
const url = `${config.BASE}${options.path}`; | ||
|
||
// Optional: Get and link the cancelation token, so the request can be aborted. | ||
const source = axiosInstance.CancelToken.source(); | ||
onCancel(() => source.cancel('The user aborted a request.')); | ||
|
||
// Execute the request. This is a minimal example, in real world scenarios | ||
// you will need to add headers, process form data, etc. | ||
// @see ./src/templates/core/axios/request.hbs | ||
axiosInstance.request({ | ||
url, | ||
data: options.body, | ||
method: options.method, | ||
cancelToken: source.token, | ||
}).then(data => { | ||
resolve(data); | ||
}).catch(error => { | ||
reject(error); | ||
}); | ||
}); | ||
}; | ||
``` | ||
|
||
To use this request file in your generated code you can execute the | ||
following command: | ||
|
||
``` | ||
npx openapi-typescript-codegen --input ./spec.json --output ./generated --request ./request.ts | ||
``` | ||
|
||
The `--request` parameter will tell the generator to not generate the default | ||
`request.ts` file, but instead copy over the custom file that was specified. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters