Skip to content

Commit 1ba5e5f

Browse files
authored
Expose new signal configuration for timeout (#484)
* Expose new signal config * Add doc
1 parent 29a584e commit 1ba5e5f

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

.generator/templates/http/http.mustache

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ export * from './jquery';
2525
{{/deno}}
2626
{{/platforms}}
2727

28+
/**
29+
* Interface for aborting fetch requests.
30+
*/
31+
export interface AbortSignal {
32+
aborted: boolean;
33+
34+
addEventListener: (type: "abort", listener: ((this: AbortSignal, event: any) => any), options?: boolean | {
35+
capture?: boolean,
36+
once?: boolean,
37+
passive?: boolean
38+
}) => void;
39+
40+
removeEventListener: (type: "abort", listener: ((this: AbortSignal, event: any) => any), options?: boolean | {
41+
capture?: boolean
42+
}) => void;
43+
44+
dispatchEvent: (event: any) => boolean;
45+
46+
onabort?: null | ((this: AbortSignal, event: any) => void);
47+
}
48+
2849
/**
2950
* Represents an HTTP method.
3051
*/
@@ -109,6 +130,7 @@ export type RequestBody = undefined | string | Buffer | FormData;
109130
*/
110131
export interface HttpConfiguration {
111132
compress?: boolean;
133+
signal?: AbortSignal;
112134
}
113135

114136
/**

.generator/templates/http/isomorphic-fetch.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
3535
body: body as any,
3636
headers: headers,
3737
compress: compress,
38+
signal: request.getHttpConfig().signal,
3839
{{#platforms}}
3940
{{#browser}}
4041
credentials: "same-origin"

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,37 @@ const configurationOpts = {
9292
const configuration = v1.createConfiguration(configurationOpts);
9393
```
9494

95+
### Adding timeout to requests
96+
97+
To add timeout or other mechanism to cancel requests, you need an abort
98+
controller, for example the one implemented by
99+
[abort-controller](https://www.npmjs.com/package/abort-controller). You can
100+
then pass the `signal method to the HTTP configuration options:
101+
102+
```typescript
103+
import { v1 } from '@datadog/datadog-api-client';
104+
import AbortController from 'abort-controller';
105+
106+
const controller = new AbortController();
107+
const timeout = setTimeout(
108+
() => { controller.abort(); },
109+
1000,
110+
);
111+
const configurationOpts = {
112+
httpConfig: {
113+
signal: controller.signal
114+
},
115+
};
116+
117+
const configuration = v1.createConfiguration(configurationOpts);
118+
119+
const apiInstance = new v1.MonitorsApi(configuration);
120+
apiInstance.listMonitors().then((data:any) => {
121+
console.log('API called successfully. Returned data: ' + data);
122+
}).catch((error:any) => console.error(error)).finally(() => clearTimeout(timeout));
123+
```
124+
125+
95126
## Documentation
96127

97128
Documentation for API endpoints can be found under the docs subdirectories, in [v1](/docs/v1/)

packages/datadog-api-client-v1/http/http.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@ import { Observable, from } from "../rxjsStub";
66

77
export * from "./isomorphic-fetch";
88

9+
/**
10+
* Interface for aborting fetch requests.
11+
*/
12+
export interface AbortSignal {
13+
aborted: boolean;
14+
15+
addEventListener: (
16+
type: "abort",
17+
listener: (this: AbortSignal, event: any) => any,
18+
options?:
19+
| boolean
20+
| {
21+
capture?: boolean;
22+
once?: boolean;
23+
passive?: boolean;
24+
}
25+
) => void;
26+
27+
removeEventListener: (
28+
type: "abort",
29+
listener: (this: AbortSignal, event: any) => any,
30+
options?:
31+
| boolean
32+
| {
33+
capture?: boolean;
34+
}
35+
) => void;
36+
37+
dispatchEvent: (event: any) => boolean;
38+
39+
onabort?: null | ((this: AbortSignal, event: any) => void);
40+
}
41+
942
/**
1043
* Represents an HTTP method.
1144
*/
@@ -45,6 +78,7 @@ export type RequestBody = undefined | string | Buffer | FormData;
4578
*/
4679
export interface HttpConfiguration {
4780
compress?: boolean;
81+
signal?: AbortSignal;
4882
}
4983

5084
/**

packages/datadog-api-client-v1/http/isomorphic-fetch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
2727
body: body as any,
2828
headers: headers,
2929
compress: compress,
30+
signal: request.getHttpConfig().signal,
3031
}).then((resp: any) => {
3132
const headers: { [name: string]: string } = {};
3233
resp.headers.forEach((value: string, name: string) => {

packages/datadog-api-client-v2/http/http.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@ import { Observable, from } from "../rxjsStub";
66

77
export * from "./isomorphic-fetch";
88

9+
/**
10+
* Interface for aborting fetch requests.
11+
*/
12+
export interface AbortSignal {
13+
aborted: boolean;
14+
15+
addEventListener: (
16+
type: "abort",
17+
listener: (this: AbortSignal, event: any) => any,
18+
options?:
19+
| boolean
20+
| {
21+
capture?: boolean;
22+
once?: boolean;
23+
passive?: boolean;
24+
}
25+
) => void;
26+
27+
removeEventListener: (
28+
type: "abort",
29+
listener: (this: AbortSignal, event: any) => any,
30+
options?:
31+
| boolean
32+
| {
33+
capture?: boolean;
34+
}
35+
) => void;
36+
37+
dispatchEvent: (event: any) => boolean;
38+
39+
onabort?: null | ((this: AbortSignal, event: any) => void);
40+
}
41+
942
/**
1043
* Represents an HTTP method.
1144
*/
@@ -45,6 +78,7 @@ export type RequestBody = undefined | string | Buffer | FormData;
4578
*/
4679
export interface HttpConfiguration {
4780
compress?: boolean;
81+
signal?: AbortSignal;
4882
}
4983

5084
/**

packages/datadog-api-client-v2/http/isomorphic-fetch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
2727
body: body as any,
2828
headers: headers,
2929
compress: compress,
30+
signal: request.getHttpConfig().signal,
3031
}).then((resp: any) => {
3132
const headers: { [name: string]: string } = {};
3233
resp.headers.forEach((value: string, name: string) => {

0 commit comments

Comments
 (0)