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

Commit

Permalink
♻️ Fix types and add args interface
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Dec 5, 2018
1 parent 1e8cf53 commit 9795fa7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/commands/delegate/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ 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 {
static args = [
{
Expand All @@ -40,7 +44,8 @@ export default class GetCommand extends BaseCommand {
};

async run(): Promise<void> {
const { args: { usernames: usernamesStr } } = this.parse(GetCommand);
const { args } = this.parse(GetCommand);
const { usernames: usernamesStr }: Args = args;
const usernames: ReadonlyArray<string> = usernamesStr.split(',').filter(Boolean);
const req = usernames.map(username => ({
query: {
Expand Down
17 changes: 11 additions & 6 deletions src/commands/delegate/voters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ import { getAPIClient } from '../../utils/api';
import { SORT_FIELDS } from '../../utils/constants';
import { query } from '../../utils/query';

const MAXIMUM_LIMIT = 100;
const DEFAULT_LIMIT = 10;
const DEFAULT_OFFSET = 0;
const DEFAULT_SORT = 'balance:desc';
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: string, offsetStr: string, sortStr: string): QueryParameters => {
const limit = parseInt(limitStr, 10);
const offset = parseInt(offsetStr, 10);
Expand Down Expand Up @@ -95,11 +99,12 @@ export default class VotersCommand extends BaseCommand {

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

const usernames: ReadonlyArray<string> = usernamesStr.split(',').filter(Boolean);
const usernames = usernamesStr.split(',').filter(Boolean);

const { limit, offset, sort } = processFlagInputs(
limitStr as string,
Expand Down
9 changes: 7 additions & 2 deletions src/commands/delegate/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { getAPIClient } from '../../utils/api';
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;
Expand Down Expand Up @@ -93,11 +97,12 @@ export default class VotesCommand extends BaseCommand {

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

const addresses: ReadonlyArray<string> = addressesStr.split(',').filter(Boolean);
const { addresses: addressesStr }: Args = args;
const addresses = addressesStr.split(',').filter(Boolean);
const { limit, offset, sort } = processFlagInputs(
limitStr as string,
offsetStr as string,
Expand Down
2 changes: 1 addition & 1 deletion test/commands/delegate/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('delegate:get', () => {
setupTest()
.stdout()
.command(['delegate:get'])
.catch(error =>
.catch((error: Error) =>
expect(error.message).to.contain('Missing 1 required arg'),
)
.it('should throw an error when arg is not provided');
Expand Down
14 changes: 7 additions & 7 deletions test/commands/delegate/voters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('delegate:voters', () => {
describe('delegate:voters', () => {
setupTest()
.command(['delegate:voters'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain('Missing 1 required arg');
})
.it('should throw an error when arg is not provided');
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('delegate:voters', () => {
describe('delegate:voters --limit=xxx', () => {
setupTest()
.command(['delegate:voters', usernames[0], '--limit=wronglimit'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Limit must be an integer and greater than 0',
);
Expand All @@ -169,7 +169,7 @@ describe('delegate:voters', () => {

setupTest()
.command(['delegate:voters', usernames[0], '--limit=0'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Limit must be an integer and greater than 0',
);
Expand All @@ -178,7 +178,7 @@ describe('delegate:voters', () => {

setupTest()
.command(['delegate:voters', usernames[0], '--limit=101'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Maximum limit amount is 100',
);
Expand Down Expand Up @@ -216,7 +216,7 @@ describe('delegate:voters', () => {
describe('delegate:voters --offset=xxx', () => {
setupTest()
.command(['delegate:voters', usernames[0], '--offset=wrongoffset'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Offset must be an integer and greater than or equal to 0',
);
Expand All @@ -225,7 +225,7 @@ describe('delegate:voters', () => {

setupTest()
.command(['delegate:voters', usernames[0], '--offset=-1'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Offset must be an integer and greater than or equal to 0',
);
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('delegate:voters', () => {
describe('delegate:voters --sort=xxx', () => {
setupTest()
.command(['delegate:voters', usernames[0], '--sort=wrongsort'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Sort must be one of: publicKey:asc, publicKey:desc, balance:asc, balance:desc, username:asc, username:desc',
);
Expand Down
14 changes: 7 additions & 7 deletions test/commands/delegate/votes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('delegate:votes', () => {
describe('delegate:votes', () => {
setupTest()
.command(['delegate:votes'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain('Missing 1 required arg');
})
.it('should throw an error when arg is not provided');
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('delegate:votes', () => {
describe('delegate:votes --limit=xxx', () => {
setupTest()
.command(['delegate:votes', addresses[0], '--limit=wronglimit'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Limit must be an integer and greater than 0',
);
Expand All @@ -170,7 +170,7 @@ describe('delegate:votes', () => {

setupTest()
.command(['delegate:votes', addresses[0], '--limit=0'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Limit must be an integer and greater than 0',
);
Expand All @@ -179,7 +179,7 @@ describe('delegate:votes', () => {

setupTest()
.command(['delegate:votes', addresses[0], '--limit=101'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Maximum limit amount is 100',
);
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('delegate:votes', () => {
describe('delegate:votes --offset=xxx', () => {
setupTest()
.command(['delegate:votes', addresses[0], '--offset=wrongoffset'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Offset must be an integer and greater than or equal to 0',
);
Expand All @@ -226,7 +226,7 @@ describe('delegate:votes', () => {

setupTest()
.command(['delegate:votes', addresses[0], '--offset=-1'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Offset must be an integer and greater than or equal to 0',
);
Expand Down Expand Up @@ -264,7 +264,7 @@ describe('delegate:votes', () => {
describe('delegate:votes --sort=xxx', () => {
setupTest()
.command(['delegate:votes', addresses[0], '--sort=wrongsort'])
.catch(error => {
.catch((error: Error) => {
return expect(error.message).to.contain(
'Sort must be one of: balance:asc, balance:desc, username:asc, username:desc',
);
Expand Down

0 comments on commit 9795fa7

Please sign in to comment.