Skip to content

Commit

Permalink
chore: lint fix in client lib src
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Jan 5, 2024
1 parent 13d3ad2 commit d0bfd31
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 48 deletions.
1 change: 1 addition & 0 deletions client/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/generated/*
63 changes: 31 additions & 32 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"build:browser:watch": "npm run build:browser -- watch",
"start": "concurrently npm:build:browser:watch npm:open",
"test": "ts-node test/test.ts",
"lint": "eslint . --ext .ts -f codeframe",
"lint": "eslint . --ext .ts -f unix",
"lint:prettier": "prettier --check ./src/**/*.ts",
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx -f unix --fix && prettier --write --check src/**/*.ts",
"open": "http-server -o 9222 -o index.html",
"prep-openapi": "rimraf ./.tmp && rimraf ./src/generated && swagger-cli bundle --dereference -o ./.tmp/openapi-temp.json ../docs/openapi.yaml && shx sed -i '^.*\\$schema.*$' '' ./.tmp/openapi-temp.json > ./.tmp/openapi.json",
"generate-openapi": "npm run prep-openapi && openapi-generator-cli generate --skip-validate-spec -g typescript-fetch --additional-properties=withInterfaces=true,typescriptThreePlus=true,supportsES6=true,legacyDiscriminatorBehavior=false,enumPropertyNaming=original,modelPropertyNaming=original -i ./.tmp/openapi.json -o ./src/generated > ./.tmp/gen.log",
Expand Down Expand Up @@ -58,6 +59,7 @@
"eslint": "8.56.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.2",
"eslint-plugin-unused-imports": "3.0.0",
"http-server": "14.0.0",
"microbundle": "0.13.3",
"prettier": "3.1.1",
Expand Down
30 changes: 22 additions & 8 deletions client/src/socket-io/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function createStacksApiSocket(opts?: StacksApiSocketConnectionOptions) {
...opts?.socketOpts?.query,
// Subscriptions can be specified on init using this handshake query param.
subscriptions: Array.from(new Set(opts?.subscriptions)).join(','),
}
},
};
const socket: StacksApiSocket = io(getWsUrl(opts?.url ?? BASE_PATH).href, socketOpts);
return socket;
Expand All @@ -53,8 +53,8 @@ function createStacksApiSocket(opts?: StacksApiSocketConnectionOptions) {
export class StacksApiSocketClient {
readonly socket: StacksApiSocket;

constructor(socket: StacksApiSocket)
constructor(opts?: StacksApiSocketConnectionOptions)
constructor(socket: StacksApiSocket);
constructor(opts?: StacksApiSocketConnectionOptions);
constructor(args?: StacksApiSocket | StacksApiSocketConnectionOptions) {
if (args instanceof Socket) {
this.socket = args;
Expand All @@ -68,7 +68,8 @@ export class StacksApiSocketClient {
}

handleSubscription(topic: Topic, subscribe = false, listener?: (...args: any[]) => void) {
const subscriptions = new Set(this.socket.io.opts.query?.subscriptions.split(',') ?? []);
const subsQuery = this.socket.io.opts.query?.subscriptions as string | undefined;
const subscriptions = new Set(subsQuery?.split(',') ?? []);
if (subscribe) {
this.socket.emit('subscribe', topic, error => {
if (error) console.error(`Error subscribing: ${error}`);
Expand Down Expand Up @@ -120,7 +121,10 @@ export class StacksApiSocketClient {
this.handleSubscription('mempool', false);
}

subscribeAddressTransactions(address: string, listener?: (address: string, tx: AddressTransactionWithTransfers) => void) {
subscribeAddressTransactions(
address: string,
listener?: (address: string, tx: AddressTransactionWithTransfers) => void
) {
if (listener) this.socket.on(`address-transaction:${address}`, listener);
return this.handleSubscription(`address-transaction:${address}`, true, listener);
}
Expand All @@ -129,7 +133,10 @@ export class StacksApiSocketClient {
this.handleSubscription(`address-transaction:${address}`, false);
}

subscribeAddressStxBalance(address: string, listener?: (address: string, stxBalance: AddressStxBalanceResponse) => void) {
subscribeAddressStxBalance(
address: string,
listener?: (address: string, stxBalance: AddressStxBalanceResponse) => void
) {
if (listener) this.socket.on(`address-stx-balance:${address}`, listener);
return this.handleSubscription(`address-stx-balance:${address}`, true, listener);
}
Expand All @@ -156,7 +163,11 @@ export class StacksApiSocketClient {
this.handleSubscription('nft-event', false);
}

subscribeNftAssetEvent(assetIdentifier: string, value: string, listener?: (assetIdentifier: string, value: string, event: NftEvent) => void) {
subscribeNftAssetEvent(
assetIdentifier: string,
value: string,
listener?: (assetIdentifier: string, value: string, event: NftEvent) => void
) {
if (listener) this.socket.on(`nft-asset-event:${assetIdentifier}+${value}`, listener);
return this.handleSubscription(`nft-asset-event:${assetIdentifier}+${value}`, true, listener);
}
Expand All @@ -165,7 +176,10 @@ export class StacksApiSocketClient {
this.handleSubscription(`nft-asset-event:${assetIdentifier}+${value}`, false);
}

subscribeNftCollectionEvent(assetIdentifier: string, listener?: (assetIdentifier: string, event: NftEvent) => void) {
subscribeNftCollectionEvent(
assetIdentifier: string,
listener?: (assetIdentifier: string, event: NftEvent) => void
) {
if (listener) this.socket.on(`nft-collection-event:${assetIdentifier}`, listener);
return this.handleSubscription(`nft-collection-event:${assetIdentifier}`, true, listener);
}
Expand Down
19 changes: 12 additions & 7 deletions client/src/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class StacksApiWebSocketClient {
constructor(webSocket: IWebSocket) {
this.webSocket = webSocket;
(webSocket as WebSocket).addEventListener('message', event => {
const parsed = JsonRpcLite.parse(event.data);
const parsed = JsonRpcLite.parse(event.data as string);
const rpcObjects = Array.isArray(parsed) ? parsed : [parsed];
rpcObjects.forEach(obj => {
if (obj.type === JsonRpcLite.RpcStatusType.notification) {
Expand All @@ -100,11 +100,10 @@ export class StacksApiWebSocketClient {
const method = data.method as RpcSubscriptionType;
switch (method) {
case 'tx_update':
this.eventEmitter.emit('txUpdate', data.params as (Transaction | MempoolTransaction));
this.eventEmitter.emit('txUpdate', data.params as Transaction | MempoolTransaction);
break;
case 'address_tx_update':
this.eventEmitter.emit('addressTxUpdate',
data.params as RpcAddressTxNotificationParams);
this.eventEmitter.emit('addressTxUpdate', data.params as RpcAddressTxNotificationParams);
break;
case 'address_balance_update':
this.eventEmitter.emit(
Expand Down Expand Up @@ -134,7 +133,7 @@ export class StacksApiWebSocketClient {
}

private rpcCall<TResult = void>(method: string, params: any): Promise<TResult> {
const rpcReq = JsonRpcLite.request(++this.idCursor, method, params);
const rpcReq = JsonRpcLite.request(++this.idCursor, method, params as JsonRpcLite.RpcParams);
return new Promise<TResult>((resolve, reject) => {
this.pendingRequests.set(rpcReq.id, { resolve, reject });
this.webSocket.send(rpcReq.serialize());
Expand Down Expand Up @@ -276,9 +275,15 @@ export class StacksApiWebSocketClient {
asset_identifier: assetIdentifier,
value,
};
const subscribed = await this.rpcCall<{ asset_identifier: string, value: string }>('subscribe', params);
const subscribed = await this.rpcCall<{ asset_identifier: string; value: string }>(
'subscribe',
params
);
const listener = (event: NftEvent) => {
if (event.asset_identifier === subscribed.asset_identifier && event.value.hex === subscribed.value) {
if (
event.asset_identifier === subscribed.asset_identifier &&
event.value.hex === subscribed.value
) {
update(event);
}
};
Expand Down

0 comments on commit d0bfd31

Please sign in to comment.