Skip to content

Commit 4445a6d

Browse files
committed
refactor: improve type safety and error handling across modules
- Added `.local(true)` to datetime validation in user creation schema - Updated Xray config schemas to use `.passthrough()` for flexible config handling - Simplified error messages and removed unnecessary console logs - Enhanced null safety in subscription and config generation modules - Improved type handling in Xray config service and inbound synchronization
2 parents 07acc41 + 528205b commit 4445a6d

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

libs/contract/commands/users/create-user.command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ export namespace CreateUserCommand {
9292
.string({
9393
invalid_type_error: 'Invalid date format',
9494
})
95-
.datetime({ message: 'Invalid date format', offset: true })
95+
.datetime({ message: 'Invalid date format', offset: true, local: true })
9696
.transform((str) => new Date(str))
9797
.describe('Date format: 2025-01-17T15:38:45.065Z')
9898
.optional(),
9999
lastTrafficResetAt: z
100100
.string({
101101
invalid_type_error: 'Invalid date format',
102102
})
103-
.datetime({ message: 'Invalid date format', offset: true })
103+
.datetime({ message: 'Invalid date format', offset: true, local: true })
104104
.transform((str) => new Date(str))
105105
.describe('Date format: 2025-01-17T15:38:45.065Z')
106106
.optional(),

libs/contract/commands/xray/get-config.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export namespace GetXrayConfigCommand {
77

88
export const ResponseSchema = z.object({
99
response: z.object({
10-
config: z.object({}),
10+
config: z.object({}).passthrough(),
1111
}),
1212
});
1313

libs/contract/commands/xray/update-config.command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export namespace UpdateXrayConfigCommand {
66
export const url = REST_API.XRAY.UPDATE_CONFIG;
77
export const TSQ_url = url;
88

9-
export const RequestSchema = z.object({});
9+
export const RequestSchema = z.object({}).passthrough();
1010

1111
export type Request = z.infer<typeof RequestSchema>;
1212

1313
export const ResponseSchema = z.object({
1414
response: z.object({
15-
config: z.object({}),
15+
config: z.object({}).passthrough(),
1616
}),
1717
});
1818

libs/contract/constants/errors/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export const ERRORS = {
302302
httpCode: 500,
303303
withMessage: (message: string) => ({
304304
code: 'A061',
305-
message: message,
305+
message,
306306
httpCode: 500,
307307
}),
308308
},

src/modules/subscription/generators/by-subcription-type/generate-singbox-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ export class SingBoxConfiguration {
372372

373373
this.add_outbound(outbound);
374374
} catch (error) {
375-
console.log(error);
375+
// silence error
376376
}
377377
}
378378
}

src/modules/subscription/generators/by-subcription-type/generate-xray-config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ export class XrayLinksGenerator {
112112
}
113113

114114
if (params.network === 'ws') {
115-
Object.assign(payload, {
116-
heartbeatPeriod: params.additionalParams?.heartbeatPeriod,
117-
});
115+
if (params.additionalParams?.heartbeatPeriod) {
116+
Object.assign(payload, {
117+
heartbeatPeriod: params.additionalParams?.heartbeatPeriod,
118+
});
119+
}
118120
}
119121

120122
const tlsParams: Record<string, unknown> = {};

src/modules/subscription/utils/format-hosts.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ export class FormatHosts {
105105
const port = inputHost.port;
106106
const network = inbound.streamSettings?.network || 'tcp';
107107

108-
let streamSettings: WebSocketObject | xHttpObject | RawObject | TcpObject;
108+
let streamSettings: WebSocketObject | xHttpObject | RawObject | TcpObject | undefined;
109109
let pathFromConfig: string | undefined;
110110
let hostFromConfig: string | undefined;
111111
let additionalParams: FormattedHosts['additionalParams'] | undefined;
112112
let headerType: string | undefined;
113113

114114
switch (network) {
115115
case 'xhttp': {
116-
const settings = inbound.streamSettings!.xhttpSettings as xHttpObject;
116+
const settings = inbound.streamSettings?.xhttpSettings as xHttpObject;
117117
streamSettings = settings;
118118
pathFromConfig = settings?.path;
119119
hostFromConfig = settings?.host;
@@ -131,20 +131,20 @@ export class FormatHosts {
131131
break;
132132
}
133133
case 'ws': {
134-
const settings = inbound.streamSettings!.wsSettings as WebSocketObject;
134+
const settings = inbound.streamSettings?.wsSettings as WebSocketObject;
135135
streamSettings = settings;
136136
pathFromConfig = settings?.path;
137137
break;
138138
}
139139
case 'raw':
140-
streamSettings = inbound.streamSettings!.rawSettings as RawObject;
140+
streamSettings = inbound.streamSettings?.rawSettings as RawObject;
141141
break;
142142
case 'tcp': {
143143
if (inbound.protocol === 'shadowsocks') {
144144
break;
145145
}
146146

147-
const settings = inbound.streamSettings!.tcpSettings as TcpObject;
147+
const settings = inbound.streamSettings?.tcpSettings as TcpObject;
148148
streamSettings = settings;
149149
headerType = settings?.header?.type;
150150

@@ -262,7 +262,7 @@ export class FormatHosts {
262262
try {
263263
return new FormatHosts(config, hosts, user, configService).generate();
264264
} catch (error) {
265-
console.log(error);
265+
// silence error
266266
return [];
267267
}
268268
}

src/modules/xray-config/xray-config.service.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,17 @@ export class XrayConfigService {
264264
return securityChanged || networkChanged;
265265
})
266266
.map((configInbound) => {
267-
const existingInbound = existingInbounds.response!.find(
267+
const existingInbound = existingInbounds.response?.find(
268268
(ei) => ei.tag === configInbound.tag,
269-
)!;
270-
return {
271-
uuid: existingInbound.uuid,
272-
tag: existingInbound.tag,
273-
type: existingInbound.type,
274-
security: configInbound.security || null,
275-
network: configInbound.network || null,
276-
};
269+
);
270+
271+
// TODO: check this
272+
273+
if (!existingInbound) {
274+
throw new Error(`Inbound with tag ${configInbound.tag} not found`);
275+
}
276+
277+
return existingInbound;
277278
});
278279

279280
if (inboundsToUpdate.length) {
@@ -294,7 +295,7 @@ export class XrayConfigService {
294295
return;
295296
}
296297

297-
this.logger.log('Inbounds synced successfully');
298+
this.logger.log('Inbounds synced/updated successfully');
298299
} catch (error) {
299300
if (error instanceof Error) {
300301
this.logger.error('Failed to sync inbounds:', error.message);

0 commit comments

Comments
 (0)