react-router-effect 2.0.0 → 2.1.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 +548 -173
- package/dist/index.d.mts +2 -342
- package/dist/index.mjs +118 -30
- package/dist/rolldown-runtime-D8jQpDG7.mjs +12 -0
- package/dist/route-data-MgSnnR33.d.mts +543 -0
- package/dist/server.d.mts +218 -0
- package/dist/server.mjs +337 -0
- package/dist/vite.d.mts +27 -0
- package/dist/vite.mjs +46 -0
- package/package.json +19 -8
- package/src/action-meta.ts +58 -0
- package/src/check.ts +205 -0
- package/src/errors.ts +3 -3
- package/src/factory.ts +282 -82
- package/src/index.ts +22 -1
- package/src/route-data.ts +151 -0
- package/src/server-entry.ts +10 -0
- package/src/server.ts +647 -0
- package/src/vite-entry.ts +2 -0
- package/src/vite.ts +92 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://schemastore.org/package.json",
|
|
3
3
|
"name": "react-router-effect",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.1.0",
|
|
5
5
|
"description": "Wrap React Router framework-mode route modules with Effect, with typed recover/throw/redirect error handling.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"action",
|
|
@@ -30,6 +30,8 @@
|
|
|
30
30
|
"type": "module",
|
|
31
31
|
"exports": {
|
|
32
32
|
".": "./dist/index.mjs",
|
|
33
|
+
"./server": "./dist/server.mjs",
|
|
34
|
+
"./vite": "./dist/vite.mjs",
|
|
33
35
|
"./package.json": "./package.json"
|
|
34
36
|
},
|
|
35
37
|
"publishConfig": {
|
|
@@ -38,27 +40,35 @@
|
|
|
38
40
|
"devDependencies": {
|
|
39
41
|
"@changesets/changelog-github": "^1.0.1",
|
|
40
42
|
"@changesets/cli": "^3.0.3",
|
|
43
|
+
"@effect/platform-node": "4.1.1",
|
|
44
|
+
"@react-router/node": "8.4.0",
|
|
41
45
|
"@types/node": "^25.6.2",
|
|
42
46
|
"@types/react": "^19.2.17",
|
|
43
47
|
"@types/react-dom": "^19.2.3",
|
|
44
48
|
"@vitejs/plugin-react": "^6.0.4",
|
|
45
|
-
"@vitest/browser": "5.0.
|
|
46
|
-
"@vitest/browser-playwright": "5.0.
|
|
47
|
-
"effect": "4.
|
|
48
|
-
"playwright": "1.
|
|
49
|
+
"@vitest/browser": "5.0.5",
|
|
50
|
+
"@vitest/browser-playwright": "5.0.5",
|
|
51
|
+
"effect": "4.1.1",
|
|
52
|
+
"playwright": "1.64.1",
|
|
49
53
|
"react": "^19.2.8",
|
|
50
54
|
"react-dom": "^19.2.8",
|
|
51
55
|
"react-router": "^8.3.0",
|
|
52
56
|
"typescript": "^7.0.2",
|
|
53
57
|
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
|
54
|
-
"vite-plus": "^1.
|
|
55
|
-
"vitest": "5.0.
|
|
58
|
+
"vite-plus": "^1.4.0",
|
|
59
|
+
"vitest": "5.0.5",
|
|
56
60
|
"vitest-browser-react": "^2.2.0"
|
|
57
61
|
},
|
|
58
62
|
"peerDependencies": {
|
|
59
|
-
"effect": "^4.
|
|
63
|
+
"@effect/platform-node": "^4.1.0",
|
|
64
|
+
"effect": "^4.1.0",
|
|
60
65
|
"react-router": "^7.16.0 || ^8.3.0"
|
|
61
66
|
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"@effect/platform-node": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
62
72
|
"devEngines": {
|
|
63
73
|
"packageManager": {
|
|
64
74
|
"name": "pnpm",
|
|
@@ -74,6 +84,7 @@
|
|
|
74
84
|
"test:browser": "vp test run --project browser",
|
|
75
85
|
"test:browser:install": "vp exec playwright install chromium",
|
|
76
86
|
"check": "vp check",
|
|
87
|
+
"check:consumer": "tsc -p test/consumer",
|
|
77
88
|
"changeset": "changeset",
|
|
78
89
|
"changeset:version": "changeset version",
|
|
79
90
|
"changeset:release": "vp run build && changeset publish"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Predicate } from "effect";
|
|
2
|
+
import { data, type UNSAFE_DataWithResponseInit as DataWithResponseInit } from "react-router";
|
|
3
|
+
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Action metadata: request-scoped facts attached to every action's data.
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
/** The key that holds the metadata in action data. */
|
|
9
|
+
export const actionMetaKey = "__meta";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Adds optional metadata to a record. Only records get metadata, because a
|
|
13
|
+
* structural type cannot tell a plain object from a class instance, and other
|
|
14
|
+
* values (strings, arrays, `Response`s) have nowhere to put it.
|
|
15
|
+
*/
|
|
16
|
+
type RecordWithMeta<T, Meta> =
|
|
17
|
+
T extends Record<string, unknown>
|
|
18
|
+
? Omit<T, typeof actionMetaKey> & { [actionMetaKey]?: Meta }
|
|
19
|
+
: T;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The type of action data after metadata is attached. A `data(...)` wrapper
|
|
23
|
+
* gets metadata on its body. With no configured metadata (`Meta` is `never`)
|
|
24
|
+
* the data is unchanged.
|
|
25
|
+
*/
|
|
26
|
+
export type WithActionMeta<T, Meta> = [Meta] extends [never]
|
|
27
|
+
? T
|
|
28
|
+
: T extends DataWithResponseInit<infer Body>
|
|
29
|
+
? DataWithResponseInit<RecordWithMeta<Body, Meta>>
|
|
30
|
+
: RecordWithMeta<T, Meta>;
|
|
31
|
+
|
|
32
|
+
// React Router exports the type of the `data(...)` wrapper, but not its class.
|
|
33
|
+
const DataWithResponseInitClass = data(undefined).constructor;
|
|
34
|
+
|
|
35
|
+
/** True for a `data(...)` wrapper, never for a business field that looks like one. */
|
|
36
|
+
const isDataWithResponseInit = (value: unknown): value is DataWithResponseInit<unknown> =>
|
|
37
|
+
value instanceof DataWithResponseInitClass;
|
|
38
|
+
|
|
39
|
+
/** True for a plain object, which can be copied without losing behavior. */
|
|
40
|
+
const isPlainRecord = (value: unknown): value is Record<string, unknown> => {
|
|
41
|
+
if (!Predicate.isObject(value) || Array.isArray(value)) return false;
|
|
42
|
+
const prototype: object | null = Object.getPrototypeOf(value);
|
|
43
|
+
return prototype === Object.prototype || prototype === null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Attaches `meta` to the action result. A plain record gets a copy with the
|
|
48
|
+
* metadata key; a `data(...)` wrapper gets it on its body and keeps its init.
|
|
49
|
+
* Any other value is returned unchanged.
|
|
50
|
+
*/
|
|
51
|
+
export const attachActionMeta = <T>(value: T, meta: unknown): T => {
|
|
52
|
+
if (isDataWithResponseInit(value)) {
|
|
53
|
+
if (!isPlainRecord(value.data)) return value;
|
|
54
|
+
// The copy has the same shape as `value` plus the metadata key.
|
|
55
|
+
return data({ ...value.data, [actionMetaKey]: meta }, value.init ?? undefined) as T;
|
|
56
|
+
}
|
|
57
|
+
return isPlainRecord(value) ? ({ ...value, [actionMetaKey]: meta } as T) : value;
|
|
58
|
+
};
|
package/src/check.ts
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type { Register as RouterRegister } from "react-router";
|
|
3
|
+
|
|
4
|
+
import type { ReturnableDataError } from "./errors.ts";
|
|
5
|
+
import type { FactoryTypes, FailureResponse, IsAny, LibraryHandled, NameError } from "./factory.ts";
|
|
6
|
+
import type { RegisteredMeta, RegisteredRecover } from "./route-data.ts";
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// The route check: every Effect a route module exports must be fully handled.
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
/** The route modules React Router's typegen registers, keyed by route ID. */
|
|
13
|
+
type RegisteredRouteModules = RouterRegister extends { readonly routeModules: infer Modules }
|
|
14
|
+
? Modules
|
|
15
|
+
: {};
|
|
16
|
+
|
|
17
|
+
/** What an effect middleware may succeed with: React Router's response, or nothing. */
|
|
18
|
+
type MiddlewareResult = Response | void | undefined;
|
|
19
|
+
|
|
20
|
+
/** What a failure says to do, under its `react-router-effect` key. */
|
|
21
|
+
type FailureMessage =
|
|
22
|
+
"This route Effect can fail with an error the route factory does not handle, or needs a service it does not provide. Catch the error in the Effect (for example with Effect.catchTag), add it to the factory's domain errors, or provide the service.";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One export that fails the check. `unhandledErrors` lists each error the
|
|
26
|
+
* effect can fail with that the factory does not handle, by its `_tag`.
|
|
27
|
+
* `missingServices` lists each service that neither the runtime nor the
|
|
28
|
+
* request context provides. `invalidResults` lists what a middleware succeeds
|
|
29
|
+
* with that is not a `Response` or nothing.
|
|
30
|
+
*
|
|
31
|
+
* It is an inline object type, not an interface, so the compiler prints its
|
|
32
|
+
* fields in the error rather than an alias name.
|
|
33
|
+
*/
|
|
34
|
+
export type RouteEffectFailure<Route, Export, Unhandled, Missing, Invalid> = {
|
|
35
|
+
readonly "react-router-effect": FailureMessage;
|
|
36
|
+
readonly route: Route;
|
|
37
|
+
readonly export: Export;
|
|
38
|
+
readonly unhandledErrors: Unhandled;
|
|
39
|
+
readonly missingServices: Missing;
|
|
40
|
+
readonly invalidResults: Invalid;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The failure of one route function, or `never` when it does not return an
|
|
45
|
+
* Effect or when its Effect is fully handled. An Effect whose error or
|
|
46
|
+
* service type is `any` comes from a mistake TypeScript already reports, so it
|
|
47
|
+
* is skipped rather than reported a second time.
|
|
48
|
+
*/
|
|
49
|
+
type FunctionFailure<Route, Export, F, Handled, Provided, Result> = F extends (
|
|
50
|
+
...args: never[]
|
|
51
|
+
) => Effect.Effect<infer A, infer E, infer R>
|
|
52
|
+
? true extends IsAny<E> | IsAny<R>
|
|
53
|
+
? never
|
|
54
|
+
: [Exclude<E, Handled>, Exclude<R, Provided>, Exclude<A, Result>] extends [never, never, never]
|
|
55
|
+
? never
|
|
56
|
+
: {
|
|
57
|
+
// Inline, not `RouteEffectFailure`, so the compiler prints the fields.
|
|
58
|
+
readonly "react-router-effect": FailureMessage;
|
|
59
|
+
readonly route: Route;
|
|
60
|
+
readonly export: Export;
|
|
61
|
+
readonly unhandledErrors: NameError<Exclude<E, Handled>>;
|
|
62
|
+
readonly missingServices: Exclude<R, Provided>;
|
|
63
|
+
readonly invalidResults: Exclude<A, Result>;
|
|
64
|
+
}
|
|
65
|
+
: never;
|
|
66
|
+
|
|
67
|
+
/** The failures of one route module's loader, action and middleware. */
|
|
68
|
+
type ModuleFailures<Route, Module, Handled, Provided> =
|
|
69
|
+
| (Module extends { readonly loader: infer F }
|
|
70
|
+
? FunctionFailure<Route, "loader", F, Handled, Provided, unknown>
|
|
71
|
+
: never)
|
|
72
|
+
| (Module extends { readonly action: infer F }
|
|
73
|
+
? FunctionFailure<Route, "action", F, Handled, Provided, unknown>
|
|
74
|
+
: never)
|
|
75
|
+
| (Module extends { readonly middleware: ReadonlyArray<infer F> }
|
|
76
|
+
? FunctionFailure<
|
|
77
|
+
Route,
|
|
78
|
+
"middleware",
|
|
79
|
+
F,
|
|
80
|
+
Handled | FailureResponse,
|
|
81
|
+
Provided,
|
|
82
|
+
MiddlewareResult
|
|
83
|
+
>
|
|
84
|
+
: never);
|
|
85
|
+
|
|
86
|
+
/** The failures of every registered route module. */
|
|
87
|
+
type AllFailures<Modules, Handled, Provided> = {
|
|
88
|
+
[Route in keyof Modules]: ModuleFailures<Route, Modules[Route], Handled, Provided>;
|
|
89
|
+
}[keyof Modules];
|
|
90
|
+
|
|
91
|
+
/** The types of a route factory. */
|
|
92
|
+
type TypesOf<Factory> = Factory extends { readonly "~types": infer Types } ? Types : never;
|
|
93
|
+
|
|
94
|
+
/** What one error handler's return recovers into, or `never` when it throws. */
|
|
95
|
+
type HandlerRecovered<Return> =
|
|
96
|
+
Return extends ReturnableDataError<infer Body>
|
|
97
|
+
? Body
|
|
98
|
+
: Return extends Effect.Effect<infer Success, infer _E, infer _R>
|
|
99
|
+
? Success
|
|
100
|
+
: never;
|
|
101
|
+
|
|
102
|
+
/** The factory's recover map: each error tag whose handler recovers, and its data. */
|
|
103
|
+
type FactoryRecover<Returns> = {
|
|
104
|
+
[
|
|
105
|
+
Tag in keyof Returns as [HandlerRecovered<Returns[Tag]>] extends [never] ? never : Tag
|
|
106
|
+
]: HandlerRecovered<Returns[Tag]>;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** True when each type is assignable to the other. */
|
|
110
|
+
type SameType<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A disagreement between the `Register` of `react-router-effect` and the
|
|
114
|
+
* factory, which would type plain Effect routes' data wrongly.
|
|
115
|
+
*/
|
|
116
|
+
export interface RegisterMismatch<Key, Factory, Registered> {
|
|
117
|
+
readonly register: Key;
|
|
118
|
+
readonly factory: Factory;
|
|
119
|
+
readonly registered: Registered;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The registration mismatches of a factory's types. */
|
|
123
|
+
type RegisterMismatches<Returns, Meta> =
|
|
124
|
+
| (SameType<FactoryRecover<Returns>, RegisteredRecover> extends true
|
|
125
|
+
? never
|
|
126
|
+
: RegisterMismatch<"recover", FactoryRecover<Returns>, RegisteredRecover>)
|
|
127
|
+
| (SameType<Meta, RegisteredMeta> extends true
|
|
128
|
+
? never
|
|
129
|
+
: RegisterMismatch<"actionMeta", Meta, RegisteredMeta>);
|
|
130
|
+
|
|
131
|
+
/** Every failure: the routes', then the registration's. */
|
|
132
|
+
type Failures<Modules, Types> =
|
|
133
|
+
Types extends FactoryTypes<infer DomainError, infer Returns, infer Services, infer Meta>
|
|
134
|
+
?
|
|
135
|
+
| AllFailures<Modules, LibraryHandled<DomainError>, Services>
|
|
136
|
+
| RegisterMismatches<Returns, Meta>
|
|
137
|
+
: never;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The failures of every Effect route function across the route modules React
|
|
141
|
+
* Router's typegen registers, and the registration mismatches, as objects.
|
|
142
|
+
* `never` when everything is handled. `RouteEffectCheck` reports them.
|
|
143
|
+
*/
|
|
144
|
+
export type RouteEffectFailures<Factory, Modules = RegisteredRouteModules> = Failures<
|
|
145
|
+
Modules,
|
|
146
|
+
TypesOf<Factory>
|
|
147
|
+
>;
|
|
148
|
+
|
|
149
|
+
/** A printable name for an error tag or another unhandled error type. */
|
|
150
|
+
type ErrorName<Name> = Name extends string ? Name : "an untagged error";
|
|
151
|
+
|
|
152
|
+
/** A printable name for a service: its key, such as `"app/Database"`. */
|
|
153
|
+
type ServiceName<Service> = Service extends { readonly key: infer Key extends string }
|
|
154
|
+
? Key
|
|
155
|
+
: "a service without a string key";
|
|
156
|
+
|
|
157
|
+
/** The diagnostic lines of one failure, one per error, service or result. */
|
|
158
|
+
type FailureLines<Failure> = Failure extends {
|
|
159
|
+
readonly route: infer Route extends string | number | symbol;
|
|
160
|
+
readonly export: infer Export extends string;
|
|
161
|
+
readonly unhandledErrors: infer Unhandled;
|
|
162
|
+
readonly missingServices: infer Missing;
|
|
163
|
+
readonly invalidResults: infer Invalid;
|
|
164
|
+
}
|
|
165
|
+
?
|
|
166
|
+
| (Unhandled extends unknown
|
|
167
|
+
? `${Route & string}#${Export} must handle ${ErrorName<Unhandled>}`
|
|
168
|
+
: never)
|
|
169
|
+
| (Missing extends unknown
|
|
170
|
+
? `${Route & string}#${Export} needs a provider for ${ServiceName<Missing>}`
|
|
171
|
+
: never)
|
|
172
|
+
| ([Invalid] extends [never]
|
|
173
|
+
? never
|
|
174
|
+
: `${Route & string}#${Export} must succeed with a Response or nothing`)
|
|
175
|
+
: Failure extends { readonly register: infer Key extends string }
|
|
176
|
+
? `the Register of react-router-effect: ${Key} does not match the route factory`
|
|
177
|
+
: never;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Checks every loader, action and middleware that returns an Effect, across
|
|
181
|
+
* the route modules React Router's typegen registers. Each failure is a
|
|
182
|
+
* required property whose name says what to fix, so assigning `{}` fails to
|
|
183
|
+
* compile with a list such as:
|
|
184
|
+
*
|
|
185
|
+
* ```text
|
|
186
|
+
* Type '{}' is missing the following properties from type ...:
|
|
187
|
+
* "routes/billing#loader must handle PlaidApiError",
|
|
188
|
+
* "routes/admin#middleware needs a provider for app/Session"
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* ```ts
|
|
192
|
+
* import type { RouteEffectCheck } from "react-router-effect/server";
|
|
193
|
+
*
|
|
194
|
+
* export const routeEffects: RouteEffectCheck<typeof routeFactory> = {};
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* An Effect may fail with the factory's domain errors, the library's `Respond`
|
|
198
|
+
* errors, and `HttpServerRespondable` errors, and require the services of the
|
|
199
|
+
* factory's runtime and request context. A middleware may also fail with what
|
|
200
|
+
* its `next()` fails with. The check also fails when the `Register` of
|
|
201
|
+
* `react-router-effect` disagrees with the factory.
|
|
202
|
+
*/
|
|
203
|
+
export type RouteEffectCheck<Factory, Modules = RegisteredRouteModules> = {
|
|
204
|
+
readonly [Line in FailureLines<RouteEffectFailures<Factory, Modules>>]: Line;
|
|
205
|
+
};
|
package/src/errors.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Data } from "effect";
|
|
2
2
|
|
|
3
3
|
// ---------------------------------------------------------------------------
|
|
4
|
-
// Standardized errors
|
|
4
|
+
// Standardized errors - the library translates these out of the box.
|
|
5
5
|
// ---------------------------------------------------------------------------
|
|
6
6
|
|
|
7
7
|
/** Recoverable: short-circuits the loader but returns `data(...)` the component reads. */
|
|
@@ -31,7 +31,7 @@ export type AnyRouteError =
|
|
|
31
31
|
/**
|
|
32
32
|
* The library's base helpers for raising route errors (`early` / `throw` /
|
|
33
33
|
* `redirect`). This is internal: it isn't exported from the package. The factory
|
|
34
|
-
*
|
|
34
|
+
* passes it to your config builder and returns it (merged with any `respond`
|
|
35
35
|
* extensions) so your app imports a single `Respond`:
|
|
36
36
|
*
|
|
37
37
|
* ```ts
|
|
@@ -44,7 +44,7 @@ export type AnyRouteError =
|
|
|
44
44
|
* ```
|
|
45
45
|
*/
|
|
46
46
|
export const Respond = {
|
|
47
|
-
/** Recover: short-circuit and
|
|
47
|
+
/** Recover: short-circuit and pass `value` to the component as loader/action data. */
|
|
48
48
|
early: <D>(value: D, init?: number | ResponseInit) =>
|
|
49
49
|
new ReturnableDataError({ data: value, init }),
|
|
50
50
|
/** Throw: short-circuit to the error boundary with `data(value, init)`. */
|