Skip to content

Commit

Permalink
types(remix-server-runtime): make AppLoadContext an empty interface…
Browse files Browse the repository at this point in the history
… instead of `any` (#1876)

* make AppLoadContext an empty interface

* Update contributors.yml

* AppLoadContext interface uses unknown for missing values

* update types for AppLoadContext

* 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

Co-authored-by: Pedro Cattori <pcattori@gmail.com>
  • Loading branch information
2 people authored and mcansh committed Aug 11, 2022
1 parent 32300ec commit a27a31a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
18 changes: 18 additions & 0 deletions .changeset/great-pumas-act.md
Original file line number Diff line number Diff line change
@@ -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!
}
}
```
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
- cysp
- damiensedgwick
- dan-gamble
- danielfgray
- danielweinmann
- davecalnan
- davecranwell-vocovo
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-netlify/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ 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 type AppLoadContext = any;
export interface AppLoadContext {
[key: string]: unknown;
}

/**
* Data for a route that was returned from a `loader()`.
Expand All @@ -18,7 +20,7 @@ export async function callRouteAction({
match,
request,
}: {
loadContext: unknown;
loadContext?: AppLoadContext;
match: RouteMatch<ServerRoute>;
request: Request;
}) {
Expand Down Expand Up @@ -65,7 +67,7 @@ export async function callRouteLoader({
}: {
request: Request;
match: RouteMatch<ServerRoute>;
loadContext: unknown;
loadContext?: AppLoadContext;
}) {
let loader = match.route.module.loader;

Expand Down
2 changes: 1 addition & 1 deletion packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface RouteModules<RouteModule> {
*/
export interface DataFunctionArgs {
request: Request;
context: AppLoadContext;
context?: AppLoadContext;
params: Params;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/remix-server-runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function handleDataRequest({
serverMode,
}: {
handleDataRequest?: HandleDataRequestFunction;
loadContext: unknown;
loadContext?: AppLoadContext;
matches: RouteMatch<ServerRoute>[];
request: Request;
serverMode: ServerMode;
Expand Down Expand Up @@ -180,7 +180,7 @@ async function handleDocumentRequest({
serverMode,
}: {
build: ServerBuild;
loadContext: unknown;
loadContext?: AppLoadContext;
matches: RouteMatch<ServerRoute>[] | null;
request: Request;
routes: ServerRoute[];
Expand Down Expand Up @@ -516,7 +516,7 @@ async function handleResourceRequest({
serverMode,
}: {
request: Request;
loadContext: unknown;
loadContext?: AppLoadContext;
matches: RouteMatch<ServerRoute>[];
serverMode: ServerMode;
}): Promise<Response> {
Expand Down

0 comments on commit a27a31a

Please sign in to comment.