@remix-run/router 0.0.0-experimental-0141b5ec → 0.0.0-experimental-71108452

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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export type { ActionFunction, ActionFunctionArgs, AgnosticDataIndexRouteObject, AgnosticDataNonIndexRouteObject, AgnosticDataRouteMatch, AgnosticDataRouteObject, AgnosticIndexRouteObject, AgnosticNonIndexRouteObject, AgnosticRouteMatch, AgnosticRouteObject, DataStrategyFunction, DataStrategyFunctionArgs, DataStrategyMatch, ErrorResponse, FormEncType, FormMethod, HTMLFormMethod, JsonFunction, LazyRouteFunction, LoaderFunction, LoaderFunctionArgs, ParamParseKey, Params, PathMatch, PathParam, PathPattern, RedirectFunction, ShouldRevalidateFunction, ShouldRevalidateFunctionArgs, TrackedPromise, UIMatch, V7_FormMethod, } from "./utils";
2
- export { AbortedDeferredError, DecodedResponse as unstable_DecodedResponse, defer, generatePath, getToPathname, isDecodedResponse as unstable_isDecodedResponse, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, redirect, redirectDocument, resolvePath, resolveTo, stripBasename, } from "./utils";
1
+ export type { ActionFunction, ActionFunctionArgs, AgnosticDataIndexRouteObject, AgnosticDataNonIndexRouteObject, AgnosticDataRouteMatch, AgnosticDataRouteObject, AgnosticIndexRouteObject, AgnosticNonIndexRouteObject, AgnosticRouteMatch, AgnosticRouteObject, DataStrategyFunction, DataStrategyFunctionArgs, DataStrategyMatch, ErrorResponse, FormEncType, FormMethod, HandlerResult as unstable_HandlerResult, HTMLFormMethod, JsonFunction, LazyRouteFunction, LoaderFunction, LoaderFunctionArgs, ParamParseKey, Params, PathMatch, PathParam, PathPattern, RedirectFunction, ShouldRevalidateFunction, ShouldRevalidateFunctionArgs, TrackedPromise, UIMatch, V7_FormMethod, } from "./utils";
2
+ export { AbortedDeferredError, defer, generatePath, getToPathname, isRouteErrorResponse, joinPaths, json, matchPath, matchRoutes, normalizePathname, redirect, redirectDocument, resolvePath, resolveTo, stripBasename, } from "./utils";
3
3
  export type { BrowserHistory, BrowserHistoryOptions, HashHistory, HashHistoryOptions, History, InitialEntry, Location, MemoryHistory, MemoryHistoryOptions, Path, To, } from "./history";
4
4
  export { Action, createBrowserHistory, createHashHistory, createMemoryHistory, createPath, parsePath, } from "./history";
5
5
  export * from "./router";
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @remix-run/router v0.0.0-experimental-0141b5ec
2
+ * @remix-run/router v0.0.0-experimental-71108452
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -1519,22 +1519,6 @@ class ErrorResponseImpl {
1519
1519
  }
1520
1520
  }
1521
1521
 
1522
- /**
1523
- * Utility class we use to hold unwrapped Responses while preserving status
1524
- * codes and headers. This is most useful for dataStrategy implementations
1525
- * where implementors want to use a custom decoding mechanism and return
1526
- * pre-decoded data while also preserving response meta information
1527
- */
1528
- class DecodedResponse {
1529
- constructor(status, statusText, headers, data) {
1530
- this._isDecodedResponse = true;
1531
- this.status = status;
1532
- this.statusText = statusText;
1533
- this.headers = headers;
1534
- this.data = data;
1535
- }
1536
- }
1537
-
1538
1522
  /**
1539
1523
  * Check if the given error is an ErrorResponse generated from a 4xx/5xx
1540
1524
  * Response thrown from an action/loader
@@ -1543,13 +1527,6 @@ function isRouteErrorResponse(error) {
1543
1527
  return error != null && typeof error.status === "number" && typeof error.statusText === "string" && typeof error.internal === "boolean" && "data" in error;
1544
1528
  }
1545
1529
 
1546
- /**
1547
- * Check if the given error is an DecodedResponse
1548
- */
1549
- function isDecodedResponse(value) {
1550
- return value != null && value._isDecodedResponse === true && typeof value.status === "number" && typeof value.statusText === "string" && value.headers != null && "data" in value;
1551
- }
1552
-
1553
1530
  ////////////////////////////////////////////////////////////////////////////////
