Skip to content

Commit eaa5f1c

Browse files
committed
add first command, remove default http controller
1 parent b503c6d commit eaa5f1c

6 files changed

Lines changed: 88 additions & 7 deletions

File tree

src/app.module.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Module } from '@nestjs/common';
2-
import { AppController } from './app.controller';
3-
import { AppService } from './app.service';
2+
import { MinterApiService } from './services/minter-api/minter-api.service';
3+
import { ApiCommand } from "./commands/api/ApiCommand";
44

55
@Module({
66
imports: [],
7-
controllers: [AppController],
8-
providers: [AppService],
7+
controllers: [],
8+
providers: [
9+
MinterApiService,
10+
...ApiCommand.registerWithSubCommands()
11+
],
912
})
1013
export class AppModule {}

src/commands/api/ApiCommand.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Command, CommandRunner } from 'nest-commander';
2+
import {CandidateCommand} from "./CandidateCommand";
3+
4+
5+
@Command({
6+
name: 'api',
7+
subCommands: [CandidateCommand],
8+
description: 'Api for Minter Node v2',
9+
// options: { isDefault: true }
10+
})
11+
export class ApiCommand extends CommandRunner {
12+
run(passedParams: string[], options?: Record<string, any>): Promise<void> {
13+
this.command.help()
14+
}
15+
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {Option, CommandRunner, SubCommand} from 'nest-commander';
2+
3+
4+
@SubCommand({
5+
name: 'candidate',
6+
arguments: '<public_key>',
7+
argsDescription: {'public_key':'Public key'},
8+
description: 'Candidate returns candidate’s info by provided public key',
9+
})
10+
export class CandidateCommand extends CommandRunner {
11+
12+
async run(inputs: string[],
13+
options: { height?: number, not_show_stakes?: boolean},
14+
): Promise<void> {
15+
16+
console.log("candidate");
17+
console.log(`inputs: ${inputs[0]}`);
18+
console.log(`height: ${options.height}`);
19+
console.log(`not_show_stakes: ${options.not_show_stakes}`);
20+
return Promise.resolve(undefined);
21+
}
22+
23+
@Option({
24+
flags: '--height [number]',
25+
name: 'Height',
26+
description: 'Blockchain state height for the current request. Optional, the last default state of the node is used',
27+
})
28+
parseNumber(val: string): number {
29+
return Number(val);
30+
}
31+
32+
@Option({
33+
flags: '--not_show_stakes [boolean]',
34+
description: 'Do not display a list of steaks. Note: used_slots, uniq_users, min_stake will be filled',
35+
defaultValue: true,
36+
})
37+
parseBoolean(val: string): boolean {
38+
return JSON.parse(val);
39+
}
40+
41+
}

src/main.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import {NestFactory} from '@nestjs/core';
21
import {AppModule} from './app.module';
32
import {CommandFactory} from "nest-commander";
43

54
async function bootstrap() {
6-
const app = await NestFactory.create(AppModule);
7-
await app.listen(process.env.PORT ?? 3000);
5+
// const app = await NestFactory.create(AppModule);
6+
// await app.listen(process.env.PORT ?? 3000);
87
await CommandFactory.run(AppModule);
98
}
109

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { MinterApiService } from './minter-api.service';
3+
4+
describe('MinterApiService', () => {
5+
let service: MinterApiService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [MinterApiService],
10+
}).compile();
11+
12+
service = module.get<MinterApiService>(MinterApiService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
@Injectable()
4+
export class MinterApiService {}

0 commit comments

Comments
 (0)