Skip to content

Commit 535601a

Browse files
authored
feat(dart_frog_gen): add toJson to RouteConfig (#872)
1 parent ac881c2 commit 535601a

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

packages/dart_frog_gen/lib/src/build_route_configuration.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ class RouteConfiguration {
346346
/// │ │ └── index.dart
347347
/// ```
348348
final List<RouteFile> rogueRoutes;
349+
350+
/// Transform into json
351+
Map<String, dynamic> toJson() {
352+
return {
353+
'invokeCustomEntrypoint': invokeCustomEntrypoint,
354+
'invokeCustomInit': invokeCustomInit,
355+
'serveStaticFiles': serveStaticFiles,
356+
'globalMiddleware': globalMiddleware?.toJson(),
357+
'middleware': middleware.map((e) => e.toJson()).toList(),
358+
'directories': directories.map((e) => e.toJson()).toList(),
359+
'routes': routes.map((e) => e.toJson()).toList(),
360+
'endpoints': endpoints.map(
361+
(key, value) => MapEntry(
362+
key,
363+
value.map((e) => e.toJson()).toList(),
364+
),
365+
),
366+
'rogueRoutes': rogueRoutes.map((e) => e.toJson()).toList(),
367+
};
368+
}
349369
}
350370

351371
/// {@template route_directory}

packages/dart_frog_gen/test/src/build_route_configuration_test.dart

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,202 @@ Future<void>init(InternetAddress ip,int port)async{}
12871287
);
12881288
});
12891289
});
1290+
1291+
group('RouteConfiguration', () {
1292+
test('toJson transforms everything into json', () {
1293+
const configuration = RouteConfiguration(
1294+
globalMiddleware: MiddlewareFile(
1295+
name: 'middleware',
1296+
path: '../routes/_middleware.dart',
1297+
),
1298+
middleware: [
1299+
MiddlewareFile(
1300+
name: 'middleware',
1301+
path: '../routes/_middleware.dart',
1302+
),
1303+
MiddlewareFile(
1304+
name: r'$id__middleware',
1305+
path: '../routes/[id]/_middleware.dart',
1306+
),
1307+
],
1308+
directories: [
1309+
RouteDirectory(
1310+
name: '_',
1311+
route: '/',
1312+
middleware: [],
1313+
files: [],
1314+
params: [],
1315+
),
1316+
RouteDirectory(
1317+
name: r'_$id',
1318+
route: '/<id>',
1319+
middleware: [],
1320+
files: [],
1321+
params: [],
1322+
),
1323+
RouteDirectory(
1324+
name: r'_$id_existing_as_directory',
1325+
route: '/<id>/existing_as_dir',
1326+
files: [],
1327+
middleware: [],
1328+
params: [],
1329+
)
1330+
],
1331+
routes: [
1332+
RouteFile(
1333+
name: 'index',
1334+
path: '../routes/index.dart',
1335+
route: '/',
1336+
params: [],
1337+
wildcard: false,
1338+
),
1339+
RouteFile(
1340+
name: r'$id_existing_as_file',
1341+
path: '../routes/[id]/existing_as_file.dart',
1342+
route: '/existing_as_file',
1343+
params: [],
1344+
wildcard: false,
1345+
),
1346+
RouteFile(
1347+
name: r'$id_existing_as_dir_index',
1348+
path: '../routes/[id]/existing_as_dir/index.dart',
1349+
route: '/',
1350+
params: [],
1351+
wildcard: false,
1352+
)
1353+
],
1354+
endpoints: {
1355+
'/': <RouteFile>[
1356+
RouteFile(
1357+
name: 'index',
1358+
path: '../routes/index.dart',
1359+
route: '/',
1360+
params: [],
1361+
wildcard: false,
1362+
)
1363+
],
1364+
'/<id>/existing_as_file': <RouteFile>[
1365+
RouteFile(
1366+
name: r'$id_existing_as_file',
1367+
path: '../routes/[id]/existing_as_file.dart',
1368+
route: '/existing_as_file',
1369+
params: [],
1370+
wildcard: false,
1371+
)
1372+
],
1373+
'/<id>/existing_as_dir': <RouteFile>[
1374+
RouteFile(
1375+
name: r'$id_existing_as_dir_index',
1376+
path: '../routes/[id]/existing_as_dir/index.dart',
1377+
route: '/',
1378+
params: [],
1379+
wildcard: false,
1380+
)
1381+
],
1382+
},
1383+
rogueRoutes: [],
1384+
serveStaticFiles: true,
1385+
);
1386+
1387+
expect(
1388+
configuration.toJson(),
1389+
equals(
1390+
{
1391+
'invokeCustomEntrypoint': false,
1392+
'invokeCustomInit': false,
1393+
'serveStaticFiles': true,
1394+
'globalMiddleware': {
1395+
'name': 'middleware',
1396+
'path': '../routes/_middleware.dart'
1397+
},
1398+
'middleware': [
1399+
{'name': 'middleware', 'path': '../routes/_middleware.dart'},
1400+
{
1401+
'name': r'$id__middleware',
1402+
'path': '../routes/[id]/_middleware.dart'
1403+
}
1404+
],
1405+
'directories': [
1406+
{
1407+
'name': '_',
1408+
'route': '/',
1409+
'middleware': [],
1410+
'files': [],
1411+
'directory_params': []
1412+
},
1413+
{
1414+
'name': r'_$id',
1415+
'route': '/<id>',
1416+
'middleware': [],
1417+
'files': [],
1418+
'directory_params': []
1419+
},
1420+
{
1421+
'name': r'_$id_existing_as_directory',
1422+
'route': '/<id>/existing_as_dir',
1423+
'middleware': [],
1424+
'files': [],
1425+
'directory_params': []
1426+
}
1427+
],
1428+
'routes': [
1429+
{
1430+
'name': 'index',
1431+
'path': '../routes/index.dart',
1432+
'route': '/',
1433+
'file_params': [],
1434+
'wildcard': false
1435+
},
1436+
{
1437+
'name': r'$id_existing_as_file',
1438+
'path': '../routes/[id]/existing_as_file.dart',
1439+
'route': '/existing_as_file',
1440+
'file_params': [],
1441+
'wildcard': false
1442+
},
1443+
{
1444+
'name': r'$id_existing_as_dir_index',
1445+
'path': '../routes/[id]/existing_as_dir/index.dart',
1446+
'route': '/',
1447+
'file_params': [],
1448+
'wildcard': false
1449+
}
1450+
],
1451+
'endpoints': {
1452+
'/': [
1453+
{
1454+
'name': 'index',
1455+
'path': '../routes/index.dart',
1456+
'route': '/',
1457+
'file_params': [],
1458+
'wildcard': false
1459+
}
1460+
],
1461+
'/<id>/existing_as_file': [
1462+
{
1463+
'name': r'$id_existing_as_file',
1464+
'path': '../routes/[id]/existing_as_file.dart',
1465+
'route': '/existing_as_file',
1466+
'file_params': [],
1467+
'wildcard': false
1468+
}
1469+
],
1470+
'/<id>/existing_as_dir': [
1471+
{
1472+
'name': r'$id_existing_as_dir_index',
1473+
'path': '../routes/[id]/existing_as_dir/index.dart',
1474+
'route': '/',
1475+
'file_params': [],
1476+
'wildcard': false
1477+
}
1478+
]
1479+
},
1480+
'rogueRoutes': []
1481+
},
1482+
),
1483+
);
1484+
});
1485+
});
12901486
}
12911487

12921488
Directory createTempDir({

0 commit comments

Comments
 (0)