Skip to content

Commit dd2d581

Browse files
committed
refactor(api): relocate RequestId to lib for proper access
Moves the `RequestId` class from `routes/_middleware.dart` to a new, more appropriate location at `lib/src/models/request_id.dart`. This resolves an architectural issue where a helper class in `lib/` could not access a model defined in `routes/`. All references to `RequestId` in the root middleware and the new `ResponseHelper` have been updated to use the new, correct import path. This change improves code structure and enables further refactoring.
1 parent 92ccee9 commit dd2d581

File tree

3 files changed

+41
-44
lines changed

3 files changed

+41
-44
lines changed

lib/src/helpers/response_helper.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import 'dart:io';
22

33
import 'package:dart_frog/dart_frog.dart';
4+
import 'package:ht_api/src/models/request_id.dart';
45
import 'package:ht_shared/ht_shared.dart';
56

6-
import '../../routes/_middleware.dart';
7-
87
/// A utility class to simplify the creation of standardized API responses.
98
abstract final class ResponseHelper {
109
/// Creates a standardized success JSON response.

lib/src/models/request_id.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/// {@template request_id}
2+
/// A wrapper class holding a unique identifier (UUID v4) generated for each
3+
/// incoming HTTP request.
4+
///
5+
/// **Purpose:**
6+
/// The primary role of this ID is **traceability for logging and debugging**.
7+
/// It allows developers to follow the entire lifecycle of a *single request*
8+
/// through various middleware, route handlers, repository calls, and potential
9+
/// external service interactions by searching logs for this specific ID.
10+
/// If an error occurs during a request, this ID provides a way to isolate all
11+
/// related log entries for that specific transaction, simplifying debugging.
12+
///
13+
/// **Scope:**
14+
/// - The ID is **transient** for the request itself; it exists only during the
15+
/// request-response cycle.
16+
/// - It is **not persisted** in the main application database alongside models
17+
/// like Headlines or Categories.
18+
/// - Its value lies in being included in **persistent logs**.
19+
///
20+
/// **Distinction from other IDs:**
21+
/// - **User ID:** Identifies the authenticated user making the request. Often
22+
/// logged alongside the `request_id` for user-specific debugging.
23+
/// - **Session ID:** Tracks a user's session across multiple requests.
24+
/// - **Correlation ID:** Often generated by the *client* and passed in headers
25+
/// to link related requests initiated by the client for a larger workflow.
26+
///
27+
/// **Implementation:**
28+
/// This class ensures type safety when providing and reading the request ID
29+
/// from the Dart Frog context using `context.provide<RequestId>` and
30+
/// `context.read<RequestId>()`. This prevents potential ambiguity if other raw
31+
/// strings were provided into the context.
32+
/// {@endtemplate}
33+
class RequestId {
34+
/// {@macro request_id}
35+
const RequestId(this.id);
36+
37+
/// The unique identifier string (UUID v4).
38+
final String id;
39+
}

routes/_middleware.dart

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:dart_frog/dart_frog.dart';
22
import 'package:ht_api/src/config/app_dependencies.dart';
3+
import 'package:ht_api/src/models/request_id.dart';
34
import 'package:ht_api/src/middlewares/error_handler.dart';
45
import 'package:ht_api/src/rbac/permission_service.dart';
56
import 'package:ht_api/src/registry/model_registry.dart';
@@ -15,48 +16,6 @@ import 'package:ht_shared/ht_shared.dart';
1516
import 'package:logging/logging.dart';
1617
import 'package:uuid/uuid.dart';
1718

18-
// --- Request ID Wrapper ---
19-
20-
/// {@template request_id}
21-
/// A wrapper class holding a unique identifier (UUID v4) generated for each
22-
/// incoming HTTP request.
23-
///
24-
/// **Purpose:**
25-
/// The primary role of this ID is **traceability for logging and debugging**.
26-
/// It allows developers to follow the entire lifecycle of a *single request*
27-
/// through various middleware, route handlers, repository calls, and potential
28-
/// external service interactions by searching logs for this specific ID.
29-
/// If an error occurs during a request, this ID provides a way to isolate all
30-
/// related log entries for that specific transaction, simplifying debugging.
31-
///
32-
/// **Scope:**
33-
/// - The ID is **transient** for the request itself; it exists only during the
34-
/// request-response cycle.
35-
/// - It is **not persisted** in the main application database alongside models
36-
/// like Headlines or Categories.
37-
/// - Its value lies in being included in **persistent logs**.
38-
///
39-
/// **Distinction from other IDs:**
40-
/// - **User ID:** Identifies the authenticated user making the request. Often
41-
/// logged alongside the `request_id` for user-specific debugging.
42-
/// - **Session ID:** Tracks a user's session across multiple requests.
43-
/// - **Correlation ID:** Often generated by the *client* and passed in headers
44-
/// to link related requests initiated by the client for a larger workflow.
45-
///
46-
/// **Implementation:**
47-
/// This class ensures type safety when providing and reading the request ID
48-
/// from the Dart Frog context using `context.provide<RequestId>` and
49-
/// `context.read<RequestId>()`. This prevents potential ambiguity if other raw
50-
/// strings were provided into the context.
51-
/// {@endtemplate}
52-
class RequestId {
53-
/// {@macro request_id}
54-
const RequestId(this.id);
55-
56-
/// The unique identifier string (UUID v4).
57-
final String id;
58-
}
59-
6019
// --- Middleware Definition ---
6120
final _log = Logger('RootMiddleware');
6221

0 commit comments

Comments
 (0)