Skip to content

Commit c0b164a

Browse files
committed
refactor: streamline various service and handler implementations across modules
1 parent 70f2993 commit c0b164a

File tree

166 files changed

+1161
-2187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+1161
-2187
lines changed

src/common/axios/axios.service.ts

Lines changed: 55 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {
1717
StopXrayCommand,
1818
} from '@remnawave/node-contract';
1919

20-
import { GetNodeJwtCommand, IGetNodeJwtResponse } from '@modules/keygen/commands/get-node-jwt';
20+
import { GetNodeJwtCommand } from '@modules/keygen/commands/get-node-jwt';
2121

22-
import { ICommandResponse } from '../types/command-response.type';
22+
import { fail, ok, TResult } from '../types';
2323

2424
@Injectable()
2525
export class AxiosService {
@@ -37,15 +37,16 @@ export class AxiosService {
3737

3838
public async setJwt() {
3939
try {
40-
const response = await this.getNodeJwtCommand();
41-
const jwt = response.response;
40+
const result = await this.commandBus.execute(new GetNodeJwtCommand());
4241

43-
if (!jwt) {
42+
if (!result.isOk) {
4443
throw new Error(
4544
'There are a problem with the JWT token. Please restart Remnawave.',
4645
);
4746
}
4847

48+
const jwt = result.response;
49+
4950
this.axiosInstance.defaults.headers.common['Authorization'] = `Bearer ${jwt.jwtToken}`;
5051

5152
const httpsAgent = new https.Agent({
@@ -66,12 +67,6 @@ export class AxiosService {
6667
}
6768
}
6869

69-
private async getNodeJwtCommand(): Promise<ICommandResponse<IGetNodeJwtResponse>> {
70-
return this.commandBus.execute<GetNodeJwtCommand, ICommandResponse<IGetNodeJwtResponse>>(
71-
new GetNodeJwtCommand(),
72-
);
73-
}
74-
7570
private getNodeUrl(url: string, path: string, port: null | number): string {
7671
const protocol = 'https';
7772
const portSuffix = port ? `:${port}` : '';
@@ -87,7 +82,7 @@ export class AxiosService {
8782
data: StartXrayCommand.Request,
8883
url: string,
8984
port: null | number,
90-
): Promise<ICommandResponse<StartXrayCommand.Response>> {
85+
): Promise<TResult<StartXrayCommand.Response>> {
9186
const nodeUrl = this.getNodeUrl(url, StartXrayCommand.url, port);
9287
try {
9388
const response = await this.axiosInstance.post<StartXrayCommand.Response>(
@@ -98,74 +93,56 @@ export class AxiosService {
9893
},
9994
);
10095

101-
return {
102-
isOk: true,
103-
response: response.data,
104-
};
96+
return ok(response.data);
10597
} catch (error) {
10698
if (error instanceof AxiosError) {
10799
// this.logger.error(
108100
// 'Error in Axios StartXray Request:',
109101
// JSON.stringify(error.message),
110102
// );
111103

112-
return {
113-
isOk: false,
114-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)),
115-
};
104+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)));
116105
} else {
117106
this.logger.error('Error in Axios StartXray Request:', error);
118107

119-
return {
120-
isOk: false,
121-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(
122-
JSON.stringify(error) ?? 'Unknown error',
123-
),
124-
};
108+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error)));
125109
}
126110
}
127111
}
128112

129113
public async stopXray(
130114
url: string,
131115
port: null | number,
132-
): Promise<ICommandResponse<StopXrayCommand.Response>> {
116+
): Promise<TResult<StopXrayCommand.Response>> {
133117
const nodeUrl = this.getNodeUrl(url, StopXrayCommand.url, port);
134118
try {
135119
const response = await this.axiosInstance.get<StopXrayCommand.Response>(nodeUrl);
136120

137-
return {
138-
isOk: true,
139-
response: response.data,
140-
};
121+
return ok(response.data);
141122
} catch (error) {
142123
if (error instanceof AxiosError) {
143124
this.logger.error(
144125
'Error in Axios StopXray Request:',
145126
JSON.stringify(error.message),
146127
);
147128

148-
return {
149-
isOk: false,
150-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)),
151-
};
129+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)));
152130
} else {
153131
this.logger.error('Error in Axios StopXray Request:', error);
154132

155-
return {
156-
isOk: false,
157-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(
133+
return fail(
134+
ERRORS.NODE_ERROR_WITH_MSG.withMessage(
158135
JSON.stringify(error) ?? 'Unknown error',
159136
),
160-
};
137+
);
161138
}
162139
}
163140
}
164141

