Code ReviewIntermediate~15 min
Review: API Response Handling
typescriptapierror-handlingvalidation
Code Review Task
Please review the following TypeScript code.
Background
This is a function that fetches and processes data from an external API. There are issues with error handling and data validation.
Review Points
- HTTP status code handling
- Response data validation
- Error handling
- Network error handling
Your Answer
productApi.tstypescript
interface Product { id: number; name: string; price: number; stock: number;} async function fetchProducts(): Promise<Product[]> { const response = await fetch('/api/products'); const data = await response.json() as Product[]; return data;} async function getProductById(id: number): Promise<Product> { const response = await fetch(`/api/products/${id}`); const product = await response.json(); return product;} export { fetchProducts, getProductById };行番号をクリックしてコメントを追加(Shift+クリックで範囲選択)
Minimum 100 characters required
Add comments and enter at least 100 characters in the summary
Model Answer
Submit your answer or click "Show" to view the model answer.