Skip to content

Commit fa93eac

Browse files
committed
refactor(api): simplify anonymous route with ResponseHelper
Updates the `/api/v1/auth/anonymous` route handler to use the new `ResponseHelper.success` method. This change removes several lines of boilerplate code related to manually creating metadata and the success response object, making the handler cleaner and more focused on its core logic.
1 parent dd2d581 commit fa93eac

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

routes/api/v1/auth/anonymous.dart

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import 'dart:io';
22

33
import 'package:dart_frog/dart_frog.dart';
4+
import 'package:ht_api/src/helpers/response_helper.dart';
45
import 'package:ht_api/src/services/auth_service.dart';
56
import 'package:ht_shared/ht_shared.dart';
67

7-
import '../../../_middleware.dart';
8-
98
/// Handles POST requests to `/api/v1/auth/anonymous`.
109
///
1110
/// Creates a new anonymous user and returns the User object along with an
@@ -29,22 +28,11 @@ Future<Response> onRequest(RequestContext context) async {
2928
token: result.token,
3029
);
3130

32-
// Create metadata, including the requestId from the context.
33-
final metadata = ResponseMetadata(
34-
requestId: context.read<RequestId>().id,
35-
timestamp: DateTime.now().toUtc(),
36-
);
37-
38-
// Wrap the payload in the standard SuccessApiResponse
39-
final responsePayload = SuccessApiResponse<AuthSuccessResponse>(
31+
// Use the helper to create a standardized success response
32+
return ResponseHelper.success(
33+
context: context,
4034
data: authPayload,
41-
metadata: metadata,
42-
);
43-
44-
// Return 200 OK with the standardized, serialized response
45-
return Response.json(
46-
// Use the toJson method, providing the toJson factory for the inner type
47-
body: responsePayload.toJson((authSuccess) => authSuccess.toJson()),
35+
toJsonT: (data) => data.toJson(),
4836
);
4937
} on HtHttpException catch (_) {
5038
// Let the central errorHandler middleware handle known exceptions

0 commit comments

Comments
 (0)