react-iten 0.0.2 → 0.4.2

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 (61) hide show
  1. package/README.md +103 -2
  2. package/dist/components/Link.cjs +21 -0
  3. package/dist/components/Link.d.cts +13 -0
  4. package/dist/components/Link.d.ts +13 -0
  5. package/dist/components/Link.mjs +21 -0
  6. package/dist/components/Navigate.cjs +15 -0
  7. package/dist/components/Navigate.d.cts +10 -0
  8. package/dist/components/Navigate.d.ts +10 -0
  9. package/dist/components/Navigate.mjs +15 -0
  10. package/dist/components/Route.cjs +22 -0
  11. package/dist/components/Route.d.cts +17 -0
  12. package/dist/components/Route.d.ts +17 -0
  13. package/dist/components/Route.mjs +22 -0
  14. package/dist/components/Switch.cjs +21 -0
  15. package/dist/components/Switch.d.cts +9 -0
  16. package/dist/components/Switch.d.ts +9 -0
  17. package/dist/components/Switch.mjs +21 -0
  18. package/dist/create-router.cjs +18 -0
  19. package/dist/create-router.d.cts +18 -0
  20. package/dist/create-router.d.ts +18 -0
  21. package/dist/create-router.mjs +18 -0
  22. package/dist/headless.cjs +83 -0
  23. package/dist/headless.d.cts +21 -0
  24. package/dist/headless.d.ts +21 -0
  25. package/dist/headless.mjs +40 -0
  26. package/dist/hooks.cjs +52 -0
  27. package/dist/hooks.d.cts +21 -0
  28. package/dist/hooks.d.ts +21 -0
  29. package/dist/hooks.mjs +52 -0
  30. package/dist/index.cjs +48 -0
  31. package/dist/index.d.cts +8 -0
  32. package/dist/index.d.ts +8 -0
  33. package/dist/index.mjs +4 -0
  34. package/dist/types.d.cts +19 -0
  35. package/dist/types.d.ts +19 -0
  36. package/dist/url.cjs +20 -0
  37. package/dist/url.d.cts +14 -0
  38. package/dist/url.d.ts +14 -0
  39. package/dist/url.mjs +19 -0
  40. package/dist/utils.cjs +44 -0
  41. package/dist/utils.d.cts +3 -0
  42. package/dist/utils.d.ts +3 -0
  43. package/dist/utils.mjs +2 -0
  44. package/dist/zod.cjs +77 -0
  45. package/dist/zod.d.cts +48 -0
  46. package/dist/zod.d.ts +48 -0
  47. package/dist/zod.mjs +73 -0
  48. package/package.json +99 -5
  49. package/src/components/Link.tsx +36 -0
  50. package/src/components/Navigate.tsx +26 -0
  51. package/src/components/Route.tsx +53 -0
  52. package/src/components/Switch.tsx +30 -0
  53. package/src/create-router.ts +33 -0
  54. package/src/headless.ts +91 -0
  55. package/src/hooks.ts +74 -0
  56. package/src/index.ts +35 -0
  57. package/src/types.ts +47 -0
  58. package/src/url.ts +49 -0
  59. package/src/utils.ts +23 -0
  60. package/src/zod.ts +150 -0
  61. package/index.js +0 -1
