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

types(remix-server-runtime): make AppLoadContext an empty interface instead of any #1876

Merged
merged 5 commits into from
Aug 2, 2022
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
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