22// ignore_for_file: lines_longer_than_80_chars
33
44import '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' ;
66
7- /// Middleware for the /api/v1/data route.
7+ /// Middleware specific to the generic ` /api/v1/data` route path .
88///
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.
1635///
1736/// If validation fails (missing/invalid model parameter), it returns a 400 Bad Request response immediately.
1837Handler middleware (Handler handler) {
1938 return (context) async {
20- // 1. Read the ' model' query parameter
39+ // --- 1. Read and Validate ` model` Parameter ---
2140 final modelName = context.request.uri.queryParameters['model' ];
22-
23- // 2. Validate the 'model' parameter
2441 if (modelName == null || modelName.isEmpty) {
2542 return Response (
2643 statusCode: 400 ,
2744 body: 'Bad Request: Missing or empty "model" query parameter.' ,
2845 );
2946 }
3047
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
3350 final registry = context.read <ModelRegistryMap >();
34-
35- // 4. Look up the ModelConfig
51+ // Look up the config for the validated model name
3652 final modelConfig = registry[modelName];
3753
38- // 2. (cont.) Validate model existence in registry
54+ // Further validation: Ensure model exists in the registry
3955 if (modelConfig == null ) {
4056 return Response (
4157 statusCode: 400 ,
@@ -44,15 +60,14 @@ Handler middleware(Handler handler) {
4460 );
4561 }
4662
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.
5166 final updatedContext = context
52- .provide <ModelConfig <dynamic >>(() => modelConfig)
67+ .provide <ModelConfig <dynamic >>(() => modelConfig) // Provide the config
5368 .provide <String >(() => modelName); // Provide the validated model name
5469
55- // Call the next handler in the chain
70+ // Call the next handler in the chain with the updated context
5671 return handler (updatedContext);
5772 };
5873}
0 commit comments