@amrshbib/react-native-pushbib 0.0.1 → 0.0.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/README.md CHANGED
@@ -88,8 +88,10 @@ architecture via interop), Android minSdk 24, iOS 13+.
88
88
  | `Pushbib.login(externalId)` | Bind this install to your user id (`/auth/link`). Survives token rotation. |
89
89
  | `Pushbib.logout()` | Unbind (`/auth/unlink`). |
90
90
  | `Pushbib.getMe()` | Server-side view of the logged-in user (all subscriptions + tags). |
91
- | `Pushbib.User.addTag(k, v)` / `addTags({...})` | Merge tags (user-level when logged in, else this install). |
91
+ | `Pushbib.User.addTag(k, v)` / `addTags({...})` | Merge tags (user-level when logged in, else this install). `sendTag` / `sendTags` are aliases. |
92
+ | `Pushbib.User.addTag(k, [a, b])` | A tag value may be a list. |
92
93
  | `Pushbib.User.removeTag(k)` / `removeTags([k])` | Delete tag keys. |
94
+ | `Pushbib.User.removeTag(k, [a])` | Drop items from a list, keeping the rest. |
93
95
  | `Pushbib.User.getTags()` | Local cache of tags set through this install. |
94
96
  | `Pushbib.User.getExternalId()` | Current external id (or null). |
95
97
  | `Pushbib.User.pushSubscription.getId()` | Stable subscription id (treat as a secret). |
package/index.d.ts CHANGED
@@ -58,7 +58,7 @@ export interface MeResult {
58
58
  subscribed: boolean;
59
59
  last_active: string;
60
60
  }>;
61
- tags: Record<string, string>;
61
+ tags: Record<string, TagValue>;
62
62
  }
63
63
 
64
64
  export type NotificationEventName = 'click' | 'received' | 'foregroundWillDisplay' | 'permissionChange';
@@ -99,12 +99,17 @@ export interface LocationNamespace {
99
99
  clearLocation(): Promise<void>;
100
100
  }
101
101
 