165142
public async getNodeHealth(
166143
url: string,
167144
port: null | number,
168-
): Promise<ICommandResponse<GetNodeHealthCheckCommand.Response['response']>> {
145+
): Promise<TResult<GetNodeHealthCheckCommand.Response['response']>> {
169146
try {
170147
const nodeUrl = this.getNodeUrl(url, GetNodeHealthCheckCommand.url, port);
171148
const { data } = await this.axiosInstance.get<GetNodeHealthCheckCommand.Response>(
@@ -175,25 +152,18 @@ export class AxiosService {
175152
},
176153
);
177154

178-
return {
179-
isOk: true,
180-
response: data.response,
181-
};
155+
return ok(data.response);
182156
} catch (error) {
183157
if (error instanceof AxiosError) {
184-
return {
185-
isOk: false,
186-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)),
187-
};
158+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)));
188159
} else {
189160
this.logger.error('Error in Axios getNodeHealth:', error);
190161

191-
return {
192-
isOk: false,
193-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(
162+
return fail(
163+
ERRORS.NODE_ERROR_WITH_MSG.withMessage(
194164
JSON.stringify(error) ?? 'Unknown error',
195165
),
196-
};
166+
);
197167
}
198168
}
199169
}
@@ -206,7 +176,7 @@ export class AxiosService {
206176
data: GetUsersStatsCommand.Request,
207177
url: string,
208178
port: null | number,
209-
): Promise<ICommandResponse<GetUsersStatsCommand.Response>> {
179+
): Promise<TResult<GetUsersStatsCommand.Response>> {
210180
const nodeUrl = this.getNodeUrl(url, GetUsersStatsCommand.url, port);
211181

212182
try {
@@ -218,74 +188,57 @@ export class AxiosService {
218188
},
219189
);
220190

221-
return {
222-
isOk: true,
223-
response: response.data,
224-
};
191+
return ok(response.data);
225192
} catch (error) {
226193
if (error instanceof AxiosError) {
227194
this.logger.error(
228195
`Error in Axios getUsersStats: ${error.message}, JSON: ${JSON.stringify(error.response?.data)}`,
229196
);
230197

231-
return {
232-
isOk: false,
233-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)),
234-
};
198+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)));
235199
} else {
236200
this.logger.error('Error in getUsersStats:', error);
237201

238-
return {
239-
isOk: false,
240-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(
202+
return fail(
203+
ERRORS.NODE_ERROR_WITH_MSG.withMessage(
241204
JSON.stringify(error) ?? 'Unknown error',
242205
),
243-
};
206+
);
244207
}
245208
}
246209
}
247210

