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

Improve linter and types #22

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export const baseWidgetUrl = 'https://buy.ramp.network/';

export const randomIntMultiplier = 10000000;

export const widgetDesktopWidth = 895;
export const widgetDesktopHeight = 590;

Choose a reason for hiding this comment

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

F


export const minWidgetMobileWidth = 320;
export const minWidgetMobileHeight = 667;
14 changes: 7 additions & 7 deletions src/init-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { baseWidgetUrl } from './consts';
import {
baseWidgetUrl,
minWidgetMobileHeight,
minWidgetMobileWidth,
widgetDesktopHeight,
widgetDesktopWidth,
} from './consts';
import {
AllWidgetVariants,
IHostConfigWithWidgetInstanceId,
InternalEventTypes,
TAllEvents,
} from './types';
import {
minWidgetMobileHeight,
minWidgetMobileWidth,
widgetDesktopHeight,
widgetDesktopWidth,
} from './utils';

export function getBaseUrl(config: IHostConfigWithWidgetInstanceId): URL {
return new URL(config.url || baseWidgetUrl);
Expand Down
6 changes: 3 additions & 3 deletions src/ramp-instant-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class RampInstantSDK {

public on<T extends TAllEvents>(
type: T['type'] | '*',
callback: (event: T) => any
callback: (event: T) => void
): RampInstantSDK {
this._on(type, callback, false);

Expand All @@ -127,7 +127,7 @@ export class RampInstantSDK {

public unsubscribe(
type: TAllEvents['type'] | '*',
callback: (event: TAllEvents) => any
callback: (event: TAllEvents) => void
): RampInstantSDK {
if (type === '*') {
const allTypes = Object.entries(this._listeners);
Expand All @@ -145,7 +145,7 @@ export class RampInstantSDK {

public _on<T extends TAllEvents>(
type: T['type'] | '*',
callback: (event: T) => any,
callback: (event: T) => void,
internal: boolean
): void {
if (type !== '*' && !this._listeners[type]) {
Expand Down
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ type TFiatValue = string;
type TAddress = string;
type TEmailAddress = string;
type TCryptoAmount = string;
type TPoolFee = number;
type TRampFee = number;
type TAssetExchangeRate = number;
type TFinalTxHash = string;
type TActionID = string;
Expand Down Expand Up @@ -122,7 +120,7 @@ export interface IConfigError {

export interface IWidgetEvent {
type: string;
payload: any | null;
payload: unknown;
internal?: boolean;
}

Expand Down Expand Up @@ -270,7 +268,7 @@ export type TEventListenerDict = {

export interface IEventListener {
internal: boolean;
callback(evt: TAllEvents): any;
callback(evt: TAllEvents): void;
}

export interface IOnRequestCryptoAccountResult {
Expand Down
22 changes: 11 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import {
minWidgetMobileHeight,
minWidgetMobileWidth,
randomIntMultiplier,
widgetDesktopHeight,
widgetDesktopWidth,
} from './consts';
import {
AllWidgetVariants,
EventSeverity,
Expand All @@ -8,7 +15,6 @@ import {
TAllEventTypes,
TEventListenerDict,
WidgetEventTypes,
WidgetVariantTypes,
} from './types';

export function getRandomIntString(): string {
Expand All @@ -17,16 +23,10 @@ export function getRandomIntString(): string {
} catch {
// if `crypto` is not supported, fall back to Math.random
// tslint:disable-next-line:no-magic-numbers
return String(Math.floor(Math.random() * 10000000));
return String(Math.floor(Math.random() * randomIntMultiplier));
}
}

export const widgetDesktopWidth = 895;
export const widgetDesktopHeight = 590;

export const minWidgetMobileWidth = 320;
export const minWidgetMobileHeight = 667;

export function normalizeConfigAndLogErrorsOnInvalidFields(
config: Partial<IHostConfig>
): IHostConfig {
Expand Down Expand Up @@ -100,7 +100,7 @@ export function initEventListenersDict(): TEventListenerDict {

return listenersDict;
},
{} as any
{} as TEventListenerDict
) as TEventListenerDict;
}

Expand Down Expand Up @@ -129,7 +129,7 @@ export function determineWidgetVariant(config: IHostConfig): AllWidgetVariants {
}

export function isHtmlElement(element: Element): element is HTMLElement {
return typeof (element as any).blur === 'function';
return typeof (element as HTMLElement).blur === 'function';
}

function validateContainerNode(
Expand Down Expand Up @@ -182,6 +182,6 @@ export function concatRelativePath(base: string | URL, path: string): URL {
return new URL(`${normalizedBase}/${normalizedPath}`);
}

export function urlWithoutTrailingSlash(url: string): string {
function urlWithoutTrailingSlash(url: string): string {
return url.endsWith('/') ? url.slice(0, -1) : url;
}
15 changes: 15 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { concatRelativePath } from '../src/utils';

describe('Utils', () => {
describe('concatRelativePath', () => {
it('Without leading and trailing slash', () =>
expect(concatRelativePath('https://buy.ramp.network', 'api/swap').href).toBe(
'https://buy.ramp.network/api/swap'
));

it('With leading and trailing slash', () =>
expect(concatRelativePath('https://buy.ramp.network/api/', '/swap').href).toBe(
'https://buy.ramp.network/api/swap'
));
});
});
12 changes: 5 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
"compilerOptions": {
"moduleResolution": "node",
"target": "es5",
"module":"es2015",
"module": "es2015",
"lib": ["es2015", "es2016", "es2017", "dom"],
"strict": true,
"sourceMap": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declarationDir": "dist/types",
"outDir": "dist/lib",
"typeRoots": [
"node_modules/@types"
]
"typeRoots": ["node_modules/@types"]
},
"include": [
"src"
]
"include": ["src"]
}
8 changes: 2 additions & 6 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
"jsRules": {},
"rules": {
"no-any": true,
"jsx-no-lambda": false,
"ban-types": false,
"object-literal-sort-keys": false,
Expand All @@ -19,12 +20,7 @@
"no-boolean-literal-compare": true,
"no-irregular-whitespace": true,
"trailing-comma": true,
"typedef": [
true,
"call-signature",
"property-declaration",
"member-variable-declaration"
],
"typedef": [true, "call-signature", "property-declaration", "member-variable-declaration"],
"no-unnecessary-callback-wrapper": true,
"no-unnecessary-qualifier": true,
"object-literal-key-quotes": [true, "as-needed"],
Expand Down