2
2
// ignore_for_file: lines_longer_than_80_chars
3
3
4
4
import 'package:dart_frog/dart_frog.dart' ;
5
- import 'package:ht_api/src/registry/model_registry.dart' ; // Adjust import if needed
5
+ import 'package:ht_api/src/registry/model_registry.dart' ;
6
6
7
- /// Middleware for the /api/v1/data route.
7
+ /// Middleware specific to the generic ` /api/v1/data` route path .
8
8
///
9
- /// Responsibilities:
10
- /// 1. Reads the 'model' query parameter from the request.
11
- /// 2. Validates the 'model' parameter (must exist and be a key in the modelRegistry).
12
- /// 3. Reads the globally provided [ModelRegistryMap] .
13
- /// 4. Looks up the corresponding [ModelConfig] for the requested model.
14
- /// 5. Provides the specific [ModelConfig<dynamic>] for the model downstream.
15
- /// 6. Provides the validated model name string downstream.
9
+ /// This middleware is crucial for the functioning of the generic data endpoint.
10
+ /// Its primary responsibilities are:
11
+ ///
12
+ /// 1. **Read and Validate `model` Parameter:** Extracts the `model` query
13
+ /// parameter from the incoming request URL (e.g., `?model=headline`).
14
+ /// It ensures this parameter exists and corresponds to a valid key
15
+ /// within the globally provided [ModelRegistryMap]. If validation fails,
16
+ /// it immediately returns a 400 Bad Request response, preventing the
17
+ /// request from reaching the actual route handlers (`index.dart`, `[id].dart`).
18
+ ///
19
+ /// 2. **Look Up Model Configuration:** Reads the globally provided
20
+ /// [ModelRegistryMap] (injected by `routes/_middleware.dart`) and uses the
21
+ /// validated `modelName` to find the corresponding [ModelConfig] instance.
22
+ /// This config contains type-specific functions (like `fromJson`) needed
23
+ /// by the downstream handlers.
24
+ ///
25
+ /// 3. **Provide Context Downstream:** Injects two crucial pieces of information
26
+ /// into the request context for the route handlers (`index.dart`, `[id].dart`)
27
+ /// to use:
28
+ /// - The specific `ModelConfig<dynamic>` for the requested model.
29
+ /// - The validated `modelName` as a `String`.
30
+ ///
31
+ /// This allows the route handlers under `/api/v1/data/` to operate generically,
32
+ /// using the provided `modelName` to select the correct repository (which are
33
+ /// also provided globally) and the `ModelConfig` for type-specific operations
34
+ /// like deserializing request bodies.
16
35
///
17
36
/// If validation fails (missing/invalid model parameter), it returns a 400 Bad Request response immediately.
18
37
Handler middleware (Handler handler) {
19
38
return (context) async {
20
- // 1. Read the ' model' query parameter
39
+ // --- 1. Read and Validate ` model` Parameter ---
21
40
final modelName = context.request.uri.queryParameters['model' ];
22
-
23
- // 2. Validate the 'model' parameter
24
41
if (modelName == null || modelName.isEmpty) {
25
42
return Response (
26
43
statusCode: 400 ,
27
44
body: 'Bad Request: Missing or empty "model" query parameter.' ,
28
45
);
29
46
}
30
47
31
- // 3. Read the globally provided ModelRegistryMap
32
- // Assumes modelRegistryProvider is used in a higher-level middleware (e.g., routes/_middleware.dart)
48
+ // --- 2. Look Up Model Configuration ---
49
+ // Read the globally provided registry
33
50
final registry = context.read <ModelRegistryMap >();
34
-
35
- // 4. Look up the ModelConfig
51
+ // Look up the config for the validated model name
36
52
final modelConfig = registry[modelName];
37
53
38
- // 2. (cont.) Validate model existence in registry
54
+ // Further validation: Ensure model exists in the registry
39
55
if (modelConfig == null ) {
40
56
return Response (
41
57
statusCode: 400 ,
@@ -44,15 +60,14 @@ Handler middleware(Handler handler) {
44
60
);
45
61
}
46
62
47
- // 5. & 6. Provide the ModelConfig and modelName downstream
48
- // We provide ModelConfig<dynamic> because the specific type T isn't known here.
49
- // The route handler will use this config along with the correct repository
50
- // instance (which should also be provided globally).
63
+ // --- 3. Provide Context Downstream ---
64
+ // Provide the specific ModelConfig and the validated modelName string
65
+ // for the route handlers (`index.dart`, `[id].dart`) to use.
51
66
final updatedContext = context
52
- .provide <ModelConfig <dynamic >>(() => modelConfig)
67
+ .provide <ModelConfig <dynamic >>(() => modelConfig) // Provide the config
53
68
.provide <String >(() => modelName); // Provide the validated model name
54
69
55
- // Call the next handler in the chain
70
+ // Call the next handler in the chain with the updated context
56
71
return handler (updatedContext);
57
72
};
58
73
}
0 commit comments