convex-helpers 0.1.85 → 0.1.86
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/package.json +1 -1
- package/react/sessions.d.ts +24 -2
- package/react/sessions.d.ts.map +1 -1
- package/react/sessions.js +19 -1
- package/react/sessions.ts +51 -0
package/package.json
CHANGED
package/react/sessions.d.ts
CHANGED
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
* for more information.
|
|
17
17
|
*/
|
|
18
18
|
import React from "react";
|
|
19
|
-
import type { FunctionArgs, FunctionReference, FunctionReturnType } from "convex/server";
|
|
20
|
-
import { ConvexReactClient, type ConvexReactClientOptions, type MutationOptions } from "convex/react";
|
|
19
|
+
import type { FunctionArgs, FunctionReference, FunctionReturnType, PaginationOptions, PaginationResult } from "convex/server";
|
|
20
|
+
import { ConvexReactClient, type ConvexReactClientOptions, type MutationOptions, type PaginatedQueryArgs, type UsePaginatedQueryReturnType } from "convex/react";
|
|
21
21
|
import type { SessionId } from "../server/sessions.js";
|
|
22
22
|
import type { EmptyObject, BetterOmit } from "../index.js";
|
|
23
23
|
import type { OptimisticUpdate } from "convex/browser";
|
|
@@ -30,6 +30,14 @@ type SessionFunction<T extends "query" | "mutation" | "action", Args extends any
|
|
|
30
30
|
export type SessionQueryArgsArray<Fn extends SessionFunction<"query">> = keyof FunctionArgs<Fn> extends "sessionId" ? [args?: EmptyObject | "skip"] : [args: BetterOmit<FunctionArgs<Fn>, "sessionId"> | "skip"];
|
|
31
31
|
export type SessionArgsArray<Fn extends SessionFunction<"query" | "mutation" | "action">> = keyof FunctionArgs<Fn> extends "sessionId" ? [args?: EmptyObject] : [args: BetterOmit<FunctionArgs<Fn>, "sessionId">];
|
|
32
32
|
export type SessionArgsAndOptions<Fn extends SessionFunction<"mutation">, Options> = keyof FunctionArgs<Fn> extends "sessionId" ? [args?: EmptyObject, options?: Options] : [args: BetterOmit<FunctionArgs<Fn>, "sessionId">, options?: Options];
|
|
33
|
+
type SessionPaginatedQueryFunction<Args extends {
|
|
34
|
+
paginationOpts: PaginationOptions;
|
|
35
|
+
} = {
|
|
36
|
+
paginationOpts: PaginationOptions;
|
|
37
|
+
}> = FunctionReference<"query", "public", {
|
|
38
|
+
sessionId: SessionId;
|
|
39
|
+
} & Args, PaginationResult<any>>;
|
|
40
|
+
export type SessionPaginatedQueryArgs<Fn extends SessionPaginatedQueryFunction> = BetterOmit<PaginatedQueryArgs<Fn>, "sessionId"> | "skip";
|
|
33
41
|
/**
|
|
34
42
|
* Context for a Convex session, creating a server session and providing the id.
|
|
35
43
|
*
|
|
@@ -67,6 +75,20 @@ export declare const SessionProvider: React.FC<{
|
|
|
67
75
|
* second render.
|
|
68
76
|
*/
|
|
69
77
|
export declare function useSessionQuery<Query extends SessionFunction<"query">>(query: Query, ...args: SessionQueryArgsArray<Query>): FunctionReturnType<Query> | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Use this in place of {@link usePaginatedQuery} to run a query, passing a sessionId.
|
|
80
|
+
*
|
|
81
|
+
* @param query Query that takes in a sessionId parameter. Like `api.foo.bar`.
|
|
82
|
+
* @param args Args for that query, without the sessionId.
|
|
83
|
+
* @param options - An object specifying the `initialNumItems` to be loaded in
|
|
84
|
+
* the first page.
|
|
85
|
+
* @returns A {@link UsePaginatedQueryRes} that includes the currently loaded
|
|
86
|
+
* items, the status of the pagination, and a `loadMore` function.
|
|
87
|
+
* For SSR, it will skip the query until the second render.
|
|
88
|
+
*/
|
|
89
|
+
export declare function useSessionPaginatedQuery<Query extends SessionPaginatedQueryFunction>(query: Query, args: SessionPaginatedQueryArgs<Query>, options: {
|
|
90
|
+
initialNumItems: number;
|
|
91
|
+
}): UsePaginatedQueryReturnType<Query> | undefined;
|
|
70
92
|
type SessionMutation<Mutation extends FunctionReference<"mutation">> = (...args: SessionArgsArray<Mutation>) => Promise<FunctionReturnType<Mutation>>;
|
|
71
93
|
interface ReactSessionMutation<Mutation extends FunctionReference<"mutation">> extends SessionMutation<Mutation> {
|
|
72
94
|
withOptimisticUpdate(optimisticUpdate: OptimisticUpdate<FunctionArgs<Mutation>>): SessionMutation<Mutation>;
|
package/react/sessions.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["sessions.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAMN,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,
|
|
1
|
+
{"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["sessions.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAMN,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EAElB,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EAIL,iBAAiB,EACjB,KAAK,wBAAwB,EAC7B,KAAK,eAAe,EAIpB,KAAK,kBAAkB,EACvB,KAAK,2BAA2B,EACjC,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,eAAO,MAAM,mBAAmB,sBAAsB,CAAC;AAEvD,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,CAC1B,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,CAAC,KAEb,SAAS,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,GAChC,SAAS,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,EAAE,MAAM,IAAI,CAAC,CAAC;AAEjD,MAAM,MAAM,gBAAgB,GAA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ext, useEffect, useMemo, useState, } from "react";
|
|
20
|
-
import { useQuery, useMutation, useAction, ConvexReactClient, useConvex, } from "convex/react";
|
|
20
|
+
import { useQuery, useMutation, useAction, ConvexReactClient, useConvex, usePaginatedQuery, } from "convex/react";
|
|
21
21
|
export const DEFAULT_STORAGE_KEY = "convex-session-id";
|
|
22
22
|
const SessionContext = React.createContext(null);
|
|
23
23
|
/**
|
|
@@ -114,6 +114,24 @@ export function useSessionQuery(query, ...args) {
|
|
|
114
114
|
const newArgs = skip ? "skip" : { ...originalArgs, sessionId };
|
|
115
115
|
return useQuery(query, ...[newArgs]);
|
|
116
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Use this in place of {@link usePaginatedQuery} to run a query, passing a sessionId.
|
|
119
|
+
*
|
|
120
|
+
* @param query Query that takes in a sessionId parameter. Like `api.foo.bar`.
|
|
121
|
+
* @param args Args for that query, without the sessionId.
|
|
122
|
+
* @param options - An object specifying the `initialNumItems` to be loaded in
|
|
123
|
+
* the first page.
|
|
124
|
+
* @returns A {@link UsePaginatedQueryRes} that includes the currently loaded
|
|
125
|
+
* items, the status of the pagination, and a `loadMore` function.
|
|
126
|
+
* For SSR, it will skip the query until the second render.
|
|
127
|
+
*/
|
|
128
|
+
export function useSessionPaginatedQuery(query, args, options) {
|
|
129
|
+
const [sessionId] = useSessionId();
|
|
130
|
+
const skip = args === "skip" || !sessionId;
|
|
131
|
+
const originalArgs = args === "skip" ? {} : (args ?? {});
|
|
132
|
+
const newArgs = skip ? "skip" : { ...originalArgs, sessionId };
|
|
133
|
+
return usePaginatedQuery(query, newArgs, options);
|
|
134
|
+
}
|
|
117
135
|
/**
|
|
118
136
|
* Use this in place of {@link useMutation} to run a mutation with a sessionId.
|
|
119
137
|
*
|
package/react/sessions.ts
CHANGED
|
@@ -28,6 +28,8 @@ import type {
|
|
|
28
28
|
FunctionReference,
|
|
29
29
|
FunctionReturnType,
|
|
30
30
|
OptionalRestArgs,
|
|
31
|
+
PaginationOptions,
|
|
32
|
+
PaginationResult,
|
|
31
33
|
} from "convex/server";
|
|
32
34
|
import {
|
|
33
35
|
useQuery,
|
|
@@ -38,6 +40,9 @@ import {
|
|
|
38
40
|
type MutationOptions,
|
|
39
41
|
useConvex,
|
|
40
42
|
type ReactMutation,
|
|
43
|
+
usePaginatedQuery,
|
|
44
|
+
type PaginatedQueryArgs,
|
|
45
|
+
type UsePaginatedQueryReturnType,
|
|
41
46
|
} from "convex/react";
|
|
42
47
|
import type { SessionId } from "../server/sessions.js";
|
|
43
48
|
import type { EmptyObject, BetterOmit } from "../index.js";
|
|
@@ -86,6 +91,21 @@ export type SessionArgsAndOptions<
|
|
|
86
91
|
? [args?: EmptyObject, options?: Options]
|
|
87
92
|
: [args: BetterOmit<FunctionArgs<Fn>, "sessionId">, options?: Options];
|
|
88
93
|
|
|
94
|
+
type SessionPaginatedQueryFunction<
|
|
95
|
+
Args extends { paginationOpts: PaginationOptions } = {
|
|
96
|
+
paginationOpts: PaginationOptions;
|
|
97
|
+
},
|
|
98
|
+
> = FunctionReference<
|
|
99
|
+
"query",
|
|
100
|
+
"public",
|
|
101
|
+
{ sessionId: SessionId } & Args,
|
|
102
|
+
PaginationResult<any>
|
|
103
|
+
>;
|
|
104
|
+
|
|
105
|
+
export type SessionPaginatedQueryArgs<
|
|
106
|
+
Fn extends SessionPaginatedQueryFunction,
|
|
107
|
+
> = BetterOmit<PaginatedQueryArgs<Fn>, "sessionId"> | "skip";
|
|
108
|
+
|
|
89
109
|
/**
|
|
90
110
|
* Context for a Convex session, creating a server session and providing the id.
|
|
91
111
|
*
|
|
@@ -203,6 +223,37 @@ export function useSessionQuery<Query extends SessionFunction<"query">>(
|
|
|
203
223
|
return useQuery(query, ...([newArgs] as OptionalRestArgs<Query>));
|
|
204
224
|
}
|
|
205
225
|
|
|
226
|
+
/**
|
|
227
|
+
* Use this in place of {@link usePaginatedQuery} to run a query, passing a sessionId.
|
|
228
|
+
*
|
|
229
|
+
* @param query Query that takes in a sessionId parameter. Like `api.foo.bar`.
|
|
230
|
+
* @param args Args for that query, without the sessionId.
|
|
231
|
+
* @param options - An object specifying the `initialNumItems` to be loaded in
|
|
232
|
+
* the first page.
|
|
233
|
+
* @returns A {@link UsePaginatedQueryRes} that includes the currently loaded
|
|
234
|
+
* items, the status of the pagination, and a `loadMore` function.
|
|
235
|
+
* For SSR, it will skip the query until the second render.
|
|
236
|
+
*/
|
|
237
|
+
export function useSessionPaginatedQuery<
|
|
238
|
+
Query extends SessionPaginatedQueryFunction,
|
|
239
|
+
>(
|
|
240
|
+
query: Query,
|
|
241
|
+
args: SessionPaginatedQueryArgs<Query>,
|
|
242
|
+
options: { initialNumItems: number },
|
|
243
|
+
): UsePaginatedQueryReturnType<Query> | undefined {
|
|
244
|
+
const [sessionId] = useSessionId();
|
|
245
|
+
const skip = args === "skip" || !sessionId;
|
|
246
|
+
const originalArgs = args === "skip" ? {} : (args ?? {});
|
|
247
|
+
|
|
248
|
+
const newArgs = skip ? "skip" : { ...originalArgs, sessionId };
|
|
249
|
+
|
|
250
|
+
return usePaginatedQuery(
|
|
251
|
+
query,
|
|
252
|
+
newArgs as PaginatedQueryArgs<Query> | "skip",
|
|
253
|
+
options,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
|
|
206
257
|
type SessionMutation<Mutation extends FunctionReference<"mutation">> = (
|
|
207
258
|
...args: SessionArgsArray<Mutation>
|
|
208
259
|
) => Promise<FunctionReturnType<Mutation>>;
|