Skip to content

Commit

Permalink
feat: Add configurable request timeout (#341)
Browse files Browse the repository at this point in the history
Adds the ability for the client to configure the timeout (default is still 300 seconds)
  • Loading branch information
mseyma authored and jdalrymple committed Jun 8, 2019
1 parent ea36dea commit 4d99902
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const api = new Gitlab({
version = 'v4', //API Version ID. Optional, Default: v4
camelize = false, //Response Key Camelize. Camelizes all response body keys. Optional, Default: false
requester = KyRequester, //Request Library Wrapper. Optional, Default: Currently wraps Ky.
requestTimeout = 300000 //Request Library Timeout. Optional, Default: 300,000 milliseconds.
});
```

Expand Down
3 changes: 3 additions & 0 deletions src/infrastructure/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { KyRequester } from './KyRequester';
export class BaseService {
public readonly url: string;
public readonly requester: Requester;
public readonly requestTimeout: number;
public readonly headers: { [header: string]: string };
public readonly camelize: boolean;
public readonly rejectUnauthorized: boolean;
Expand All @@ -18,12 +19,14 @@ export class BaseService {
camelize = false,
rejectUnauthorized = true,
requester = KyRequester,
requestTimeout = 300000
}: BaseServiceOptions) {
this.url = [host, 'api', version, url].join('/');
this.headers = {};
this.rejectUnauthorized = rejectUnauthorized;
this.camelize = camelize;
this.requester = requester;
this.requestTimeout = requestTimeout;

// Handle auth tokens
if (oauthToken) this.headers.authorization = `Bearer ${oauthToken}`;
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/KyRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function defaultRequest(service: any, { body, query, sudo, method }) {
if (sudo) headers.append('sudo', `${sudo}`);

return {
timeout: 300000,
timeout: service.requestTimeout,
headers,
method: (method === 'stream') ? 'get' : method,
onProgress: (method === 'stream') ? () => {} : undefined,
Expand Down
2 changes: 2 additions & 0 deletions test/unit/services/Pipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let service: Pipelines;
beforeEach(() => {
service = new Pipelines({
token: 'abcdefg',
requestTimeout: 3000,
});
});

Expand All @@ -31,6 +32,7 @@ describe('Instantiating Pipelines service', () => {
expect(service.url).toBeDefined();
expect(service.rejectUnauthorized).toBeTruthy();
expect(service.headers).toMatchObject({ 'private-token': 'abcdefg' });
expect(service.requestTimeout).toBe(3000);
});
});

Expand Down
1 change: 1 addition & 0 deletions typings/infrastructure.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface BaseServiceOptions extends Sudo {
rejectUnauthorized?: boolean;
camelize?: boolean;
requester?: Requester;
requestTimeout?: number;
}

// RequestHelper
Expand Down

0 comments on commit 4d99902

Please sign in to comment.