@koolbase/core 12.38.2 → 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.
@@ -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
  }
@@ -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.2",
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",