1554
1531
  //#region Types and Constants
1555
1532
  ////////////////////////////////////////////////////////////////////////////////
@@ -1751,7 +1728,26 @@ function createRouter(init) {
1751
1728
  // were marked for explicit hydration
1752
1729
  let loaderData = init.hydrationData ? init.hydrationData.loaderData : null;
1753
1730
  let errors = init.hydrationData ? init.hydrationData.errors : null;
1754
- initialized = initialMatches.every(m => m.route.loader && (typeof m.route.loader !== "function" || m.route.loader.hydrate !== true) && (loaderData && loaderData[m.route.id] !== undefined || errors && errors[m.route.id] !== undefined));
1731
+ let isRouteInitialized = m => {
1732
+ // No loader, nothing to initialize
1733
+ if (!m.route.loader) {
1734
+ return true;
1735
+ }
1736
+ // Explicitly opting-in to running on hydration
1737
+ if (typeof m.route.loader === "function" && m.route.loader.hydrate === true) {
1738
+ return false;
1739
+ }
1740
+ // Otherwise, initialized if hydrated with data or an error
1741
+ return loaderData && loaderData[m.route.id] !== undefined || errors && errors[m.route.id] !== undefined;
1742
+ };
1743
+
1744
+ // If errors exist, don't consider routes below the boundary
1745
+ if (errors) {
1746
+ let idx = initialMatches.findIndex(m => errors[m.route.id] !== undefined);
1747
+ initialized = initialMatches.slice(0, idx + 1).every(isRouteInitialized);
1748
+ } else {
1749
+ initialized = initialMatches.every(isRouteInitialized);
1750
+ }
1755
1751
  } else {
1756
1752
  // Without partial hydration - we're initialized if we were provided any
1757
1753
  // hydrationData - which is expected to be complete
@@ -4223,10 +4219,12 @@ async function callLoaderOrAction(type, request, match, manifest, mapRouteProper
4223
4219
  let runHandler = handler => {
4224
4220
  // Setup a promise we can race against so that abort signals short circuit
4225
4221
  let reject;
4222
+ // This will never resolve so safe to type it as Promise<HandlerResult> to
4223
+ // satisfy the function return value
4226
4224
  let abortPromise = new Promise((_, r) => reject = r);
4227
4225
  onReject = () => reject();
4228
4226
  request.signal.addEventListener("abort", onReject);
4229
- let runHandlerForReal = ctx => {
4227
+ let actualHandler = ctx => {
4230
4228
  if (typeof handler !== "function") {
4231
4229
  return Promise.reject(new Error("You cannot call the handler for a route which defines a boolean " + ("\"" + type + "\" [routeId: " + match.route.id + "]")));
4232
4230
  }
@@ -4236,7 +4234,25 @@ async function callLoaderOrAction(type, request, match, manifest, mapRouteProper
4236
4234
  context: staticContext
4237
4235
  }, ...(ctx !== undefined ? [ctx] : []));
4238
4236
  };
4239
- let handlerPromise = handlerOverride ? handlerOverride(async ctx => runHandlerForReal(ctx)) : runHandlerForReal();
4237
+ let handlerPromise;
4238
+ if (handlerOverride) {
4239
+ handlerPromise = handlerOverride(ctx => actualHandler(ctx));
4240
+ } else {
4241
+ handlerPromise = (async () => {
4242
+ try {
4243
+ let val = await actualHandler();
4244
+ return {
4245
+ type: "data",
4246
+ result: val
4247
+ };
4248
+ } catch (e) {
4249
+ return {
4250
+ type: "error",
4251
+ result: e
4252
+ };
4253
+ }
4254
+ })();
4255
+ }
4240
4256
  return Promise.race([handlerPromise, abortPromise]);
4241
4257
  };
4242
4258
  try {
@@ -4291,8 +4307,11 @@ async function callLoaderOrAction(type, request, match, manifest, mapRouteProper
4291
4307
  } else {
4292
4308
  result = await runHandler(handler);
4293
4309
  }
4294
- invariant(result !== undefined, "You defined " + (type === "action" ? "an action" : "a loader") + " for route " + ("\"" + match.route.id + "\" but didn't return anything from your `" + type + "` ") + "function. Please return a value or `null`.");
4310
+ invariant(result.result !== undefined, "You defined " + (type === "action" ? "an action" : "a loader") + " for route " + ("\"" + match.route.id + "\" but didn't return anything from your `" + type + "` ") + "function. Please return a value or `null`.");
4295
4311
  } catch (e) {
4312
+ // We should already be catching and converting normal handler executions to
4313
+ // HandlerResults and returning them, so anything that throws here is an
4314
+ // unexpected error we still need to wrap
4296
4315
  return {
4297
4316
  type: ResultType.error,
4298
4317
  result: e
@@ -4302,15 +4321,13 @@ async function callLoaderOrAction(type, request, match, manifest, mapRouteProper
4302
4321
  request.signal.removeEventListener("abort", onReject);
4303
4322
  }
4304
4323
  }
4305
- return {
4306
- type: ResultType.data,
4307
- result
4308
- };
4324
+ return result;
4309
4325
  }
4310
4326
  async function convertHandlerResultToDataResult(handlerResult) {
4311
4327
  let {
4312
4328
  result,
4313
- type
4329
+ type,
4330
+ status
4314
4331
  } = handlerResult;
4315
4332
  if (isResponse(result)) {
4316
4333
  let data;
@@ -4335,7 +4352,7 @@ async function convertHandlerResultToDataResult(handlerResult) {
4335
4352
  }
4336
4353
  if (type === ResultType.error) {
4337
4354
  return {
4338
- type,
4355
+ type: ResultType.error,
4339
4356
  error: new ErrorResponseImpl(result.status, result.statusText, data),
4340
4357
  statusCode: result.status,
4341
4358
  headers: result.headers
@@ -4348,27 +4365,11 @@ async function convertHandlerResultToDataResult(handlerResult) {
4348
4365
  headers: result.headers
4349
4366
  };
4350
4367
  }
4351
- if (isDecodedResponse(result)) {
4352
- if (type === ResultType.error) {
4353
- return {
4354
- type,
4355
- error: new ErrorResponseImpl(result.status, result.statusText, result.data),
4356
- statusCode: result.status,
4357
- headers: result.headers
4358
- };
4359
- }
4360
- return {
4361
- type: ResultType.data,
4362
- data: result.data,
4363
- statusCode: result.status,
4364
- headers: result.headers
4365
- };
4366
- }
4367
4368
  if (type === ResultType.error) {
4368
4369
  return {
4369
- type,
4370
+ type: ResultType.error,
4370
4371
  error: result,
4371
- statusCode: isRouteErrorResponse(result) ? result.status : undefined
4372
+ statusCode: isRouteErrorResponse(result) ? result.status : status
4372
4373
  };
4373
4374
  }
4374
4375
  if (isDeferredData(result)) {
@@ -4382,7 +4383,8 @@ async function convertHandlerResultToDataResult(handlerResult) {
4382
4383
  }
4383
4384
  return {
4384
4385
  type: ResultType.data,
4385
- data: result
4386
+ data: result,
4387
+ statusCode: status
4386
4388
  };
4387
4389
  }
4388
4390
 
@@ -5012,6 +5014,4 @@ exports.redirectDocument = redirectDocument;
5012
5014
  exports.resolvePath = resolvePath;
5013
5015
  exports.resolveTo = resolveTo;
5014
5016
  exports.stripBasename = stripBasename;
5015
- exports.unstable_DecodedResponse = DecodedResponse;
5016
- exports.unstable_isDecodedResponse = isDecodedResponse;
5017
5017
  //# sourceMappingURL=router.cjs.js.map