@koolbase/core 12.38.1 → 12.39.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.
@@ -215,6 +215,13 @@ export declare class KoolbaseDatabase {
215
215
  * rebase onto: the conflict is removed and the held writes stay as they are.
216
216
  */
217
217
  private settleConflict;
218
+ /**
219
+ * The server's version stands (keep theirs, abandon): the device's saved
220
+ * copy takes it -- it still held the refused change -- and what shows the
221
+ * collection hears of it (R125), so the screen stops showing what was
222
+ * discarded, offline too.
223
+ */
224
+ private showServerVersion;
218
225
  private dropConflict;
219
226
  /**
220
227
  * Issues the resolving write, conditional on the revision the refusal
@@ -72,12 +72,14 @@ class KoolbaseDatabase {
72
72
  // conflict, rather than the change quietly disappearing -- and what
73
73
  // stands is what the writes held behind it now build on.
74
74
  await this.settleConflict(c, c.server, c.serverRevision);
75
+ await this.showServerVersion(c);
75
76
  },
76
77
  abandon: async (id) => {
77
78
  const c = await this.requireConflict(id);
78
79
  // Neither version claimed to win, so the record stands as the server
79
80
  // has it: what anything still queued must build on.
80
81
  await this.settleConflict(c, c.server, c.serverRevision);
82
+ await this.showServerVersion(c);
81
83
  },
82
84
  };
83
85
  this.config = config;
@@ -695,6 +697,21 @@ class KoolbaseDatabase {
695
697
  (0, offline_state_js_1.rebaseHeldWrites)(s, c.recordId, resolved, revision);
696
698
  });
697
699
  }
700
+ /**
701
+ * The server's version stands (keep theirs, abandon): the device's saved
702
+ * copy takes it -- it still held the refused change -- and what shows the
703
+ * collection hears of it (R125), so the screen stops showing what was
704
+ * discarded, offline too.
705
+ */
706
+ async showServerVersion(c) {
707
+ const userId = this.getUserId();
708
+ if (userId && c.recordId && c.server) {
709
+ const fields = Object.fromEntries(Object.entries(c.server).filter(([k]) => !k.startsWith('$')));
710
+ await (0, cache_store_js_1.cacheRecord)(userId, c.collection, c.recordId, fields, c.serverRevision);
711
+ await (0, cache_store_js_2.updateInCachedQueries)(userId, c.collection, c.recordId, fields);
712
+ }
713
+ void (0, query_refresh_js_1.refreshCollectionQueries)(c.collection);
714
+ }
698
715
  async dropConflict(id) {
699
716
  const userId = this.requireUserId('conflict resolution');
700
717
  await (0, offline_state_js_1.mutateOfflineState)(userId, (s) => {
@@ -34,7 +34,7 @@ export { KoolbaseAnalytics, disabledAnalytics } from './analytics.js';
34
34
  export { KoolbaseFiscal, KoolbaseFiscalError, decodeFiscalResponse, isFiscalized, isPending } from './fiscal.js';
35
35
  export type { FiscalIntentResult, FiscalStatus } from './fiscal.js';
36
36
  export { KoolbaseMessaging } from './messaging.js';
37
- export type { RegisterTokenOptions } from './messaging.js';
37
+ export type { RegisterTokenOptions, KoolbaseMessagingPlatform } from './messaging.js';
38
38
  export { getOrCreateDeviceId } from './device-id.js';
39
39
  export { koolbaseSdkVersion } from './device-metadata.js';
40
40
  export { RestoreResult } from './types.js';
@@ -1,13 +1,30 @@
1
1
  import { KoolbaseConfig } from './types.js';
2
+ /** The platforms a token registers as: a phone's FCM token, or a browser's (R138). */
3
+ export type KoolbaseMessagingPlatform = 'android' | 'ios' | 'web';
2
4
  export interface RegisterTokenOptions {
3
5
  token: string;
4
- platform: 'android' | 'ios';
6
+ platform: KoolbaseMessagingPlatform;
7
+ /** Ignored by the API (the owner comes from the session); kept for compatibility. */
5
8
  userId?: string;
6
9
  }
7
10
  export declare class KoolbaseMessaging {
8
11
  private config;
9
12
  private deviceId;
10
- constructor(config: KoolbaseConfig);
13
+ private accessToken;
14
+ /** The last token registered, and its platform: registered again when
15
+ * someone signs in or out, so the device follows who is signed in. */
16
+ private last;
17
+ /** [accessToken]: the signed-in person's, or null -- the device is theirs
18
+ * only while they are signed in (the API takes the owner from it, never
19
+ * from the request body). */
20
+ constructor(config: KoolbaseConfig, accessToken?: () => Promise<string | null>);
11
21
  setDeviceId(deviceId: string): void;
22
+ /** Registers an FCM token with Koolbase -- tied to the signed-in person, if
23
+ * someone is signed in (R138). Call it after getting the token, and again
24
+ * when it changes; signing in or out registers it again by itself. */
12
25
  registerToken(options: RegisterTokenOptions): Promise<boolean>;
26
+ /** Someone signed in or out (Koolbase wires it): the last token is
27
+ * registered again. Nothing before a token is registered. */
28
+ sessionChanged(): Promise<void>;
29
+ private send;
13
30
  }
@@ -2,30 +2,51 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.KoolbaseMessaging = void 0;
4
4
  const network_js_1 = require("./network.js");
5
+ const PLATFORMS = new Set(['android', 'ios', 'web']);
5
6
  // ─── KoolbaseMessaging ────────────────────────────────────────────────────────
6
7
  class KoolbaseMessaging {
7
- constructor(config) {
8
+ /** [accessToken]: the signed-in person's, or null -- the device is theirs
9
+ * only while they are signed in (the API takes the owner from it, never
10
+ * from the request body). */
11
+ constructor(config, accessToken = () => Promise.resolve(null)) {
8
12
  this.deviceId = '';
13
+ /** The last token registered, and its platform: registered again when
14
+ * someone signs in or out, so the device follows who is signed in. */
15
+ this.last = null;
9
16
  this.config = config;
17
+ this.accessToken = accessToken;
10
18
  }
11
19
  setDeviceId(deviceId) {
12
20
  this.deviceId = deviceId;
13
21
  }
14
22
  // ─── Register token ───────────────────────────────────────────────────────
23
+ /** Registers an FCM token with Koolbase -- tied to the signed-in person, if
24
+ * someone is signed in (R138). Call it after getting the token, and again
25
+ * when it changes; signing in or out registers it again by itself. */
15
26
  async registerToken(options) {
27
+ if (!PLATFORMS.has(options.platform))
28
+ return false;
29
+ this.last = { token: options.token, platform: options.platform };
30
+ return this.send(options.token, options.platform);
31
+ }
32
+ /** Someone signed in or out (Koolbase wires it): the last token is
33
+ * registered again. Nothing before a token is registered. */
34
+ async sessionChanged() {
35
+ if (this.last === null)
36
+ return;
37
+ await this.send(this.last.token, this.last.platform);
38
+ }
39
+ async send(token, platform) {
16
40
  try {
41
+ const access = await this.accessToken().catch(() => null);
17
42
  const response = await (0, network_js_1.koolbaseFetch)(`${this.config.baseUrl}/v1/messaging/register`, {
18
43
  method: 'POST',
19
44
  headers: {
20
45
  'Content-Type': 'application/json',
21
46
  'x-api-key': this.config.publicKey,
47
+ ...(access ? { Authorization: `Bearer ${access}` } : {}),
22
48
  },
23
- body: JSON.stringify({
24
- device_id: this.deviceId,
25
- token: options.token,
26
- platform: options.platform,
27
- ...(options.userId && { user_id: options.userId }),
28
- }),
49
+ body: JSON.stringify({ device_id: this.deviceId, token, platform }),
29
50
  });
30
51
  return response.ok;
31
52
  }
@@ -215,6 +215,13 @@ export declare class KoolbaseDatabase {
215
215
  * rebase onto: the conflict is removed and the held writes stay as they are.
216
216
  */
217
217
  private settleConflict;
218
+ /**
219
+ * The server's version stands (keep theirs, abandon): the device's saved
220
+ * copy takes it -- it still held the refused change -- and what shows the
221
+ * collection hears of it (R125), so the screen stops showing what was
222
+ * discarded, offline too.
223
+ */
224
+ private showServerVersion;
218
225
  private dropConflict;
219
226
  /**
220
227
  * Issues the resolving write, conditional on the revision the refusal
@@ -69,12 +69,14 @@ export class KoolbaseDatabase {
69
69
  // conflict, rather than the change quietly disappearing -- and what
70
70
  // stands is what the writes held behind it now build on.
71
71
  await this.settleConflict(c, c.server, c.serverRevision);
72
+ await this.showServerVersion(c);
72
73
  },
73
74
  abandon: async (id) => {
74
75
  const c = await this.requireConflict(id);
75
76
  // Neither version claimed to win, so the record stands as the server
76
77
  // has it: what anything still queued must build on.
77
78
  await this.settleConflict(c, c.server, c.serverRevision);
79
+ await this.showServerVersion(c);
78
80
  },
79
81
  };
80
82
  this.config = config;
@@ -692,6 +694,21 @@ export class KoolbaseDatabase {
692
694
  rebaseHeldWrites(s, c.recordId, resolved, revision);
693
695
  });
694
696
  }
697
+ /**
698
+ * The server's version stands (keep theirs, abandon): the device's saved
699
+ * copy takes it -- it still held the refused change -- and what shows the
700
+ * collection hears of it (R125), so the screen stops showing what was
701
+ * discarded, offline too.
702
+ */
703
+ async showServerVersion(c) {
704
+ const userId = this.getUserId();
705
+ if (userId && c.recordId && c.server) {
706
+ const fields = Object.fromEntries(Object.entries(c.server).filter(([k]) => !k.startsWith('$')));
707
+ await cacheRecord(userId, c.collection, c.recordId, fields, c.serverRevision);
708
+ await updateInCachedQueries(userId, c.collection, c.recordId, fields);
709
+ }
710
+ void refreshCollectionQueries(c.collection);
711
+ }
695
712
  async dropConflict(id) {
696
713
  const userId = this.requireUserId('conflict resolution');
697
714
  await mutateOfflineState(userId, (s) => {
@@ -34,7 +34,7 @@ export { KoolbaseAnalytics, disabledAnalytics } from './analytics.js';
34
34
  export { KoolbaseFiscal, KoolbaseFiscalError, decodeFiscalResponse, isFiscalized, isPending } from './fiscal.js';
35
35
  export type { FiscalIntentResult, FiscalStatus } from './fiscal.js';
36
36
  export { KoolbaseMessaging } from './messaging.js';
37
- export type { RegisterTokenOptions } from './messaging.js';
37
+ export type { RegisterTokenOptions, KoolbaseMessagingPlatform } from './messaging.js';
38
38
  export { getOrCreateDeviceId } from './device-id.js';
39
39
  export { koolbaseSdkVersion } from './device-metadata.js';
40
40
  export { RestoreResult } from './types.js';
@@ -1,13 +1,30 @@
1
1
  import { KoolbaseConfig } from './types.js';
2
+ /** The platforms a token registers as: a phone's FCM token, or a browser's (R138). */
3
+ export type KoolbaseMessagingPlatform = 'android' | 'ios' | 'web';
2
4
  export interface RegisterTokenOptions {
3
5
  token: string;
4
- platform: 'android' | 'ios';
6
+ platform: KoolbaseMessagingPlatform;
7
+ /** Ignored by the API (the owner comes from the session); kept for compatibility. */
5
8
  userId?: string;
6
9
  }
7
10
  export declare class KoolbaseMessaging {
8
11
  private config;
9
12
  private deviceId;
10
- constructor(config: KoolbaseConfig);
13
+ private accessToken;
14
+ /** The last token registered, and its platform: registered again when
15
+ * someone signs in or out, so the device follows who is signed in. */
16
+ private last;
17
+ /** [accessToken]: the signed-in person's, or null -- the device is theirs
18
+ * only while they are signed in (the API takes the owner from it, never
19
+ * from the request body). */
20
+ constructor(config: KoolbaseConfig, accessToken?: () => Promise<string | null>);
11
21
  setDeviceId(deviceId: string): void;
22
+ /** Registers an FCM token with Koolbase -- tied to the signed-in person, if
23
+ * someone is signed in (R138). Call it after getting the token, and again
24
+ * when it changes; signing in or out registers it again by itself. */
12
25
  registerToken(options: RegisterTokenOptions): Promise<boolean>;
26
+ /** Someone signed in or out (Koolbase wires it): the last token is
27
+ * registered again. Nothing before a token is registered. */
28
+ sessionChanged(): Promise<void>;
29
+ private send;
13
30
  }
@@ -1,28 +1,49 @@
1
1
  import { koolbaseFetch } from './network.js';
2
+ const PLATFORMS = new Set(['android', 'ios', 'web']);
2
3
  // ─── KoolbaseMessaging ────────────────────────────────────────────────────────
3
4
  export class KoolbaseMessaging {
4
- constructor(config) {
5
+ /** [accessToken]: the signed-in person's, or null -- the device is theirs
6
+ * only while they are signed in (the API takes the owner from it, never
7
+ * from the request body). */
8
+ constructor(config, accessToken = () => Promise.resolve(null)) {
5
9
  this.deviceId = '';
10
+ /** The last token registered, and its platform: registered again when
11
+ * someone signs in or out, so the device follows who is signed in. */
12
+ this.last = null;
6
13
  this.config = config;
14
+ this.accessToken = accessToken;
7
15
  }
8
16
  setDeviceId(deviceId) {
9
17
  this.deviceId = deviceId;
10
18
  }
11
19
  // ─── Register token ───────────────────────────────────────────────────────
20
+ /** Registers an FCM token with Koolbase -- tied to the signed-in person, if
21
+ * someone is signed in (R138). Call it after getting the token, and again
22
+ * when it changes; signing in or out registers it again by itself. */
12
23
  async registerToken(options) {
24
+ if (!PLATFORMS.has(options.platform))
25
+ return false;
26
+ this.last = { token: options.token, platform: options.platform };
27
+ return this.send(options.token, options.platform);
28
+ }
29
+ /** Someone signed in or out (Koolbase wires it): the last token is
30
+ * registered again. Nothing before a token is registered. */
31
+ async sessionChanged() {
32
+ if (this.last === null)
33
+ return;
34
+ await this.send(this.last.token, this.last.platform);
35
+ }
36
+ async send(token, platform) {
13
37
  try {
38
+ const access = await this.accessToken().catch(() => null);
14
39
  const response = await koolbaseFetch(`${this.config.baseUrl}/v1/messaging/register`, {
15
40
  method: 'POST',
16
41
  headers: {
17
42
  'Content-Type': 'application/json',
18
43
  'x-api-key': this.config.publicKey,
44
+ ...(access ? { Authorization: `Bearer ${access}` } : {}),
19
45
  },
20
- body: JSON.stringify({
21
- device_id: this.deviceId,
22
- token: options.token,
23
- platform: options.platform,
24
- ...(options.userId && { user_id: options.userId }),
25
- }),
46
+ body: JSON.stringify({ device_id: this.deviceId, token, platform }),
26
47
  });
27
48
  return response.ok;
28
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koolbase/core",
3
- "version": "12.38.1",
3
+ "version": "12.39.0",
4
4
  "description": "Koolbase SDK core \u2014 shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/esm/index.d.ts",