Skip to content

Commit d6d5427

Browse files
committed
docs(README): update documentation on success responses, error handling, and request tracing
- Add information about standardized success responses and error handling - Include details on request traceability using unique request IDs - Update architecture section with new packages and shared core components - Revise API endpoint documentation to reflect standard response structure - Remove redundant error handling information
1 parent b40a98b commit d6d5427

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

README.md

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* `category`
1919
* `source`
2020
* `country`
21+
* **Standardized Success Responses:** Returns consistent JSON success responses wrapped in a `SuccessApiResponse` structure, including request metadata (`requestId`, `timestamp`).
22+
* **Standardized Error Handling:** Returns consistent JSON error responses via centralized middleware (`lib/src/middlewares/error_handler.dart`) for predictable client-side handling.
23+
* **Request Traceability:** Generates a unique `requestId` (UUID v4) for each incoming request, included in success response metadata and available for server-side logging via context.
2124
* **In-Memory Demo Mode:** Utilizes pre-loaded fixture data (`lib/src/fixtures/`) for demonstration and development purposes, simulating a live backend without external dependencies.
2225
* **Standardized Error Handling:** Returns consistent JSON error responses via centralized middleware (`lib/src/middlewares/error_handler.dart`) for predictable client-side handling.
2326

@@ -28,16 +31,17 @@
2831
* **Architecture:** Layered architecture leveraging shared packages and a generic API pattern.
2932
* **Key Packages & Shared Core:**
3033
* `dart_frog`: The web framework foundation.
31-
* `ht_shared`: Contains shared data models (`Headline`, `Category`, `Source`, `Country`, etc.) used across the HT ecosystem.
34+
* `uuid`: Used to generate unique request IDs.
35+
* `ht_shared`: Contains shared data models (`Headline`, `Category`, `Source`, `Country`, `SuccessApiResponse`, `ResponseMetadata`, etc.) used across the HT ecosystem.
3236
* `ht_data_client`: Defines the generic `HtDataClient<T>` interface for data access operations.
3337
* `ht_data_inmemory`: Provides the `HtDataInMemoryClient<T>` implementation, used here for the demo mode, seeded with fixture data.
3438
* `ht_data_repository`: Defines the generic `HtDataRepository<T>` which abstracts the data client, providing a clean interface to the API route handlers.
3539
* `ht_http_client`: Provides custom HTTP exceptions (`HtHttpException` subtypes) used for consistent error signaling, even by the in-memory client.
3640
* **Key Patterns:**
3741
* **Generic Repository Pattern:** `HtDataRepository<T>` provides a type-safe abstraction over data fetching logic. Route handlers interact with repositories, not directly with data clients.
3842
* **Generic Data Endpoint:** A single set of route handlers (`/api/v1/data/index.dart`, `/api/v1/data/[id].dart`) serves multiple data models.
39-
* **Model Registry:** A central map (`lib/src/registry/model_registry.dart`) links model name strings (from the `?model=` query parameter) to model-specific configurations (`ModelConfig`), primarily containing serialization/deserialization functions (`fromJson`, `toJson`) and ID extraction logic (`getId`).
40-
* **Dependency Injection (Dart Frog Providers):** Middleware (`routes/_middleware.dart`) is used to instantiate and provide singleton instances of each `HtDataRepository<T>` (configured with the in-memory client and fixture data) and the `ModelRegistryMap`. Route-specific middleware (`routes/api/v1/data/_middleware.dart`) resolves the requested model, validates it, and provides the corresponding `ModelConfig` and model name string to the handler.
43+
* **Model Registry:** A central map (`lib/src/registry/model_registry.dart`) links model name strings (from the `?model=` query parameter) to model-specific configurations (`ModelConfig`), primarily containing serialization functions (`fromJson`) and ID extraction logic (`getId`).
44+
* **Dependency Injection (Dart Frog Providers):** Middleware (`routes/_middleware.dart`) is used to instantiate and provide singleton instances of each `HtDataRepository<T>` (configured with the in-memory client and fixture data), the `ModelRegistryMap`, and a unique `RequestId` per request. Route-specific middleware (`routes/api/v1/data/_middleware.dart`) resolves the requested model, validates it, and provides the corresponding `ModelConfig` and model name string to the handler.
4145
* **Centralized Error Handling:** The `errorHandler` middleware intercepts exceptions (especially `HtHttpException` subtypes and `FormatException`) and maps them to standardized JSON error responses with appropriate HTTP status codes.
4246