248211
public async getSystemStats(
249212
url: string,
250213
port: null | number,
251-
): Promise<ICommandResponse<GetSystemStatsCommand.Response>> {
214+
): Promise<TResult<GetSystemStatsCommand.Response>> {
252215
const nodeUrl = this.getNodeUrl(url, GetSystemStatsCommand.url, port);
253216

254217
try {
255218
const response = await this.axiosInstance.get<GetSystemStatsCommand.Response>(nodeUrl, {
256219
timeout: 15_000,
257220
});
258221

259-
return {
260-
isOk: true,
261-
response: response.data,
262-
};
222+
return ok(response.data);
263223
} catch (error) {
264224
if (error instanceof AxiosError) {
265225
// this.logger.error(`Error in axios request: ${JSON.stringify(error.message)}`);
266226

267227
if (error.code === '500') {
268-
return {
269-
isOk: false,
270-
...ERRORS.NODE_ERROR_500_WITH_MSG.withMessage(
271-
JSON.stringify(error.message),
272-
),
273-
};
228+
return fail(
229+
ERRORS.NODE_ERROR_500_WITH_MSG.withMessage(JSON.stringify(error.message)),
230+
);
274231
}
275232

276-
return {
277-
isOk: false,
278-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)),
279-
};
233+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)));
280234
} else {
281235
this.logger.error('Error in getSystemStats:', error);
282236

283-
return {
284-
isOk: false,
285-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(
237+
return fail(
238+
ERRORS.NODE_ERROR_WITH_MSG.withMessage(
286239
JSON.stringify(error) ?? 'Unknown error',
287240
),
288-
};
241+
);
289242
}
290243
}
291244
}
@@ -294,7 +247,7 @@ export class AxiosService {
294247
data: GetCombinedStatsCommand.Request,
295248
url: string,
296249
port: null | number,
297-
): Promise<ICommandResponse<GetCombinedStatsCommand.Response['response']>> {
250+
): Promise<TResult<GetCombinedStatsCommand.Response['response']>> {
298251
const nodeUrl = this.getNodeUrl(url, GetCombinedStatsCommand.url, port);
299252

300253
try {
@@ -303,34 +256,24 @@ export class AxiosService {
303256
data,
304257
);
305258

306-
return {
307-
isOk: true,
308-
response: nodeResult.data.response,
309-
};
259+
return ok(nodeResult.data.response);
310260
} catch (error) {
311261
if (error instanceof AxiosError) {
312262
if (error.code === '500') {
313-
return {
314-
isOk: false,
315-
...ERRORS.NODE_ERROR_500_WITH_MSG.withMessage(
316-
JSON.stringify(error.message),
317-
),
318-
};
263+
return fail(
264+
ERRORS.NODE_ERROR_500_WITH_MSG.withMessage(JSON.stringify(error.message)),
265+
);
319266
}
320267

321-
return {
322-
isOk: false,
323-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)),
324-
};
268+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)));
325269
} else {
326270
this.logger.error('Error in getAllInboundStats:', error);
327271

328-
return {
329-
isOk: false,
330-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(
272+
return fail(
273+
ERRORS.NODE_ERROR_WITH_MSG.withMessage(
331274
JSON.stringify(error) ?? 'Unknown error',
332275
),
333-
};
276+
);
334277
}
335278
}
336279
}
@@ -343,35 +286,28 @@ export class AxiosService {
343286
data: AddUserCommand.Request,
344287
url: string,
345288
port: null | number,
346-
): Promise<ICommandResponse<AddUserCommand.Response>> {
289+
): Promise<TResult<AddUserCommand.Response>> {
347290
const nodeUrl = this.getNodeUrl(url, AddUserCommand.url, port);
348291

349292
try {
350293
const response = await this.axiosInstance.post<AddUserCommand.Response>(nodeUrl, data, {
351294
timeout: 20_000,
352295
});
353296

354-
return {
355-
isOk: true,
356-
response: response.data,
357-
};
297+
return ok(response.data);
358298
} catch (error) {
359299
if (error instanceof AxiosError) {
360300
this.logger.error(`Error in axios request: ${error.message}`);
361301

362-
return {
363-
isOk: false,
364-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)),
365-
};
302+
return fail(ERRORS.NODE_ERROR_WITH_MSG.withMessage(JSON.stringify(error.message)));
366303
} else {
367304
this.logger.error('Error in addUser:', error);
368305

369-
return {
370-
isOk: false,
371-
...ERRORS.NODE_ERROR_WITH_MSG.withMessage(
306+
return fail(
307+
ERRORS.NODE_ERROR_WITH_MSG.withMessage(
372308
JSON.stringify(error) ?? 'Unknown error',
373309
),
374-
};
310+
);
375311
}
376312
}
377313
}
@@ -380,7 +316,7 @@ export class AxiosService {
380316
data: RemoveUserCommand.Request,
381317
url: string,
382318
port: null | number,
383-
): Promise<ICommandResponse<RemoveUserCommand.Response>> {
319+
): Promise<TResult<RemoveUserCommand.Response>> {
384320
const nodeUrl = this.getNodeUrl(url, RemoveUserCommand.url, port);
385321

386322
try {
@@ -392,21 +328,15 @@ export class AxiosService {
392328
},
393329
);
394330

395-
return {
396-
isOk: true,
397-
response: response.data,
398-
};
331+
return ok(response.data);
399332
} catch (error) {
400333
if (error instanceof AxiosError) {
401334
this.logger.error('Error in deleteUser:', error.response?.data);
402335
} else {
403336
this.logger.error('Error in deleteUser:', error);
404337
}
405338

406-
return {
407-
isOk: false,
408-
...ERRORS.INTERNAL_SERVER_ERROR,
409-
};
339+
return fail(ERRORS.INTERNAL_SERVER_ERROR);
410340
}
411341
}
412342
}

0 commit comments

Comments
 (0)