@koolbase/core 10.0.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.
Files changed (104) hide show
  1. package/README.md +19 -0
  2. package/dist/cjs/analytics.d.ts +24 -0
  3. package/dist/cjs/analytics.js +110 -0
  4. package/dist/cjs/auth-errors.d.ts +117 -0
  5. package/dist/cjs/auth-errors.js +250 -0
  6. package/dist/cjs/auth.d.ts +213 -0
  7. package/dist/cjs/auth.js +813 -0
  8. package/dist/cjs/cache-store.d.ts +50 -0
  9. package/dist/cjs/cache-store.js +194 -0
  10. package/dist/cjs/conflict.d.ts +80 -0
  11. package/dist/cjs/conflict.js +84 -0
  12. package/dist/cjs/database-errors.d.ts +101 -0
  13. package/dist/cjs/database-errors.js +200 -0
  14. package/dist/cjs/database.d.ts +298 -0
  15. package/dist/cjs/database.js +852 -0
  16. package/dist/cjs/device-id.d.ts +1 -0
  17. package/dist/cjs/device-id.js +57 -0
  18. package/dist/cjs/device-metadata.d.ts +36 -0
  19. package/dist/cjs/device-metadata.js +94 -0
  20. package/dist/cjs/errors.d.ts +64 -0
  21. package/dist/cjs/errors.js +85 -0
  22. package/dist/cjs/flags.d.ts +15 -0
  23. package/dist/cjs/flags.js +76 -0
  24. package/dist/cjs/function-errors.d.ts +51 -0
  25. package/dist/cjs/function-errors.js +103 -0
  26. package/dist/cjs/functions.d.ts +15 -0
  27. package/dist/cjs/functions.js +83 -0
  28. package/dist/cjs/index.d.ts +22 -0
  29. package/dist/cjs/index.js +56 -0
  30. package/dist/cjs/messaging.d.ts +13 -0
  31. package/dist/cjs/messaging.js +36 -0
  32. package/dist/cjs/offline-state.d.ts +97 -0
  33. package/dist/cjs/offline-state.js +198 -0
  34. package/dist/cjs/package.json +3 -0
  35. package/dist/cjs/pending-write.d.ts +47 -0
  36. package/dist/cjs/pending-write.js +22 -0
  37. package/dist/cjs/platform.d.ts +51 -0
  38. package/dist/cjs/platform.js +41 -0
  39. package/dist/cjs/realtime.d.ts +44 -0
  40. package/dist/cjs/realtime.js +195 -0
  41. package/dist/cjs/record.d.ts +2 -0
  42. package/dist/cjs/record.js +23 -0
  43. package/dist/cjs/shared.d.ts +9 -0
  44. package/dist/cjs/shared.js +43 -0
  45. package/dist/cjs/storage-errors.d.ts +163 -0
  46. package/dist/cjs/storage-errors.js +253 -0
  47. package/dist/cjs/storage.d.ts +198 -0
  48. package/dist/cjs/storage.js +451 -0
  49. package/dist/cjs/sync-engine.d.ts +30 -0
  50. package/dist/cjs/sync-engine.js +286 -0
  51. package/dist/cjs/types.d.ts +487 -0
  52. package/dist/cjs/types.js +40 -0
  53. package/dist/esm/analytics.d.ts +24 -0
  54. package/dist/esm/analytics.js +106 -0
  55. package/dist/esm/auth-errors.d.ts +117 -0
  56. package/dist/esm/auth-errors.js +222 -0
  57. package/dist/esm/auth.d.ts +213 -0
  58. package/dist/esm/auth.js +809 -0
  59. package/dist/esm/cache-store.d.ts +50 -0
  60. package/dist/esm/cache-store.js +179 -0
  61. package/dist/esm/conflict.d.ts +80 -0
  62. package/dist/esm/conflict.js +80 -0
  63. package/dist/esm/database-errors.d.ts +101 -0
  64. package/dist/esm/database-errors.js +189 -0
  65. package/dist/esm/database.d.ts +298 -0
  66. package/dist/esm/database.js +848 -0
  67. package/dist/esm/device-id.d.ts +1 -0
  68. package/dist/esm/device-id.js +54 -0
  69. package/dist/esm/device-metadata.d.ts +36 -0
  70. package/dist/esm/device-metadata.js +90 -0
  71. package/dist/esm/errors.d.ts +64 -0
  72. package/dist/esm/errors.js +79 -0
  73. package/dist/esm/flags.d.ts +15 -0
  74. package/dist/esm/flags.js +72 -0
  75. package/dist/esm/function-errors.d.ts +51 -0
  76. package/dist/esm/function-errors.js +93 -0
  77. package/dist/esm/functions.d.ts +15 -0
  78. package/dist/esm/functions.js +79 -0
  79. package/dist/esm/index.d.ts +22 -0
  80. package/dist/esm/index.js +25 -0
  81. package/dist/esm/messaging.d.ts +13 -0
  82. package/dist/esm/messaging.js +32 -0
  83. package/dist/esm/offline-state.d.ts +97 -0
  84. package/dist/esm/offline-state.js +189 -0
  85. package/dist/esm/package.json +3 -0
  86. package/dist/esm/pending-write.d.ts +47 -0
  87. package/dist/esm/pending-write.js +19 -0
  88. package/dist/esm/platform.d.ts +51 -0
  89. package/dist/esm/platform.js +36 -0
  90. package/dist/esm/realtime.d.ts +44 -0
  91. package/dist/esm/realtime.js +191 -0
  92. package/dist/esm/record.d.ts +2 -0
  93. package/dist/esm/record.js +20 -0
  94. package/dist/esm/shared.d.ts +9 -0
  95. package/dist/esm/shared.js +39 -0
  96. package/dist/esm/storage-errors.d.ts +163 -0
  97. package/dist/esm/storage-errors.js +239 -0
  98. package/dist/esm/storage.d.ts +198 -0
  99. package/dist/esm/storage.js +447 -0
  100. package/dist/esm/sync-engine.d.ts +30 -0
  101. package/dist/esm/sync-engine.js +282 -0
  102. package/dist/esm/types.d.ts +487 -0
  103. package/dist/esm/types.js +37 -0
  104. package/package.json +45 -0
