Skip to content

Commit 7d6fd8a

Browse files
Copilothotlong
andcommitted
Fix ESLint warnings: replace any with proper types and remove unused variables
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent e59c76e commit 7d6fd8a

4 files changed

Lines changed: 51 additions & 44 deletions

File tree

packages/data-objectstack/src/cache/MetadataCache.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ describe('MetadataCache', () => {
412412

413413
it('should handle very large cache', async () => {
414414
const largeCache = new MetadataCache({ maxSize: 10000, ttl: 60000 });
415-
const fetcher = vi.fn(async () => ({ data: 'test' }));
416415

417416
// Add many entries
418417
for (let i = 0; i < 1000; i++) {

packages/data-objectstack/src/cache/MetadataCache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Represents a cached schema entry with metadata
1111
*/
1212
interface CachedSchema {
13-
data: any;
13+
data: unknown;
1414
timestamp: number;
1515
accessCount: number;
1616
lastAccessed: number;
@@ -88,7 +88,7 @@ export class MetadataCache {
8888
* @param fetcher - Async function to fetch data if not in cache
8989
* @returns Promise resolving to the cached or fetched data
9090
*/
91-
async get(key: string, fetcher: () => Promise<any>): Promise<any> {
91+
async get<T = unknown>(key: string, fetcher: () => Promise<T>): Promise<T> {
9292
const now = Date.now();
9393
const cached = this.cache.get(key);
9494

@@ -106,7 +106,7 @@ export class MetadataCache {
106106
this.cache.delete(key);
107107
this.cache.set(key, cached);
108108

109-
return cached.data;
109+
return cached.data as T;
110110
} else {
111111
// Expired entry - remove it
112112
this.cache.delete(key);
@@ -129,7 +129,7 @@ export class MetadataCache {
129129
* @param key - Cache key
130130
* @param data - Data to cache
131131
*/
132-
private set(key: string, data: any): void {
132+
private set(key: string, data: unknown): void {
133133
const now = Date.now();
134134

135135
// Check if we need to evict entries

packages/data-objectstack/src/errors.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class ObjectStackError extends Error {
2222
message: string,
2323
public code: string,
2424
public statusCode?: number,
25-
public details?: any
25+
public details?: Record<string, unknown>
2626
) {
2727
super(message);
2828
this.name = 'ObjectStackError';
@@ -54,7 +54,7 @@ export class ObjectStackError extends Error {
5454
export class MetadataNotFoundError extends ObjectStackError {
5555
constructor(
5656
objectName: string,
57-
details?: any
57+
details?: Record<string, unknown>
5858
) {
5959
super(
6060
`Metadata not found for object: ${objectName}`,
@@ -83,8 +83,8 @@ export class BulkOperationError extends ObjectStackError {
8383
operation: 'create' | 'update' | 'delete',
8484
public successCount: number,
8585
public failureCount: number,
86-
public errors: Array<{ index: number; error: any }>,
87-
details?: any
86+
public errors: Array<{ index: number; error: unknown }>,
87+
details?: Record<string, unknown>
8888
) {
8989
super(
9090
`Bulk ${operation} operation failed: ${successCount} succeeded, ${failureCount} failed`,
@@ -109,7 +109,7 @@ export class BulkOperationError extends ObjectStackError {
109109
const failureRate = total > 0 ? this.failureCount / total : 0;
110110

111111
return {
112-
operation: this.details.operation,
112+
operation: this.details?.operation as string,
113113
total: total,
114114
successful: this.successCount,
115115
failed: this.failureCount,
@@ -126,7 +126,7 @@ export class ConnectionError extends ObjectStackError {
126126
constructor(
127127
message: string,
128128
public url?: string,
129-
details?: any,
129+
details?: Record<string, unknown>,
130130
statusCode?: number
131131
) {
132132
super(
@@ -145,7 +145,7 @@ export class ConnectionError extends ObjectStackError {
145145
export class AuthenticationError extends ObjectStackError {
146146
constructor(
147147
message: string = 'Authentication failed',
148-
details?: any,
148+
details?: Record<string, unknown>,
149149
statusCode?: number
150150
) {
151151
super(
@@ -174,7 +174,7 @@ export class ValidationError extends ObjectStackError {
174174
message: string,
175175
public field?: string,
176176
public validationErrors?: Array<{ field: string; message: string }>,
177-
details?: any
177+
details?: Record<string, unknown>
178178
) {
179179
super(
180180
message,
@@ -204,9 +204,9 @@ export class ValidationError extends ObjectStackError {
204204
* @param context - Additional context for debugging
205205
* @returns Appropriate error instance
206206
*/
207-
export function createErrorFromResponse(response: any, context?: string): ObjectStackError {
208-
const status = response?.status || response?.statusCode || 500;
209-
const message = response?.message || response?.statusText || 'Unknown error';
207+
export function createErrorFromResponse(response: Record<string, unknown>, context?: string): ObjectStackError {
208+
const status = (response?.status as number) || (response?.statusCode as number) || 500;
209+
const message = (response?.message as string) || (response?.statusText as string) || 'Unknown error';
210210
const details = {
211211
context,
212212
response: {
@@ -232,13 +232,13 @@ export function createErrorFromResponse(response: any, context?: string): Object
232232
return new ObjectStackError(message, 'NOT_FOUND', 404, details);
233233

234234
case 400:
235-
return new ValidationError(message, undefined, response?.data?.errors, details);
235+
return new ValidationError(message, undefined, (response?.data as Record<string, unknown>)?.errors as Array<{ field: string; message: string }>, details);
236236

237237
case 503:
238-
return new ConnectionError(message, response?.config?.url, details, 503);
238+
return new ConnectionError(message, (response?.config as Record<string, unknown>)?.url as string, details, 503);
239239

240240
case 504:
241-
return new ConnectionError(message, response?.config?.url, details, 504);
241+
return new ConnectionError(message, (response?.config as Record<string, unknown>)?.url as string, details, 504);
242242

243243
default:
244244
return new ObjectStackError(message, 'UNKNOWN_ERROR', status, details);
@@ -259,15 +259,16 @@ function extractObjectName(context?: string): string {
259259
/**
260260
* Type guard to check if an error is an ObjectStackError
261261
*/
262-
export function isObjectStackError(error: any): error is ObjectStackError {
262+
export function isObjectStackError(error: unknown): error is ObjectStackError {
263263
return error instanceof ObjectStackError;
264264
}
265265

266266
/**
267267
* Type guard to check if an error is a specific ObjectStack error type
268268
*/
269269
export function isErrorType<T extends ObjectStackError>(
270-
error: any,
270+
error: unknown,
271+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
271272
errorClass: new (...args: any[]) => T
272273
): error is T {
273274
return error instanceof errorClass;

packages/data-objectstack/src/index.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
* });
4141
* ```
4242
*/
43-
export class ObjectStackAdapter<T = any> implements DataSource<T> {
43+
export class ObjectStackAdapter<T = unknown> implements DataSource<T> {
4444
private client: ObjectStackClient;
4545
private connected: boolean = false;
4646
private metadataCache: MetadataCache;
@@ -67,9 +67,10 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
6767
try {
6868
await this.client.connect();
6969
this.connected = true;
70-
} catch (error: any) {
70+
} catch (error: unknown) {
71+
const errorMessage = error instanceof Error ? error.message : 'Failed to connect to ObjectStack server';
7172
throw new ConnectionError(
72-
error?.message || 'Failed to connect to ObjectStack server',
73+
errorMessage,
7374
undefined,
7475
{ originalError: error }
7576
);
@@ -85,7 +86,7 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
8586
await this.connect();
8687

8788
const queryOptions = this.convertQueryParams(params);
88-
const result: any = await this.client.data.find<T>(resource, queryOptions);
89+
const result: unknown = await this.client.data.find<T>(resource, queryOptions);
8990

9091
// Handle legacy/raw array response (e.g. from some mock servers or non-OData endpoints)
9192
if (Array.isArray(result)) {
@@ -98,13 +99,14 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
9899
};
99100
}
100101

102+
const resultObj = result as { value?: T[]; count?: number };
101103
return {
102-
data: result.value || [],
103-
total: result.count || (result.value ? result.value.length : 0),
104+
data: resultObj.value || [],
105+
total: resultObj.count || (resultObj.value ? resultObj.value.length : 0),
104106
// Calculate page number safely
105107
page: params?.$skip && params.$top ? Math.floor(params.$skip / params.$top) + 1 : 1,
106108
pageSize: params?.$top,
107-
hasMore: params?.$top ? (result.value?.length || 0) === params.$top : false,
109+
hasMore: params?.$top ? (resultObj.value?.length || 0) === params.$top : false,
108110
};
109111
}
110112

@@ -117,9 +119,9 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
117119
try {
118120
const record = await this.client.data.get<T>(resource, String(id));
119121
return record;
120-
} catch (error) {
122+
} catch (error: unknown) {
121123
// If record not found, return null instead of throwing
122-
if ((error as any)?.status === 404) {
124+
if ((error as Record<string, unknown>)?.status === 404) {
123125
return null;
124126
}
125127
throw error;
@@ -172,7 +174,7 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
172174
return await this.client.data.createMany<T>(resource, data);
173175

174176
case 'delete': {
175-
const ids = data.map(item => (item as any).id).filter(Boolean);
177+
const ids = data.map(item => (item as Record<string, unknown>).id).filter(Boolean);
176178

177179
if (ids.length === 0) {
178180
// Track which items are missing IDs
@@ -190,23 +192,25 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
190192

191193
case 'update': {
192194
// Check if client supports updateMany
195+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
193196
if (typeof (this.client.data as any).updateMany === 'function') {
194197
try {
198+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
195199
const updateMany = (this.client.data as any).updateMany;
196200
return await updateMany(resource, data) as T[];
197-
} catch (error) {
201+
} catch {
198202
// If updateMany is not supported, fall back to individual updates
199-
console.warn('updateMany not supported, falling back to individual updates');
203+
// Silently fallback without logging
200204
}
201205
}
202206

203207
// Fallback: Process updates individually with detailed error tracking
204208
const results: T[] = [];
205-
const errors: Array<{ index: number; error: any }> = [];
209+
const errors: Array<{ index: number; error: unknown }> = [];
206210

207211
for (let i = 0; i < data.length; i++) {
208212
const item = data[i];
209-
const id = (item as any).id;
213+
const id = (item as Record<string, unknown>).id;
210214

211215
if (!id) {
212216
errors.push({ index: i, error: 'Missing ID' });
@@ -216,8 +220,9 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
216220
try {
217221
const result = await this.client.data.update<T>(resource, String(id), item);
218222
results.push(result);
219-
} catch (error: any) {
220-
errors.push({ index: i, error: error.message || error });
223+
} catch (error: unknown) {
224+
const errorMessage = error instanceof Error ? error.message : String(error);
225+
errors.push({ index: i, error: errorMessage });
221226
}
222227
}
223228

@@ -242,7 +247,7 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
242247
400
243248
);
244249
}
245-
} catch (error: any) {
250+
} catch (error: unknown) {
246251
// If it's already a BulkOperationError, re-throw it
247252
if (error instanceof BulkOperationError) {
248253
throw error;
@@ -254,9 +259,10 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
254259
}
255260

256261
// Wrap other errors in BulkOperationError with proper error tracking
262+
const errorMessage = error instanceof Error ? error.message : String(error);
257263
const errors = data.map((_, index) => ({
258264
index,
259-
error: error.message || String(error)
265+
error: errorMessage
260266
}));
261267

262268
throw new BulkOperationError(
@@ -315,7 +321,7 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
315321
* @param objectName - Object name
316322
* @returns Promise resolving to the object schema
317323
*/
318-
async getObjectSchema(objectName: string): Promise<any> {
324+
async getObjectSchema(objectName: string): Promise<unknown> {
319325
await this.connect();
320326

321327
try {
@@ -326,9 +332,10 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
326332
});
327333

328334
return schema;
329-
} catch (error: any) {
335+
} catch (error: unknown) {
330336
// Check if it's a 404 error
331-
if (error?.status === 404 || error?.statusCode === 404) {
337+
const errorObj = error as Record<string, unknown>;
338+
if (errorObj?.status === 404 || errorObj?.statusCode === 404) {
332339
throw new MetadataNotFoundError(objectName, { originalError: error });
333340
}
334341

@@ -337,7 +344,7 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
337344
throw error;
338345
}
339346

340-
throw createErrorFromResponse(error, `getObjectSchema(${objectName})`);
347+
throw createErrorFromResponse(errorObj, `getObjectSchema(${objectName})`);
341348
}
342349
}
343350

@@ -384,7 +391,7 @@ export class ObjectStackAdapter<T = any> implements DataSource<T> {
384391
* });
385392
* ```
386393
*/
387-
export function createObjectStackAdapter<T = any>(config: {
394+
export function createObjectStackAdapter<T = unknown>(config: {
388395
baseUrl: string;
389396
token?: string;
390397
fetch?: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;

0 commit comments

Comments
 (0)