Skip to content

Commit

Permalink
Merge pull request #293 from blockfrost/feat/got-options
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek authored Jul 17, 2024
2 parents 0be1e0c + 090dc21 commit 8099a5b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- 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).


## [5.5.0] - 2023-12-20

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const API = new Blockfrost.BlockFrostAPI({
- `debug` - `boolean`, whether to enable debug logging. It is also possible to enable it by setting environment variable `DEBUG` to `true` (optional, default `false`).
- `customBackend` - `string`, option to set URL to a non-official backend (optional)
- `version` - `number`, version of the Blockfrost API (optional, default `0`)
- `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).


## Error handling

Expand Down
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MaxRedirectsError,
UnsupportedProtocolError,
RequiredRetryOptions,
Options as GotOptions,
} from 'got';
import { RateLimiterConfig } from '../utils/limiter';

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

export type Options = (OptionCombination1 | OptionCombination2) &
Expand All @@ -61,6 +63,7 @@ export interface ValidatedOptions {
projectId?: string;
network?: BlockfrostNetwork;
retrySettings?: RequiredRetryOptions;
gotOptions?: GotOptions;
}

export type HashOrNumber = string | number;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ export const getInstance = (
timeout: {
request: options.requestTimeout,
},
// https://github.com/sindresorhus/got/blob/main/documentation/2-options.md
...options.gotOptions,
});
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const validateOptions = (options?: Options): ValidatedOptions => {
version: options.version || DEFAULT_API_VERSION,
debug,
http2: options.http2 ?? false,
gotOptions: options.gotOptions || undefined,
requestTimeout: options.requestTimeout ?? 20000, // 20 seconds
// see: https://github.com/sindresorhus/got/blob/main/documentation/7-retry.md#retry
retrySettings: options.retrySettings ?? {
Expand Down
29 changes: 28 additions & 1 deletion test/tests/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import {
AdditionalEndpointOptions,
PaginationOptions,
} from '../../../src/types';
import { DEFAULT_PAGINATION_PAGE_ITEMS_COUNT } from '../../../src/config';
import {
API_URLS,
DEFAULT_PAGINATION_PAGE_ITEMS_COUNT,
} from '../../../src/config';
import { RATE_LIMITER_DEFAULT_CONFIG } from '../../../src/utils/limiter';
import nock from 'nock';
import { SDK } from '../../utils';

describe('utils', () => {
test('no options', () => {
Expand Down Expand Up @@ -290,4 +295,26 @@ describe('utils', () => {
).length,
).toBe(201); // check if order option really propagated to a method passed as param
});

test('gotOptions passed and override default options', async () => {
const api = new BlockFrostAPI({
projectId: 'xxx',
gotOptions: {
headers: {
'User-Agent': 'from gotOptions',
},
},
});

const BASE_URL = `${API_URLS.mainnet}/v0`;
const path = /.*/;

nock(BASE_URL)
.get(path)
.reply(200, { message: 'response' })
.matchHeader('User-Agent', 'from gotOptions');

const response = await api.blocksLatest();
expect(response).toMatchObject({ message: 'response' });
});
});

0 comments on commit 8099a5b

Please sign in to comment.