Skip to content

Commit 090dc21

Browse files
committed
feat: gotOptions to override default Got configuration
1 parent 0be1e0c commit 090dc21

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

CHANGELOG.MD

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- added the `gotOptions` property to the `BlockFrostAPI` class. This property can be passed during the initialization of the `BlockFrostAPI` object. For more details, refer to the [Got Options documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md).
13+
1014

1115
## [5.5.0] - 2023-12-20
1216

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const API = new Blockfrost.BlockFrostAPI({
6767
- `debug` - `boolean`, whether to enable debug logging. It is also possible to enable it by setting environment variable `DEBUG` to `true` (optional, default `false`).
6868
- `customBackend` - `string`, option to set URL to a non-official backend (optional)
6969
- `version` - `number`, version of the Blockfrost API (optional, default `0`)
70+
- `gotOptions` - Additional options to be passed to Got instance. For more details, refer to the [Got Options documentation](https://github.com/sindresorhus/got/blob/main/documentation/2-options.md).
71+
7072

7173
## Error handling
7274

src/types/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
MaxRedirectsError,
1111
UnsupportedProtocolError,
1212
RequiredRetryOptions,
13+
Options as GotOptions,
1314
} from 'got';
1415
import { RateLimiterConfig } from '../utils/limiter';
1516

@@ -44,6 +45,7 @@ type AdditionalOptions = {
4445
userAgent?: string;
4546
requestTimeout?: number;
4647
retrySettings?: RequiredRetryOptions;
48+
gotOptions?: GotOptions; // https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
4749
};
4850

4951
export type Options = (OptionCombination1 | OptionCombination2) &
@@ -61,6 +63,7 @@ export interface ValidatedOptions {
6163
projectId?: string;
6264
network?: BlockfrostNetwork;
6365
retrySettings?: RequiredRetryOptions;
66+
gotOptions?: GotOptions;
6467
}
6568

6669
export type HashOrNumber = string | number;

src/utils/got.ts

+2
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ export const getInstance = (
4141
timeout: {
4242
request: options.requestTimeout,
4343
},
44+
// https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
45+
...options.gotOptions,
4446
});
4547
};

src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const validateOptions = (options?: Options): ValidatedOptions => {
6060
version: options.version || DEFAULT_API_VERSION,
6161
debug,
6262
http2: options.http2 ?? false,
63+
gotOptions: options.gotOptions || undefined,
6364
requestTimeout: options.requestTimeout ?? 20000, // 20 seconds
6465
// see: https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#retry
6566
retrySettings: options.retrySettings ?? {

test/tests/utils/utils.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import {
55
AdditionalEndpointOptions,
66
PaginationOptions,
77
} from '../../../src/types';
8-
import { DEFAULT_PAGINATION_PAGE_ITEMS_COUNT } from '../../../src/config';
8+
import {
9+
API_URLS,
10+
DEFAULT_PAGINATION_PAGE_ITEMS_COUNT,
11+
} from '../../../src/config';
912
import { RATE_LIMITER_DEFAULT_CONFIG } from '../../../src/utils/limiter';
13+
import nock from 'nock';
14+
import { SDK } from '../../utils';
1015

1116
describe('utils', () => {
1217
test('no options', () => {
@@ -290,4 +295,26 @@ describe('utils', () => {
290295
).length,
291296
).toBe(201); // check if order option really propagated to a method passed as param
292297
});
298+
299+
test('gotOptions passed and override default options', async () => {
300+
const api = new BlockFrostAPI({
301+
projectId: 'xxx',
302+
gotOptions: {
303+
headers: {
304+
'User-Agent': 'from gotOptions',
305+
},
306+
},
307+
});
308+
309+
const BASE_URL = `${API_URLS.mainnet}/v0`;
310+
const path = /.*/;
311+
312+
nock(BASE_URL)
313+
.get(path)
314+
.reply(200, { message: 'response' })
315+
.matchHeader('User-Agent', 'from gotOptions');
316+
317+
const response = await api.blocksLatest();
318+
expect(response).toMatchObject({ message: 'response' });
319+
});
293320
});

0 commit comments

Comments
 (0)