Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #673 from LiskHQ/657-update_delegate_cmd_ts
Browse files Browse the repository at this point in the history
Update delegate commands to typescript - Closes #657
  • Loading branch information
shuse2 authored Dec 13, 2018
2 parents 4662173 + 2b62d40 commit e0370bf
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 156 deletions.
53 changes: 29 additions & 24 deletions src/commands/delegate/get.js → src/commands/delegate/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,36 @@ import BaseCommand from '../../base';
import { getAPIClient } from '../../utils/api';
import { query } from '../../utils/query';

interface Args {
readonly usernames: string;
}

export default class GetCommand extends BaseCommand {
async run() {
const { args: { usernames } } = this.parse(GetCommand);
static args = [
{
name: 'usernames',
required: true,
description: 'Comma-separated username(s) to get information about.',
},
];

static description = `
Gets delegate information from the blockchain.
`;

static examples = [
'delegate:get lightcurve',
'delegate:get lightcurve,4miners.net',
];

static flags = {
...BaseCommand.flags,
};

async run(): Promise<void> {
const { args } = this.parse(GetCommand);
const { usernames: usernamesStr }: Args = args;
const usernames: ReadonlyArray<string> = usernamesStr.split(',').filter(Boolean);
const req = usernames.map(username => ({
query: {
limit: 1,
Expand All @@ -35,25 +62,3 @@ export default class GetCommand extends BaseCommand {
this.print(results);
}
}

GetCommand.args = [
{
name: 'usernames',
required: true,
description: 'Comma-separated username(s) to get information about.',
parse: input => input.split(',').filter(Boolean),
},
];

GetCommand.flags = {
...BaseCommand.flags,
};

GetCommand.description = `
Gets delegate information from the blockchain.
`;

GetCommand.examples = [
'delegate:get lightcurve',
'delegate:get lightcurve,4miners.net',
];
106 changes: 61 additions & 45 deletions src/commands/delegate/voters.js → src/commands/delegate/voters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@
import { flags as flagParser } from '@oclif/command';
import BaseCommand from '../../base';
import { getAPIClient } from '../../utils/api';
import { query } from '../../utils/query';
import { SORT_FIELDS } from '../../utils/constants';
import { query } from '../../utils/query';

interface Args {
readonly usernames: string;
}

interface QueryParameters {
readonly limit: number;
readonly offset: number;
readonly sort?: string;
}

const MAXIMUM_LIMIT = 100;
const DEFAULT_LIMIT = 10;
const DEFAULT_OFFSET = 0;
const DEFAULT_SORT = 'balance:desc';

const processFlagInputs = (limitStr, offsetStr, sortStr) => {
const processFlagInputs = (limitStr: string, offsetStr: string, sortStr: string): QueryParameters => {
const limit = parseInt(limitStr, 10);
const offset = parseInt(offsetStr, 10);
const sort = sortStr ? sortStr.trim() : undefined;
Expand All @@ -38,7 +51,7 @@ const processFlagInputs = (limitStr, offsetStr, sortStr) => {
) {
throw new Error('Offset must be an integer and greater than or equal to 0');
}
if (!SORT_FIELDS.includes(sort)) {
if (sort !== undefined && !SORT_FIELDS.includes(sort)) {
throw new Error(`Sort must be one of: ${SORT_FIELDS.join(', ')}`);
}

Expand All @@ -50,24 +63,61 @@ const processFlagInputs = (limitStr, offsetStr, sortStr) => {
};

export default class VotersCommand extends BaseCommand {
async run() {
static args = [
{
name: 'usernames',
required: true,
description: 'Comma-separated username(s) to get information about.',
},
];

static description = `
Gets voters information for given delegate(s) from the blockchain.
`;

static examples = [
'delegate:voters lightcurve',
'delegate:voters lightcurve,4miners.net',
'delegate:voters lightcurve,4miners.net --limit 20 --offset 5 --sort publicKey:asc --pretty',
];

static flags = {
...BaseCommand.flags,
limit: flagParser.string({
description: 'Limit applied to results.',
default: '10',
}),
offset: flagParser.string({
description: 'Offset applied to results.',
default: '0',
}),
sort: flagParser.string({
description: 'Fields to sort results by.',
default: DEFAULT_SORT,
}),
};

async run(): Promise<void> {
const {
args: { usernames },
args,
flags: { limit: limitStr, offset: offsetStr, sort: sortStr },
} = this.parse(VotersCommand);
const { usernames: usernamesStr }: Args = args;

const usernames = usernamesStr.split(',').filter(Boolean);

const { limit, offset, sort } = processFlagInputs(
limitStr,
offsetStr,
sortStr,
limitStr as string,
offsetStr as string,
sortStr as string,
);

const req = usernames.map(username => ({
query: {
username,
limit: limit || 10,
offset: offset || 0,
sort: sort || 'balance:desc',
limit: limit || DEFAULT_LIMIT,
offset: offset || DEFAULT_OFFSET,
sort: sort || DEFAULT_SORT,
},
placeholder: {
username,
Expand All @@ -80,37 +130,3 @@ export default class VotersCommand extends BaseCommand {
}
}

VotersCommand.args = [
{
name: 'usernames',
required: true,
description: 'Comma-separated username(s) to get information about.',
parse: input => input.split(',').filter(Boolean),
},
];

VotersCommand.flags = {
...BaseCommand.flags,
limit: flagParser.string({
description: 'Limit applied to results.',
default: '10',
}),
offset: flagParser.string({
description: 'Offset applied to results.',
default: '0',
}),
sort: flagParser.string({
description: 'Fields to sort results by.',
default: 'balance:desc',
}),
};

VotersCommand.description = `
Gets voters information for given delegate(s) from the blockchain.
`;

VotersCommand.examples = [
'delegate:voters lightcurve',
'delegate:voters lightcurve,4miners.net',
'delegate:voters lightcurve,4miners.net --limit 20 --offset 5 --sort publicKey:asc --pretty',
];
99 changes: 54 additions & 45 deletions src/commands/delegate/votes.js → src/commands/delegate/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@
import { flags as flagParser } from '@oclif/command';
import BaseCommand from '../../base';
import { getAPIClient } from '../../utils/api';
import { query } from '../../utils/query';
import { SORT_FIELDS } from '../../utils/constants';
import { query } from '../../utils/query';

interface Args {
readonly addresses: string;
}

const MAXIMUM_LIMIT = 100;
const DEFAULT_LIMIT = 10;
const DEFAULT_OFFSET = 0;
const DEFAULT_SORT = 'balance:desc';

const VOTES_SORT_FIELDS = SORT_FIELDS.filter(
field => !field.includes('publicKey'),
);

const processFlagInputs = (limitStr, offsetStr, sortStr) => {
const processFlagInputs = (limitStr: string, offsetStr: string, sortStr: string) => {
const limit = parseInt(limitStr, 10);
const offset = parseInt(offsetStr, 10);
const sort = sortStr ? sortStr.trim() : undefined;
Expand All @@ -42,7 +49,7 @@ const processFlagInputs = (limitStr, offsetStr, sortStr) => {
) {
throw new Error('Offset must be an integer and greater than or equal to 0');
}
if (!VOTES_SORT_FIELDS.includes(sort)) {
if (sort && !VOTES_SORT_FIELDS.includes(sort)) {
throw new Error(`Sort must be one of: ${VOTES_SORT_FIELDS.join(', ')}`);
}

Expand All @@ -54,24 +61,60 @@ const processFlagInputs = (limitStr, offsetStr, sortStr) => {
};

export default class VotesCommand extends BaseCommand {
async run() {
static args = [
{
name: 'addresses',
required: true,
description: 'Comma-separated address(es) to get information about.',
},
];

static description = `
Gets votes information for given account(s) from the blockchain.
`;

static examples = [
'delegate:votes 13133549779353512613L',
'delegate:votes 13133549779353512613L,16010222169256538112L',
'delegate:votes 13133549779353512613L,16010222169256538112L --limit 20 --offset 5 --sort balance:asc --pretty',
];

static flags = {
...BaseCommand.flags,
limit: flagParser.string({
description: 'Limit applied to results.',
default: '10',
}),
offset: flagParser.string({
description: 'Offset applied to results.',
default: '0',
}),
sort: flagParser.string({
description: 'Fields to sort results by.',
default: DEFAULT_SORT,
}),
};

async run(): Promise<void> {
const {
args: { addresses },
args,
flags: { limit: limitStr, offset: offsetStr, sort: sortStr },
} = this.parse(VotesCommand);

const { addresses: addressesStr }: Args = args;
const addresses = addressesStr.split(',').filter(Boolean);
const { limit, offset, sort } = processFlagInputs(
limitStr,
offsetStr,
sortStr,
limitStr as string,
offsetStr as string,
sortStr as string,
);

const req = addresses.map(address => ({
query: {
address,
limit: limit || 10,
offset: offset || 0,
sort: sort || 'balance:desc',
limit: limit || DEFAULT_LIMIT,
offset: offset || DEFAULT_OFFSET,
sort: sort || DEFAULT_SORT,
},
placeholder: {
address,
Expand All @@ -84,37 +127,3 @@ export default class VotesCommand extends BaseCommand {
}
}

VotesCommand.args = [
{
name: 'addresses',
required: true,
description: 'Comma-separated address(es) to get information about.',
parse: input => input.split(',').filter(Boolean),
},
];

VotesCommand.flags = {
...BaseCommand.flags,
limit: flagParser.string({
description: 'Limit applied to results.',
default: '10',
}),
offset: flagParser.string({
description: 'Offset applied to results.',
default: '0',
}),
sort: flagParser.string({
description: 'Fields to sort results by.',
default: 'balance:desc',
}),
};

VotesCommand.description = `
Gets votes information for given account(s) from the blockchain.
`;

VotesCommand.examples = [
'delegate:votes 13133549779353512613L',
'delegate:votes 13133549779353512613L,16010222169256538112L',
'delegate:votes 13133549779353512613L,16010222169256538112L --limit 20 --offset 5 --sort balance:asc --pretty',
];
Loading

0 comments on commit e0370bf

Please sign in to comment.