@nextrush/router 3.0.6 → 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +250 -492
- package/dist/index.d.ts +142 -185
- package/dist/index.js +816 -665
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
- package/src/__tests__/allowed-methods.test.ts +144 -0
- package/src/__tests__/audit-fixes.test.ts +71 -0
- package/src/__tests__/dispatch-deasync.test.ts +312 -0
- package/src/__tests__/find-node-differential.test.ts +220 -0
- package/src/__tests__/fixtures/match-golden.json +556 -0
- package/src/__tests__/helpers/differential-corpus.ts +316 -0
- package/src/__tests__/match-differential.test.ts +46 -0
- package/src/__tests__/match-hotpath-guard.test.ts +71 -0
- package/src/__tests__/match-normalize-fastpath.test.ts +66 -0
- package/src/__tests__/match-safety.test.ts +177 -0
- package/src/__tests__/match-single-alloc.test.ts +84 -0
- package/src/__tests__/middleware-pipeline.test.ts +306 -0
- package/src/__tests__/param-decoding.test.ts +78 -0
- package/src/__tests__/public-surface.test.ts +59 -0
- package/src/__tests__/route-metadata.test.ts +172 -0
- package/src/__tests__/router-audit.test.ts +326 -0
- package/src/__tests__/router-edge-cases.test.ts +2 -2
- package/src/__tests__/router.test.ts +148 -2
- package/src/__tests__/static-map-reset.test.ts +50 -0
- package/src/composition.ts +75 -0
- package/src/constants.ts +25 -0
- package/src/dispatch.ts +117 -0
- package/src/find-node.ts +125 -0
- package/src/group-router.ts +208 -0
- package/src/index.ts +8 -5
- package/src/match-route.ts +178 -0
- package/src/matching.ts +240 -0
- package/src/middleware-adapter.ts +59 -0
- package/src/redirect.ts +97 -0
- package/src/registration.ts +291 -0
- package/src/route-metadata.ts +68 -0
- package/src/router.ts +150 -881
- package/src/segment-trie.ts +219 -0
- package/src/state.ts +52 -0
- package/src/radix-tree.ts +0 -184
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @nextrush/router - Route Metadata Contributions
|
|
3
|
+
*
|
|
4
|
+
* Inline route metadata (`endpoint()`) and the registration-time merge of
|
|
5
|
+
* contributions from `validate()`, `endpoint()`, etc. Extracted from `router.ts`
|
|
6
|
+
* (audit RT-3). None of this is on the request hot path — it is read only at
|
|
7
|
+
* registration and by `getRoutes()` at doc-generation time.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
ROUTE_METADATA,
|
|
14
|
+
type MetadataContribution,
|
|
15
|
+
type RouteEntry,
|
|
16
|
+
type RouteMetadata,
|
|
17
|
+
type RouteMetaMarker,
|
|
18
|
+
} from '@nextrush/types';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Declare route metadata inline in a route's argument list. Returns a pure data
|
|
22
|
+
* marker (not a middleware) — the router reads its metadata at registration and
|
|
23
|
+
* never executes it per request.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* router.post('/users',
|
|
28
|
+
* validate(User),
|
|
29
|
+
* endpoint({ summary: 'Create a user', responses: { 201: UserResponse } }),
|
|
30
|
+
* handler,
|
|
31
|
+
* );
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function endpoint(metadata: MetadataContribution): RouteMetaMarker {
|
|
35
|
+
return { [ROUTE_METADATA]: metadata };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Mutable view of RouteMetadata, used only while merging contributions. */
|
|
39
|
+
type MutableRouteMetadata = { -readonly [K in keyof RouteMetadata]?: RouteMetadata[K] };
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Merge metadata contributions (from `validate()`, `endpoint()`, etc.) in
|
|
43
|
+
* registration order. Scalars and arrays are last-write-wins; the `request` and
|
|
44
|
+
* `responses` maps merge per key. Returns `undefined` when nothing contributed
|
|
45
|
+
* (an undocumented route).
|
|
46
|
+
*/
|
|
47
|
+
export function mergeContributions(
|
|
48
|
+
contributions: readonly MetadataContribution[]
|
|
49
|
+
): RouteMetadata | undefined {
|
|
50
|
+
if (contributions.length === 0) return undefined;
|
|
51
|
+
|
|
52
|
+
const meta: MutableRouteMetadata = {};
|
|
53
|
+
for (const c of contributions) {
|
|
54
|
+
if (c.summary !== undefined) meta.summary = c.summary;
|
|
55
|
+
if (c.description !== undefined) meta.description = c.description;
|
|
56
|
+
if (c.deprecated !== undefined) meta.deprecated = c.deprecated;
|
|
57
|
+
if (c.visibility !== undefined) meta.visibility = c.visibility;
|
|
58
|
+
if (c.tags !== undefined) meta.tags = c.tags;
|
|
59
|
+
if (c.request) meta.request = { ...meta.request, ...c.request };
|
|
60
|
+
if (c.responses) meta.responses = { ...meta.responses, ...c.responses };
|
|
61
|
+
}
|
|
62
|
+
return meta;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Read a route entry's metadata contribution, if it carries one. */
|
|
66
|
+
export function readContribution(entry: RouteEntry): MetadataContribution | undefined {
|
|
67
|
+
return (entry as Partial<RouteMetaMarker>)[ROUTE_METADATA];
|
|
68
|
+
}
|