fix(typescript-client): Add error handling to all API methods (#139)

Previously, most methods in HindsightClient would silently return
undefined when API calls failed (e.g., connection refused). Only
the `recall` method had proper error checking.

This change adds a `validateResponse` helper method and applies it
consistently to all API methods:
- retain
- retainBatch
- recall
- reflect
- listMemories
- createBank
- getBankProfile

Now all methods properly throw an error with details when the API
request fails, instead of returning undefined.
This commit is contained in:
Nicolò Boschi 2026-01-09 14:25:10 +01:00 committed by GitHub
parent a6798f7e2a
commit 0d0abaaa9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -78,6 +78,16 @@ export class HindsightClient {
);
}
/**
* Validates the API response and throws an error if the request failed.
*/
private validateResponse<T>(response: { data?: T; error?: unknown }, operation: string): T {
if (!response.data) {
throw new Error(`${operation} failed: ${JSON.stringify(response.error || 'Unknown error')}`);
}
return response.data;
}
/**
* Retain a single memory for a bank.
*/
@ -126,7 +136,7 @@ export class HindsightClient {
body: { items: [item], async: options?.async },
});
return response.data!;
return this.validateResponse(response, 'retain');
}
/**
@ -160,7 +170,7 @@ export class HindsightClient {
},
});
return response.data!;
return this.validateResponse(response, 'retainBatch');
}
/**
@ -198,11 +208,7 @@ export class HindsightClient {
},
});
if (!response.data) {
throw new Error(`API returned no data: ${JSON.stringify(response.error || 'Unknown error')}`);
}
return response.data;
return this.validateResponse(response, 'recall');
}
/**
@ -223,7 +229,7 @@ export class HindsightClient {
},
});
return response.data!;
return this.validateResponse(response, 'reflect');
}
/**
@ -244,7 +250,7 @@ export class HindsightClient {
},
});
return response.data!;
return this.validateResponse(response, 'listMemories');
}
/**
@ -264,7 +270,7 @@ export class HindsightClient {
},
});
return response.data!;
return this.validateResponse(response, 'createBank');
}
/**
@ -276,7 +282,7 @@ export class HindsightClient {
path: { bank_id: bankId },
});
return response.data!;
return this.validateResponse(response, 'getBankProfile');
}
}