Skip to content

Commit

Permalink
feat: http request timeout option (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored May 28, 2023
1 parent 9d9c549 commit 54fd697
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,21 @@ async function test() {
test();
```

### Http request timeout

By default request timeout will vary on http client implementation and/or environment (e.g. `fetch` uses timeout configured by the browser).
But there is an option to set constant value:

```javascript
const crowdin = require('@crowdin/crowdin-api-client');

const credentials = { token: 'token' };

const httpRequestTimeout = 60 * 1000; // 60 seconds

const client = new crowdin.default(credentials, { httpRequestTimeout });
```

## Over-The-Air Content Delivery

:dizzy: Recommended for translations delivery to your website or mobile application.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crowdin/crowdin-api-client",
"version": "1.23.0",
"version": "1.23.1",
"description": "JavaScript library for Crowdin API",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
11 changes: 9 additions & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export interface ClientConfig {

/** Retry strategy configuration */
retryConfig?: RetryConfig;

/** Http request timeout in ms */
httpRequestTimeout?: number;
}

export interface ResponseList<T> {
Expand Down Expand Up @@ -228,6 +231,11 @@ export abstract class CrowdinApi {
}
this.retryService = new RetryService(retryConfig);

if (config?.httpRequestTimeout) {
CrowdinApi.FETCH_INSTANCE.withTimeout(config?.httpRequestTimeout);
CrowdinApi.AXIOS_INSTANCE.defaults.timeout = config?.httpRequestTimeout;
}

this.config = config;
}

Expand Down Expand Up @@ -286,8 +294,7 @@ export abstract class CrowdinApi {
return CrowdinApi.AXIOS_INSTANCE;
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
public withFetchAll(maxLimit?: number) {
public withFetchAll(maxLimit?: number): this {
this.fetchAllFlag = true;
this.maxLimit = maxLimit;
return this;
Expand Down
27 changes: 22 additions & 5 deletions src/core/internal/fetch/fetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export class FetchClient implements HttpClient {
private requestIntervalMs = 10;
private pendingRequests = 0;

private timeout: number | undefined;

withTimeout(timeout?: number): this {
this.timeout = timeout;
return this;
}

get<T>(url: string, config?: { headers: Record<string, string> }): Promise<T> {
return this.request(url, 'GET', config);
}
Expand Down Expand Up @@ -52,11 +59,21 @@ export class FetchClient implements HttpClient {
}
await this.waitInQueue();

return fetch(url, {
method: method,
headers: config ? config.headers : {},
body: body,
})
let request;
const headers = config ? config.headers : {};

if (this.timeout) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
request = fetch(url, { method, headers, body, signal: controller.signal }).then(res => {
clearTimeout(timeoutId);
return res;
});
} else {
request = fetch(url, { method, headers, body });
}

return request
.then(async res => {
if (res.status === 204) {
return {};
Expand Down

0 comments on commit 54fd697

Please sign in to comment.