Skip to content

Commit

Permalink
Merge pull request #47 from benhurley/main
Browse files Browse the repository at this point in the history
Add support for meta in `buildStrongRoute`
  • Loading branch information
tatemz authored Nov 22, 2023
2 parents bd7cb6e + b5cd1d0 commit 75b64a4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/buildStrongRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const buildStrongRoute = <
RouteId
>,
): StrongRemixRouteExports<typeof opts, Exclude<LoaderSuccess, never>> => {
const { loader, action, Component, ErrorBoundary } = opts;
const { loader, action, Component, ErrorBoundary, meta } = opts;
const output = {} as StrongRemixRouteExports<typeof opts, LoaderSuccess>;

if (loader) {
Expand All @@ -107,6 +107,10 @@ export const buildStrongRoute = <
output["action"] = async (args) => handleDataFunctionForRemix(action, args);
}

if (meta) {
output["meta"] = (args) => meta(args);
}

if (Component) {
output["Component"] = () => {
// eslint-disable-next-line react-hooks/rules-of-hooks
Expand Down
17 changes: 17 additions & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
strongLoader,
} from "./";
import { strongResponse } from "./strongResponse";
import { StrongMeta } from "./types";

describe("strongResponse", () => {
it("should create and format a response with a data object and status code", async () => {
Expand Down Expand Up @@ -116,6 +117,14 @@ describe("buildStrongRoute", () => {
return redirect(redirectResponse);
});

const meta: StrongMeta<FooResponse | BazResponse> = ({ data }) => {
if (data) {
console.log(data.data);
}

return [];
};

const Component: StrongComponent<FooResponse | BazResponse> = (props) => {
if (props.status === HttpStatusCode.ACCEPTED) return null;

Expand All @@ -142,6 +151,7 @@ describe("buildStrongRoute", () => {
ErrorBoundary,
loader,
action,
meta,
});

// TODO - add test cases for routeWithoutAction
Expand Down Expand Up @@ -453,4 +463,11 @@ describe("buildStrongRoute", () => {
`);
});
});

describe("meta", () => {
it("should return a empty aray", async () => {
const res = (route1 as any).meta({ data: {} } as any);
expect(res).toStrictEqual([]);
});
});
});
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { V2_MetaFunction } from "@remix-run/react";
import {
RouteComponent,
V2_ErrorBoundaryComponent,
V2_MetaArgs,
V2_MetaDescriptor,
} from "@remix-run/react/dist/routeModules";
import {
ActionArgs,
ActionFunction,
DataFunctionArgs,
LoaderArgs,
LoaderFunction,
} from "@remix-run/server-runtime";
Expand Down Expand Up @@ -53,6 +56,12 @@ export type StrongLoader<
Redirect extends StrongRedirect<string, RedirectStatus> = never,
> = (args: LoaderArgs) => Promise<Either.Either<Failure, Success | Redirect>>;

export type StrongMeta<
Success extends StrongResponse<unknown, NonRedirectStatus> = never,
> = (
args: V2_MetaArgs<(args: DataFunctionArgs) => Promise<Success>>,
) => V2_MetaDescriptor[];

export type StrongAction<
Failure extends StrongResponse<unknown, NonRedirectStatus> = never,
Success extends StrongResponse<unknown, NonRedirectStatus> = never,
Expand Down Expand Up @@ -99,6 +108,7 @@ export type BuildStrongRemixRouteExportsOpts<
action?: StrongAction<ActionFailure, ActionSuccess, ActionRedirect>;
Component?: StrongComponent<LoaderSuccess>;
ErrorBoundary?: StrongErrorBoundary<LoaderFailure | ActionFailure>;
meta?: StrongMeta<LoaderSuccess>;
};

export type StrongRemixRouteExports<
Expand Down

0 comments on commit 75b64a4

Please sign in to comment.