Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Apr 25, 2023
1 parent b005ff2 commit a2d0abc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
76 changes: 49 additions & 27 deletions packages/remix-react/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ export function createClientRoutes(
// handle gets added in via useMatches since we aren't guaranteed to
// have the route module available here
handle: undefined,
loader: createDataFunction(route, routeModulesCache, false),
action: createDataFunction(route, routeModulesCache, true),
loader: createLoaderFunction(route, routeModulesCache),
action: createActionFunction(route, routeModulesCache),
shouldRevalidate: createShouldRevalidate(route, routeModulesCache),
};
let children = createClientRoutes(
Expand Down Expand Up @@ -172,50 +172,72 @@ async function loadRouteModuleWithBlockingLinks(
return routeModule;
}

function createDataFunction(
function createLoaderFunction(
route: EntryRoute,
routeModules: RouteModules,
isAction: boolean
): LoaderFunction | ActionFunction {
routeModules: RouteModules
): LoaderFunction {
return async ({ request }) => {
let routeModulePromise = loadRouteModuleWithBlockingLinks(
route,
routeModules
);
try {
if (!route.hasLoader) {
return null;
}
let result = await fetchAndProcessResult(request, route.id);
return result;
} finally {
await routeModulePromise;
}
};
}

function createActionFunction(
route: EntryRoute,
routeModules: RouteModules
): ActionFunction {
return async ({ request }) => {
let routeModulePromise = loadRouteModuleWithBlockingLinks(
route,
routeModules
);
try {
if (isAction && !route.hasAction) {
if (!route.hasAction) {
let msg =
`Route "${route.id}" does not have an action, but you are trying ` +
`to submit to it. To fix this, please add an \`action\` function to the route`;
console.error(msg);
throw new Error(msg);
} else if (!isAction && !route.hasLoader) {
return null;
}
let result = await fetchAndProcessResult(request, route.id);
return result;
} finally {
await routeModulePromise;
}
};
}

let result = await fetchData(request, route.id);
async function fetchAndProcessResult(request: Request, routeId: string) {
let result = await fetchData(request, routeId);

if (result instanceof Error) {
throw result;
}
if (result instanceof Error) {
throw result;
}

if (isRedirectResponse(result)) {
throw getRedirect(result);
}
if (isRedirectResponse(result)) {
throw getRedirect(result);
}

if (isCatchResponse(result)) {
throw result;
}
if (isCatchResponse(result)) {
throw result;
}

if (isDeferredResponse(result) && result.body) {
return await parseDeferredReadableStream(result.body);
}
if (isDeferredResponse(result) && result.body) {
return await parseDeferredReadableStream(result.body);
}

return result;
} finally {
await routeModulePromise;
}
};
return result;
}

function getRedirect(response: Response): Response {
Expand Down
1 change: 1 addition & 0 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function callRouteActionRR({
request: stripDataParam(stripIndexParam(request)),
context: loadContext,
params,
payload: undefined,
});

if (result === undefined) {
Expand Down
14 changes: 3 additions & 11 deletions packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ export interface DataFunctionArgs {

export type LoaderArgs = DataFunctionArgs;

export type ActionArgs = DataFunctionArgs;
export type ActionArgs = DataFunctionArgs & { payload: undefined };

/**
* A function that handles data mutations for a route.
*/
export interface ActionFunction {
(args: DataFunctionArgs):
| Promise<Response>
| Response
| Promise<AppData>
| AppData;
(args: ActionArgs): Promise<Response> | Response | Promise<AppData> | AppData;
}

/**
Expand Down Expand Up @@ -90,11 +86,7 @@ export interface LinksFunction {
* A function that loads data for a route.
*/
export interface LoaderFunction {
(args: DataFunctionArgs):
| Promise<Response>
| Response
| Promise<AppData>
| AppData;
(args: LoaderArgs): Promise<Response> | Response | Promise<AppData> | AppData;
}

/**
Expand Down

0 comments on commit a2d0abc

Please sign in to comment.