Skip to content

Commit

Permalink
Rename rest library to clash-client
Browse files Browse the repository at this point in the history
  • Loading branch information
csuvajit committed Feb 11, 2024
1 parent 8435e90 commit e724a2a
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 106 deletions.
2 changes: 0 additions & 2 deletions apps/service-auth/src/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MongoDbModule } from '@app/mongodb';
import { RedisModule } from '@app/redis';
import { RestModule } from '@app/rest';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
Expand All @@ -13,7 +12,6 @@ import { JwtStrategy } from './strategies';
ConfigModule.forRoot({ isGlobal: true }),
MongoDbModule,
RedisModule,
RestModule,
JwtModule.registerAsync({
useFactory: (configService: ConfigService) => ({
secret: configService.getOrThrow('JWT_SECRET'),
Expand Down
2 changes: 0 additions & 2 deletions apps/service-auth/src/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { JwtUser } from '@app/auth';
import { Tokens } from '@app/constants';
import { MongoDbService } from '@app/mongodb';
import { RedisClient, RedisService } from '@app/redis';
import RestHandler from '@app/rest/rest.module';
import { Inject, Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Db } from 'mongodb';
Expand All @@ -13,7 +12,6 @@ export class AuthService {
constructor(
@Inject(Tokens.MONGODB) private readonly db: Db,
@Inject(Tokens.REDIS) private readonly redis: RedisClient,
@Inject(Tokens.REST) private readonly restHandler: RestHandler,
private readonly redisService: RedisService,
private readonly mongoDbService: MongoDbService,
private readonly jwtService: JwtService,
Expand Down
9 changes: 7 additions & 2 deletions apps/service-capital/src/service-capital.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { ClashClientModule } from '@app/clash-client';
import { MongoDbModule } from '@app/mongodb';
import { RedisModule } from '@app/redis';
import { RestModule } from '@app/rest';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ServiceCapitalController } from './service-capital.controller';
import { CapitalService } from './service-capital.service';

@Module({
imports: [ConfigModule.forRoot({ isGlobal: true }), MongoDbModule, RedisModule, RestModule],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongoDbModule,
RedisModule,
ClashClientModule,
],
controllers: [ServiceCapitalController],
providers: [CapitalService],
})
Expand Down
12 changes: 5 additions & 7 deletions apps/service-capital/src/service-capital.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ClashClient, ClashClientService } from '@app/clash-client';
import { Collections, RedisKeyPrefixes, Tokens } from '@app/constants';
import {
CapitalRaidRemindersEntity,
Expand All @@ -13,8 +14,6 @@ import {
RedisService,
getRedisKey,
} from '@app/redis';
import { RestService } from '@app/rest';
import RestHandler from '@app/rest/rest.module';
import { Inject, Injectable, Logger, NotFoundException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
Expand All @@ -32,11 +31,10 @@ export class CapitalService {
private configService: ConfigService,
@Inject(Tokens.MONGODB) private db: Db,
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
private restService: RestService,
private redisService: RedisService,
private mongoDbService: MongoDbService,

private clashClientService: ClashClientService,
@Inject(Tokens.CLASH_CLIENT) private clashClient: ClashClient,
@Inject(Collections.CAPITAL_RAID_SEASONS)
private raidSeasonsCollection: Collection<CapitalRaidSeasonsEntity>,
@Inject(Collections.RAID_REMINDERS)
Expand Down Expand Up @@ -78,10 +76,10 @@ export class CapitalService {
}

private async fetchCapitalRaidWeekend(clanTag: string) {
const clan = await this.restService.getClan(clanTag);
const clan = await this.clashClientService.getClan(clanTag);
if (!clan) return null;

const { body, res } = await this.restHandler.getCapitalRaidSeasons(clanTag, { limit: 1 });
const { body, res } = await this.clashClient.getCapitalRaidSeasons(clanTag, { limit: 1 });
if (!res.ok || !body.items.length) return null;

const season = body.items.at(0)!;
Expand Down
9 changes: 7 additions & 2 deletions apps/service-clans/src/service-clans.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { ClashClientModule } from '@app/clash-client';
import { MongoDbModule } from '@app/mongodb';
import { RedisModule } from '@app/redis';
import { RestModule } from '@app/rest';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ServiceClansController } from './service-clans.controller';
import { ClansService } from './service-clans.service';

@Module({
imports: [ConfigModule.forRoot({ isGlobal: true }), MongoDbModule, RedisModule, RestModule],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongoDbModule,
RedisModule,
ClashClientModule,
],
controllers: [ServiceClansController],
providers: [ClansService],
})
Expand Down
5 changes: 2 additions & 3 deletions apps/service-clans/src/service-clans.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ClashClient } from '@app/clash-client';
import { Collections, Tokens } from '@app/constants';
import { PlayersEntity } from '@app/entities';
import { MongoDbService, TrackedClanList } from '@app/mongodb';
import { RedisClient, RedisService } from '@app/redis';
import RestHandler from '@app/rest/rest.module';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Collection, Db } from 'mongodb';
Expand All @@ -13,10 +13,9 @@ export class ClansService {
private configService: ConfigService,
@Inject(Tokens.MONGODB) private db: Db,
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
@Inject(Tokens.CLASH_CLIENT) private clashClient: ClashClient,
private redisService: RedisService,
private mongoDbService: MongoDbService,

@Inject(Collections.PLAYERS)
private lastSeenCollection: Collection<PlayersEntity>,
) {}
Expand Down
22 changes: 0 additions & 22 deletions apps/service-players/src/service-players.controller.spec.ts

This file was deleted.

9 changes: 7 additions & 2 deletions apps/service-players/src/service-players.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { ClashClientModule } from '@app/clash-client';
import { MongoDbModule } from '@app/mongodb';
import { RedisModule } from '@app/redis';
import { RestModule } from '@app/rest';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ServicePlayersController } from './service-players.controller';
import { PlayersService } from './service-players.service';

@Module({
imports: [ConfigModule.forRoot({ isGlobal: true }), MongoDbModule, RedisModule, RestModule],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongoDbModule,
RedisModule,
ClashClientModule,
],
controllers: [ServicePlayersController],
providers: [PlayersService],
})
Expand Down
4 changes: 2 additions & 2 deletions apps/service-players/src/service-players.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ClashClient } from '@app/clash-client';
import { Collections, Tokens } from '@app/constants';
import { PlayersEntity } from '@app/entities';
import { MongoDbService, TrackedClanList } from '@app/mongodb';
import { RedisClient, RedisService } from '@app/redis';
import RestHandler from '@app/rest/rest.module';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Collection, Db } from 'mongodb';
Expand All @@ -13,7 +13,7 @@ export class PlayersService {
private configService: ConfigService,
@Inject(Tokens.MONGODB) private db: Db,
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
@Inject(Tokens.CLASH_CLIENT) private clashClient: ClashClient,
private redisService: RedisService,
private mongoDbService: MongoDbService,

Expand Down
22 changes: 0 additions & 22 deletions apps/service-ranking/src/service-ranking.controller.spec.ts

This file was deleted.

9 changes: 7 additions & 2 deletions apps/service-ranking/src/service-ranking.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { ClashClientModule } from '@app/clash-client';
import { MongoDbModule } from '@app/mongodb';
import { RedisModule } from '@app/redis';
import { RestModule } from '@app/rest';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ServiceRankingController } from './service-ranking.controller';
import { RankingService } from './service-ranking.service';

