Skip to content

Commit 13fafc5

Browse files
committed
fix: extract duplicated stripUndefined function to shared utility
1 parent a2a4ee9 commit 13fafc5

File tree

6 files changed

+21
-52
lines changed

6 files changed

+21
-52
lines changed

src/config/index.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createLogger, type Logger } from "../logging";
88
import type { ClientConfig, ProxyCheckOptions } from "../types";
99
import { DEFAULTS } from "../types/constants";
1010
import { ClientConfigSchema, ProxyCheckOptionsSchema } from "../types/schemas";
11+
import { stripUndefined } from "../utils/object";
1112

1213
/**
1314
* Query parameters interface
@@ -236,18 +237,6 @@ export class ConfigManager {
236237
}
237238
}
238239

239-
/**
240-
* Helper to remove undefined values from an object for exactOptionalPropertyTypes
241-
*/
242-
function stripUndefined<T extends Record<string, unknown>>(obj: T): T {
243-
const result = {} as T;
244-
for (const [key, value] of Object.entries(obj)) {
245-
if (value !== undefined) {
246-
result[key as keyof T] = value as T[keyof T];
247-
}
248-
}
249-
return result;
250-
}
251240

252241
/**
253242
* Validate and merge ProxyCheck options

src/services/check.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ProxyCheckValidationError } from "../errors";
1313
import type { AddressCheckResult, CheckResponse, ProxyCheckOptions } from "../types";
1414
import { API_ENDPOINTS } from "../types/constants";
1515
import { ProxyCheckOptionsSchema } from "../types/schemas";
16+
import { stripUndefined } from "../utils/object";
1617
import { BaseService } from "./base";
1718

1819
/**
@@ -216,21 +217,12 @@ export class CheckService extends BaseService {
216217
private validateOptions(options: ProxyCheckOptions): ProxyCheckOptions {
217218
try {
218219
const parsed = ProxyCheckOptionsSchema.parse(options) as any;
219-
return this.stripUndefined(parsed) as ProxyCheckOptions;
220+
return stripUndefined(parsed) as ProxyCheckOptions;
220221
} catch (_error) {
221222
throw new ProxyCheckValidationError("Invalid options provided", "options", options);
222223
}
223224
}
224225

225-
private stripUndefined<T extends Record<string, unknown>>(obj: T): T {
226-
const result = {} as T;
227-
for (const [key, value] of Object.entries(obj)) {
228-
if (value !== undefined) {
229-
result[key as keyof T] = value as T[keyof T];
230-
}
231-
}
232-
return result;
233-
}
234226

235227
/**
236228
* Add blocking logic for single address checks

src/services/listing.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ProxyCheckValidationError } from "../errors";
66
import type { ListOptions, ListResponse } from "../types";
77
import { API_ENDPOINTS } from "../types/constants";
88
import { ListOptionsSchema } from "../types/schemas";
9+
import { stripUndefined } from "../utils/object";
910
import { BaseService } from "./base";
1011

1112
/**
@@ -229,19 +230,10 @@ export class ListingService extends BaseService {
229230
private validateOptions(options: ListOptions): ListOptions {
230231
try {
231232
const parsed = ListOptionsSchema.parse(options) as any;
232-
return this.stripUndefined(parsed) as ListOptions;
233+
return stripUndefined(parsed) as ListOptions;
233234
} catch (_error) {
234235
throw new ProxyCheckValidationError("Invalid list options provided", "options", options);
235236
}
236237
}
237238

238-
private stripUndefined<T extends Record<string, unknown>>(obj: T): T {
239-
const result = {} as T;
240-
for (const [key, value] of Object.entries(obj)) {
241-
if (value !== undefined) {
242-
result[key as keyof T] = value as T[keyof T];
243-
}
244-
}
245-
return result;
246-
}
247239
}

src/services/rules.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ProxyCheckValidationError } from "../errors";
66
import type { RuleOptions, RuleResponse } from "../types";
77
import { API_ENDPOINTS } from "../types/constants";
88
import { RuleOptionsSchema } from "../types/schemas";
9+
import { stripUndefined } from "../utils/object";
910
import { BaseService } from "./base";
1011

1112
/**
@@ -225,19 +226,10 @@ export class RulesService extends BaseService {
225226
private validateOptions(options: RuleOptions): RuleOptions {
226227
try {
227228
const parsed = RuleOptionsSchema.parse(options) as any;
228-
return this.stripUndefined(parsed) as RuleOptions;
229+
return stripUndefined(parsed) as RuleOptions;
229230
} catch (_error) {
230231
throw new ProxyCheckValidationError("Invalid rule options provided", "options", options);
231232
}
232233
}
233234

234-
private stripUndefined<T extends Record<string, unknown>>(obj: T): T {
235-
const result = {} as T;
236-
for (const [key, value] of Object.entries(obj)) {
237-
if (value !== undefined) {
238-
result[key as keyof T] = value as T[keyof T];
239-
}
240-
}
241-
return result;
242-
}
243235
}

src/services/stats.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ProxyCheckValidationError } from "../errors";
66
import type { StatsOptions, StatsResponse } from "../types";
77
import { API_ENDPOINTS } from "../types/constants";
88
import { StatsOptionsSchema } from "../types/schemas";
9+
import { stripUndefined } from "../utils/object";
910
import { BaseService } from "./base";
1011

1112
/**
@@ -205,19 +206,10 @@ export class StatsService extends BaseService {
205206
private validateOptions(options: StatsOptions): StatsOptions {
206207
try {
207208
const parsed = StatsOptionsSchema.parse(options) as any;
208-
return this.stripUndefined(parsed) as StatsOptions;
209+
return stripUndefined(parsed) as StatsOptions;
209210
} catch (_error) {
210211
throw new ProxyCheckValidationError("Invalid stats options provided", "options", options);
211212
}
212213
}
213214

214-
private stripUndefined<T extends Record<string, unknown>>(obj: T): T {
215-
const result = {} as T;
216-
for (const [key, value] of Object.entries(obj)) {
217-
if (value !== undefined) {
218-
result[key as keyof T] = value as T[keyof T];
219-
}
220-
}
221-
return result;
222-
}
223215
}

src/utils/object.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Helper to remove undefined values from an object for exactOptionalPropertyTypes
3+
*/
4+
export function stripUndefined<T extends Record<string, unknown>>(obj: T): T {
5+
const result = {} as T;
6+
for (const [key, value] of Object.entries(obj)) {
7+
if (value !== undefined) {
8+
result[key as keyof T] = value as T[keyof T];
9+
}
10+
}
11+
return result;
12+
}

0 commit comments

Comments
 (0)