package/src/zod.ts ADDED
@@ -0,0 +1,150 @@
1
+ import type { RouteMap, RouteUnion } from 'iten-core'
2
+ import { z } from 'zod'
3
+
4
+ type ObjectParams = Record<string, unknown> & { name?: never }
5
+
6
+ declare const noParamsBrand: unique symbol
7
+
8
+ type ZodNoParamsSchema = z.ZodDefault<z.ZodObject<Record<string, never>>> & {
9
+ readonly [noParamsBrand]: true
10
+ }
11
+
12
+ export type ZodRouteSchemas = Record<string, z.ZodType<ObjectParams, unknown>>
13
+
14
+ export type ZodRouteMap<Schemas extends ZodRouteSchemas> = RouteMap<{
15
+ [K in Extract<keyof Schemas, string>]: z.output<Schemas[K]>
16
+ }>
17
+
18
+ export type ZodRouteUnion<Schemas extends ZodRouteSchemas> = RouteUnion<ZodRouteMap<Schemas>>
19
+
20
+ type EmptySchemaKeys<Schemas extends ZodRouteSchemas> = {
21
+ [K in keyof Schemas]: Schemas[K] extends ZodNoParamsSchema ? K : never
22
+ }[keyof Schemas]
23
+
24
+ export type ZodRouteFactory<Schemas extends ZodRouteSchemas> = {
25
+ <K extends Extract<EmptySchemaKeys<Schemas>, string>>(input: {
26
+ name: K
27
+ input?: never
28
+ }): Extract<ZodRouteUnion<Schemas>, { name: K }>
29
+ <
30
+ K extends Exclude<Extract<keyof Schemas, string>, Extract<EmptySchemaKeys<Schemas>, string>>,
31
+ >(input: {
32
+ name: K
33
+ input: z.input<Schemas[K]>
34
+ }): Extract<ZodRouteUnion<Schemas>, { name: K }>
35
+ }
36
+
37
+ export type ZodRouteParseResult<Schemas extends ZodRouteSchemas> =
38
+ | { success: true; route: ZodRouteUnion<Schemas> }
39
+ | { success: false; error: unknown }
40
+
41
+ function hasOwnName({ value }: { value: unknown }): boolean {
42
+ return typeof value === 'object' && value !== null && Object.hasOwn(value, 'name')
43
+ }
44
+
45
+ function splitInput({ value }: { value: unknown }): { name: unknown; params: unknown } | null {
46
+ if (typeof value !== 'object' || value === null || !Object.hasOwn(value, 'name')) {
47
+ return null
48
+ }
49
+
50
+ const { name, ...params } = value as { name: unknown } & Record<string, unknown>
51
+ return { name, params }
52
+ }
53
+
54
+ function getSchema<const Schemas extends ZodRouteSchemas>({
55
+ schemas,
56
+ name,
57
+ }: {
58
+ schemas: Schemas
59
+ name: unknown
60
+ }): {
61
+ name: Extract<keyof Schemas, string>
62
+ schema: Schemas[Extract<keyof Schemas, string>]
63
+ } | null {
64
+ if (typeof name !== 'string' || !Object.hasOwn(schemas, name)) {
65
+ return null
66
+ }
67
+ const routeName = name as Extract<keyof Schemas, string>
68
+ return { name: routeName, schema: schemas[routeName] }
69
+ }
70
+
71
+ export function defineZodRoutes<const Schemas extends ZodRouteSchemas>(schemas: Schemas): Schemas {
72
+ return schemas
73
+ }
74
+
75
+ export function zodNoParams(): ZodNoParamsSchema {
76
+ return z.object({}).strict().default({}) as ZodNoParamsSchema
77
+ }
78
+
79
+ export function createZodRoute<const Schemas extends ZodRouteSchemas>(
80
+ schemas: Schemas,
81
+ ): ZodRouteFactory<Schemas> {
82
+ return (({
83
+ name,
84
+ input,
85
+ }: {
86
+ name: Extract<keyof Schemas, string>
87
+ input?: z.input<Schemas[typeof name]>
88
+ }) => {
89
+ const schema = schemas[name]
90
+ if (!schema) {
91
+ throw new TypeError(`[iten] Unknown route name: ${String(name)}`)
92
+ }
93
+
94
+ if (hasOwnName({ value: input })) {
95
+ throw new TypeError('[iten] Zod route params must not include a name field')
96
+ }
97
+
98
+ const params = schema.parse(input ?? {})
99
+
100
+ if (hasOwnName({ value: params })) {
101
+ throw new TypeError('[iten] Zod route params must not include a name field')
102
+ }
103
+
104
+ return { name, ...params }
105
+ }) as unknown as ZodRouteFactory<Schemas>
106
+ }
107
+
108
+ export function parseZodRoute<const Schemas extends ZodRouteSchemas>({
109
+ schemas,
110
+ value,
111
+ }: {
112
+ schemas: Schemas
113
+ value: unknown
114
+ }): ZodRouteParseResult<Schemas> {
115
+ const input = splitInput({ value })
116
+
117
+ if (!input) {
118
+ return {
119
+ success: false,
120
+ error: new TypeError('[iten] Expected a route object with a name field'),
121
+ }
122
+ }
123
+
124
+ const resolved = getSchema({ schemas, name: input.name })
125
+
126
+ if (!resolved) {
127
+ return {
128
+ success: false,
129
+ error: new TypeError(`[iten] Unknown route name: ${String(input.name)}`),
130
+ }
131
+ }
132
+
133
+ const result = resolved.schema.safeParse(input.params)
134
+
135
+ if (!result.success) {
136
+ return { success: false, error: result.error }
137
+ }
138
+
139
+ if (hasOwnName({ value: result.data })) {
140
+ return {
141
+ success: false,
142
+ error: new TypeError('[iten] Zod route params must not include a name field'),
143
+ }
144
+ }
145
+
146
+ return {
147
+ success: true,
148
+ route: { name: resolved.name, ...result.data } as ZodRouteUnion<Schemas>,
149
+ }
150
+ }
package/index.js DELETED
@@ -1 +0,0 @@
1
- throw new Error('react-iten is reserved for the upcoming React adapter. Use jotai-iten today.')