Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new type in api - settings in order to support different types of… #476

Merged
merged 9 commits into from
Jun 15, 2021
14 changes: 12 additions & 2 deletions packages/ui-settings/src/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright 2017-2021 @polkadot/ui-settings authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Option, SettingsStruct } from './types';

import EventEmitter from 'eventemitter3';
import store from 'store';

import { isUndefined } from '@polkadot/util';

import { CAMERA, CAMERA_DEFAULT, CRYPTOS, CRYPTOS_ETH, CRYPTOS_LEDGER, ENDPOINT_DEFAULT, ENDPOINTS, ICON_DEFAULT, ICONS, LANGUAGE_DEFAULT, LEDGER_CONN, LEDGER_CONN_DEFAULT, LOCKING, LOCKING_DEFAULT, PREFIX_DEFAULT, PREFIXES, UIMODE_DEFAULT, UIMODES, UITHEME_DEFAULT, UITHEMES } from './defaults';
import { Endpoint, EndpointType, Option, SettingsStruct } from './types';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be import type


type ChangeCallback = (settings: SettingsStruct) => void;
type OnTypes = 'change';
Expand All @@ -24,6 +23,9 @@ function withDefault (options: Option[], option: string | undefined, fallback: s
export class Settings implements SettingsStruct {
readonly #emitter: EventEmitter;

#apiType: Endpoint;

// will become deprecated for supporting substrate connect light clients. apiType structure should be used instead
#apiUrl: string;

#camera: string;
Expand All @@ -47,7 +49,9 @@ export class Settings implements SettingsStruct {

this.#emitter = new EventEmitter();

// will become deprecated for supporting substrate connect light clients. apiType structure should be used instead
this.#apiUrl = (typeof settings.apiUrl === 'string' && settings.apiUrl) || process.env.WS_URL || (ENDPOINT_DEFAULT.value as string);
this.#apiType = { type: EndpointType.jrpc, url: this.#apiUrl };
this.#camera = withDefault(CAMERA, settings.camera, CAMERA_DEFAULT);
this.#ledgerConn = withDefault(LEDGER_CONN, settings.ledgerConn, LEDGER_CONN_DEFAULT);
this.#i18nLang = settings.i18nLang || LANGUAGE_DEFAULT;
Expand All @@ -62,6 +66,10 @@ export class Settings implements SettingsStruct {
return this.#camera;
}

public get apiType (): Endpoint {
return this.#apiType;
}

public get apiUrl (): string {
return this.#apiUrl;
}
Expand Down Expand Up @@ -140,6 +148,7 @@ export class Settings implements SettingsStruct {

public get (): SettingsStruct {
return {
apiType: this.#apiType,
apiUrl: this.#apiUrl,
camera: this.#camera,
i18nLang: this.#i18nLang,
Expand All @@ -153,6 +162,7 @@ export class Settings implements SettingsStruct {
}

public set (settings: Partial<SettingsStruct>): void {
this.#apiType = settings.apiType || this.#apiType;
this.#apiUrl = settings.apiUrl || this.#apiUrl;
this.#camera = settings.camera || this.#camera;
this.#ledgerConn = settings.ledgerConn || this.#ledgerConn;
Expand Down
3 changes: 2 additions & 1 deletion packages/ui-settings/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

import { Settings, settings } from './Settings';
import { Endpoint, EndpointType, JsonRpcEndpoint, SubstrateConnectEndpoint } from './types';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import type (and move up to the correct place)


export { ENDPOINT_DEFAULT, ICON_DEFAULT, ICON_DEFAULT_HOST, LANGUAGE_DEFAULT, LOCKING_DEFAULT, PREFIX_DEFAULT, UIMODE_DEFAULT, UITHEME_DEFAULT } from './defaults';
export { packageInfo } from './packageInfo';

export { settings, Settings };
export { settings, Settings, Endpoint, JsonRpcEndpoint, SubstrateConnectEndpoint, EndpointType };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the type exports here. (We always only have the types in /types.ts)


export default settings;
17 changes: 17 additions & 0 deletions packages/ui-settings/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Option = {
}

export interface SettingsStruct {
apiType: Endpoint;
apiUrl: string;
camera: string;
i18nLang: string;
Expand All @@ -28,3 +29,19 @@ export interface NetworkSpecsStruct {
title: string;
unit: string;
}

export type Endpoint = JsonRpcEndpoint | SubstrateConnectEndpoint;
export interface JsonRpcEndpoint {
type: EndpointType.jrpc;
url: string;
}

export interface SubstrateConnectEndpoint {
type: EndpointType.substrateconnect;
chain: string;
}

export enum EndpointType {
Copy link
Member

@jacogr jacogr Jun 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the enum, it is hugely problematic (polkadot-js doesn't use these at all) -

  • it creates a bunch of code to make this work in the transpiling phase
  • it turns type-only imports into type & code imports (types files now contain actual JS logic)

This is sufficient: export type EndpointType = 'json-rpc' | 'substrate-connect';

jrpc = 'json-rpc',
substrateconnect = 'substrate-connect'
}