rwsdk 1.0.0-alpha.7 → 1.0.0-alpha.8
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.
|
@@ -180,8 +180,14 @@ export function index(handler) {
|
|
|
180
180
|
export function prefix(prefixPath, routes) {
|
|
181
181
|
return routes.map((r) => {
|
|
182
182
|
if (typeof r === "function") {
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
const middleware = (requestInfo) => {
|
|
184
|
+
const url = new URL(requestInfo.request.url);
|
|
185
|
+
if (url.pathname.startsWith(prefixPath)) {
|
|
186
|
+
return r(requestInfo);
|
|
187
|
+
}
|
|
188
|
+
return;
|
|
189
|
+
};
|
|
190
|
+
return middleware;
|
|
185
191
|
}
|
|
186
192
|
if (Array.isArray(r)) {
|
|
187
193
|
// Recursively process nested route arrays
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect } from "vitest";
|
|
2
2
|
import React from "react";
|
|
3
|
-
import { matchPath, defineRoutes, route, render, layout } from "./router";
|
|
3
|
+
import { matchPath, defineRoutes, route, render, layout, prefix, } from "./router";
|
|
4
4
|
describe("matchPath", () => {
|
|
5
5
|
// Test case 1: Static paths
|
|
6
6
|
it("should match static paths", () => {
|
|
@@ -209,6 +209,90 @@ describe("defineRoutes - Request Handling Behavior", () => {
|
|
|
209
209
|
expect(await response.text()).toBe("Rendered: Element");
|
|
210
210
|
});
|
|
211
211
|
});
|
|
212
|
+
describe("Prefix Handling", () => {
|
|
213
|
+
it("should only run middleware within the specified prefix", async () => {
|
|
214
|
+
const executionOrder = [];
|
|
215
|
+
const prefixedMiddleware = () => {
|
|
216
|
+
executionOrder.push("prefixedMiddleware");
|
|
217
|
+
};
|
|
218
|
+
const PageComponent = () => {
|
|
219
|
+
executionOrder.push("PageComponent");
|
|
220
|
+
return React.createElement("div", {}, "Page");
|
|
221
|
+
};
|
|
222
|
+
const AdminPageComponent = () => {
|
|
223
|
+
executionOrder.push("AdminPageComponent");
|
|
224
|
+
return React.createElement("div", {}, "Admin Page");
|
|
225
|
+
};
|
|
226
|
+
const router = defineRoutes([
|
|
227
|
+
...prefix("/admin", [
|
|
228
|
+
prefixedMiddleware,
|
|
229
|
+
route("/", AdminPageComponent),
|
|
230
|
+
]),
|
|
231
|
+
route("/", PageComponent),
|
|
232
|
+
]);
|
|
233
|
+
const deps = createMockDependencies();
|
|
234
|
+
// Test 1: Request to a path outside the prefix
|
|
235
|
+
deps.mockRequestInfo.request = new Request("http://localhost:3000/");
|
|
236
|
+
const request1 = new Request("http://localhost:3000/");
|
|
237
|
+
await router.handle({
|
|
238
|
+
request: request1,
|
|
239
|
+
renderPage: deps.mockRenderPage,
|
|
240
|
+
getRequestInfo: deps.getRequestInfo,
|
|
241
|
+
onError: deps.onError,
|
|
242
|
+
runWithRequestInfoOverrides: deps.mockRunWithRequestInfoOverrides,
|
|
243
|
+
rscActionHandler: deps.mockRscActionHandler,
|
|
244
|
+
});
|
|
245
|
+
expect(executionOrder).toEqual(["PageComponent"]);
|
|
246
|
+
// Reset execution order
|
|
247
|
+
executionOrder.length = 0;
|
|
248
|
+
// Test 2: Request to a path inside the prefix
|
|
249
|
+
deps.mockRequestInfo.request = new Request("http://localhost:3000/admin/");
|
|
250
|
+
const request2 = new Request("http://localhost:3000/admin/");
|
|
251
|
+
await router.handle({
|
|
252
|
+
request: request2,
|
|
253
|
+
renderPage: deps.mockRenderPage,
|
|
254
|
+
getRequestInfo: deps.getRequestInfo,
|
|
255
|
+
onError: deps.onError,
|
|
256
|
+
runWithRequestInfoOverrides: deps.mockRunWithRequestInfoOverrides,
|
|
257
|
+
rscActionHandler: deps.mockRscActionHandler,
|
|
258
|
+
});
|
|
259
|
+
expect(executionOrder).toEqual([
|
|
260
|
+
"prefixedMiddleware",
|
|
261
|
+
"AdminPageComponent",
|
|
262
|
+
]);
|
|
263
|
+
});
|
|
264
|
+
it("should short-circuit from a prefixed middleware", async () => {
|
|
265
|
+
const executionOrder = [];
|
|
266
|
+
const prefixedMiddleware = () => {
|
|
267
|
+
executionOrder.push("prefixedMiddleware");
|
|
268
|
+
return new Response("From prefixed middleware");
|
|
269
|
+
};
|
|
270
|
+
const AdminPageComponent = () => {
|
|
271
|
+
executionOrder.push("AdminPageComponent");
|
|
272
|
+
return React.createElement("div", {}, "Admin Page");
|
|
273
|
+
};
|
|
274
|
+
const router = defineRoutes([
|
|
275
|
+
...prefix("/admin", [
|
|
276
|
+
prefixedMiddleware,
|
|
277
|
+
route("/", AdminPageComponent),
|
|
278
|
+
]),
|
|
279
|
+
]);
|
|
280
|
+
const deps = createMockDependencies();
|
|
281
|
+
// Request to a path inside the prefix
|
|
282
|
+
deps.mockRequestInfo.request = new Request("http://localhost:3000/admin/");
|
|
283
|
+
const request = new Request("http://localhost:3000/admin/");
|
|
284
|
+
const response = await router.handle({
|
|
285
|
+
request,
|
|
286
|
+
renderPage: deps.mockRenderPage,
|
|
287
|
+
getRequestInfo: deps.getRequestInfo,
|
|
288
|
+
onError: deps.onError,
|
|
289
|
+
runWithRequestInfoOverrides: deps.mockRunWithRequestInfoOverrides,
|
|
290
|
+
rscActionHandler: deps.mockRscActionHandler,
|
|
291
|
+
});
|
|
292
|
+
expect(executionOrder).toEqual(["prefixedMiddleware"]);
|
|
293
|
+
expect(await response.text()).toBe("From prefixed middleware");
|
|
294
|
+
});
|
|
295
|
+
});
|
|
212
296
|
describe("RSC Action Handling", () => {
|
|
213
297
|
it("should handle RSC actions before the first route definition", async () => {
|
|
214
298
|
const executionOrder = [];
|