102
+ export type TagScalar = string | number | boolean;
103
+ export type TagValue = TagScalar | TagScalar[];
104
+
102
105
  export interface UserNamespace {
103
- addTag(key: string, value: string | number | boolean): Promise<void>;
104
- addTags(tags: Record<string, string | number | boolean>): Promise<void>;
105
- removeTag(key: string): Promise<void>;
106
+ addTag(key: string, value: TagValue): Promise<void>;
107
+ addTags(tags: Record<string, TagValue>): Promise<void>;
108
+ sendTag(key: string, value: TagValue): Promise<void>;
109
+ sendTags(tags: Record<string, TagValue>): Promise<void>;
110
+ removeTag(key: string, items?: TagScalar | TagScalar[]): Promise<void>;
106
111
  removeTags(keys: string[]): Promise<void>;
107
- getTags(): Record<string, string>;
112
+ getTags(): Record<string, string | string[]>;
108
113
  getExternalId(): Promise<string | null>;
109
114
  pushSubscription: PushSubscriptionNamespace;
110
115
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amrshbib/react-native-pushbib",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Self-hosted push notification SDK for React Native. Pushbib-style API (login, tags, click events) with zero Firebase setup in the app — no google-services.json, no GoogleService-Info.plist.",
5
5
  "main": "src/index.js",
6
6
  "types": "index.d.ts",
package/src/index.js CHANGED
@@ -5,6 +5,42 @@ import { log, setLogLevel } from './Logger';
5
5
  import { NATIVE_EVENTS, PushbibNative, isNativeAvailable, nativeEmitter } from './NativePushbib';
6
6
  import { SDK_NAME, SDK_VERSION } from './version';
7
7
 
8
+ const MAX_TAG_ITEMS = 100;
9
+
10
+ function toTagScalar(value) {
11
+ if (value === null || value === undefined) return null;
12
+ if (typeof value === 'object') return null;
13
+ return String(value);
14
+ }
15
+
16
+ function normalizeList(values) {
17
+ const out = [];
18
+ const seen = new Set();
19
+ for (const raw of values) {
20
+ const item = toTagScalar(raw);
21
+ if (item === null || item === '') continue;
22
+ if (seen.has(item)) continue;
23
+ seen.add(item);
24
+ out.push(item);
25
+ if (out.length >= MAX_TAG_ITEMS) break;
26
+ }
27
+ return out;
28
+ }
29
+
30
+ function normalizeTagValue(value) {
31
+ if (Array.isArray(value)) {
32
+ const list = normalizeList(value);
33
+ return list.length === 0 ? null : list;
34
+ }
35
+ return toTagScalar(value);
36
+ }
37
+
38
+ function tagItems(value) {
39
+ if (Array.isArray(value)) return value.map((v) => String(v));
40
+ if (value === null || value === undefined) return [];
41
+ return [String(value)];
42
+ }
43
+
8
44
  const PREFS = {
9
45
  subscriptionId: 'subscription_id',
10
46
  externalId: 'external_id',
@@ -151,17 +187,49 @@ class PushbibSdk {
151
187
  return sdk.User.addTags({ [key]: value });
152
188
  },
153
189
  addTags(tags) {
154
- if (!tags || typeof tags !== 'object' || Object.keys(tags).length === 0) {
190
+ if (!tags || typeof tags !== 'object' || Array.isArray(tags) || Object.keys(tags).length === 0) {
155
191
  throw new Error('User.addTags requires a non-empty object');
156
192
  }
193
+ const normalized = {};
194
+ for (const [k, v] of Object.entries(tags)) {
195
+ const value = normalizeTagValue(v);
196
+ if (value !== null) normalized[k] = value;
197
+ }
198
+ if (Object.keys(normalized).length === 0) {
199
+ throw new Error('User.addTags requires at least one usable value');
200
+ }
157
201
  return sdk._enqueue(async () => {
158
- for (const [k, v] of Object.entries(tags)) sdk._state.tags[k] = String(v);
202
+ for (const [k, v] of Object.entries(normalized)) sdk._state.tags[k] = v;
159
203
  await sdk._prefsSet(PREFS.tags, JSON.stringify(sdk._state.tags));
160
- await sdk._api.addTags({ ...sdk._tagTarget(), tags });
204
+ await sdk._api.addTags({ ...sdk._tagTarget(), tags: normalized });
161
205
  });
162
206
  },
163
- removeTag(key) {
164
- return sdk.User.removeTags([key]);
207
+ sendTag(key, value) {
208
+ return sdk.User.addTags({ [key]: value });
209
+ },
210
+ sendTags(tags) {
211
+ return sdk.User.addTags(tags);
212
+ },
213
+ removeTag(key, items) {
214
+ if (typeof key !== 'string' || key.length === 0) {
215
+ throw new Error('User.removeTag requires a tag key');
216
+ }
217
+ if (items === undefined || items === null) return sdk.User.removeTags([key]);
218
+
219
+ const drop = normalizeList(Array.isArray(items) ? items : [items]);
220
+ if (drop.length === 0) {
221
+ throw new Error('User.removeTag requires at least one item to remove');
222
+ }
223
+ return sdk._enqueue(async () => {
224
+ const current = sdk._state.tags[key];
225
+ if (current !== undefined) {
226
+ const kept = tagItems(current).filter((item) => !drop.includes(item));
227
+ if (kept.length === 0) delete sdk._state.tags[key];
228
+ else sdk._state.tags[key] = Array.isArray(current) ? kept : kept[0];
229
+ await sdk._prefsSet(PREFS.tags, JSON.stringify(sdk._state.tags));
230
+ }
231
+ await sdk._api.removeTags({ ...sdk._tagTarget(), tags: { [key]: drop } });
232
+ });
165
233
  },
166
234
  removeTags(keys) {
167
235
  if (!Array.isArray(keys) || keys.length === 0) {
package/src/version.js CHANGED
@@ -1,2 +1,2 @@
1
- export const SDK_VERSION = '0.0.1';
1
+ export const SDK_VERSION = '0.0.2';
2
2
  export const SDK_NAME = `pushbib-rn/${SDK_VERSION}`;