@hyper-fetch/core 9.5.2 → 9.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/features/infinite/infinite.fetch.spec.ts +213 -0
- package/__tests__/features/infinite/infinite.utils.spec.ts +388 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +265 -1
- package/dist/index.mjs.map +1 -1
- package/dist/infinite/index.d.ts +4 -0
- package/dist/infinite/index.d.ts.map +1 -0
- package/dist/infinite/infinite.d.ts +16 -0
- package/dist/infinite/infinite.d.ts.map +1 -0
- package/dist/infinite/infinite.types.d.ts +151 -0
- package/dist/infinite/infinite.types.d.ts.map +1 -0
- package/dist/infinite/infinite.utils.d.ts +53 -0
- package/dist/infinite/infinite.utils.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/infinite/index.ts +3 -0
- package/src/infinite/infinite.ts +75 -0
- package/src/infinite/infinite.types.ts +180 -0
- package/src/infinite/infinite.utils.ts +306 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { Client, fetchInfinite } from "@hyper-fetch/core";
|
|
2
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
+
import { http, HttpResponse } from "msw";
|
|
4
|
+
|
|
5
|
+
const baseUrl = "http://localhost:3000";
|
|
6
|
+
|
|
7
|
+
type Post = { id: number };
|
|
8
|
+
|
|
9
|
+
describe("Infinite [ fetchInfinite ]", () => {
|
|
10
|
+
const { server, startServer, stopServer, resetMocks } = createHttpMockingServer();
|
|
11
|
+
const calls: Record<string, string>[] = [];
|
|
12
|
+
|
|
13
|
+
let client: Client;
|
|
14
|
+
|
|
15
|
+
const createPosts = () =>
|
|
16
|
+
client.createRequest<{ response: Post[]; queryParams: { page?: number; limit?: number; search?: string } }>()({
|
|
17
|
+
endpoint: "/posts",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const createFeed = () =>
|
|
21
|
+
client.createRequest<{
|
|
22
|
+
response: { data: Post[]; next: string | null; total: number };
|
|
23
|
+
queryParams: { cursor?: string };
|
|
24
|
+
}>()({ endpoint: "/feed" });
|
|
25
|
+
|
|
26
|
+
const mock = (path: string, handler: (query: Record<string, string>) => unknown, status = 200) => {
|
|
27
|
+
server.use(
|
|
28
|
+
http.get(`${baseUrl}${path}`, ({ request }) => {
|
|
29
|
+
const query = Object.fromEntries(new URL(request.url).searchParams.entries());
|
|
30
|
+
calls.push(query);
|
|
31
|
+
return HttpResponse.json(handler(query) as any, { status });
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const mockPosts = (lastPage = 9) => {
|
|
37
|
+
mock("/posts", ({ page }) => (Number(page) > lastPage ? [] : [{ id: Number(page) }]));
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
beforeAll(() => startServer());
|
|
41
|
+
afterAll(() => stopServer());
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
client = new Client({ url: baseUrl });
|
|
45
|
+
calls.length = 0;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
afterEach(() => resetMocks());
|
|
49
|
+
|
|
50
|
+
it("should fetch only the first page by default", async () => {
|
|
51
|
+
mockPosts();
|
|
52
|
+
|
|
53
|
+
const result = await fetchInfinite(createPosts(), { pagination: { page: "page" } });
|
|
54
|
+
|
|
55
|
+
expect(calls).toEqual([{ page: "1" }]);
|
|
56
|
+
expect(result.items).toEqual([{ id: 1 }]);
|
|
57
|
+
expect(result.pages).toEqual([[{ id: 1 }]]);
|
|
58
|
+
expect(result.pageParams).toEqual([1]);
|
|
59
|
+
expect(result.hasNextPage).toBe(true);
|
|
60
|
+
expect(result.error).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it.each([0, -3])("should fetch one page when asked for %s pages", async (pages) => {
|
|
64
|
+
mockPosts();
|
|
65
|
+
|
|
66
|
+
const result = await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages });
|
|
67
|
+
|
|
68
|
+
expect(result.pages).toHaveLength(1);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should fetch the pages one after another", async () => {
|
|
72
|
+
mockPosts();
|
|
73
|
+
|
|
74
|
+
const result = await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages: 3 });
|
|
75
|
+
|
|
76
|
+
expect(calls.map((call) => call.page)).toEqual(["1", "2", "3"]);
|
|
77
|
+
expect(result.items).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }]);
|
|
78
|
+
expect(result.responses).toHaveLength(3);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should start from the page set on the request and keep other query params", async () => {
|
|
82
|
+
mockPosts();
|
|
83
|
+
|
|
84
|
+
const result = await fetchInfinite(createPosts().setQueryParams({ page: 4, search: "a" }), {
|
|
85
|
+
pagination: { page: "page" },
|
|
86
|
+
pages: 2,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
expect(calls).toEqual([
|
|
90
|
+
{ page: "4", search: "a" },
|
|
91
|
+
{ page: "5", search: "a" },
|
|
92
|
+
]);
|
|
93
|
+
expect(result.pageParams).toEqual([4, 5]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("should stop at the end of the list", async () => {
|
|
97
|
+
mockPosts(1);
|
|
98
|
+
|
|
99
|
+
const result = await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages: 9 });
|
|
100
|
+
|
|
101
|
+
expect(calls).toHaveLength(2);
|
|
102
|
+
expect(result.hasNextPage).toBe(false);
|
|
103
|
+
expect(result.items).toEqual([{ id: 1 }]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("should follow cursors and read the total", async () => {
|
|
107
|
+
mock("/feed", ({ cursor }) => {
|
|
108
|
+
return cursor ? { data: [{ id: 2 }], next: null, total: 2 } : { data: [{ id: 1 }], next: "second", total: 2 };
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const result = await fetchInfinite(createFeed(), {
|
|
112
|
+
pagination: { cursor: "cursor", next: "next", items: "data", total: "total" },
|
|
113
|
+
pages: 5,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(calls).toEqual([{}, { cursor: "second" }]);
|
|
117
|
+
expect(result.items).toEqual([{ id: 1 }, { id: 2 }]);
|
|
118
|
+
expect(result.pageParams).toEqual([undefined, "second"]);
|
|
119
|
+
expect(result.total).toBe(2);
|
|
120
|
+
expect(result.hasNextPage).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should keep the last known total when a page does not report it", async () => {
|
|
124
|
+
mock("/feed", ({ cursor }) => {
|
|
125
|
+
return cursor ? { data: [], next: null } : { data: [{ id: 1 }], next: "second", total: 7 };
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const result = await fetchInfinite(createFeed(), {
|
|
129
|
+
pagination: { cursor: "cursor", next: "next", items: "data", total: "total" },
|
|
130
|
+
pages: 2,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(result.total).toBe(7);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("should stop on a failed first page", async () => {
|
|
137
|
+
mock("/posts", () => ({ message: "Failed" }), 500);
|
|
138
|
+
|
|
139
|
+
const result = await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages: 3 });
|
|
140
|
+
|
|
141
|
+
expect(calls).toHaveLength(1);
|
|
142
|
+
expect(result.error).toEqual({ message: "Failed" });
|
|
143
|
+
expect(result.items).toEqual([]);
|
|
144
|
+
expect(result.pages).toEqual([]);
|
|
145
|
+
expect(result.dehydrated).toEqual([]);
|
|
146
|
+
expect(result.responses).toHaveLength(1);
|
|
147
|
+
expect(result.hasNextPage).toBe(false);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("should keep the pages fetched before a failure", async () => {
|
|
151
|
+
server.use(
|
|
152
|
+
http.get(`${baseUrl}/posts`, ({ request }) => {
|
|
153
|
+
const page = new URL(request.url).searchParams.get("page");
|
|
154
|
+
return page === "1"
|
|
155
|
+
? HttpResponse.json([{ id: 1 }])
|
|
156
|
+
: HttpResponse.json({ message: "Failed" }, { status: 500 });
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const result = await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages: 3 });
|
|
161
|
+
|
|
162
|
+
expect(result.items).toEqual([{ id: 1 }]);
|
|
163
|
+
expect(result.dehydrated).toHaveLength(1);
|
|
164
|
+
expect(result.error).toEqual({ message: "Failed" });
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("should dehydrate every page under its own cache key", async () => {
|
|
168
|
+
mockPosts();
|
|
169
|
+
|
|
170
|
+
const { dehydrated } = await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages: 2 });
|
|
171
|
+
|
|
172
|
+
expect(dehydrated.map((item) => item.cacheKey)).toEqual([
|
|
173
|
+
createPosts().setQueryParams({ page: 1 }).cacheKey,
|
|
174
|
+
createPosts().setQueryParams({ page: 2 }).cacheKey,
|
|
175
|
+
]);
|
|
176
|
+
expect(dehydrated[1].response.data).toEqual([{ id: 2 }]);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("should keep the scope in the dehydrated pages", async () => {
|
|
180
|
+
mockPosts();
|
|
181
|
+
|
|
182
|
+
const { dehydrated } = await fetchInfinite(createPosts().setScope("user"), { pagination: { page: "page" } });
|
|
183
|
+
|
|
184
|
+
expect(dehydrated[0].scope).toBe("user");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should fill the cache only after hydrating", async () => {
|
|
188
|
+
mockPosts();
|
|
189
|
+
|
|
190
|
+
const { dehydrated } = await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages: 2 });
|
|
191
|
+
|
|
192
|
+
expect(client.cache.keys()).toHaveLength(0);
|
|
193
|
+
|
|
194
|
+
client.hydrate(dehydrated);
|
|
195
|
+
|
|
196
|
+
expect(client.cache.keys()).toHaveLength(2);
|
|
197
|
+
expect(createPosts().setQueryParams({ page: 2 }).read()?.data).toEqual([{ id: 2 }]);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("should not use the queues", async () => {
|
|
201
|
+
mockPosts();
|
|
202
|
+
const add = vi.spyOn(client.fetchDispatcher, "add");
|
|
203
|
+
|
|
204
|
+
await fetchInfinite(createPosts(), { pagination: { page: "page" }, pages: 2 });
|
|
205
|
+
|
|
206
|
+
expect(add).not.toHaveBeenCalled();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("should explain a missing pagination", async () => {
|
|
210
|
+
await expect(fetchInfinite(createPosts(), {} as any)).rejects.toThrow('"pagination" option');
|
|
211
|
+
expect(calls).toHaveLength(0);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/* eslint-disable max-params */
|
|
2
|
+
import type { PaginationConfig, RequestInstance } from "@hyper-fetch/core";
|
|
3
|
+
import {
|
|
4
|
+
Client,
|
|
5
|
+
getCachedPages,
|
|
6
|
+
getHasNextPage,
|
|
7
|
+
getHasPreviousPage,
|
|
8
|
+
getInitialPageParam,
|
|
9
|
+
getNextPageParam,
|
|
10
|
+
getPageItems,
|
|
11
|
+
getPageRequest,
|
|
12
|
+
getPageTotal,
|
|
13
|
+
getPaginationMode,
|
|
14
|
+
getPreviousPageParam,
|
|
15
|
+
} from "@hyper-fetch/core";
|
|
16
|
+
|
|
17
|
+
describe("Infinite [ Utils ]", () => {
|
|
18
|
+
const client = new Client({ url: "http://localhost:3000" });
|
|
19
|
+
const request = client.createRequest<{ response: any; queryParams: any }>()({
|
|
20
|
+
endpoint: "/posts",
|
|
21
|
+
}) as RequestInstance;
|
|
22
|
+
|
|
23
|
+
// Setting a custom key flags the request it was called on, so those tests cannot share one
|
|
24
|
+
const createRequest = () =>
|
|
25
|
+
client.createRequest<{ response: any; queryParams: any }>()({ endpoint: "/posts" }) as RequestInstance;
|
|
26
|
+
|
|
27
|
+
const context = (pagination: PaginationConfig, data: any, param: unknown, target: RequestInstance = request) => ({
|
|
28
|
+
request: target,
|
|
29
|
+
pagination,
|
|
30
|
+
data,
|
|
31
|
+
param,
|
|
32
|
+
pages: [data],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("getPaginationMode", () => {
|
|
36
|
+
it("should recognize every mode", () => {
|
|
37
|
+
expect(getPaginationMode({ page: "page" })).toBe("page");
|
|
38
|
+
expect(getPaginationMode({ offset: "offset", limit: "limit" })).toBe("offset");
|
|
39
|
+
expect(getPaginationMode({ cursor: "cursor", next: "next" })).toBe("cursor");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should explain a missing or incomplete pagination", () => {
|
|
43
|
+
expect(() => getPaginationMode(undefined as any)).toThrow('"pagination" option');
|
|
44
|
+
expect(() => getPaginationMode({})).toThrow('"pagination" option');
|
|
45
|
+
expect(() => getPaginationMode({ cursor: "cursor" })).toThrow('"next"');
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("getInitialPageParam", () => {
|
|
50
|
+
it("should default to the start of the list", () => {
|
|
51
|
+
expect(getInitialPageParam(request, { page: "page" })).toBe(1);
|
|
52
|
+
expect(getInitialPageParam(request, { offset: "offset" })).toBe(0);
|
|
53
|
+
expect(getInitialPageParam(request, { cursor: "cursor", next: "next" })).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should start from the value of the request", () => {
|
|
57
|
+
expect(getInitialPageParam(request.setQueryParams({ page: 5 }), { page: "page" })).toBe(5);
|
|
58
|
+
expect(getInitialPageParam(request.setQueryParams({ page: 0 }), { page: "page" })).toBe(0);
|
|
59
|
+
expect(getInitialPageParam(request.setQueryParams({ page: "7" }), { page: "page" })).toBe(7);
|
|
60
|
+
expect(getInitialPageParam(request.setQueryParams({ offset: 40 }), { offset: "offset" })).toBe(40);
|
|
61
|
+
expect(getInitialPageParam(request.setQueryParams({ c: "abc" }), { cursor: "c", next: "next" })).toBe("abc");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should treat an empty cursor as the start", () => {
|
|
65
|
+
expect(getInitialPageParam(request.setQueryParams({ c: "" }), { cursor: "c", next: "next" })).toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("getPageRequest", () => {
|
|
70
|
+
it("should set the paging field and keep the other query params", () => {
|
|
71
|
+
const page = getPageRequest(request.setQueryParams({ search: "a" }), { page: "page" }, 2);
|
|
72
|
+
|
|
73
|
+
expect(page.queryParams).toEqual({ search: "a", page: 2 });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should give every page its own generated keys", () => {
|
|
77
|
+
const first = getPageRequest(request, { page: "page" }, 1);
|
|
78
|
+
const second = getPageRequest(request, { page: "page" }, 2);
|
|
79
|
+
|
|
80
|
+
expect(first.cacheKey).not.toBe(second.cacheKey);
|
|
81
|
+
expect(first.cacheKey).toBe(request.setQueryParams({ page: 1 }).cacheKey);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should return the request itself for the first cursor page", () => {
|
|
85
|
+
// oxlint-disable-next-line unicorn/no-useless-undefined
|
|
86
|
+
expect(getPageRequest(request, { cursor: "cursor", next: "next" }, undefined)).toBe(request);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should page through URL params", () => {
|
|
90
|
+
const target = client.createRequest<{ response: any }>()({ endpoint: "/posts/:page" }) as RequestInstance;
|
|
91
|
+
|
|
92
|
+
expect(getPageRequest(target, { page: "page", in: "params" }, 3).endpoint).toBe("/posts/3");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should separate the keys of pages sharing a custom cache key", () => {
|
|
96
|
+
const target = createRequest().setCacheKey("posts");
|
|
97
|
+
|
|
98
|
+
const first = getPageRequest(target, { page: "page" }, 1);
|
|
99
|
+
const second = getPageRequest(target, { page: "page" }, 2);
|
|
100
|
+
|
|
101
|
+
expect(first.cacheKey).not.toBe(second.cacheKey);
|
|
102
|
+
expect(first.cacheKey.startsWith("posts")).toBe(true);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("should separate the keys of pages inside the payload", () => {
|
|
106
|
+
const target = client.createRequest<{ response: any; payload: any }>()({
|
|
107
|
+
endpoint: "/search",
|
|
108
|
+
method: "POST",
|
|
109
|
+
}) as RequestInstance;
|
|
110
|
+
|
|
111
|
+
const first = getPageRequest(target, { page: "page", in: "payload" }, 1);
|
|
112
|
+
const second = getPageRequest(target, { page: "page", in: "payload" }, 2);
|
|
113
|
+
|
|
114
|
+
expect(first.payload).toEqual({ page: 1 });
|
|
115
|
+
expect(first.cacheKey).not.toBe(second.cacheKey);
|
|
116
|
+
expect(first.queryKey).not.toBe(second.queryKey);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("getPageItems and getPageTotal", () => {
|
|
121
|
+
it("should read an array response", () => {
|
|
122
|
+
expect(getPageItems(context({ page: "page" }, [1, 2], 1))).toEqual([1, 2]);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("should read a field and a function", () => {
|
|
126
|
+
expect(getPageItems(context({ page: "page", items: "list" }, { list: [1] }, 1))).toEqual([1]);
|
|
127
|
+
expect(getPageItems(context({ page: "page", items: ({ data }) => data.list }, { list: [2] }, 1))).toEqual([2]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should not fail on data that is not a list", () => {
|
|
131
|
+
expect(getPageItems(context({ page: "page" }, { nope: true }, 1))).toEqual([]);
|
|
132
|
+
expect(getPageItems(context({ page: "page", items: "list" }, null, 1))).toEqual([]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should read the total only when declared", () => {
|
|
136
|
+
expect(getPageTotal(context({ page: "page" }, { total: 3 }, 1))).toBeNull();
|
|
137
|
+
expect(getPageTotal(context({ page: "page", total: "total" }, { total: 3 }, 1))).toBe(3);
|
|
138
|
+
expect(getPageTotal(context({ page: "page", total: () => 8 }, {}, 1))).toBe(8);
|
|
139
|
+
expect(getPageTotal(context({ page: "page", total: "total" }, { total: "many" }, 1))).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("next page", () => {
|
|
144
|
+
const limited = request.setQueryParams({ limit: 2 });
|
|
145
|
+
|
|
146
|
+
it("should increase page numbers", () => {
|
|
147
|
+
expect(getNextPageParam(context({ page: "page" }, [1], 4))).toBe(5);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("should move offsets by the limit, or by the items without one", () => {
|
|
151
|
+
expect(getNextPageParam(context({ offset: "o", limit: "limit" }, [1], 4, limited))).toBe(6);
|
|
152
|
+
expect(getNextPageParam(context({ offset: "o" }, [1, 2, 3], 4))).toBe(7);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it.each([null, undefined, ""])("should end a cursor list on %s", (cursor) => {
|
|
156
|
+
const pagination = { cursor: "c", next: "next" };
|
|
157
|
+
|
|
158
|
+
expect(getNextPageParam(context(pagination, { next: cursor }, "a"))).toBeUndefined();
|
|
159
|
+
expect(getHasNextPage(context(pagination, { next: cursor }, "a"))).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("should keep a cursor of zero", () => {
|
|
163
|
+
expect(getHasNextPage(context({ cursor: "c", next: "next" }, { next: 0 }, "a"))).toBe(true);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it.each<[string, PaginationConfig, any, number, RequestInstance, boolean]>([
|
|
167
|
+
["an empty page", { page: "page" }, [], 1, request, false],
|
|
168
|
+
["a page without a known size", { page: "page" }, [1], 1, request, true],
|
|
169
|
+
["a full page", { page: "page", limit: "limit" }, [1, 2], 1, limited, true],
|
|
170
|
+
["a short page", { page: "page", limit: "limit" }, [1], 1, limited, false],
|
|
171
|
+
["pages below the total", { page: "page", limit: "limit", total: () => 5 }, [1, 2], 2, limited, true],
|
|
172
|
+
["the total reached", { page: "page", limit: "limit", total: () => 4 }, [1, 2], 2, limited, false],
|
|
173
|
+
["an offset below the total", { offset: "o", limit: "limit", total: () => 5 }, [1, 2], 2, limited, true],
|
|
174
|
+
["an offset at the total", { offset: "o", limit: "limit", total: () => 4 }, [1, 2], 2, limited, false],
|
|
175
|
+
])("should detect the end for %s", (_name, pagination, data, param, target, expected) => {
|
|
176
|
+
expect(getHasNextPage(context(pagination, data, param, target))).toBe(expected);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("should count the total from zero for lists numbered from zero", () => {
|
|
180
|
+
const zero = request.setQueryParams({ page: 0, limit: 2 });
|
|
181
|
+
const pagination = { page: "page", limit: "limit", total: () => 4 };
|
|
182
|
+
|
|
183
|
+
expect(getHasNextPage(context(pagination, [1, 2], 0, zero))).toBe(true);
|
|
184
|
+
expect(getHasNextPage(context(pagination, [1, 2], 1, zero))).toBe(false);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should prefer the hasNext override", () => {
|
|
188
|
+
expect(getHasNextPage(context({ page: "page", hasNext: () => true }, [], 1))).toBe(true);
|
|
189
|
+
expect(getHasNextPage(context({ page: "page", hasNext: () => false }, [1], 1))).toBe(false);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("should not let hasNext continue a list without a next cursor", () => {
|
|
193
|
+
const pagination = { cursor: "c", next: "next", hasNext: () => true };
|
|
194
|
+
|
|
195
|
+
expect(getHasNextPage(context(pagination, { next: null }, "a"))).toBe(false);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("previous page", () => {
|
|
200
|
+
it("should stop at the first page", () => {
|
|
201
|
+
expect(getPreviousPageParam(context({ page: "page" }, [], 3))).toBe(2);
|
|
202
|
+
expect(getPreviousPageParam(context({ page: "page" }, [], 1))).toBeUndefined();
|
|
203
|
+
expect(getHasPreviousPage(context({ page: "page" }, [], 1))).toBe(false);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("should reach page zero only in lists numbered from zero", () => {
|
|
207
|
+
const zero = request.setQueryParams({ page: 0 });
|
|
208
|
+
|
|
209
|
+
expect(getPreviousPageParam(context({ page: "page" }, [], 1, zero))).toBe(0);
|
|
210
|
+
expect(getPreviousPageParam(context({ page: "page" }, [], 0, zero))).toBeUndefined();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it("should not move offsets below zero", () => {
|
|
214
|
+
const limited = request.setQueryParams({ limit: 10 });
|
|
215
|
+
|
|
216
|
+
expect(getPreviousPageParam(context({ offset: "o", limit: "limit" }, [], 4, limited))).toBe(0);
|
|
217
|
+
expect(getPreviousPageParam(context({ offset: "o", limit: "limit" }, [], 0, limited))).toBeUndefined();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("should read the previous cursor", () => {
|
|
221
|
+
const pagination = { cursor: "c", next: "next", previous: "before" };
|
|
222
|
+
|
|
223
|
+
expect(getPreviousPageParam(context(pagination, { before: "a" }, "b"))).toBe("a");
|
|
224
|
+
expect(getHasPreviousPage(context(pagination, { before: null }, "b"))).toBe(false);
|
|
225
|
+
expect(getHasPreviousPage(context({ cursor: "c", next: "next" }, {}, "b"))).toBe(false);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("should prefer the hasPrevious override", () => {
|
|
229
|
+
expect(getHasPreviousPage(context({ page: "page", hasPrevious: () => false }, [], 3))).toBe(false);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe("page size", () => {
|
|
234
|
+
it.each([0, -2, "many", null])("should ignore a limit of %s", (limit) => {
|
|
235
|
+
const target = request.setQueryParams({ limit });
|
|
236
|
+
|
|
237
|
+
expect(getNextPageParam(context({ offset: "o", limit: "limit" }, [1, 2, 3], 4, target))).toBe(7);
|
|
238
|
+
expect(getHasNextPage(context({ page: "page", limit: "limit" }, [1], 1, target))).toBe(true);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("should go back by the size of the page without a limit", () => {
|
|
242
|
+
expect(getPreviousPageParam(context({ offset: "o" }, [1, 2], 6))).toBe(4);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("should compare the total with the size of the page without a limit", () => {
|
|
246
|
+
const pagination = { page: "page", total: () => 4 };
|
|
247
|
+
|
|
248
|
+
expect(getHasNextPage(context(pagination, [1, 2], 1))).toBe(true);
|
|
249
|
+
expect(getHasNextPage(context(pagination, [1, 2], 2))).toBe(false);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("should read the limit from the place of the paging field", () => {
|
|
253
|
+
const target = client.createRequest<{ response: any }>()({ endpoint: "/m/:page/:limit" }) as RequestInstance;
|
|
254
|
+
const page = target.setParams({ page: 1, limit: 2 } as any) as RequestInstance;
|
|
255
|
+
|
|
256
|
+
expect(getHasNextPage(context({ page: "page", limit: "limit", in: "params" }, [1], 1, page))).toBe(false);
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
describe("paging values", () => {
|
|
261
|
+
it.each([
|
|
262
|
+
[{ page: "page" }, { page: "x" }, 1],
|
|
263
|
+
[{ offset: "offset" }, { offset: "x" }, 0],
|
|
264
|
+
])("should fall back to the start for an invalid value", (pagination, queryParams, expected) => {
|
|
265
|
+
expect(getInitialPageParam(request.setQueryParams(queryParams), pagination)).toBe(expected);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("should start from the URL params and the payload", () => {
|
|
269
|
+
const inParams = client.createRequest<{ response: any }>()({ endpoint: "/m/:page" }) as RequestInstance;
|
|
270
|
+
const inPayload = client.createRequest<{ response: any; payload: any }>()({
|
|
271
|
+
endpoint: "/m",
|
|
272
|
+
method: "POST",
|
|
273
|
+
}) as RequestInstance;
|
|
274
|
+
|
|
275
|
+
expect(
|
|
276
|
+
getInitialPageParam(inParams.setParams({ page: 3 } as any) as RequestInstance, { page: "page", in: "params" }),
|
|
277
|
+
).toBe(3);
|
|
278
|
+
expect(
|
|
279
|
+
getInitialPageParam(inPayload.setPayload({ page: 8 }) as RequestInstance, { page: "page", in: "payload" }),
|
|
280
|
+
).toBe(8);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("should prefer the cursor when modes are mixed at runtime", () => {
|
|
284
|
+
expect(getPaginationMode({ cursor: "c", next: "n", offset: "o", page: "p" })).toBe("cursor");
|
|
285
|
+
expect(getPaginationMode({ offset: "o", page: "p" })).toBe("offset");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("should read cursors from functions", () => {
|
|
289
|
+
const pagination = { cursor: "c", next: () => "after", previous: () => "before" };
|
|
290
|
+
|
|
291
|
+
expect(getNextPageParam(context(pagination, {}, "x"))).toBe("after");
|
|
292
|
+
expect(getPreviousPageParam(context(pagination, {}, "x"))).toBe("before");
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it("should pass the page details to callbacks", () => {
|
|
296
|
+
const next = vi.fn(() => "after");
|
|
297
|
+
|
|
298
|
+
getNextPageParam({ ...context({ cursor: "c", next }, { a: 1 }, "x"), pages: [{ b: 2 }] });
|
|
299
|
+
|
|
300
|
+
expect(next).toHaveBeenCalledWith({ data: { a: 1 }, param: "x", pages: [{ b: 2 }] });
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("should keep the query params of the request untouched", () => {
|
|
304
|
+
const target = request.setQueryParams({ search: "a" });
|
|
305
|
+
|
|
306
|
+
getPageRequest(target, { page: "page" }, 2);
|
|
307
|
+
|
|
308
|
+
expect(target.queryParams).toEqual({ search: "a" });
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("should give custom query keys of the pages their own value", () => {
|
|
312
|
+
const target = createRequest().setQueryKey("posts");
|
|
313
|
+
|
|
314
|
+
const first = getPageRequest(target, { page: "page" }, 1);
|
|
315
|
+
const second = getPageRequest(target, { page: "page" }, 2);
|
|
316
|
+
|
|
317
|
+
expect(first.queryKey).not.toBe(second.queryKey);
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("should not touch the generated keys of a custom query key sibling", () => {
|
|
321
|
+
const target = createRequest().setCacheKey("posts");
|
|
322
|
+
|
|
323
|
+
expect(getPageRequest(target, { page: "page" }, 1).queryKey).toBe(target.setQueryParams({ page: 1 }).queryKey);
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
describe("getCachedPages", () => {
|
|
328
|
+
const stored = (pages: Record<string, any>) => (pageRequest: RequestInstance) => {
|
|
329
|
+
const value = pages[String((pageRequest.queryParams as any)?.page ?? (pageRequest.queryParams as any)?.c)];
|
|
330
|
+
return value ? { data: value } : undefined;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
it("should return nothing without the first page", () => {
|
|
334
|
+
expect(getCachedPages({ request, pagination: { page: "page" }, read: () => null })).toEqual([]);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it("should walk forwards until a page is missing", () => {
|
|
338
|
+
const pages = getCachedPages({
|
|
339
|
+
request,
|
|
340
|
+
pagination: { page: "page" },
|
|
341
|
+
read: stored({ 1: [1], 2: [2], 4: [4] }),
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
expect(pages.map((page) => page.param)).toEqual([1, 2]);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("should walk in both directions from the starting page", () => {
|
|
348
|
+
const pages = getCachedPages({
|
|
349
|
+
request: request.setQueryParams({ page: 3 }),
|
|
350
|
+
pagination: { page: "page" },
|
|
351
|
+
read: stored({ 2: [2], 3: [3], 4: [4] }),
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
expect(pages.map((page) => page.param)).toEqual([2, 3, 4]);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("should stop at the end of the list", () => {
|
|
358
|
+
const pages = getCachedPages({
|
|
359
|
+
request,
|
|
360
|
+
pagination: { page: "page" },
|
|
361
|
+
read: stored({ 1: [1], 2: [], 3: [3] }),
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
expect(pages.map((page) => page.param)).toEqual([1, 2]);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("should respect maxPages", () => {
|
|
368
|
+
const pages = getCachedPages({
|
|
369
|
+
request,
|
|
370
|
+
pagination: { page: "page" },
|
|
371
|
+
maxPages: 2,
|
|
372
|
+
read: stored({ 1: [1], 2: [2], 3: [3] }),
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
expect(pages).toHaveLength(2);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it("should not loop over cursors pointing back at each other", () => {
|
|
379
|
+
const pages = getCachedPages({
|
|
380
|
+
request: request.setQueryParams({ c: "a" }),
|
|
381
|
+
pagination: { cursor: "c", next: "next", previous: "before" },
|
|
382
|
+
read: stored({ a: { next: "b", before: "b" }, b: { next: "a", before: "a" } }),
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
expect(pages.map((page) => page.param)).toEqual(["a", "b"]);
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
});
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|