Improves Anthropic models API response validation

Enhances type checking and validation for API response structure

Adds more robust type validation to ensure:
- Correct response object structure
- Presence of required model fields
- Type safety for model data retrieval

Prevents potential runtime errors by implementing stricter type checks
This commit is contained in:
AlexKucera committed 2025-10-24 13:11:06 +02:00
1 parent e96ef65200
commit def8bbcfe5
1 file changed
+10 -2
+10 -2
View File
@@ -136,11 +136,19 @@ export class AnthropicClient {
}
// Validate basic response structure
const data = response.json as any;
if (!data || !Array.isArray(data.data)) {
const data = response.json as unknown;
if (!data || typeof data !== 'object' || !('data' in data) || !Array.isArray((data as { data: unknown }).data)) {
throw new Error('Invalid models API response structure');
}
// Validate that each model has required fields
const modelsData = (data as { data: unknown[] }).data;
if (!modelsData.every((model: unknown) =>
model && typeof model === 'object' && 'id' in model && typeof (model as { id: unknown }).id === 'string'
)) {
throw new Error('Invalid model structure in API response');
}
return data as ModelsListResponse;
} catch (error) {
if (error instanceof Error) {