-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.ts
More file actions
35 lines (33 loc) · 1.28 KB
/
Copy pathquery.ts
File metadata and controls
35 lines (33 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* TanStack Query integration over the SSR RPC bridge.
*
* It wraps the *browser* RPC client (`./rpc-client`) with the paths-stripped
* `rpcModules` descriptor (`./types/generated/api.rpc`) — which carries only the
* HTTP verb + `hasPathParams`, never a backend path. So the usual ergonomics
* work client-side with zero path leakage:
*
* useQuery(q.products.queryOptions.getProductById({ id: 1 }))
* useInfiniteQuery(q.products.infiniteQueryOptions.listProducts({ limit: 10 }))
*
* Query keys stay stable: ['developerEhsan', 'products', 'listProducts', params].
*/
import { createQueryIntegration } from '@developerehsan/api-client-query/react';
import { api } from './rpc-client';
import { rpcModules } from './types/generated/api.rpc';
interface Page {
total?: number;
skip?: number;
limit?: number;
}
export const q = createQueryIntegration(api, {
modules: rpcModules,
// DummyJSON paginates by `skip`; advance it by the page size until `total`.
pageParamName: 'skip',
getNextPageParam: (lastPage): number | undefined => {
const p = lastPage as Page;
if (typeof p?.skip !== 'number' || typeof p?.limit !== 'number' || typeof p?.total !== 'number')
return undefined;
const next = p.skip + p.limit;
return next < p.total ? next : undefined;
},
});