@instantdb/react 0.22.90-experimental.drewh-ssr.20312174628.1 → 0.22.90-experimental.drewh-ssr.20347747146.1

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.
Files changed (48) hide show
  1. package/dist/commonjs/index.d.ts +2 -2
  2. package/dist/commonjs/index.d.ts.map +1 -1
  3. package/dist/commonjs/index.js +1 -2
  4. package/dist/commonjs/index.js.map +1 -1
  5. package/dist/commonjs/next-ssr/InstantNextDatabase.d.ts +12 -0
  6. package/dist/commonjs/next-ssr/InstantNextDatabase.d.ts.map +1 -0
  7. package/dist/commonjs/next-ssr/InstantNextDatabase.js +43 -0
  8. package/dist/commonjs/next-ssr/InstantNextDatabase.js.map +1 -0
  9. package/dist/commonjs/next-ssr/InstantSuspenseProvider.d.ts +28 -0
  10. package/dist/commonjs/next-ssr/InstantSuspenseProvider.d.ts.map +1 -0
  11. package/dist/commonjs/next-ssr/InstantSuspenseProvider.js +98 -0
  12. package/dist/commonjs/next-ssr/InstantSuspenseProvider.js.map +1 -0
  13. package/dist/commonjs/next-ssr/getUserOnServer.d.ts +2 -0
  14. package/dist/commonjs/next-ssr/getUserOnServer.d.ts.map +1 -0
  15. package/dist/commonjs/next-ssr/getUserOnServer.js +21 -0
  16. package/dist/commonjs/next-ssr/getUserOnServer.js.map +1 -0
  17. package/dist/commonjs/next-ssr/index.d.ts +6 -28
  18. package/dist/commonjs/next-ssr/index.d.ts.map +1 -1
  19. package/dist/commonjs/next-ssr/index.js +11 -126
  20. package/dist/commonjs/next-ssr/index.js.map +1 -1
  21. package/dist/esm/index.d.ts +2 -2
  22. package/dist/esm/index.d.ts.map +1 -1
  23. package/dist/esm/index.js +2 -2
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/next-ssr/InstantNextDatabase.d.ts +12 -0
  26. package/dist/esm/next-ssr/InstantNextDatabase.d.ts.map +1 -0
  27. package/dist/esm/next-ssr/InstantNextDatabase.js +36 -0
  28. package/dist/esm/next-ssr/InstantNextDatabase.js.map +1 -0
  29. package/dist/esm/next-ssr/InstantSuspenseProvider.d.ts +28 -0
  30. package/dist/esm/next-ssr/InstantSuspenseProvider.d.ts.map +1 -0
  31. package/dist/esm/next-ssr/InstantSuspenseProvider.js +93 -0
  32. package/dist/esm/next-ssr/InstantSuspenseProvider.js.map +1 -0
  33. package/dist/esm/next-ssr/getUserOnServer.d.ts +2 -0
  34. package/dist/esm/next-ssr/getUserOnServer.d.ts.map +1 -0
  35. package/dist/esm/next-ssr/getUserOnServer.js +17 -0
  36. package/dist/esm/next-ssr/getUserOnServer.js.map +1 -0
  37. package/dist/esm/next-ssr/index.d.ts +6 -28
  38. package/dist/esm/next-ssr/index.d.ts.map +1 -1
  39. package/dist/esm/next-ssr/index.js +5 -121
  40. package/dist/esm/next-ssr/index.js.map +1 -1
  41. package/dist/standalone/index.js +1322 -1383
  42. package/dist/standalone/index.umd.cjs +13 -13
  43. package/package.json +4 -4
  44. package/src/index.ts +0 -2
  45. package/src/next-ssr/InstantNextDatabase.tsx +57 -0
  46. package/src/next-ssr/InstantSuspenseProvider.tsx +187 -0
  47. package/src/next-ssr/getUserOnServer.ts +8 -0
  48. package/src/next-ssr/index.tsx +6 -223
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instantdb/react",
3
- "version": "0.22.90-experimental.drewh-ssr.20312174628.1",
3
+ "version": "0.22.90-experimental.drewh-ssr.20347747146.1",
4
4
  "description": "Instant DB for React",
5
5
  "homepage": "https://github.com/instantdb/instant/tree/main/client/packages/react",
6
6
  "repository": {
@@ -66,9 +66,9 @@
66
66
  },
