Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions hindsight-clients/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -126,7 +136,7 @@ export class HindsightClient {
body: { items: [item], async: options?.async },
});

return response.data!;
return this.validateResponse(response, 'retain');
}

/**
Expand Down Expand Up @@ -160,7 +170,7 @@ export class HindsightClient {
},
});

return response.data!;
return this.validateResponse(response, 'retainBatch');
}

/**
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -223,7 +229,7 @@ export class HindsightClient {
},
});

return response.data!;
return this.validateResponse(response, 'reflect');
}

/**
Expand All @@ -244,7 +250,7 @@ export class HindsightClient {
},
});

return response.data!;
return this.validateResponse(response, 'listMemories');
}

/**
Expand All @@ -264,7 +270,7 @@ export class HindsightClient {
},
});

return response.data!;
return this.validateResponse(response, 'createBank');
}

/**
Expand All @@ -276,7 +282,7 @@ export class HindsightClient {
path: { bank_id: bankId },
});

return response.data!;
return this.validateResponse(response, 'getBankProfile');
}
}

Expand Down
Loading