Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(remix-server-runtime): revert AppLoadContext back to an interface #7360

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .changeset/align-rr-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ Remove/align Remix types with those used in React Router
* `useFetcher().data`
* `MetaMatch.handle`
* `useMatches()[i].handle` type changed from `{ [k: string]: any }` to `unknown`
* `AppLoadContext` type changed from `{ [k: string]: unknown }` to `unknown`
* Rename the `useMatches()` return type from `RouteMatch` to `UIMatch`
* Rename `LoaderArgs`/`ActionArgs` to `LoaderFunctionArgs`/`ActionFunctionArgs` and add a generic to accept a `context` type
* Rename `LoaderArgs`/`ActionArgs` to `LoaderFunctionArgs`/`ActionFunctionArgs`
5 changes: 5 additions & 0 deletions .changeset/lemon-dodos-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/server-runtime": patch
---

[REMOVE] Revert AppLoadContext back to an interface for use with module augmentation
18 changes: 12 additions & 6 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import type { ActionFunction, LoaderFunction } from "@remix-run/router";

import {
redirect,
json,
isDeferredData,
isResponse,
isRedirectStatusCode,
} from "./responses";
import type { DataFunctionArgs } from "./routeModules";
import type {
ActionFunction,
DataFunctionArgs,
LoaderFunction,
} from "./routeModules";

/**
* An unknown type for route loaders and actions provided by the server's
* `getLoadContext()` function.
* An object of unknown type for route loaders and actions provided by the
* server's `getLoadContext()` function. This is defined as an empty interface
* specifically so apps can leverage declaration merging to augment this type
* globally: https://www.typescriptlang.org/docs/handbook/declaration-merging.html
*/
export type AppLoadContext = unknown;
export interface AppLoadContext {
[key: string]: unknown;
}

/**
* Data for a route that was returned from a `loader()`.
Expand Down
33 changes: 27 additions & 6 deletions packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,40 @@ import type {
Params,
} from "@remix-run/router";

import type { AppData } from "./data";
import type { AppData, AppLoadContext } from "./data";
import type { LinkDescriptor } from "./links";
import type { SerializeFrom } from "./serialize";

export interface RouteModules<RouteModule> {
[routeId: string]: RouteModule;
}

export type ActionFunctionArgs = RRActionFunctionArgs<unknown>;
export type ActionFunction = RRActionFunction<unknown>;
export type LoaderFunctionArgs = RRLoaderFunctionArgs<unknown>;
export type LoaderFunction = RRLoaderFunction<unknown>;
export type DataFunctionArgs = LoaderFunctionArgs | ActionFunctionArgs;
// Context is always provided in Remix, and typed for module augmentation support.
// RR also doesn't export DataFunctionArgs so we extend the two interfaces here
// even tough they're identical under the hood
export interface DataFunctionArgs
extends RRActionFunctionArgs,
RRLoaderFunctionArgs {
context: AppLoadContext;
}

/**
* A function that handles data mutations for a route.
*/
export type ActionFunction = (
args: DataFunctionArgs
) => ReturnType<RRActionFunction>;

export type ActionFunctionArgs = DataFunctionArgs;

/**
* A function that loads data for a route.
*/
export type LoaderFunction = (
args: DataFunctionArgs
) => ReturnType<RRLoaderFunction>;

export type LoaderFunctionArgs = DataFunctionArgs;

export type HeadersArgs = {
loaderHeaders: Headers;
Expand Down
18 changes: 10 additions & 8 deletions packages/remix-server-runtime/routes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AgnosticDataRouteObject } from "@remix-run/router";
import type {
AgnosticDataRouteObject,
LoaderFunctionArgs as RRLoaderFunctionArgs,
ActionFunctionArgs as RRActionFunctionArgs,
} from "@remix-run/router";

import { callRouteActionRR, callRouteLoaderRR } from "./data";
import type { FutureConfig } from "./entry";
import type {
ServerRouteModule,
ActionFunctionArgs,
LoaderFunctionArgs,
} from "./routeModules";
import type { ServerRouteModule } from "./routeModules";

export interface RouteManifest<Route> {
[routeId: string]: Route;
Expand Down Expand Up @@ -87,7 +87,9 @@ export function createStaticHandlerDataRoutes(
id: route.id,
path: route.path,
loader: route.module.loader
? (args: LoaderFunctionArgs) =>
? // Need to use RR's version here to permit the optional context even
// though we know it'll always be provided in remix
(args: RRLoaderFunctionArgs) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point it could make sense to change RR from context?: unknown to context: unknown | undefined which would, I think, eliminate this typing oddity.

callRouteLoaderRR({
request: args.request,
params: args.params,
Expand All @@ -97,7 +99,7 @@ export function createStaticHandlerDataRoutes(
})
: undefined,
action: route.module.action
? (args: ActionFunctionArgs) =>
? (args: RRActionFunctionArgs) =>
callRouteActionRR({
request: args.request,
params: args.params,
Expand Down
8 changes: 4 additions & 4 deletions packages/remix-testing/create-remix-stub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
type HydrationState,
type InitialEntry,
type Router,
type ActionFunctionArgs as RRActionFunctionArgs,
type LoaderFunctionArgs as RRLoaderFunctionArgs,
} from "@remix-run/router";
import { UNSAFE_RemixContext as RemixContext } from "@remix-run/react";
import type {
Expand All @@ -22,11 +24,9 @@ import type {
import { createMemoryRouter, Outlet, RouterProvider } from "react-router-dom";
import type {
ActionFunction,
ActionFunctionArgs,
AppLoadContext,
LinksFunction,
LoaderFunction,
LoaderFunctionArgs,
} from "@remix-run/server-runtime";

interface StubIndexRouteObject
Expand Down Expand Up @@ -160,10 +160,10 @@ function processRoutes(
Component: route.Component,
ErrorBoundary: route.ErrorBoundary,
action: action
? (args: ActionFunctionArgs) => action!({ ...args, context })
? (args: RRActionFunctionArgs) => action!({ ...args, context })
: undefined,
loader: loader
? (args: LoaderFunctionArgs) => loader!({ ...args, context })
? (args: RRLoaderFunctionArgs) => loader!({ ...args, context })
: undefined,
handle: route.handle,
shouldRevalidate: route.shouldRevalidate,
Expand Down