@Module({
imports: [ConfigModule.forRoot({ isGlobal: true }), MongoDbModule, RedisModule, RestModule],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongoDbModule,
RedisModule,
ClashClientModule,
],
controllers: [ServiceRankingController],
providers: [RankingService],
})
Expand Down
4 changes: 2 additions & 2 deletions apps/service-ranking/src/service-ranking.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ClashClient } from '@app/clash-client';
import { Collections, Tokens } from '@app/constants';
import { PlayersEntity } from '@app/entities';
import { MongoDbService, TrackedClanList } from '@app/mongodb';
import { RedisClient, RedisService } from '@app/redis';
import RestHandler from '@app/rest/rest.module';
import { Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Collection, Db } from 'mongodb';
Expand All @@ -13,7 +13,7 @@ export class RankingService {
private configService: ConfigService,
@Inject(Tokens.MONGODB) private db: Db,
@Inject(Tokens.REDIS) private redis: RedisClient,
@Inject(Tokens.REST) private restHandler: RestHandler,
@Inject(Tokens.CLASH_CLIENT) private clashClient: ClashClient,
private redisService: RedisService,
private mongoDbService: MongoDbService,

Expand Down
9 changes: 7 additions & 2 deletions apps/service-wars/src/service-wars.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { MongoDbModule } from '@app/mongodb';
import { RedisModule } from '@app/redis';
import * as repositories from '@app/repositories';
import { RestModule } from '@app/rest';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ServiceWarsController } from './service-wars.controller';
import { WarsService } from './service-wars.service';
import { ClashClientModule } from '@app/clash-client';

