@basictech/react 0.7.0 → 0.8.0-beta.2
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/.turbo/turbo-build.log +13 -12
- package/AUTH_IMPLEMENTATION_GUIDE.md +20 -18
- package/changelog.md +24 -2
- package/dist/index.d.mts +121 -48
- package/dist/index.d.ts +121 -48
- package/dist/index.js +1996 -758
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1981 -749
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
- package/readme.md +50 -1
- package/src/AuthContext.tsx +294 -818
- package/src/config.ts +1 -19
- package/src/context.tsx +104 -0
- package/src/core/auth/AuthManager.ts +858 -0
- package/src/core/db/RemoteCollection.ts +30 -16
- package/src/core/db/index.ts +1 -1
- package/src/core/db/types.ts +13 -1
- package/src/dev/BasicDevToolbar.tsx +665 -0
- package/src/index.ts +10 -3
- package/src/sync/index.ts +15 -29
- package/src/sync/syncProtocol.js +84 -22
- package/src/sync/tokenRegistry.ts +20 -0
- package/src/updater/updateMigrations.ts +3 -3
- package/src/updater/versionUpdater.ts +3 -10
- package/src/utils/network.ts +68 -15
- package/src/utils/normalizeClientId.ts +22 -0
- package/src/utils/resolveDid.ts +101 -0
- package/src/utils/schema.ts +3 -4
- package/src/utils/storage.ts +4 -1
package/src/config.ts
CHANGED
|
@@ -1,27 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
export const SERVER_URL = "https://api.basic.tech"
|
|
3
|
-
// export const SERVER_URL = "http://localhost:3003"
|
|
4
|
-
|
|
5
|
-
|
|
6
1
|
export const log = (...args: any[]) => {
|
|
7
2
|
try {
|
|
8
3
|
if (localStorage.getItem('basic_debug') === 'true') {
|
|
9
4
|
console.log('[basic]', ...args)
|
|
10
5
|
}
|
|
11
6
|
} catch (e) {
|
|
12
|
-
//
|
|
7
|
+
// silently fail if localStorage is unavailable (SSR)
|
|
13
8
|
}
|
|
14
9
|
}
|
|
15
|
-
|
|
16
|
-
// export const log = (message: string, ...args: any[]) => {
|
|
17
|
-
// try {
|
|
18
|
-
// if (process.env.NODE_ENV === 'development') {
|
|
19
|
-
// const stack = new Error().stack;
|
|
20
|
-
// const caller = stack?.split('\n')[2]?.trim();
|
|
21
|
-
// console.log(`[basic] ${message}`, ...args);
|
|
22
|
-
// // console.log(`[stack] ${caller}`);
|
|
23
|
-
// }
|
|
24
|
-
// } catch (e) {
|
|
25
|
-
// console.error('Error in logWithStack:', e);
|
|
26
|
-
// }
|
|
27
|
-
// }
|
package/src/context.tsx
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react'
|
|
2
|
+
import type { BasicDB } from './core/db'
|
|
3
|
+
import type { User, AuthResult, GetTokenOptions } from './core/auth/AuthManager'
|
|
4
|
+
import type { DBMode } from './core/db'
|
|
5
|
+
|
|
6
|
+
export enum DBStatus {
|
|
7
|
+
LOADING = 'LOADING',
|
|
8
|
+
OFFLINE = 'OFFLINE',
|
|
9
|
+
CONNECTING = 'CONNECTING',
|
|
10
|
+
ONLINE = 'ONLINE',
|
|
11
|
+
SYNCING = 'SYNCING',
|
|
12
|
+
ERROR = 'ERROR',
|
|
13
|
+
ERROR_WILL_RETRY = 'ERROR_WILL_RETRY',
|
|
14
|
+
ERROR_TOKEN_EXPIRED = 'ERROR_TOKEN_EXPIRED',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Snapshot of local schema vs server (for dev toolbar and debugging). */
|
|
18
|
+
export type BasicSchemaDevInfo = {
|
|
19
|
+
projectId: string | null
|
|
20
|
+
localVersion: number | undefined
|
|
21
|
+
status: string
|
|
22
|
+
valid: boolean
|
|
23
|
+
lastCheckedAt: number
|
|
24
|
+
error?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Context type for useBasic hook
|
|
29
|
+
*/
|
|
30
|
+
export type BasicContextType = {
|
|
31
|
+
isReady: boolean
|
|
32
|
+
isSignedIn: boolean
|
|
33
|
+
user: User | null
|
|
34
|
+
did: string | null
|
|
35
|
+
scope: string | null
|
|
36
|
+
hasScope: (scope: string) => boolean
|
|
37
|
+
missingScopes: () => string[]
|
|
38
|
+
|
|
39
|
+
signIn: () => Promise<void>
|
|
40
|
+
signInWithHandle: (handle: string) => Promise<void>
|
|
41
|
+
signOut: () => Promise<void>
|
|
42
|
+
signInWithCode: (code: string, state?: string) => Promise<AuthResult>
|
|
43
|
+
|
|
44
|
+
getToken: (options?: GetTokenOptions) => Promise<string>
|
|
45
|
+
getSignInUrl: (redirectUri?: string) => Promise<string>
|
|
46
|
+
|
|
47
|
+
db: BasicDB
|
|
48
|
+
dbStatus: DBStatus
|
|
49
|
+
dbMode: DBMode
|
|
50
|
+
|
|
51
|
+
/** Local schema vs server status; null if no schema on the provider. */
|
|
52
|
+
devInfo: BasicSchemaDevInfo | null
|
|
53
|
+
/** Re-run remote schema check (dev toolbar). */
|
|
54
|
+
refreshSchemaStatus: () => Promise<void>
|
|
55
|
+
|
|
56
|
+
isAuthReady: boolean
|
|
57
|
+
signin: () => Promise<void>
|
|
58
|
+
signout: () => Promise<void>
|
|
59
|
+
signinWithCode: (code: string, state?: string) => Promise<AuthResult>
|
|
60
|
+
getSignInLink: (redirectUri?: string) => Promise<string>
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const noDb: BasicDB = {
|
|
64
|
+
collection: () => {
|
|
65
|
+
throw new Error('no basicdb found - initialization failed. double check your schema.')
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const BasicContext = createContext<BasicContextType>({
|
|
70
|
+
isReady: false,
|
|
71
|
+
isSignedIn: false,
|
|
72
|
+
user: null,
|
|
73
|
+
did: null,
|
|
74
|
+
scope: null,
|
|
75
|
+
hasScope: () => false,
|
|
76
|
+
missingScopes: () => [],
|
|
77
|
+
|
|
78
|
+
signIn: () => Promise.resolve(),
|
|
79
|
+
signInWithHandle: () => Promise.resolve(),
|
|
80
|
+
signOut: () => Promise.resolve(),
|
|
81
|
+
signInWithCode: () => Promise.resolve({ success: false }),
|
|
82
|
+
|
|
83
|
+
getToken: (_options?: GetTokenOptions) => Promise.reject(new Error('no token')),
|
|
84
|
+
getSignInUrl: () => Promise.resolve(''),
|
|
85
|
+
|
|
86
|
+
db: noDb,
|
|
87
|
+
dbStatus: DBStatus.LOADING,
|
|
88
|
+
dbMode: 'sync',
|
|
89
|
+
|
|
90
|
+
devInfo: null,
|
|
91
|
+
refreshSchemaStatus: async () => {},
|
|
92
|
+
|
|
93
|
+
isAuthReady: false,
|
|
94
|
+
signin: () => Promise.resolve(),
|
|
95
|
+
signout: () => Promise.resolve(),
|
|
96
|
+
signinWithCode: () => Promise.resolve({ success: false }),
|
|
97
|
+
getSignInLink: () => Promise.resolve(''),
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
export function useBasic() {
|
|
101
|
+
return useContext(BasicContext)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { noDb }
|