Skip to content

Commit

Permalink
fix: restored append to user agent (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck authored Aug 9, 2023
1 parent 3398e96 commit dcab7ab
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/server-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The constructor for the client takes in two parameters `credentials` and `option
* `apiHost: string` (optional) - Allows overwriting the default `https://api.nexmo.com`.
* `videoHost: string` (optional) - Allows overwriting the default `https://video.api.vonage.com`.
* `timeout: int` (optional) - Set a custom timeout for requests to Vonage in milliseconds. Defaults to the standard for Node http requests, which is 120,000 ms.
* `appendUserAgent: string` (optional) - Set a custom string to be added to the `user-agent` header for the request

## Testing

Expand Down
17 changes: 9 additions & 8 deletions packages/server-client/lib/types/ConfigParams.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export type ConfigParams = {
restHost: string
apiHost: string
videoHost: string
responseType: string
timeout: number
proactiveHost: string
meetingsHost: string
}
restHost?: string;
apiHost?: string;
videoHost?: string;
responseType?: string;
timeout?: number;
proactiveHost?: string;
meetingsHost?: string;
appendUserAgent?: string;
};
17 changes: 17 additions & 0 deletions packages/vetch/__tests__/vetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ describe('option configuration', () => {
expect(nock.isDone()).toBeTruthy();
});

test('should append to user agent', async () => {
const options = {
reqheaders: {
'user-agent': (val: string) => {
return /^@vonage\/server-sdk\/[\d].[\d].[\d].* node\/.* foo$/.test(
val,
);
},
},
};

nock(url, options).get('/').reply(200);
const inst = new Vetch();
await inst.request({ url, appendUserAgent: 'foo' });
expect(nock.isDone()).toBeTruthy();
});

test('should timeout', async () => {
nock(url).get('/').delayConnection(5).reply(200);
const inst = new Vetch({ timeout: 1 });
Expand Down
43 changes: 22 additions & 21 deletions packages/vetch/lib/interfaces/vetchOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ import URL from 'url';
import { VetchPromise } from '../types/vetchPromise';

export interface VetchOptions {
adapter?: <T = any>(
options: VetchOptions,
defaultAdapter: (options: VetchOptions) => VetchPromise<T>
) => VetchPromise<T>
url?: string
baseUrl?: string
baseURL?: string
method?: HTTPMethods
headers?: Headers
data?: any
body?: any
params?: any
responseType?: ResponseTypes
checkStatus?: (status: number) => boolean
size?: number
timeout?: number
agent?:
| boolean
| http.Agent
| https.Agent
| ((parsedUrl: URL) => boolean | https.Agent | http.Agent)
adapter?: <T = any>(
options: VetchOptions,
defaultAdapter: (options: VetchOptions) => VetchPromise<T>
) => VetchPromise<T>;
url?: string;
baseUrl?: string;
baseURL?: string;
method?: HTTPMethods;
headers?: Headers;
data?: any;
body?: any;
params?: any;
responseType?: ResponseTypes;
checkStatus?: (status: number) => boolean;
size?: number;
timeout?: number;
appendUserAgent?: string;
agent?:
| boolean
| http.Agent
| https.Agent
| ((parsedUrl: URL) => boolean | https.Agent | http.Agent);
}
8 changes: 5 additions & 3 deletions packages/vetch/lib/vetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ export class Vetch {
}

// Set our user agent
opts.headers[
'user-agent'
] = `@vonage/server-sdk/3.0.0 node/${process.version.replace('v', '')}`;
opts.headers['user-agent'] = [
`@vonage/server-sdk/3.0.0`,
` node/${process.version.replace('v', '')}`,
opts.appendUserAgent ? ` ${opts.appendUserAgent}` : '',
].join('');

// Allow a custom timeout to be used
const httpAgent = new http.Agent({
Expand Down

0 comments on commit dcab7ab

Please sign in to comment.