@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.
Files changed (40) hide show
  1. package/README.md +250 -492
  2. package/dist/index.d.ts +142 -185
  3. package/dist/index.js +816 -665
  4. package/dist/index.js.map +1 -1
  5. package/package.json +7 -6
  6. package/src/__tests__/allowed-methods.test.ts +144 -0
  7. package/src/__tests__/audit-fixes.test.ts +71 -0
  8. package/src/__tests__/dispatch-deasync.test.ts +312 -0
  9. package/src/__tests__/find-node-differential.test.ts +220 -0
  10. package/src/__tests__/fixtures/match-golden.json +556 -0
  11. package/src/__tests__/helpers/differential-corpus.ts +316 -0
  12. package/src/__tests__/match-differential.test.ts +46 -0
  13. package/src/__tests__/match-hotpath-guard.test.ts +71 -0
  14. package/src/__tests__/match-normalize-fastpath.test.ts +66 -0
  15. package/src/__tests__/match-safety.test.ts +177 -0
  16. package/src/__tests__/match-single-alloc.test.ts +84 -0
  17. package/src/__tests__/middleware-pipeline.test.ts +306 -0
  18. package/src/__tests__/param-decoding.test.ts +78 -0
  19. package/src/__tests__/public-surface.test.ts +59 -0
  20. package/src/__tests__/route-metadata.test.ts +172 -0
  21. package/src/__tests__/router-audit.test.ts +326 -0
  22. package/src/__tests__/router-edge-cases.test.ts +2 -2
  23. package/src/__tests__/router.test.ts +148 -2
  24. package/src/__tests__/static-map-reset.test.ts +50 -0
  25. package/src/composition.ts +75 -0
  26. package/src/constants.ts +25 -0
  27. package/src/dispatch.ts +117 -0
  28. package/src/find-node.ts +125 -0
  29. package/src/group-router.ts +208 -0
  30. package/src/index.ts +8 -5
  31. package/src/match-route.ts +178 -0
  32. package/src/matching.ts +240 -0
  33. package/src/middleware-adapter.ts +59 -0
  34. package/src/redirect.ts +97 -0
  35. package/src/registration.ts +291 -0
  36. package/src/route-metadata.ts +68 -0
  37. package/src/router.ts +150 -881
  38. package/src/segment-trie.ts +219 -0
  39. package/src/state.ts +52 -0
  40. 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
+ }