Skip to content

Commit

Permalink
fix: allow to use buildBlockExplorer without any param (#758)
Browse files Browse the repository at this point in the history
* fix: make `buildBlockExplorerUrl` more flexible

* add changeset

* refactor code

---------

Co-authored-by: Anderson Arboleya <[email protected]>
Co-authored-by: Cameron Manavian <[email protected]>
  • Loading branch information
3 people authored Feb 17, 2023
1 parent 4fe70de commit 0ce7e93
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-squids-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": patch
---

Make `buildBlockExplorer` more flexible. It won't throw when not passed anything now.
35 changes: 23 additions & 12 deletions packages/providers/src/utils/block-explorer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,29 @@ const testBlockExplorerUrlWithInputs = ({

describe('BlockExplorer Utils', () => {
test('buildBlockExplorerUrl - empty/undefined inputs', () => {
const url = buildBlockExplorerUrl({
blockExplorerUrl: undefined,
path: '/transaction/0x123',
providerUrl: undefined,
});

expect(url).toEqual(`${DEFAULT_BLOCK_EXPLORER_URL}/transaction/0x123`);
expect(buildBlockExplorerUrl()).toEqual(`${DEFAULT_BLOCK_EXPLORER_URL}/`);
expect(
buildBlockExplorerUrl({
providerUrl: 'http://localhost:4000',
})
).toEqual(
`${DEFAULT_BLOCK_EXPLORER_URL}/?providerUrl=${encodeURIComponent('http://localhost:4000')}`
);
expect(
buildBlockExplorerUrl({
blockExplorerUrl: undefined,
})
).toEqual(`${DEFAULT_BLOCK_EXPLORER_URL}/`);
expect(
buildBlockExplorerUrl({
path: '/transaction/0x123',
})
).toEqual(`${DEFAULT_BLOCK_EXPLORER_URL}/transaction/0x123`);
expect(
buildBlockExplorerUrl({
providerUrl: undefined,
})
).toEqual(`${DEFAULT_BLOCK_EXPLORER_URL}/`);
});

test('buildBlockExplorerUrl - string inputs', () => {
Expand Down Expand Up @@ -84,11 +100,6 @@ describe('BlockExplorer Utils', () => {
'Only one of the following can be passed in to buildBlockExplorerUrl: address, txId, blockNumber'
);

// passing in neither path nor a helper param should throw
expect(() => buildBlockExplorerUrl({})).toThrow(
'One of the following must be passed in to buildBlockExplorerUrl: address, txId, blockNumber, path'
);

// passing in path AND a helper param should throw
expect(() =>
buildBlockExplorerUrl({
Expand Down
50 changes: 21 additions & 29 deletions packages/providers/src/utils/block-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@ type BuildBlockExplorerUrlHelperParam = 'address' | 'txId' | 'blockNumber';
/**
* Builds a block explorer url based on and the given path, block explorer URL and provider URL
*/
export const buildBlockExplorerUrl = ({
blockExplorerUrl,
path,
providerUrl,
address,
txId,
blockNumber,
}: {
blockExplorerUrl?: string;
path?: string;
providerUrl?: string;
address?: string;
txId?: string;
blockNumber?: number;
}) => {
export const buildBlockExplorerUrl = (
options: {
blockExplorerUrl?: string;
path?: string;
providerUrl?: string;
address?: string;
txId?: string;
blockNumber?: number;
} = {}
) => {
const { blockExplorerUrl, path, providerUrl, address, txId, blockNumber } = options;
const explorerUrl = blockExplorerUrl || DEFAULT_BLOCK_EXPLORER_URL;

// make sure that only ONE or none of the following is defined: address, txId, blockNumber
Expand All @@ -58,6 +54,8 @@ export const buildBlockExplorerUrl = ({
value,
}));

const hasAnyDefinedValues = definedValues.length > 0;

if (definedValues.length > 1) {
throw new Error(
`Only one of the following can be passed in to buildBlockExplorerUrl: ${customInputParams
Expand All @@ -66,29 +64,23 @@ export const buildBlockExplorerUrl = ({
);
}

if (definedValues.length === 0 && !path) {
throw new Error(
`One of the following must be passed in to buildBlockExplorerUrl: ${customInputParams
.map((param) => param.key)
.join(', ')}, path`
);
}

if (path && definedValues.length > 0) {
const inputKeys = customInputParams.map(({ key }) => key).join(', ');
throw new Error(
`You cannot pass in a path to buildBlockExplorerUrl along with any of the following: ${inputKeys}`
);
}

// Remove leading and trailing slashes from the path and block explorer url respectively, if present
const trimSlashes = /^\/|\/$/gm;
const cleanPath = path
? path.replace(trimSlashes, '')
: getPathFromInput(
const pathGeneratedFromInputParams = hasAnyDefinedValues
? getPathFromInput(
definedValues[0].key as BuildBlockExplorerUrlHelperParam,
definedValues[0].value
);
)
: '';

// Remove leading and trailing slashes from the path and block explorer url respectively, if present
const trimSlashes = /^\/|\/$/gm;
const cleanPath = path ? path.replace(trimSlashes, '') : pathGeneratedFromInputParams;
const cleanBlockExplorerUrl = explorerUrl.replace(trimSlashes, '');
const cleanProviderUrl = providerUrl?.replace(trimSlashes, '');
const encodedProviderUrl = cleanProviderUrl ? encodeURIComponent(cleanProviderUrl) : undefined;
Expand Down

0 comments on commit 0ce7e93

Please sign in to comment.