Skip to content

Commit e4a13f6

Browse files
committed
Response helpers return responses, cache/action filter them out
1 parent 5d9263b commit e4a13f6

6 files changed

Lines changed: 27 additions & 12 deletions

File tree

.changeset/afraid-donkeys-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": minor
3+
---
4+
5+
Response helpers return responses, cache/action filter them out

src/data/action.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { $TRACK, createMemo, createSignal, JSX, onCleanup, getOwner } from "solid-js";
22
import { isServer } from "solid-js/web";
33
import { useRouter } from "../routing.js";
4-
import type { RouterContext, Submission, SubmissionStub, Navigator } from "../types.js";
4+
import type { RouterContext, Submission, SubmissionStub, Navigator, NarrowResponse } from "../types.js";
55
import { mockBase } from "../utils.js";
66
import { cacheKeyOp, hashKey, revalidate, cache } from "./cache.js";
77

88
export type Action<T extends Array<any>, U> = (T extends [FormData] | []
99
? JSX.SerializableAttributeValue
1010
: unknown) &
11-
((...vars: T) => Promise<U>) & {
11+
((...vars: T) => Promise<NarrowResponse<U>>) & {
1212
url: string;
1313
with<A extends any[], B extends any[]>(
14-
this: (this: any, ...args: [...A, ...B]) => Promise<U>,
14+
this: (this: any, ...args: [...A, ...B]) => Promise<NarrowResponse<U>>,
1515
...args: A
1616
): Action<B, U>;
1717
};

src/data/cache.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "solid-js";
1010
import { getRequestEvent, isServer } from "solid-js/web";
1111
import { useNavigate, getIntent, getInPreloadFn } from "../routing.js";
12-
import { CacheEntry } from "../types.js";
12+
import type { CacheEntry, NarrowResponse } from "../types.js";
1313

1414
const LocationHeader = "Location";
1515
const PRELOAD_TIMEOUT = 5000;
@@ -56,8 +56,12 @@ export type CachedFunction<T extends (...args: any) => any> = T extends (
5656
...args: infer A
5757
) => infer R
5858
? ([] extends { [K in keyof A]-?: A[K] } // A tuple full of optional values is equivalent to an empty tuple
59-
? (...args: never[]) => R
60-
: T) & {
59+
? (
60+
...args: never[]
61+
) => R extends Promise<infer P> ? Promise<NarrowResponse<P>> : NarrowResponse<R>
62+
: (
63+
...args: A
64+
) => R extends Promise<infer P> ? Promise<NarrowResponse<P>> : NarrowResponse<R>) & {
6165
keyFor: (...args: A) => string;
6266
key: string;
6367
}

src/data/response.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type RouterResponseInit = Omit<ResponseInit, "body"> & { revalidate?: string | string[] };
1+
import type { RouterResponseInit, CustomResponse } from "../types";
22

33
export function redirect(url: string, init: number | RouterResponseInit = 302) {
44
let responseInit: ResponseInit;
@@ -21,7 +21,7 @@ export function redirect(url: string, init: number | RouterResponseInit = 302) {
2121
headers: headers
2222
});
2323

24-
return response as never;
24+
return response as CustomResponse<never>;
2525
}
2626

2727
export function reload(init: RouterResponseInit = {}) {
@@ -32,7 +32,7 @@ export function reload(init: RouterResponseInit = {}) {
3232
return new Response(null, {
3333
...responseInit,
3434
headers
35-
}) as never;
35+
}) as CustomResponse<never>;
3636
}
3737

3838
export function json<T>(data: T, init: RouterResponseInit = {}) {
@@ -45,6 +45,6 @@ export function json<T>(data: T, init: RouterResponseInit = {}) {
4545
...responseInit,
4646
headers
4747
});
48-
(response as any).customBody = () => data;
49-
return response as T;
48+
(response as CustomResponse<T>).customBody = () => data;
49+
return response as CustomResponse<T>;
5050
}

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ export type {
3838
BeforeLeaveEventArgs,
3939
RouteLoadFunc,
4040
RouteLoadFuncArgs,
41+
RouterResponseInit,
42+
CustomResponse
4143
} from "./types.js";

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export type RouteDefinition<S extends string | string[] = any, T = unknown> = {
8888
children?: RouteDefinition | RouteDefinition[];
8989
component?: Component<RouteSectionProps<T>>;
9090
info?: Record<string, any>;
91-
/** @deprecated */
91+
/** @deprecated use preload */
9292
load?: RoutePreloadFunc;
9393
};
9494

@@ -220,6 +220,10 @@ export interface MaybePreloadableComponent extends Component {
220220

221221
export type CacheEntry = [number, any, Intent | undefined, Signal<number> & { count: number }];
222222

223+
export type NarrowResponse<T> = T extends CustomResponse<infer U> ? U : Exclude<T, Response>;
224+
export type RouterResponseInit = Omit<ResponseInit, "body"> & { revalidate?: string | string[] };
225+
export type CustomResponse<T> = Response & { customBody: () => T };
226+
223227
/** @deprecated */
224228
export type RouteLoadFunc = RoutePreloadFunc;
225229
/** @deprecated */

0 commit comments

Comments
 (0)