From 87d2eaa2e3343f98af24617e9b99ffafbd39048c Mon Sep 17 00:00:00 2001 From: Daniel Gray Date: Thu, 10 Feb 2022 14:45:23 -0600 Subject: [PATCH 1/5] make AppLoadContext an empty interface --- packages/remix-server-runtime/data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/remix-server-runtime/data.ts b/packages/remix-server-runtime/data.ts index 0cad4e2909a..a916704168d 100644 --- a/packages/remix-server-runtime/data.ts +++ b/packages/remix-server-runtime/data.ts @@ -6,7 +6,7 @@ import { json, isResponse, isRedirectResponse } from "./responses"; * An object of arbitrary for route loaders and actions provided by the * server's `getLoadContext()` function. */ -export type AppLoadContext = any; +export interface AppLoadContext {}; /** * Data for a route that was returned from a `loader()`. From 296dc0c6c4578b327e7b1b39ed42e4287baea86e Mon Sep 17 00:00:00 2001 From: DanielFGray Date: Wed, 20 Apr 2022 16:49:24 -0500 Subject: [PATCH 2/5] Update contributors.yml --- contributors.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/contributors.yml b/contributors.yml index 5ca6dce6c87..dbafe37c1fd 100644 --- a/contributors.yml +++ b/contributors.yml @@ -80,6 +80,7 @@ - cysp - damiensedgwick - dan-gamble +- danielfgray - danielweinmann - davecalnan - davecranwell-vocovo From ad9987fdde5793ac5235f1c719558b0c188d14de Mon Sep 17 00:00:00 2001 From: DanielFGray Date: Wed, 20 Apr 2022 16:52:24 -0500 Subject: [PATCH 3/5] AppLoadContext interface uses unknown for missing values --- packages/remix-server-runtime/data.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/remix-server-runtime/data.ts b/packages/remix-server-runtime/data.ts index a916704168d..b397507efcb 100644 --- a/packages/remix-server-runtime/data.ts +++ b/packages/remix-server-runtime/data.ts @@ -6,7 +6,9 @@ import { json, isResponse, isRedirectResponse } from "./responses"; * An object of arbitrary for route loaders and actions provided by the * server's `getLoadContext()` function. */ -export interface AppLoadContext {}; +export interface AppLoadContext { + [key: string]: unknown; +}; /** * Data for a route that was returned from a `loader()`. From a8c3f9d90f693bceb321cecd326dc7ea8a86560b Mon Sep 17 00:00:00 2001 From: DanielFGray Date: Fri, 15 Jul 2022 08:21:49 -0500 Subject: [PATCH 4/5] update types for AppLoadContext --- packages/remix-server-runtime/data.ts | 6 +++--- packages/remix-server-runtime/server.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/remix-server-runtime/data.ts b/packages/remix-server-runtime/data.ts index b397507efcb..03fb5e862ef 100644 --- a/packages/remix-server-runtime/data.ts +++ b/packages/remix-server-runtime/data.ts @@ -3,7 +3,7 @@ import type { ServerRoute } from "./routes"; import { json, isResponse, isRedirectResponse } from "./responses"; /** - * An object of arbitrary for route loaders and actions provided by the + * An object of unknown type for route loaders and actions provided by the * server's `getLoadContext()` function. */ export interface AppLoadContext { @@ -20,7 +20,7 @@ export async function callRouteAction({ match, request, }: { - loadContext: unknown; + loadContext: AppLoadContext; match: RouteMatch; request: Request; }) { @@ -67,7 +67,7 @@ export async function callRouteLoader({ }: { request: Request; match: RouteMatch; - loadContext: unknown; + loadContext: AppLoadContext; }) { let loader = match.route.module.loader; diff --git a/packages/remix-server-runtime/server.ts b/packages/remix-server-runtime/server.ts index ac933ef2d7a..400cf296e9f 100644 --- a/packages/remix-server-runtime/server.ts +++ b/packages/remix-server-runtime/server.ts @@ -16,7 +16,7 @@ import { createServerHandoffString } from "./serverHandoff"; export type RequestHandler = ( request: Request, - loadContext?: AppLoadContext + loadContext: AppLoadContext ) => Promise; export type CreateRequestHandlerFunction = ( @@ -82,7 +82,7 @@ async function handleDataRequest({ serverMode, }: { handleDataRequest?: HandleDataRequestFunction; - loadContext: unknown; + loadContext: AppLoadContext; matches: RouteMatch[]; request: Request; serverMode: ServerMode; @@ -180,7 +180,7 @@ async function handleDocumentRequest({ serverMode, }: { build: ServerBuild; - loadContext: unknown; + loadContext: AppLoadContext; matches: RouteMatch[] | null; request: Request; routes: ServerRoute[]; @@ -516,7 +516,7 @@ async function handleResourceRequest({ serverMode, }: { request: Request; - loadContext: unknown; + loadContext: AppLoadContext; matches: RouteMatch[]; serverMode: ServerMode; }): Promise { From 33fd91b890062aa96f93024bb0e1e8db9c31c775 Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Mon, 1 Aug 2022 21:20:07 -0400 Subject: [PATCH 5/5] fix: make `AppLoadContext` parameter optional parameter used to be implicitly optional as it used to be typed as `any`. it _is_ intended to be optional, so this restores it as an optional param --- .changeset/great-pumas-act.md | 18 ++++++++++++++++++ packages/remix-netlify/server.ts | 2 +- packages/remix-server-runtime/data.ts | 6 +++--- packages/remix-server-runtime/routeModules.ts | 2 +- packages/remix-server-runtime/server.ts | 8 ++++---- 5 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 .changeset/great-pumas-act.md diff --git a/.changeset/great-pumas-act.md b/.changeset/great-pumas-act.md new file mode 100644 index 00000000000..9ce529926f8 --- /dev/null +++ b/.changeset/great-pumas-act.md @@ -0,0 +1,18 @@ +--- +"@remix-run/netlify": minor +"@remix-run/server-runtime": minor +--- + +Type safety for load context. + +Change `AppLoadContext` to be an interface mapping `string` to `unknown`, allowing users to extend it via: + +```ts + + +declare module "@remix-run/server-runtime" { + interface AppLoadContext { + // add custom properties here! + } +} +``` \ No newline at end of file diff --git a/packages/remix-netlify/server.ts b/packages/remix-netlify/server.ts index 3c4a15911bf..bb3afe1cd19 100644 --- a/packages/remix-netlify/server.ts +++ b/packages/remix-netlify/server.ts @@ -39,7 +39,7 @@ export function createRequestHandler({ mode = process.env.NODE_ENV, }: { build: ServerBuild; - getLoadContext?: AppLoadContext; + getLoadContext?: GetLoadContextFunction; mode?: string; }): RequestHandler { let handleRequest = createRemixRequestHandler(build, mode); diff --git a/packages/remix-server-runtime/data.ts b/packages/remix-server-runtime/data.ts index 03fb5e862ef..82c54f7bf80 100644 --- a/packages/remix-server-runtime/data.ts +++ b/packages/remix-server-runtime/data.ts @@ -8,7 +8,7 @@ import { json, isResponse, isRedirectResponse } from "./responses"; */ export interface AppLoadContext { [key: string]: unknown; -}; +} /** * Data for a route that was returned from a `loader()`. @@ -20,7 +20,7 @@ export async function callRouteAction({ match, request, }: { - loadContext: AppLoadContext; + loadContext?: AppLoadContext; match: RouteMatch; request: Request; }) { @@ -67,7 +67,7 @@ export async function callRouteLoader({ }: { request: Request; match: RouteMatch; - loadContext: AppLoadContext; + loadContext?: AppLoadContext; }) { let loader = match.route.module.loader; diff --git a/packages/remix-server-runtime/routeModules.ts b/packages/remix-server-runtime/routeModules.ts index b6bdf23c74a..ffea0987576 100644 --- a/packages/remix-server-runtime/routeModules.ts +++ b/packages/remix-server-runtime/routeModules.ts @@ -15,7 +15,7 @@ export interface RouteModules { */ export interface DataFunctionArgs { request: Request; - context: AppLoadContext; + context?: AppLoadContext; params: Params; } diff --git a/packages/remix-server-runtime/server.ts b/packages/remix-server-runtime/server.ts index 400cf296e9f..33cc2dd22a1 100644 --- a/packages/remix-server-runtime/server.ts +++ b/packages/remix-server-runtime/server.ts @@ -16,7 +16,7 @@ import { createServerHandoffString } from "./serverHandoff"; export type RequestHandler = ( request: Request, - loadContext: AppLoadContext + loadContext?: AppLoadContext ) => Promise; export type CreateRequestHandlerFunction = ( @@ -82,7 +82,7 @@ async function handleDataRequest({ serverMode, }: { handleDataRequest?: HandleDataRequestFunction; - loadContext: AppLoadContext; + loadContext?: AppLoadContext; matches: RouteMatch[]; request: Request; serverMode: ServerMode; @@ -180,7 +180,7 @@ async function handleDocumentRequest({ serverMode, }: { build: ServerBuild; - loadContext: AppLoadContext; + loadContext?: AppLoadContext; matches: RouteMatch[] | null; request: Request; routes: ServerRoute[]; @@ -516,7 +516,7 @@ async function handleResourceRequest({ serverMode, }: { request: Request; - loadContext: AppLoadContext; + loadContext?: AppLoadContext; matches: RouteMatch[]; serverMode: ServerMode; }): Promise {