Skip to content

Commit 447496f

Browse files
authored
chore(routing): reorganize routing internals (#148)
chore: reorganize routing internals
1 parent ade02bc commit 447496f

26 files changed

+141
-128
lines changed

src/Config/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type HttpMiddleware } from '@/Http';
22
import { type StaticFilesOptions } from '@/Http/Files';
33
import { type EventSubscriber } from '@/Kernel';
4-
import type { RouteSource } from '@/routing/route-source';
4+
import type { RouteSource } from '@/routing';
55

66
/**
77
* @deprecated Use function-first routes from `@koala-ts/framework/routing` with `KoalaConfig.routes` instead.

src/application/create-application.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { applyConfiguredGlobalMiddleware } from '@/Http/middleware/apply-configu
55
import { initializeRequestScopeStorage } from '@/Http/Scope/request-scope-storage';
66
import { registerEventSubscribers } from '@/Kernel';
77
import { registerLegacyRoutes } from '@/routing/decorator/legacy-router';
8-
import { registerRoutes } from '@/routing/register-routes';
8+
import { registerRoutes } from '@/routing/registration/register-routes';
99
import { verifyRoutingMode } from '@/routing/verify-routing-mode';
1010
import Koa from 'koa';
1111
import { type Application } from './application';

src/routing/create-route-definition.test.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/routing/decorator/legacy-router.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { type Application } from '@/application/application';
2-
import { registerRoutes } from '@/routing/register-routes';
3-
import 'reflect-metadata';
42
import { type HttpMiddleware } from '@/Http';
5-
import { createRouteDefinition } from '@/routing/create-route-definition';
6-
import type { RouteDefinition } from '@/routing/route-definition';
3+
import { createRouteDefinition } from '@/routing/definition/create-route-definition';
4+
import type { RouteDefinition } from '@/routing/definition/route-definition';
5+
import { registerRoutes } from '@/routing/registration/register-routes';
6+
import 'reflect-metadata';
77
import type { Route } from './route';
88
import type { RouteMetadata } from './route-metadata';
99

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { describe, expect, test, vi } from 'vitest';
2+
import { createRouteDefinition } from './create-route-definition';
3+
4+
describe('create route definition', () => {
5+
test('it creates a route definition for a single method', () => {
6+
const handler = vi.fn(async () => undefined);
7+
8+
const route = createRouteDefinition({
9+
method: 'GET',
10+
path: '/users',
11+
handler,
12+
});
13+
14+
expect(route).toEqual({
15+
path: '/users',
16+
methods: ['get'],
17+
handler,
18+
middleware: [],
19+
parseBody: true,
20+
bodyOptions: {},
21+
});
22+
});
23+
24+
test('it creates a route definition with name middleware and body options', () => {
25+
const handler = vi.fn(async () => undefined);
26+
const middleware = [vi.fn(async () => undefined)];
27+
28+
const route = createRouteDefinition({
29+
name: 'users.create',
30+
method: 'POST',
31+
path: '/users',
32+
handler,
33+
middleware,
34+
options: {
35+
multipart: true,
36+
parseBody: false,
37+
},
38+
});
39+
40+
expect(route).toEqual({
41+
name: 'users.create',
42+
path: '/users',
43+
methods: ['post'],
44+
handler,
45+
middleware,
46+
parseBody: false,
47+
bodyOptions: {
48+
multipart: true,
49+
},
50+
});
51+
});
52+
53+
test('it qualifies multiple methods', () => {
54+
const handler = vi.fn(async () => undefined);
55+
56+
const route = createRouteDefinition({
57+
method: ['GET', 'POST'],
58+
path: '/users',
59+
handler,
60+
});
61+
62+
expect(route.methods).toEqual(['get', 'post']);
63+
});
64+
65+
test('it normalizes any and all to all', () => {
66+
const handler = vi.fn(async () => undefined);
67+
68+
const route = createRouteDefinition({
69+
method: ['ANY', 'ALL'],
70+
path: '/users',
71+
handler,
72+
});
73+
74+
expect(route.methods).toEqual(['all']);
75+
});
76+
77+
test('it normalizes any with specific methods to all', () => {
78+
const handler = vi.fn(async () => undefined);
79+
80+
const route = createRouteDefinition({
81+
method: ['ANY', 'GET'],
82+
path: '/users',
83+
handler,
84+
});
85+
86+
expect(route.methods).toEqual(['all']);
87+
});
88+
89+
test('it removes duplicate specific methods case-insensitively', () => {
90+
const handler = vi.fn(async () => undefined);
91+
92+
const route = createRouteDefinition({
93+
method: ['GET', 'get', 'POST'],
94+
path: '/users',
95+
handler,
96+
});
97+
98+
expect(route.methods).toEqual(['get', 'post']);
99+
});
100+
});

src/routing/create-route-definition.ts renamed to src/routing/definition/create-route-definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { HttpMiddleware } from '@/Http';
22
import type { HttpMethod } from '@/routing/http-method';
33
import type { RouteOptions } from '@/routing/route-options';
4-
import { resolveRouteOptions } from '@/routing/resolve-route-options';
4+
import { resolveRouteOptions } from '@/routing/definition/resolve-route-options';
55
import type { RouterMethod } from '@/routing/router-method';
66
import type { RouteDefinition } from './route-definition';
77

File renamed without changes.

src/routing/resolve-route-options.ts renamed to src/routing/definition/resolve-route-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { RouteDefinition } from '@/routing/route-definition';
21
import type { RouteOptions } from '@/routing/route-options';
2+
import type { RouteDefinition } from './route-definition';
33

44
export function resolveRouteOptions(options: RouteOptions): Pick<RouteDefinition, 'parseBody' | 'bodyOptions'> {
55
return {

src/routing/route-definition.ts renamed to src/routing/definition/route-definition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HttpMiddleware } from '@/Http';
2-
import type { KoaBodyMiddlewareOptions } from 'koa-body';
32
import type { RouterMethod } from '@/routing/router-method';
3+
import type { KoaBodyMiddlewareOptions } from 'koa-body';
44

55
export interface RouteDefinition {
66
name?: string;
File renamed without changes.

0 commit comments

Comments
 (0)