@@ -0,0 +1,213 @@
1
+ import { AuthStateListener, KoolbaseConfig, KoolbaseSession, KoolbaseUser, LinkPhoneParams, LoginParams, OtpSendResult, PhoneVerifyResult, RegisterParams, RestoreResult, SendOtpParams, SignInWithAppleParams, VerifyOtpParams } from './types.js';
2
+ import type { SignInWithGoogleParams } from './types.js';
3
+ export declare class KoolbaseAuth {
4
+ private config;
5
+ private storage;
6
+ private session;
7
+ private metadata;
8
+ private fetchFn;
9
+ private timeoutMs;
10
+ private ongoingRefresh;
11
+ private listeners;
12
+ constructor(config: KoolbaseConfig);
13
+ /**
14
+ * Subscribe to authentication state changes. The listener fires:
15
+ * - Immediately on subscribe, with the current user (or null).
16
+ * - On every successful login, register, refresh, session restoration.
17
+ * - On logout / explicit setSession(null).
18
+ * - On linkPhone success (user object updated with phone fields).
19
+ *
20
+ * Returns an unsubscribe function. Call it when the consumer no longer
21
+ * needs updates (e.g. in a React useEffect cleanup).
22
+ *
23
+ * Listener errors are swallowed so a buggy listener can't break auth
24
+ * state propagation to other listeners.
25
+ *
26
+ * @example
27
+ * const unsubscribe = auth.onAuthStateChange((user) => {
28
+ * setCurrentUser(user);
29
+ * });
30
+ * // later:
31
+ * unsubscribe();
32
+ */
33
+ onAuthStateChange(listener: AuthStateListener): () => void;
34
+ private fireAuthStateChange;
35
+ /**
36
+ * Compose the full header set for an outbound request: base headers,
37
+ * device metadata, and optionally the Authorization bearer token.
38
+ * Async because device metadata's first build may read from keychain.
39
+ */
40
+ private prepareHeaders;
41
+ /**
42
+ * Low-level request helper used by every endpoint. Wires together:
43
+ * - The injected fetch implementation (config.fetch or global fetch)
44
+ * - Device metadata + x-api-key + auth header in one place
45
+ * - AbortController-based timeout (config.authTimeout, default 10s)
46
+ *
47
+ * On timeout, fetch rejects with an AbortError; callers see this as a
48
+ * non-KoolbaseAuthError exception, which restoreSession() treats as
49
+ * Offline (preserving optimistic state).
50
+ */
51
+ private authRequest;
52
+ /**
53
+ * Authenticated request wrapper. Refreshes the access token if it's
54
+ * stale (within 1-min buffer of expiry) before issuing the call, then
55
+ * delegates to {@link authRequest} with includeAuth=true.
56
+ */
57
+ private authedRequest;
58
+ private setSessionInternal;
59
+ /**
60
+ * Discards the stored session without contacting the server.
61
+ *
62
+ * For when the session is already known to be unusable — the server rejected
63
+ * the token, or a build was pointed at a different project and the persisted
64
+ * session belongs to the old one. Unlike `logout()` there is no server call:
65
+ * the token has already been refused, and asking for it to be revoked would
66
+ * only add a round trip that cannot succeed.
67
+ *
68
+ * Safe in any state, including with no session at all. The SDK calls this
69
+ * itself when a request is rejected as unauthenticated, so most apps will not
70
+ * need to.
71
+ */
72
+ clearStoredSession(): Promise<void>;
73
+ private clearSessionInternal;
74
+ restoreSession(): Promise<RestoreResult>;
75
+ register(params: RegisterParams): Promise<KoolbaseUser>;
76
+ login(params: LoginParams): Promise<KoolbaseSession>;
77
+ /**
78
+ * Sign in with Apple using a credential obtained from a native Apple
79
+ * Sign-In SDK.
80
+ *
81
+ * The SDK is library-agnostic — use any native Apple Sign-In package
82
+ * (`@invertase/react-native-apple-authentication`, etc.) and pass the
83
+ * resulting `identityToken`, optional `nonce`, and optional `fullName`.
84
+ *
85
+ * `fullName` is meaningful only on first sign-in — Apple omits name
86
+ * data on subsequent sign-ins. The server persists at link time and
87
+ * ignores on subsequent sign-ins.
88
+ *
89
+ * On success the session is persisted via the configured storage and
90
+ * `onAuthStateChange` fires with the resolved user.
91
+ *
92
+ * @throws AppleSignInNotConfiguredError when Apple is not enabled in
93
+ * the dashboard OAuth config for this environment (400).
94
+ * @throws InvalidAppleTokenError when the token signature, audience,
95
+ * expiry, replay, or nonce check failed server-side (401).
96
+ * @throws UserDisabledError when the account flag is set to disabled (403).
97
+ * @throws AppleEmailRequiredError when Apple did not return email for
98
+ * a new-account sign-in (400).
99
+ * @throws OAuthEmailConflictError when email matches existing user
100
+ * but auto-link rule blocked (409).
101
+ */
102
+ signInWithApple(params: SignInWithAppleParams): Promise<KoolbaseSession>;
103
+ /**
104
+ * Sign in with Google using an idToken from a native Google Sign-In SDK.
105
+ *
106
+ * The SDK is library-agnostic — use any native Google Sign-In package
107
+ * (`@react-native-google-signin/google-signin`, etc.) and pass the
108
+ * resulting `idToken`. Google embeds the user's name and email in the
109
+ * idToken itself, so this method does not take a `fullName` parameter
110
+ * (unlike `signInWithApple`).
111
+ *
112
+ * On success the session is persisted via the configured storage and
113
+ * `onAuthStateChange` fires with the resolved user.
114
+ *
115
+ * @throws GoogleSignInNotConfiguredError when Google is not enabled
116
+ * in the OAuth config for this environment (400).
117
+ * @throws InvalidGoogleTokenError when the token signature, audience,
118
+ * expiry, replay, or nonce check failed server-side (401).
119
+ * @throws UserDisabledError when the account flag is set to disabled (403).
120
+ * @throws GoogleEmailRequiredError when Google did not return email
121
+ * for a new-account sign-in (400).
122
+ * @throws OAuthEmailConflictError when email matches existing user
123
+ * but auto-link rule blocked (409).
124
+ */
125
+ signInWithGoogle(params: SignInWithGoogleParams): Promise<KoolbaseSession>;
126
+ /**
127
+ * Parses a /v1/sdk/auth/oauth/google response. Code-first: the server
128
+ * emits unified OAuth codes (oauth_not_configured, invalid_oauth_token,
129
+ * oauth_email_required, oauth_email_conflict) for both providers; the
130
+ * provider distinction is made here so Google codes map to Google-specific
131
+ * errors. Status + message logic is retained as a fallback for older servers.
132
+ */
133
+ private parseGoogleSessionResponse;
134
+ /**
135
+ * Parses a /v1/sdk/auth/oauth/apple response. Code-first; the provider
136
+ * distinction is made here so the server's unified OAuth codes map to
137
+ * Apple-specific errors. Status + message logic is retained as a fallback
138
+ * for older servers.
139
+ */
140
+ private parseAppleSessionResponse;
141
+ refresh(refreshToken?: string): Promise<KoolbaseSession>;
142
+ private _doRefresh;
143
+ logout(): Promise<boolean>;
144
+ forgotPassword(email: string): Promise<void>;
145
+ resetPassword(token: string, password: string): Promise<void>;
146
+ unlock(token: string): Promise<void>;
147
+ get currentUser(): KoolbaseUser | null;
148
+ get accessToken(): string | null;
149
+ /**
150
+ * Currently-valid access token for data-plane requests, refreshing
151
+ * (via refresh()) if the cached one is near expiry. Returns null when no
152
+ * session exists or refresh fails — callers then go api-key-only and the
153
+ * server treats it as having no end-user identity. The db/storage/functions
154
+ * clients pull from this per request so identity follows the live session.
155
+ */
156
+ validAccessToken(): Promise<string | null>;
157
+ setSession(session: KoolbaseSession | null): Promise<void>;
158
+ /**
159
+ * @deprecated v1.9.0: Server endpoint /v1/sdk/auth/oauth not yet
160
+ * shipped. This method previously routed to /v1/auth/oauth (dashboard
161
+ * developer OAuth) which never created project-scoped end-user
162
+ * sessions. Properly implemented in v1.10.0 with provider-specific
163
+ * server endpoints under /v1/sdk/auth/oauth/{apple,google,github}.
164
+ * Use email/password sign-in for now.
165
+ *
166
+ * @throws Always throws KoolbaseAuthError('not_implemented').
167
+ */
168
+ oauthLogin(_params: {
169
+ provider: string;
170
+ token: string;
171
+ email?: string;
172
+ name?: string;
173
+ avatarUrl?: string;
174
+ }): Promise<never>;
175
+ sendOtp(params: SendOtpParams): Promise<OtpSendResult>;
176
+ verifyOtp(params: VerifyOtpParams): Promise<PhoneVerifyResult>;
177
+ linkPhone(params: LinkPhoneParams): Promise<void>;
178
+ /**
179
+ * Release resources held by this auth client. Clears the in-memory
180
+ * listener set. Does not invalidate sessions or clear storage — call
181
+ * {@link logout} for that.
182
+ */
183
+ dispose(): void;
184
+ private validatePhone;
185
+ private _ensureValidToken;
186
+ private mapUser;
187
+ /**
188
+ * Parse a session-returning response (login, register, refresh).
189
+ * Non-2xx is delegated to throwTypedError, which is code-first
190
+ * (reads body.code) with a status/message fallback. isRefresh only
191
+ * affects how a bare 401 (no code, older server) is interpreted.
192
+ */
193
+ private parseSessionResponse;
194
+ private checkResponse;
195
+ /**
196
+ * Map a non-2xx credential/session response to a typed error.
197
+ *
198
+ * Code-first: the server now emits a stable `code` on every error
199
+ * (contract conformance), so we switch on body.code. The status +
200
+ * message logic is retained as a fallback for older servers or any
201
+ * response that arrives without a code. isRefresh only changes how a
202
+ * bare 401 is interpreted in the fallback path.
203
+ */
204
+ private throwTypedError;
205
+ /**
206
+ * Parse a phone-auth response. Code-first, with a phone-specific twist:
207
+ * the server emits the generic `rate_limit` code for the phone endpoints
208
+ * (they share the default 429), but phone has a dedicated server-side
209
+ * rate-limiter, so we surface OtpRateLimitError rather than RateLimitError.
210
+ * Status + message logic is retained as a fallback for older servers.
211
+ */
212
+ private parsePhoneResponse;
213
+ }