67
67
  "dependencies": {
68
68
  "eventsource": "^4.0.0",
69
- "@instantdb/core": "0.22.90-experimental.drewh-ssr.20312174628.1",
70
- "@instantdb/version": "0.22.90-experimental.drewh-ssr.20312174628.1",
71
- "@instantdb/react-common": "0.22.90-experimental.drewh-ssr.20312174628.1"
69
+ "@instantdb/core": "0.22.90-experimental.drewh-ssr.20347747146.1",
70
+ "@instantdb/react-common": "0.22.90-experimental.drewh-ssr.20347747146.1",
71
+ "@instantdb/version": "0.22.90-experimental.drewh-ssr.20347747146.1"
72
72
  },
73
73
  "scripts": {
74
74
  "test": "vitest",
package/src/index.ts CHANGED
@@ -83,7 +83,6 @@ import {
83
83
  type SyncTableSyncTransaction,
84
84
  type SyncTableLoadFromStorage,
85
85
  type SyncTableSetupError,
86
- createInstantRouteHandler,
87
86
  } from '@instantdb/core';
88
87
 
89
88
  import { InstantReactAbstractDatabase } from '@instantdb/react-common';
@@ -97,7 +96,6 @@ export {
97
96
  lookup,
98
97
  init,
99
98
  init_experimental,
100
- createInstantRouteHandler,
101
99
  InstantReactWebDatabase,
102
100
  Cursors,
103
101
  i,
@@ -0,0 +1,57 @@
1
+ 'use client';
2
+ import {
3
+ AuthState,
4
+ InstantSchemaDef,
5
+ InstaQLResponse,
6
+ PageInfoResponse,
7
+ RuleParams,
8
+ ValidQuery,
9
+ } from '@instantdb/core';
10
+ import InstantReactWebDatabase from '../InstantReactWebDatabase.ts';
11
+ import { useContext } from 'react';
12
+ import { SuspsenseQueryContext } from './InstantSuspenseProvider.tsx';
13
+
14
+ export class InstantNextDatabase<
15
+ Schema extends InstantSchemaDef<any, any, any>,
16
+ UseDates extends boolean,
17
+ > extends InstantReactWebDatabase<Schema, UseDates> {
18
+ public useSuspenseQuery = <Q extends ValidQuery<Q, Schema>>(
19
+ q: Q,
20
+ opts?: {
21
+ ruleParams: RuleParams;
22
+ },
23
+ ): {
24
+ data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
25
+ pageInfo?: PageInfoResponse<Q>;
26
+ } => {
27
+ const ctx = useContext(SuspsenseQueryContext);
28
+ if (!ctx) {
29
+ throw new Error(
30
+ 'useSuspenseQuery must be used within a SuspenseQueryProvider',
31
+ );
32
+ }
33
+ return ctx.useSuspenseQuery(q, opts) as any;
34
+ };
35
+
36
+ useAuth = (): AuthState => {
37
+ const ctx = useContext(SuspsenseQueryContext);
38
+ const realAuthResult = this._useAuth();
39
+ if (!ctx) {
40
+ return realAuthResult;
41
+ }
42
+
43
+ const { ssrUser } = ctx;
44
+ if (ssrUser === undefined) {
45
+ return realAuthResult;
46
+ }
47
+ if (realAuthResult.isLoading) {
48
+ return {
49
+ error: undefined,
50
+ isLoading: false,
51
+ user: ssrUser ?? undefined, // null -> undefined for the response
52
+ };
53
+ }
54
+
55
+ return realAuthResult;
56
+ };
57
+ }
@@ -0,0 +1,187 @@
1
+ 'use client';
2
+ import {
3
+ FrameworkClient,
4
+ InstantConfig,
5
+ InstantSchemaDef,
6
+ InstaQLResponse,
7
+ PageInfoResponse,
8
+ RuleParams,
9
+ User,
10
+ ValidQuery,
11
+ } from '@instantdb/core';
12
+ import InstantReactWebDatabase from '../InstantReactWebDatabase.ts';
13
+ import {
14
+ createHydrationStreamProvider,
15
+ isServer,
16
+ } from './HydrationStreamProvider.tsx';
17
+ import { createContext, useContext, useRef, useState } from 'react';
18
+ import { InstantReactAbstractDatabase } from '@instantdb/react-common';
19
+
20
+ type InstantSuspenseProviderProps<
21
+ Schema extends InstantSchemaDef<any, any, any>,
22
+ > = {
23
+ nonce?: string;
24
+ children: React.ReactNode;
25
+ db?: InstantReactWebDatabase<Schema, any>;
26
+ config?: Omit<InstantConfig<any, any>, 'schema'> & {
27
+ schema: string;
28
+ };
29
+ user?: User | null;
30
+ };
31
+
32
+ const stream = createHydrationStreamProvider<any>();
33
+
34
+ type SuspenseQueryContextValue = {
35
+ useSuspenseQuery: (query: any, opts?: SuspenseQueryOpts) => any;
36
+ ssrUser: User | null | undefined;
37
+ };
38
+
39
+ export const SuspsenseQueryContext =
40
+ createContext<SuspenseQueryContextValue | null>(null);
41
+
42
+ // Creates a typed useSuspense hook
43
+ export const createUseSuspenseQuery = <
44
+ Schema extends InstantSchemaDef<any, any, any>,
45
+ UseDates extends boolean,
46
+ >(
47
+ _db: InstantReactWebDatabase<Schema, UseDates>,
48
+ ): (<Q extends ValidQuery<Q, Schema>>(
49
+ q: Q,
50
+ opts?: {
51
+ ruleParams: RuleParams;
52
+ },
53
+ ) => {
54
+ data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
55
+ pageInfo?: PageInfoResponse<Q>;
56
+ }) => {
57
+ return <Q extends ValidQuery<Q, Schema>>(q: any, opts: any) => {
58
+ const ctx = useContext(SuspsenseQueryContext);
59
+ if (!ctx) {
60
+ throw new Error(
61
+ 'useSuspenseQuery must be used within a SuspenseQueryProvider',
62
+ );
63
+ }
64
+ return ctx.useSuspenseQuery(q, opts) as any;
65
+ };
66
+ };
67
+
68
+ type SuspenseQueryOpts = {
69
+ ruleParams: RuleParams;
70
+ };
71
+
72
+ export const InstantSuspenseProvider = (
73
+ props: InstantSuspenseProviderProps<any>,
74
+ ) => {
75
+ const clientRef = useRef<FrameworkClient | null>(null);
76
+
77
+ if (!props.db) {
78
+ throw new Error(
79
+ 'Must provide either a db or config to InstantSuspenseProvider',
80
+ );
81
+ }
82
+
83
+ const db = useRef<InstantReactAbstractDatabase<any, any>>(props.db);
84
+
85
+ const [trackedKeys] = useState(() => new Set<string>());
86
+
87
+ if (!clientRef.current) {
88
+ if (props.user && !props.user.refresh_token) {
89
+ throw new Error(
90
+ 'User must have a refresh_token field. Recieved: ' +
91
+ JSON.stringify(props.user, null, 2),
92
+ );
93
+ }
94
+ clientRef.current = new FrameworkClient({
95
+ token: props.user?.refresh_token,
96
+ db: db.current.core,
97
+ });
98
+ }
99
+
100
+ if (isServer) {
101
+ clientRef.current.subscribe((result) => {
102
+ const { queryHash } = result;
103
+ trackedKeys.add(queryHash);
104
+ });
105
+ }
106
+
107
+ const useSuspenseQuery = (query: any, opts: SuspenseQueryOpts) => {
108
+ const nonSuspenseResult = db.current.useQuery(query, {
109
+ ...opts,
110
+ });
111
+
112
+ if (nonSuspenseResult.data) {
113
+ return {
114
+ data: nonSuspenseResult.data,
115
+ pageInfo: nonSuspenseResult.pageInfo,
116
+ };
117
+ }
118
+
119
+ // should never happen (typeguard)
120
+ if (!clientRef.current) {
121
+ throw new Error('Client ref not set up');
122
+ }
123
+
124
+ let entry = clientRef.current.getExistingResultForQuery(query, {
125
+ ruleParams: opts?.ruleParams,
126
+ });
127
+
128
+ if (!entry) {
129
+ entry = clientRef.current!.query(query, opts);
130
+ }
131
+
132
+ if (entry.status === 'pending') {
133
+ throw entry.promise;
134
+ }
135
+
136
+ if (entry.status === 'error') {
137
+ throw entry.error;
138
+ }
139
+
140
+ if (entry.status === 'success' && entry.type === 'session') {
141
+ return entry.data;
142
+ }
143
+
144
+ if (entry.status === 'success') {
145
+ const data = entry.data;
146
+ const result = clientRef.current.completeIsomorphic(
147
+ query,
148
+ data.triples,
149
+ data.attrs,
150
+ data.pageInfo,
151
+ );
152
+
153
+ return result;
154
+ }
155
+ };
156
+
157
+ return (
158
+ <SuspsenseQueryContext.Provider
159
+ value={{ useSuspenseQuery, ssrUser: props.user }}
160
+ >
161
+ <stream.Provider
162
+ nonce={props.nonce}
163
+ onFlush={() => {
164
+ const toSend: { queryKey: string; value: any }[] = [];
165
+ for (const [key, value] of clientRef.current!.resultMap.entries()) {
166
+ if (trackedKeys.has(key) && value.status === 'success') {
167
+ toSend.push({
168
+ queryKey: key,
169
+ value: value.data,
170
+ });
171
+ }
172
+ }
173
+
174
+ trackedKeys.clear();
175
+ return toSend;
176
+ }}
177
+ onEntries={(entries) => {
178
+ entries.forEach((entry) => {
179
+ clientRef.current!.addQueryResult(entry.queryKey, entry.value);
180
+ });
181
+ }}
182
+ >
183
+ {props.children}
184
+ </stream.Provider>
185
+ </SuspsenseQueryContext.Provider>
186
+ );
187
+ };
@@ -0,0 +1,8 @@
1
+ import { cookies } from 'next/headers.js';
2
+
3
+ export const getUserOnServer = async () => {
4
+ const cookieStore = await cookies();
5
+ const userJSON = cookieStore.get('instant_user');
6
+ const user = userJSON ? JSON.parse(userJSON.value) : null;
7
+ return user;
8
+ };
@@ -1,191 +1,19 @@
1
- 'use client';
2
1
  import {
3
- AuthState,
4
- FrameworkClient,
5
2
  InstantConfig,
6
3
  InstantSchemaDef,
7
4
  InstantUnknownSchema,
8
- InstaQLResponse,
9
- PageInfoResponse,
10
- RuleParams,
11
- User,
12
- ValidQuery,
13
5
  } from '@instantdb/core';
14
- import { createContext, useContext, useRef, useState } from 'react';
15
- import {
16
- createHydrationStreamProvider,
17
- isServer,
18
- } from './HydrationStreamProvider.tsx';
19
- import version from '../version.ts';
20
-
21
- import InstantReactWebDatabase from '../InstantReactWebDatabase.ts';
22
- import { InstantReactAbstractDatabase } from '@instantdb/react-common';
23
-
24
- type InstantSuspenseProviderProps<
25
- Schema extends InstantSchemaDef<any, any, any>,
26
- > = {
27
- nonce?: string;
28
- children: React.ReactNode;
29
- db?: InstantReactWebDatabase<Schema, any>;
30
- config?: Omit<InstantConfig<any, any>, 'schema'> & {
31
- schema: string;
32
- };
33
- user?: User | null;
34
- };
35
-
36
- const stream = createHydrationStreamProvider<any>();
37
-
38
- type SuspenseQueryContextValue = {
39
- useSuspenseQuery: (query: any, opts?: SuspenseQueryOpts) => any;
40
- ssrUser: User | null | undefined;
41
- };
42
-
43
- const SuspsenseQueryContext = createContext<SuspenseQueryContextValue | null>(
44
- null,
45
- );
46
-
47
- // Creates a typed useSuspense hook
48
- export const createUseSuspenseQuery = <
49
- Schema extends InstantSchemaDef<any, any, any>,
50
- UseDates extends boolean,
51
- >(
52
- _db: InstantReactWebDatabase<Schema, UseDates>,
53
- ): (<Q extends ValidQuery<Q, Schema>>(
54
- q: Q,
55
- opts?: {
56
- ruleParams: RuleParams;
57
- },
58
- ) => {
59
- data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
60
- pageInfo?: PageInfoResponse<Q>;
61
- }) => {
62
- return <Q extends ValidQuery<Q, Schema>>(q: any, opts: any) => {
63
- const ctx = useContext(SuspsenseQueryContext);
64
- if (!ctx) {
65
- throw new Error(
66
- 'useSuspenseQuery must be used within a SuspenseQueryProvider',
67
- );
68
- }
69
- return ctx.useSuspenseQuery(q, opts) as any;
70
- };
71
- };
72
-
73
- type SuspenseQueryOpts = {
74
- ruleParams: RuleParams;
75
- };
76
-
77
- export const InstantSuspenseProvider = (
78
- props: InstantSuspenseProviderProps<any>,
79
- ) => {
80
- const clientRef = useRef<FrameworkClient | null>(null);
81
-
82
- if (!props.db) {
83
- throw new Error(
84
- 'Must provide either a db or config to InstantSuspenseProvider',
85
- );
86
- }
87
-
88
- const db = useRef<InstantReactAbstractDatabase<any, any>>(props.db);
89
-
90
- const [trackedKeys] = useState(() => new Set<string>());
91
6
 
92
- if (!clientRef.current) {
93
- if (props.user && !props.user.refresh_token) {
94
- throw new Error(
95
- 'User must have a refresh_token field. Recieved: ' +
96
- JSON.stringify(props.user, null, 2),
97
- );
98
- }
99
- clientRef.current = new FrameworkClient({
100
- token: props.user?.refresh_token,
101
- db: db.current.core,
102
- });
103
- }
104
-
105
- if (isServer) {
106
- clientRef.current.subscribe((result) => {
107
- const { queryHash } = result;
108
- trackedKeys.add(queryHash);
109
- });
110
- }
111
-
112
- const useSuspenseQuery = (query: any, opts: SuspenseQueryOpts) => {
113
- const nonSuspenseResult = db.current.useQuery(query, {
114
- ...opts,
115
- });
116
-
117
- if (nonSuspenseResult.data) {
118
- return {
119
- data: nonSuspenseResult.data,
120
- pageInfo: nonSuspenseResult.pageInfo,
121
- };
122
- }
123
-
124
- // should never happen (typeguard)
125
- if (!clientRef.current) {
126
- throw new Error('Client ref not set up');
127
- }
128
-
129
- let entry = clientRef.current.getExistingResultForQuery(query, {
130
- ruleParams: opts?.ruleParams,
131
- });
132
-
133
- if (!entry) {
134
- entry = clientRef.current!.query(query, opts);
135
- }
136
-
137
- if (entry.status === 'pending') {
138
- throw entry.promise;
139
- }
140
-
141
- if (entry.status === 'error') {
142
- throw entry.error;
143
- }
7
+ import version from '../version.ts';
144
8
 
145
- if (entry.status === 'success') {
146
- const data = entry.data;
147
- const result = clientRef.current.completeIsomorphic(
148
- query,
149
- data.triples,
150
- data.attrs,
151
- data.pageInfo,
152
- );
9
+ import { InstantNextDatabase } from './InstantNextDatabase.tsx';
153
10
 
154
- return result;
155
- }
156
- };
11
+ export { getUserOnServer } from './getUserOnServer.ts';
157
12
 
158
- return (
159
- <SuspsenseQueryContext.Provider
160
- value={{ useSuspenseQuery, ssrUser: props.user }}
161
- >
162
- <stream.Provider
163
- nonce={props.nonce}
164
- onFlush={() => {
165
- const toSend: { queryKey: string; value: any }[] = [];
166
- for (const [key, value] of clientRef.current!.resultMap.entries()) {
167
- if (trackedKeys.has(key) && value.status === 'success') {
168
- toSend.push({
169
- queryKey: key,
170
- value: value.data,
171
- });
172
- }
173
- }
13
+ export { InstantNextDatabase } from './InstantNextDatabase.tsx';
14
+ export { InstantSuspenseProvider } from './InstantSuspenseProvider.tsx';
174
15
 
175
- trackedKeys.clear();
176
- return toSend;
177
- }}
178
- onEntries={(entries) => {
179
- entries.forEach((entry) => {
180
- clientRef.current!.addQueryResult(entry.queryKey, entry.value);
181
- });
182
- }}
183
- >
184
- {props.children}
185
- </stream.Provider>
186
- </SuspsenseQueryContext.Provider>
187
- );
188
- };
16
+ export { createInstantRouteHandler } from '@instantdb/core';
189
17
 
190
18
  /**
191
19
  *
@@ -217,48 +45,3 @@ export function init<
217
45
  '@instantdb/react': version,
218
46
  });
219
47
  }
220
-
221
- export class InstantNextDatabase<
222
- Schema extends InstantSchemaDef<any, any, any>,
223
- UseDates extends boolean,
224
- > extends InstantReactWebDatabase<Schema, UseDates> {
225
- public useSuspenseQuery = <Q extends ValidQuery<Q, Schema>>(
226
- q: Q,
227
- opts?: {
228
- ruleParams: RuleParams;
229
- },
230
- ): {
231
- data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
232
- pageInfo?: PageInfoResponse<Q>;
233
- } => {
234
- const ctx = useContext(SuspsenseQueryContext);
235
- if (!ctx) {
236
- throw new Error(
237
- 'useSuspenseQuery must be used within a SuspenseQueryProvider',
238
- );
239
- }
240
- return ctx.useSuspenseQuery(q, opts) as any;
241
- };
242
-
243
- useAuth = (): AuthState => {
244
- const ctx = useContext(SuspsenseQueryContext);
245
- const realAuthResult = this._useAuth();
246
- if (!ctx) {
247
- return realAuthResult;
248
- }
249
-
250
- const { ssrUser } = ctx;
251
- if (ssrUser === undefined) {
252
- return realAuthResult;
253
- }
254
- if (realAuthResult.isLoading) {
255
- return {
256
- error: undefined,
257
- isLoading: false,
258
- user: ssrUser ?? undefined, // null -> undefined for the response
259
- };
260
- }
261
-
262
- return realAuthResult;
263
- };
264
- }