Skip to content

Commit aa6ea3a

Browse files
authored
Rework API urls (#5840)
* Rework API urls * Adjust * Fix build
1 parent 27b82e7 commit aa6ea3a

File tree

14 files changed

+52
-114
lines changed

14 files changed

+52
-114
lines changed

packages/apps-config/src/ci/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function checkEndpoints (issueFile: string, failures: string[]): void {
3030
value &&
3131
isString(value) &&
3232
!value.includes('127.0.0.1') &&
33-
!value.includes('substrate-connect')
33+
!value.startsWith('light://')
3434
)
3535
.map(({ text, value }): Partial<Endpoint> => ({
3636
name: text as string,

packages/apps-config/src/endpoints/index.spec.ts

+6-30
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import { createWsEndpoints } from '.';
77

88
interface Endpoint {
99
name: string;
10-
ws: string;
11-
}
12-
13-
interface LightClientEndpoint {
14-
name: string;
15-
param: string;
10+
value: string;
1611
}
1712

1813
const allEndpoints = createWsEndpoints((k: string, v?: string) => v || k);
@@ -22,34 +17,15 @@ describe('WS urls are all valid', (): void => {
2217
.filter(({ value }) =>
2318
value &&
2419
isString(value) &&
25-
!value.includes('127.0.0.1') &&
26-
!value.includes('substrate-connect')
20+
!value.includes('127.0.0.1')
2721
)
2822
.map(({ text, value }): Endpoint => ({
2923
name: text as string,
30-
ws: value
31-
}))
32-
.forEach(({ name, ws }) =>
33-
it(`${name} @ ${ws}`, (): void => {
34-
assert(ws.startsWith('wss://'), `${name} @ ${ws} should start with wss://`);
35-
})
36-
);
37-
});
38-
39-
describe('light client urls are all valid', (): void => {
40-
allEndpoints
41-
.filter(({ value }) =>
42-
value &&
43-
isString(value) &&
44-
value.includes('substrate-connect')
45-
)
46-
.map(({ text, value }): LightClientEndpoint => ({
47-
name: text as string,
48-
param: value
24+
value
4925
}))
50-
.forEach(({ name, param }) =>
51-
it(`${name} @ ${param}`, (): void => {
52-
assert(param.substr(param.indexOf('-')) === '-substrate-connect', `${name} @ ${param} should end with '-substrate-connect'`);
26+
.forEach(({ name, value }) =>
27+
it(`${name} @ ${value}`, (): void => {
28+
assert(value.startsWith('wss://') || value.startsWith('light://substrate-connect/'), `${name} @ ${value} should start with wss:// or light://`);
5329
})
5430
);
5531
});

packages/apps-config/src/endpoints/productionRelayKusama.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { TFunction } from 'i18next';
55
import type { EndpointOption } from './types';
66

77
import { KUSAMA_GENESIS } from '../api/constants';
8-
import { createProviderUrl } from './util';
98

109
/* eslint-disable sort-keys */
1110

@@ -24,7 +23,7 @@ export function createKusama (t: TFunction): EndpointOption {
2423
Parity: 'wss://kusama-rpc.polkadot.io',
2524
OnFinality: 'wss://kusama.api.onfinality.io/public-ws',
2625
'Patract Elara': 'wss://kusama.elara.patract.io',
27-
'light client': createProviderUrl('kusama-substrate-connect', 'substrate-connect')
26+
'light client': 'light://substrate-connect/kusama'
2827
// Pinknode: 'wss://rpc.pinknode.io/kusama/explorer' // https://github.com/polkadot-js/apps/issues/5721
2928
},
3029
teleport: [1000],

packages/apps-config/src/endpoints/productionRelayPolkadot.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { TFunction } from 'i18next';
55
import type { EndpointOption } from './types';
66

77
import { POLKADOT_GENESIS } from '../api/constants';
8-
import { createProviderUrl } from './util';
98

109
/* eslint-disable sort-keys */
1110

@@ -24,7 +23,7 @@ export function createPolkadot (t: TFunction): EndpointOption {
2423
Parity: 'wss://rpc.polkadot.io',
2524
OnFinality: 'wss://polkadot.api.onfinality.io/public-ws',
2625
'Patract Elara': 'wss://polkadot.elara.patract.io',
27-
'light client': createProviderUrl('polkadot-substrate-connect', 'substrate-connect')
26+
'light client': 'light://substrate-connect/polkadot'
2827
// Pinknode: 'wss://rpc.pinknode.io/polkadot/explorer' // https://github.com/polkadot-js/apps/issues/5721
2928
},
3029
linked: [

packages/apps-config/src/endpoints/testingRelayWestend.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { TFunction } from 'i18next';
55
import type { EndpointOption } from './types';
66

77
import { WESTEND_GENESIS } from '../api/constants';
8-
import { createProviderUrl } from './util';
98

109
/* eslint-disable sort-keys */
1110

@@ -26,7 +25,7 @@ export function createWestend (t: TFunction): EndpointOption {
2625
Parity: 'wss://westend-rpc.polkadot.io',
2726
'Patract Elara': 'wss://westend.elara.patract.io',
2827
OnFinality: 'wss://westend.api.onfinality.io/public-ws',
29-
'light client': createProviderUrl('westend-substrate-connect', 'substrate-connect')
28+
'light client': 'light://substrate-connect/westend'
3029
// 'NodeFactory(Vedran)': 'wss://westend.vedran.nodefactory.io/ws', // https://github.com/polkadot-js/apps/issues/5580
3130
// Pinknode: 'wss://rpc.pinknode.io/westend/explorer' // https://github.com/polkadot-js/apps/issues/5721
3231
},

packages/apps-config/src/endpoints/types.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
import type { Option } from '../settings/types';
55

6-
import { Endpoint } from '@polkadot/ui-settings/types';
7-
86
export interface EndpointOption {
97
dnslink?: string;
108
genesisHash?: string;
@@ -16,7 +14,7 @@ export interface EndpointOption {
1614
linked?: EndpointOption[];
1715
info?: string;
1816
paraId?: number;
19-
providers: Record<string, string | Endpoint>;
17+
providers: Record<string, string>;
2018
summary?: string;
2119
teleport?: number[];
2220
text: React.ReactNode;

packages/apps-config/src/endpoints/util.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import type { TFunction } from 'i18next';
55
import type { EndpointOption, LinkOption } from './types';
66

7-
import { Endpoint, EndpointType } from '@polkadot/ui-settings/types';
8-
import { isString } from '@polkadot/util';
9-
107
interface SortOption {
118
isUnreachable?: boolean;
129
}
@@ -55,12 +52,12 @@ export function expandEndpoint (t: TFunction, { dnslink, genesisHash, homepage,
5552
.map(([host, value], index): LinkOption => ({
5653
...base,
5754
dnslink: index === 0 ? dnslink : undefined,
58-
isLightClient: isString(value) ? false : value.type === 'substrate-connect',
55+
isLightClient: value.startsWith('light://'),
5956
isRelay: false,
60-
textBy: isString(value)
61-
? t('rpc.hosted.by', 'hosted by {{host}}', { ns: 'apps-config', replace: { host } })
62-
: t('lightclient.experimental', 'light client (experimental)', { ns: 'apps-config' }),
63-
value: isString(value) ? value : value.param
57+
textBy: value.startsWith('light://')
58+
? t('lightclient.experimental', 'light client (experimental)', { ns: 'apps-config' })
59+
: t('rpc.hosted.by', 'hosted by {{host}}', { ns: 'apps-config', replace: { host } }),
60+
value
6461
}));
6562

6663
if (linked) {
@@ -84,7 +81,3 @@ export function expandEndpoint (t: TFunction, { dnslink, genesisHash, homepage,
8481
export function expandEndpoints (t: TFunction, input: EndpointOption[], firstOnly?: boolean): LinkOption[] {
8582
return input.sort(sortLinks).reduce((result: LinkOption[], input) => result.concat(expandEndpoint(t, input, firstOnly)), []);
8683
}
87-
88-
export function createProviderUrl (param: string, type: EndpointType): Endpoint {
89-
return { param, type };
90-
}

packages/apps/src/Endpoints/Network.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function NetworkDisplay ({ apiUrl, className = '', setApiUrl, value: { icon, isC
2626

2727
const _selectUrl = useCallback(
2828
() => {
29-
const filteredProviders = providers.filter((p) => !p.name.startsWith('light client'));
29+
const filteredProviders = providers.filter(({ url }) => !url.startsWith('light://'));
3030

3131
return setApiUrl(name, filteredProviders[Math.floor(Math.random() * filteredProviders.length)].url);
3232
},

packages/apps/src/Endpoints/index.tsx

+6-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import styled from 'styled-components';
1414
import { createWsEndpoints, CUSTOM_ENDPOINT_KEY } from '@polkadot/apps-config';
1515
import { Button, Input, Sidebar } from '@polkadot/react-components';
1616
import { settings } from '@polkadot/ui-settings';
17-
import { Endpoint, EndpointType } from '@polkadot/ui-settings/types';
1817
import { isAscii } from '@polkadot/util';
1918

2019
import { useTranslation } from '../translate';
@@ -40,18 +39,10 @@ function isValidUrl (url: string): boolean {
4039
// some random length... we probably want to parse via some lib
4140
(url.length >= 7) &&
4241
// check that it starts with a valid ws identifier
43-
(url.startsWith('ws://') || url.startsWith('wss://'))
42+
(url.startsWith('ws://') || url.startsWith('wss://') || url.startsWith('light://'))
4443
);
4544
}
4645

47-
function getApiType (param: string): Endpoint {
48-
if (param.includes('-substrate-connect')) {
49-
return { param, type: 'substrate-connect' };
50-
}
51-
52-
return { param, type: 'json-rpc' };
53-
}
54-
5546
function combineEndpoints (endpoints: LinkOption[]): Group[] {
5647
return endpoints.reduce((result: Group[], e): Group[] => {
5748
if (e.isHeader) {
@@ -127,10 +118,10 @@ function loadAffinities (groups: Group[]): Record<string, string> {
127118
}), {});
128119
}
129120

130-
function isSwitchDisabled (hasUrlChanged: boolean, apiType: EndpointType, isUrlValid: boolean): boolean {
121+
function isSwitchDisabled (hasUrlChanged: boolean, apiUrl: string, isUrlValid: boolean): boolean {
131122
if (!hasUrlChanged) {
132123
return true;
133-
} else if (apiType === 'substrate-connect') {
124+
} else if (apiUrl.startsWith('light://')) {
134125
return false;
135126
} else if (isUrlValid) {
136127
return false;
@@ -237,15 +228,15 @@ function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElem
237228
const _onApply = useCallback(
238229
(): void => {
239230
settings.set({ ...(settings.get()), apiUrl });
240-
window.location.assign(`${window.location.origin}${window.location.pathname}${getApiType(apiUrl).type === 'substrate-connect' ? '?sc=' : '?rpc='}${encodeURIComponent(apiUrl)}${window.location.hash}`);
231+
window.location.assign(`${window.location.origin}${window.location.pathname}?rpc=${encodeURIComponent(apiUrl)}${window.location.hash}`);
241232
// window.location.reload();
242233
onClose();
243234
},
244235
[apiUrl, onClose]
245236
);
246237

247238
const canSwitch = useMemo(
248-
() => isSwitchDisabled(hasUrlChanged, getApiType(apiUrl).type, isUrlValid),
239+
() => isSwitchDisabled(hasUrlChanged, apiUrl, isUrlValid),
249240
[hasUrlChanged, apiUrl, isUrlValid]
250241
);
251242

@@ -275,7 +266,7 @@ function Endpoints ({ className = '', offset, onClose }: Props): React.ReactElem
275266
setGroup={_changeGroup}
276267
value={group}
277268
>
278-
{group.isDevelopment && getApiType(apiUrl).type === 'json-rpc' && (
269+
{group.isDevelopment && (
279270
<div className='endpointCustomWrapper'>
280271
<Input
281272
className='endpointCustom'

packages/apps/src/Root.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function Root ({ store }: Props): React.ReactElement<Props> {
4444
<ThemeProvider theme={theme}>
4545
<Queue>
4646
<Api
47-
apiType={settings.apiType}
47+
apiUrl={settings.apiUrl}
4848
store={store}
4949
>
5050
<BlockAuthors>

packages/apps/src/initSettings.ts

+16-25
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ import store from 'store';
77
import { createWsEndpoints } from '@polkadot/apps-config';
88
import { extractIpfsDetails } from '@polkadot/react-hooks/useIpfs';
99
import { settings } from '@polkadot/ui-settings';
10-
import { Endpoint } from '@polkadot/ui-settings/types';
1110
import { assert } from '@polkadot/util';
1211

13-
function networkOrUrl (apiType: Endpoint): void {
14-
if (apiType.type === 'json-rpc') {
15-
console.log('WS endpoint=', apiType.param);
16-
} else if (apiType.type === 'substrate-connect') {
17-
console.log('Chain of light client is =', apiType.param);
12+
function networkOrUrl (apiUrl: string): void {
13+
if (apiUrl.startsWith('light://')) {
14+
console.log('Light endpoint=', apiUrl.replace('light://', ''));
15+
} else {
16+
console.log('WS endpoint=', apiUrl);
1817
}
1918
}
2019

21-
function getApiType (): Endpoint {
20+
function getApiUrl (): string {
2221
// we split here so that both these forms are allowed
2322
// - http://localhost:3000/?rpc=wss://substrate-rpc.parity.io/#/explorer
2423
// - http://localhost:3000/#/explorer?rpc=wss://substrate-rpc.parity.io
@@ -31,17 +30,9 @@ function getApiType (): Endpoint {
3130
// https://polkadot.js.org/apps/?rpc=ws://127.0.0.1:9944#/explorer;
3231
const url = decodeURIComponent(urlOptions.rpc.split('#')[0]);
3332

34-
assert(url.startsWith('ws://') || url.startsWith('wss://'), 'Non-prefixed ws/wss url');
33+
assert(url.startsWith('ws://') || url.startsWith('wss://') || url.startsWith('light://'), 'Non-prefixed ws/wss/light url');
3534

36-
return { param: url, type: 'json-rpc' };
37-
} else if (urlOptions.sc) {
38-
assert(!Array.isArray(urlOptions.sc), 'Invalid network specified');
39-
40-
// https://polkadot.js.org/apps/?sc=kusama#/explorer;
41-
const network = decodeURIComponent(urlOptions.sc.split('#')[0]);
42-
const chain = network.split('-')[0];
43-
44-
return { param: chain, type: 'substrate-connect' };
35+
return url;
4536
}
4637

4738
const endpoints = createWsEndpoints(<T = string>(): T => ('' as unknown as T));
@@ -52,25 +43,25 @@ function getApiType (): Endpoint {
5243
const option = endpoints.find(({ dnslink }) => dnslink === ipnsChain);
5344

5445
if (option) {
55-
return { param: option.value, type: 'json-rpc' };
46+
return option.value;
5647
}
5748
}
5849

5950
const stored = store.get('settings') as Record<string, unknown> || {};
6051
const fallbackUrl = endpoints.find(({ value }) => !!value);
6152

6253
// via settings, or the default chain
63-
return [stored.apiType, process.env.WS_URL].includes(settings.apiType)
64-
? settings.apiType // keep as-is
54+
return [stored.apiUrl, process.env.WS_URL].includes(settings.apiUrl)
55+
? settings.apiUrl // keep as-is
6556
: fallbackUrl
66-
? { param: fallbackUrl.value, type: 'json-rpc' } // grab the fallback
67-
: { param: 'ws://127.0.0.1:9944', type: 'json-rpc' }; // nothing found, go local
57+
? fallbackUrl.value // grab the fallback
58+
: 'ws://127.0.0.1:9944'; // nothing found, go local
6859
}
6960

7061
// There cannot be a Substrate Connect light client default (expect only jrpc EndpointType)
71-
const apiType = getApiType();
62+
const apiUrl = getApiUrl();
7263

7364
// set the default as retrieved here
74-
settings.set({ apiType });
65+
settings.set({ apiUrl });
7566

76-
networkOrUrl(apiType);
67+
networkOrUrl(apiUrl);

packages/page-accounts/src/CreateAccount.slow.spec.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ import { Api } from '@polkadot/react-api';
1414
import { MemoryStore } from '@polkadot/test-support/keyring';
1515
import { WaitForApi } from '@polkadot/test-support/react';
1616
import { SUBSTRATE_PORT } from '@polkadot/test-support/substrate';
17-
import { Endpoint } from '@polkadot/ui-settings/types';
1817

1918
const renderAccounts = () => {
2019
const memoryStore = new MemoryStore();
21-
const apiType: Endpoint = { param: `ws://127.0.0.1:${SUBSTRATE_PORT}`, type: 'json-rpc' };
2220

2321
return render(
2422
<MemoryRouter>
2523
<ThemeProvider theme={lightTheme}>
2624
<Api
27-
apiType={apiType}
25+
apiUrl={`ws://127.0.0.1:${SUBSTRATE_PORT}`}
2826
store={memoryStore}
2927
>
3028
<WaitForApi>

packages/page-bounties/src/Bounties.slow.spec.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@ import { MemoryStore } from '@polkadot/test-support/keyring';
1717
import { aliceSigner } from '@polkadot/test-support/keyring/signers';
1818
import { WaitForApi } from '@polkadot/test-support/react';
1919
import { execute } from '@polkadot/test-support/transaction/execute';
20-
import { Endpoint } from '@polkadot/ui-settings/types';
2120

2221
const SUBSTRATE_PORT = Number.parseInt(process.env.TEST_SUBSTRATE_PORT || '30333');
2322

2423
const renderBounties = () => {
2524
const memoryStore = new MemoryStore();
2625

27-
const apiType: Endpoint = { param: `ws://127.0.0.1:${SUBSTRATE_PORT}`, type: 'json-rpc' };
28-
2926
return render(
3027
<Suspense fallback='...'>
3128
<MemoryRouter>
3229
<ThemeProvider theme={lightTheme}>
3330
<Api
34-
apiType={apiType}
31+
apiUrl={`ws://127.0.0.1:${SUBSTRATE_PORT}`}
3532
store={memoryStore}
3633
>
3734
<WaitForApi>

0 commit comments

Comments
 (0)