| 1 |
|
| 2 | import { Location } from "../router/history.js";
|
| 3 | import { DataStrategyResult, MiddlewareNextFunction } from "../router/utils.js";
|
| 4 | import { LinkDescriptor } from "../router/links.js";
|
| 5 | import { MetaDescriptor } from "../dom/ssr/routeModules.js";
|
| 6 | import { Func, Pretty } from "./utils.js";
|
| 7 | import { RouteModule } from "./route-module.js";
|
| 8 | import { ClientDataFunctionArgs, GetLoaderData, ServerDataFrom, ServerDataFunctionArgs } from "./route-data.js";
|
| 9 |
|
| 10 |
|
| 11 | type MaybePromise<T> = T | Promise<T>;
|
| 12 | type Props = {
|
| 13 | params: unknown;
|
| 14 | loaderData: unknown;
|
| 15 | actionData: unknown;
|
| 16 | };
|
| 17 | type MatchInfo = {
|
| 18 | id: string;
|
| 19 | module: RouteModule;
|
| 20 | };
|
| 21 | type MatchInfoWithLoaderData = {
|
| 22 | id: string;
|
| 23 | module?: never;
|
| 24 | loaderData: unknown;
|
| 25 | };
|
| 26 | type AnyMatchInfo = MatchInfo | MatchInfoWithLoaderData;
|
| 27 | type RouteInfo = Props & {
|
| 28 | module: RouteModule;
|
| 29 | matches: Array<AnyMatchInfo>;
|
| 30 | };
|
| 31 | type MatchData<T extends AnyMatchInfo> = T extends MatchInfo ? GetLoaderData<T["module"]> : T extends MatchInfoWithLoaderData ? T["loaderData"] : never;
|
| 32 | type MetaMatch<T extends AnyMatchInfo> = Pretty<{
|
| 33 | id: T["id"];
|
| 34 | params: Record<string, string | undefined>;
|
| 35 | pathname: string;
|
| 36 | meta: MetaDescriptor[];
|
| 37 | loaderData: MatchData<T>;
|
| 38 | handle?: unknown;
|
| 39 | error?: unknown;
|
| 40 | }>;
|
| 41 | type MetaMatches<T extends Array<AnyMatchInfo>> = T extends [infer F extends AnyMatchInfo, ...infer R extends Array<AnyMatchInfo>] ? [MetaMatch<F>, ...MetaMatches<R>] : T extends [] ? [] : Array<MetaMatch<AnyMatchInfo> | undefined>;
|
| 42 | type HasErrorBoundary<T extends RouteInfo> = T["module"] extends {
|
| 43 | ErrorBoundary: Func;
|
| 44 | } ? true : false;
|
| 45 | type CreateMetaArgs<T extends RouteInfo> = {
|
| 46 | location: Location;
|
| 47 | params: T["params"];
|
| 48 | loaderData: T["loaderData"] | (HasErrorBoundary<T> extends true ? undefined : never);
|
| 49 | error?: unknown;
|
| 50 | matches: MetaMatches<T["matches"]>;
|
| 51 | };
|
| 52 | type MetaDescriptors = MetaDescriptor[];
|
| 53 | type HeadersArgs = {
|
| 54 | loaderHeaders: Headers;
|
| 55 | parentHeaders: Headers;
|
| 56 | actionHeaders: Headers;
|
| 57 | errorHeaders: Headers | undefined;
|
| 58 | };
|
| 59 | type CreateServerMiddlewareFunction<T extends RouteInfo> = (args: ServerDataFunctionArgs<T["params"]>, next: MiddlewareNextFunction<Response>) => MaybePromise<Response | void>;
|
| 60 | type CreateClientMiddlewareFunction<T extends RouteInfo> = (args: ClientDataFunctionArgs<T["params"]>, next: MiddlewareNextFunction<Record<string, DataStrategyResult>>) => MaybePromise<Record<string, DataStrategyResult> | void>;
|
| 61 | type CreateServerLoaderArgs<T extends RouteInfo> = ServerDataFunctionArgs<T["params"]>;
|
| 62 | type CreateClientLoaderArgs<T extends RouteInfo> = ClientDataFunctionArgs<T["params"]> & {
|
| 63 | serverLoader: () => Promise<ServerDataFrom<T["module"]["loader"]>>;
|
| 64 | };
|
| 65 | type CreateServerActionArgs<T extends RouteInfo> = ServerDataFunctionArgs<T["params"]>;
|
| 66 | type CreateClientActionArgs<T extends RouteInfo> = ClientDataFunctionArgs<T["params"]> & {
|
| 67 | serverAction: () => Promise<ServerDataFrom<T["module"]["action"]>>;
|
| 68 | };
|
| 69 | type CreateHydrateFallbackProps<T extends RouteInfo, RSCEnabled extends boolean> = {
|
| 70 | params: T["params"];
|
| 71 | } & (RSCEnabled extends true ? {
|
| 72 | loaderData?: ServerDataFrom<T["module"]["loader"]>;
|
| 73 | actionData?: ServerDataFrom<T["module"]["action"]>;
|
| 74 | } : {
|
| 75 | loaderData?: T["loaderData"];
|
| 76 | actionData?: T["actionData"];
|
| 77 | });
|
| 78 | type Match<T extends AnyMatchInfo> = Pretty<{
|
| 79 | id: T["id"];
|
| 80 | params: Record<string, string | undefined>;
|
| 81 | pathname: string;
|
| 82 | loaderData: MatchData<T>;
|
| 83 | handle: unknown;
|
| 84 | }>;
|
| 85 | type Matches<T extends Array<AnyMatchInfo>> = T extends [infer F extends AnyMatchInfo, ...infer R extends Array<AnyMatchInfo>] ? [Match<F>, ...Matches<R>] : Array<Match<AnyMatchInfo> | undefined>;
|
| 86 | type CreateComponentProps<T extends RouteInfo, RSCEnabled extends boolean> = {
|
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 |
|
| 101 | params: T["params"];
|
| 102 | matches: Matches<T["matches"]>;
|
| 103 | } & (RSCEnabled extends true ? {
|
| 104 | loaderData: ServerDataFrom<T["module"]["loader"]>;
|
| 105 | actionData?: ServerDataFrom<T["module"]["action"]>;
|
| 106 | } : {
|
| 107 | loaderData: T["loaderData"];
|
| 108 | actionData?: T["actionData"];
|
| 109 | });
|
| 110 | type CreateErrorBoundaryProps<T extends RouteInfo, RSCEnabled extends boolean> = {
|
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 |
|
| 125 | params: T["params"];
|
| 126 | error: unknown;
|
| 127 | } & (RSCEnabled extends true ? {
|
| 128 | loaderData?: ServerDataFrom<T["module"]["loader"]>;
|
| 129 | actionData?: ServerDataFrom<T["module"]["action"]>;
|
| 130 | } : {
|
| 131 | loaderData?: T["loaderData"];
|
| 132 | actionData?: T["actionData"];
|
| 133 | });
|
| 134 | type GetAnnotations<Info extends RouteInfo> = {
|
| 135 | LinkDescriptors: LinkDescriptor[];
|
| 136 | LinksFunction: () => LinkDescriptor[];
|
| 137 | MetaArgs: CreateMetaArgs<Info>;
|
| 138 | MetaDescriptors: MetaDescriptors;
|
| 139 | MetaFunction: (args: CreateMetaArgs<Info>) => MetaDescriptors;
|
| 140 | HeadersArgs: HeadersArgs;
|
| 141 | HeadersFunction: (args: HeadersArgs) => Headers | HeadersInit;
|
| 142 | MiddlewareFunction: CreateServerMiddlewareFunction<Info>;
|
| 143 | ClientMiddlewareFunction: CreateClientMiddlewareFunction<Info>;
|
| 144 | LoaderArgs: CreateServerLoaderArgs<Info>;
|
| 145 | ClientLoaderArgs: CreateClientLoaderArgs<Info>;
|
| 146 | ActionArgs: CreateServerActionArgs<Info>;
|
| 147 | ClientActionArgs: CreateClientActionArgs<Info>;
|
| 148 | HydrateFallbackProps: CreateHydrateFallbackProps<Info, false>;
|
| 149 | ServerHydrateFallbackProps: CreateHydrateFallbackProps<Info, true>;
|
| 150 | ComponentProps: CreateComponentProps<Info, false>;
|
| 151 | ServerComponentProps: CreateComponentProps<Info, true>;
|
| 152 | ErrorBoundaryProps: CreateErrorBoundaryProps<Info, false>;
|
| 153 | ServerErrorBoundaryProps: CreateErrorBoundaryProps<Info, true>;
|
| 154 | };
|
| 155 |
|
| 156 | export { GetAnnotations }; |
| \ | No newline at end of file |