@Module({
imports: [ConfigModule.forRoot({ isGlobal: true }), MongoDbModule, RedisModule, RestModule],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongoDbModule,
RedisModule,
ClashClientModule,
],
controllers: [ServiceWarsController],
providers: [WarsService, ...Object.values(repositories)],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
RequestOptions,
Result,
} from 'clashofclans.js';
import { RestService } from './rest.service';
import { ClashClientService } from './clash-client.service';

class ReqHandler extends RequestHandler {
private readonly logger = new Logger('ClashApiRest');
Expand All @@ -28,7 +28,7 @@ class ReqHandler extends RequestHandler {
}
}

export default class RestHandler extends RESTManager {
export class ClashClient extends RESTManager {
public constructor({
rateLimit,
baseURL,
Expand All @@ -53,10 +53,10 @@ export default class RestHandler extends RESTManager {
}
}

const RestProvider: Provider = {
provide: Tokens.REST,
useFactory: (configService: ConfigService): RestHandler => {
return new RestHandler({
const ClashClientProvider: Provider = {
provide: Tokens.CLASH_CLIENT,
useFactory: (configService: ConfigService): ClashClient => {
return new ClashClient({
rateLimit: 0,
baseURL: configService.getOrThrow('CLASH_API_BASE_URL'),
keys: configService.getOrThrow<string>('CLASH_API_TOKENS').split(','),
Expand All @@ -67,7 +67,7 @@ const RestProvider: Provider = {

@Module({
imports: [RedisModule],
providers: [RestService, RestProvider],
exports: [RestService, RestProvider],
providers: [ClashClientService, ClashClientProvider],
exports: [ClashClientService, ClashClientProvider],
})
export class RestModule {}
export class ClashClientModule {}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Tokens } from '@app/constants';
import { RedisService } from '@app/redis';
import { Inject, Injectable } from '@nestjs/common';
import RestHandler from './rest.module';
import { ClashClient } from './clash-client.module';

@Injectable()
export class RestService {
export class ClashClientService {
constructor(
@Inject(Tokens.REST) private readonly restHandler: RestHandler,
@Inject(Tokens.CLASH_CLIENT) private readonly clashClient: ClashClient,
private redisService: RedisService,
) {}

async getClan(clanTag: string) {
const cached = await this.redisService.getClan(clanTag);
if (cached) return cached;

const { body, res } = await this.restHandler.getClan(clanTag);
const { body, res } = await this.clashClient.getClan(clanTag);
if (!res.ok) return null;

return body;
Expand Down
2 changes: 2 additions & 0 deletions libs/clash-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './clash-client.module';
export * from './clash-client.service';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist/libs/rest"
"outDir": "../../dist/libs/clash-client"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
Expand Down
2 changes: 1 addition & 1 deletion libs/constants/src/constants.values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export enum Tokens {
REDIS_PUB = 'REDIS_PUB',
REDIS_SUB = 'REDIS_SUB',
ELASTIC = 'ELASTIC',
REST = 'REST',
CLASH_CLIENT = 'CLASH_CLIENT',
}

export enum TokenType {
Expand Down
2 changes: 0 additions & 2 deletions libs/rest/src/index.ts

This file was deleted.

Loading

0 comments on commit e724a2a

Please sign in to comment.