4347
## API Endpoint: `/api/v1/data`
@@ -46,6 +50,28 @@ This endpoint serves as the single entry point for accessing different data mode
4650

4751
**Supported `model` values:** `headline`, `category`, `source`, `country`
4852

53+
**Standard Response Structure:**
54+
55+
* **Success:**
56+
```json
57+
{
58+
"data": <item_or_paginated_list>,
59+
"metadata": {
60+
"request_id": "unique-uuid-v4-per-request",
61+
"timestamp": "iso-8601-utc-timestamp"
62+
}
63+
}
64+
```
65+
* **Error:**
66+
```json
67+
{
68+
"error": {
69+
"code": "ERROR_CODE_STRING",
70+
"message": "Descriptive error message"
71+
}
72+
}
73+
```
74+
4975
**Operations:**
5076

5177
1. **Get All Items (Collection)**
@@ -55,35 +81,35 @@ This endpoint serves as the single entry point for accessing different data mode
5581
* `limit=<int>`: Limit the number of items returned.
5682
* `startAfterId=<string>`: Paginate results, starting after the item with this ID.
5783
* *Other query parameters*: Passed directly to the repository's `readAllByQuery` method for filtering (e.g., `?model=headline&category=Technology`).
58-
* **Success Response:** `200 OK` with JSON array of items.
84+
* **Success Response:** `200 OK` with `SuccessApiResponse<PaginatedResponse<T>>`.
5985
* **Example:** `GET /api/v1/data?model=headline&limit=10`
6086

6187
2. **Create Item**
6288
* **Method:** `POST`
6389
* **Path:** `/api/v1/data?model=<model_name>`
6490
* **Request Body:** JSON object representing the item to create (ID is usually omitted if auto-generated).
65-
* **Success Response:** `201 Created` with JSON object of the created item (including its ID).
91+
* **Success Response:** `201 Created` with `SuccessApiResponse<T>` containing the created item.
6692
* **Example:** `POST /api/v1/data?model=category` with body `{"name": "Sports", "description": "News about sports"}`
6793

6894
3. **Get Item by ID**
6995
* **Method:** `GET`
7096
* **Path:** `/api/v1/data/<item_id>?model=<model_name>`
71-
* **Success Response:** `200 OK` with JSON object of the requested item.
97+
* **Success Response:** `200 OK` with `SuccessApiResponse<T>` containing the requested item.
7298
* **Error Response:** `404 Not Found` if the ID doesn't exist for the given model.
7399
* **Example:** `GET /api/v1/data/some-headline-id?model=headline`
74100

75101
4. **Update Item by ID**
76102
* **Method:** `PUT`
77103
* **Path:** `/api/v1/data/<item_id>?model=<model_name>`
78104
* **Request Body:** JSON object representing the complete updated item (must include the correct `id`).
79-
* **Success Response:** `200 OK` with JSON object of the updated item.
105+
* **Success Response:** `200 OK` with `SuccessApiResponse<T>` containing the updated item.
80106
* **Error Response:** `404 Not Found`, `400 Bad Request` (e.g., ID mismatch).
81107
* **Example:** `PUT /api/v1/data/some-category-id?model=category` with updated category JSON in the body.
82108

83109
5. **Delete Item by ID**
84110
* **Method:** `DELETE`
85111
* **Path:** `/api/v1/data/<item_id>?model=<model_name>`
86-
* **Success Response:** `204 No Content`.
112+
* **Success Response:** `204 No Content` (No body).
87113
* **Error Response:** `404 Not Found`.
88114
* **Example:** `DELETE /api/v1/data/some-source-id?model=source`
89115

0 commit comments

Comments
 (0)