@temboplus/afloat 0.1.77-beta.7 → 0.1.77-beta.9

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.
@@ -1,40 +1,144 @@
1
1
  import { ContactDTO, ContactType } from "@/modules/contact/contact.dtos.js";
2
2
  import { ContactInfo } from "./contact-info.model.js";
3
+ import { z } from "zod";
3
4
  /**
4
- * Contact class that wraps the Zod schema and provides additional functionality
5
+ * Zod schema for Contact JSON serialization
6
+ * This schema validates the JSON representation of a Contact instance
7
+ *
8
+ * The Contact JSON format wraps the ContactDTO structure for consistency
9
+ * and future extensibility (e.g., adding metadata, version info)
10
+ */
11
+ export declare const ContactJSONSchema: z.ZodObject<{
12
+ /** The complete contact data transfer object */
13
+ data: z.ZodObject<{
14
+ id: z.ZodString;
15
+ profileId: z.ZodString;
16
+ createdAt: z.ZodString;
17
+ updatedAt: z.ZodString;
18
+ } & {
19
+ displayName: z.ZodString;
20
+ accountNo: z.ZodString;
21
+ channel: z.ZodString;
22
+ type: z.ZodNativeEnum<typeof ContactType>;
23
+ }, "strip", z.ZodTypeAny, {
24
+ type: ContactType;
25
+ id: string;
26
+ displayName: string;
27
+ accountNo: string;
28
+ profileId: string;
29
+ createdAt: string;
30
+ updatedAt: string;
31
+ channel: string;
32
+ }, {
33
+ type: ContactType;
34
+ id: string;
35
+ displayName: string;
36
+ accountNo: string;
37
+ profileId: string;
38
+ createdAt: string;
39
+ updatedAt: string;
40
+ channel: string;
41
+ }>;
42
+ /** Version for future compatibility */
43
+ version: z.ZodDefault<z.ZodOptional<z.ZodString>>;
44
+ }, "strip", z.ZodTypeAny, {
45
+ version: string;
46
+ data: {
47
+ type: ContactType;
48
+ id: string;
49
+ displayName: string;
50
+ accountNo: string;
51
+ profileId: string;
52
+ createdAt: string;
53
+ updatedAt: string;
54
+ channel: string;
55
+ };
56
+ }, {
57
+ data: {
58
+ type: ContactType;
59
+ id: string;
60
+ displayName: string;
61
+ accountNo: string;
62
+ profileId: string;
63
+ createdAt: string;
64
+ updatedAt: string;
65
+ channel: string;
66
+ };
67
+ version?: string | undefined;
68
+ }>;
69
+ /**
70
+ * Infer the ContactJSON type from the schema
71
+ */
72
+ export type ContactJSON = z.infer<typeof ContactJSONSchema>;
73
+ /**
74
+ * Contact class that wraps the Zod schema and provides additional functionality.
75
+ * Represents a contact entity with validation and type-safe access to contact information.
76
+ *
77
+ * @class Contact
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * // Preferred: Use static factory methods
82
+ * const contact = Contact.from(contactData);
83
+ *
84
+ * // From JSON
85
+ * const jsonContact = Contact.fromJSON(jsonString);
86
+ *
87
+ * // Type-safe access
88
+ * if (contact) {
89
+ * console.log(contact.displayName);
90
+ * console.log(contact.info?.channelName);
91
+ * }
92
+ * ```
5
93
  */
6
94
  export declare class Contact {
7
95
  private readonly data;
8
96
  /**
9
- * Private constructor - use static methods to create instances
97
+ * Private constructor - use static factory methods to create instances.
98
+ *
99
+ * @deprecated Use {@link Contact.from} or other static factory methods instead
100
+ * @private
101
+ * @param {ContactDTO} data - Validated contact data
10
102
  */
11
103
  private constructor();
12
104
  /**
13
- * Unique identifier for the contact
105
+ * Unique identifier for the contact.
106
+ *
107
+ * @returns {string} The contact's unique ID
14
108
  */
15
109
  get id(): string;
16
110
  /**
17
- * Profile identifier associated with this contact
111
+ * Profile identifier associated with this contact.
112
+ *
113
+ * @returns {string} The profile ID
18
114
  */
19
115
  get profileId(): string;
20
116
  /**
21
- * Display name of the contact
117
+ * Display name of the contact.
118
+ *
119
+ * @returns {string} The contact's display name
22
120
  */
23
121
  get displayName(): string;
24
122
  /**
25
- * Type of contact (Bank or Mobile)
123
+ * Type of contact (Bank or Mobile).
124
+ *
125
+ * @returns {ContactType} The contact type
26
126
  */
27
127
  get type(): ContactType;
28
128
  /**
29
- * Creation timestamp of the contact
129
+ * Creation timestamp of the contact.
130
+ *
131
+ * @returns {Date} The creation date
30
132
  */
31
133
  get createdAt(): Date;
32
134
  /**
33
- * Update timestamp of the contact
135
+ * Update timestamp of the contact.
136
+ *
137
+ * @returns {Date} The last update date
34
138
  */
35
139
  get updatedAt(): Date;
36
140
  /**
37
- * Detailed contact information based on contact type
141
+ * Detailed contact information based on contact type.
38
142
  *
39
143
  * @returns {ContactInfo | undefined} Contact information object:
40
144
  * - MobileContactInfo for mobile money contacts
@@ -42,12 +146,12 @@ export declare class Contact {
42
146
  * - undefined if contact information cannot be constructed
43
147
  *
44
148
  * @remarks
45
- * For mobile contacts, constructs from phone number
46
- * For bank contacts, constructs from SWIFT code and account number
149
+ * For mobile contacts, constructs from phone number.
150
+ * For bank contacts, constructs from SWIFT code and account number.
47
151
  */
48
152
  get info(): ContactInfo | undefined;
49
153
  /**
50
- * Account number for the contact
154
+ * Account number for the contact.
51
155
  *
52
156
  * @returns {string} Account number:
53
157
  * - For valid contacts, returns formatted account number from ContactInfo
@@ -55,12 +159,14 @@ export declare class Contact {
55
159
  */
56
160
  get accNo(): string;
57
161
  /**
58
- * Account name for the contact
59
- * Always returns the display name
162
+ * Account name for the contact.
163
+ * Always returns the display name.
164
+ *
165
+ * @returns {string} The account name
60
166
  */
61
167
  get accName(): string;
62
168
  /**
63
- * Label for the account number field based on contact type
169
+ * Label for the account number field based on contact type.
64
170
  *
65
171
  * @returns {string} Appropriate label:
66
172
  * - "Phone Number" for mobile contacts
@@ -69,7 +175,7 @@ export declare class Contact {
69
175
  */
70
176
  get accNoLabel(): string;
71
177
  /**
72
- * Label for the channel field based on contact type
178
+ * Label for the channel field based on contact type.
73
179
  *
74
180
  * @returns {string} Appropriate label:
75
181
  * - "Channel" for mobile contacts
@@ -78,7 +184,7 @@ export declare class Contact {
78
184
  */
79
185
  get channelLabel(): string;
80
186
  /**
81
- * Label for the account name field based on contact type
187
+ * Label for the account name field based on contact type.
82
188
  *
83
189
  * @returns {string} Appropriate label:
84
190
  * - "Full Name" for mobile contacts
@@ -86,26 +192,88 @@ export declare class Contact {
86
192
  * - "Display Name" as fallback
87
193
  */
88
194
  get accNameLabel(): string;
195
+ /**
196
+ * Human-readable channel name for the contact.
197
+ *
198
+ * @returns {string} The channel name (e.g., "M-Pesa", "CRDB") or empty string
199
+ */
89
200
  get channelName(): string;
90
201
  /**
91
- * Creates a Contact instance from raw data
92
- * @throws {ZodError} if validation fails
202
+ * Creates a Contact instance from raw data.
203
+ * This is the preferred method for creating Contact instances.
204
+ *
205
+ * @static
206
+ * @param {ContactDTO} data - Raw contact data to validate and wrap
207
+ * @returns {Contact | undefined} Contact instance or undefined if validation fails
208
+ *
209
+ * @example
210
+ * ```typescript
211
+ * const contact = Contact.from({
212
+ * id: "123",
213
+ * profileId: "profile-456",
214
+ * displayName: "John Doe",
215
+ * type: "Mobile",
216
+ * accountNo: "+255712345678",
217
+ * channel: "VODACOM",
218
+ * createdAt: "2024-01-01T00:00:00Z",
219
+ * updatedAt: "2024-01-01T00:00:00Z"
220
+ * });
221
+ * ```
222
+ */
223
+ static from(data: ContactDTO): Contact | undefined;
224
+ /**
225
+ * Creates a Contact instance from raw data, throwing on validation failure.
226
+ *
227
+ * @static
228
+ * @param {ContactDTO} data - Raw contact data to validate and wrap
229
+ * @returns {Contact} Contact instance
230
+ * @throws {ZodError} If validation fails
231
+ *
232
+ * @example
233
+ * ```typescript
234
+ * try {
235
+ * const contact = Contact.create(contactData);
236
+ * } catch (error) {
237
+ * console.error("Validation failed:", error);
238
+ * }
239
+ * ```
93
240
  */
94
241
  static create(data: ContactDTO): Contact;
95
242
  /**
96
- * Creates multiple Contact instances from an array of raw data
97
- * @throws {ZodError} if validation fails for any item
243
+ * Creates multiple Contact instances from an array of raw data.
244
+ *
245
+ * @static
246
+ * @param {ContactDTO[]} dataArray - Array of contact data
247
+ * @returns {Contact[]} Array of Contact instances
248
+ * @throws {ZodError} If validation fails for any item
249
+ *
250
+ * @example
251
+ * ```typescript
252
+ * const contacts = Contact.createMany([contactData1, contactData2]);
253
+ * ```
98
254
  */
99
255
  static createMany(dataArray: ContactDTO[]): Contact[];
100
256
  /**
101
- * Creates a Contact instance from raw data without throwing
257
+ * Creates a Contact instance from raw data without throwing.
258
+ *
259
+ * @static
260
+ * @param {ContactDTO} data - Raw contact data
102
261
  * @returns {Contact | null} Contact instance or null if validation fails
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * const contact = Contact.createSafe(contactData);
266
+ * if (contact) {
267
+ * console.log("Contact created successfully");
268
+ * }
269
+ * ```
103
270
  */
104
271
  static createSafe(data: ContactDTO): Contact | null;
105
272
  /**
106
273
  * Checks if an unknown value contains valid data to construct a Contact instance.
107
274
  * This is useful when validating raw data structures before instantiation.
108
275
  *
276
+ * @static
109
277
  * @param {unknown} obj - The value containing potential contact data
110
278
  * @returns {obj is Contact} Type predicate indicating if a Contact can be constructed
111
279
  *
@@ -114,13 +282,10 @@ export declare class Contact {
114
282
  * const rawData = await fetchFromAPI();
115
283
  * if (Contact.canConstruct(rawData)) {
116
284
  * const contact = Contact.create(rawData);
117
- * // TypeScript knows contact is valid here
118
285
  * console.log(contact.displayName);
119
286
  * }
120
287
  * ```
121
288
  *
122
- * @throws {never} This method never throws errors
123
- *
124
289
  * @remarks
125
290
  * This method performs strict validation against the {@link ContactDTO} schema.
126
291
  */
@@ -129,6 +294,7 @@ export declare class Contact {
129
294
  * Validates if an unknown value is a Contact instance.
130
295
  * This is a runtime type guard that ensures proper object structure and data validity.
131
296
  *
297
+ * @static
132
298
  * @param {unknown} obj - The value to validate
133
299
  * @returns {obj is Contact} Type predicate indicating if the value is a valid Contact
134
300
  *
@@ -136,18 +302,15 @@ export declare class Contact {
136
302
  * ```typescript
137
303
  * const maybeContact = getContactFromCache();
138
304
  * if (Contact.is(maybeContact)) {
139
- * // TypeScript knows maybeContact is a Contact here
140
- * console.log(maybeContact.displayName);
305
+ * console.log(maybeContact.displayName); // Type-safe
141
306
  * }
142
307
  * ```
143
308
  *
144
- * @throws {never} This method never throws errors
145
- *
146
309
  * @remarks
147
310
  * This method performs a complete structural validation:
148
311
  * 1. Checks if the value is an object
149
312
  * 2. Verifies presence of internal data property
150
- * 3. Validates the data against ContactData schema
313
+ * 3. Validates the data against ContactDTO schema
151
314
  * 4. Ensures the object is a proper Contact instance
152
315
  *
153
316
  * Use this method when:
@@ -158,7 +321,121 @@ export declare class Contact {
158
321
  */
159
322
  static is(obj: unknown): obj is Contact;
160
323
  /**
161
- * Converts Payout instance to a plain object
324
+ * Serializes the Contact instance to a JSON-compatible object
325
+ *
326
+ * This method creates a structured representation suitable for storage or transmission.
327
+ * The serialized format includes all contact data and a version identifier for
328
+ * future compatibility.
329
+ *
330
+ * @returns {ContactJSON} JSON-compatible object representation
331
+ *
332
+ * @example
333
+ * ```typescript
334
+ * const contact = Contact.from(contactData);
335
+ * const json = contact.toJSON();
336
+ * // { data: { id: "123", displayName: "John", ... }, version: "1.0" }
337
+ *
338
+ * // Store in localStorage
339
+ * localStorage.setItem('contact', JSON.stringify(json));
340
+ * ```
341
+ */
342
+ toJSON(): ContactJSON;
343
+ /**
344
+ * Serializes the Contact instance to a JSON string
345
+ *
346
+ * Convenience method that combines toJSON() with JSON.stringify()
347
+ *
348
+ * @returns {string} JSON string representation
349
+ *
350
+ * @example
351
+ * ```typescript
352
+ * const contact = Contact.from(contactData);
353
+ * const jsonString = contact.toJSONString();
354
+ * localStorage.setItem('contact', jsonString);
355
+ * ```
356
+ */
357
+ toJSONString(): string;
358
+ /**
359
+ * Creates a Contact instance from a JSON-compatible object or string
360
+ *
361
+ * This static method reconstructs a Contact instance from data that was
362
+ * previously serialized using toJSON(). It performs comprehensive validation
363
+ * to ensure data integrity.
364
+ *
365
+ * **Validation Process:**
366
+ * 1. Parses JSON string if provided
367
+ * 2. Validates structure using Zod schema
368
+ * 3. Extracts and validates ContactDTO data
369
+ * 4. Constructs Contact instance with validated data
370
+ *
371
+ * @static
372
+ * @param {ContactJSON | string} json - JSON object or string containing contact data
373
+ * @returns {Contact | undefined} New Contact instance if successful, undefined if parsing/validation fails
374
+ *
375
+ * @example
376
+ * ```typescript
377
+ * // From JSON object
378
+ * const json = { data: { id: "123", displayName: "John", ... }, version: "1.0" };
379
+ * const contact = Contact.fromJSON(json);
380
+ *
381
+ * // From JSON string
382
+ * const jsonString = localStorage.getItem('contact');
383
+ * const contact = Contact.fromJSON(jsonString);
384
+ *
385
+ * // With error handling
386
+ * const contact = Contact.fromJSON(jsonString);
387
+ * if (contact) {
388
+ * console.log("Contact restored:", contact.displayName);
389
+ * } else {
390
+ * console.error("Failed to restore contact from JSON");
391
+ * }
392
+ * ```
393
+ */
394
+ static fromJSON(json: ContactJSON | string): Contact | undefined;
395
+ /**
396
+ * Creates a Contact instance from a JSON string
397
+ *
398
+ * Convenience method that delegates to fromJSON()
399
+ *
400
+ * @static
401
+ * @param {string} jsonString - JSON string containing contact data
402
+ * @returns {Contact | undefined} New Contact instance if successful, undefined if parsing fails
403
+ *
404
+ * @example
405
+ * ```typescript
406
+ * const jsonString = '{"data":{"id":"123","displayName":"John",...},"version":"1.0"}';
407
+ * const contact = Contact.fromJSONString(jsonString);
408
+ * ```
409
+ */
410
+ static fromJSONString(jsonString: string): Contact | undefined;
411
+ /**
412
+ * Type guard to check if an object is a valid ContactJSON using Zod validation
413
+ *
414
+ * This method validates the structure of a JSON object to ensure it contains
415
+ * all required fields and meets the ContactJSON schema requirements.
416
+ *
417
+ * @static
418
+ * @param {unknown} obj - The object to validate
419
+ * @returns {obj is ContactJSON} Type predicate indicating if the object is valid ContactJSON
420
+ *
421
+ * @example
422
+ * ```typescript
423
+ * const data = JSON.parse(jsonString);
424
+ *
425
+ * if (Contact.isContactJSON(data)) {
426
+ * // TypeScript now knows data is ContactJSON
427
+ * const contact = Contact.fromJSON(data);
428
+ * } else {
429
+ * console.error("Invalid ContactJSON structure");
430
+ * }
431
+ * ```
432
+ *
433
+ * @remarks
434
+ * Use this method when:
435
+ * - Validating JSON data before deserialization
436
+ * - Implementing type guards in conditional logic
437
+ * - Checking API responses before processing
438
+ * - Validating cached data integrity
162
439
  */
163
- toJSON(): ContactDTO;
440
+ static isContactJSON(obj: unknown): obj is ContactJSON;
164
441
  }
@@ -1,10 +1,43 @@
1
1
  import { LogInDTO } from "./login.dtos.js";
2
- export type LogInJson = {
2
+ import { z } from "zod";
3
+ /**
4
+ * Zod schema for LogIn JSON serialization
5
+ * This schema validates the JSON representation of a LogIn instance
6
+ */
7
+ export declare const LogInJSONSchema: z.ZodObject<{
8
+ id: z.ZodString;
9
+ profileId: z.ZodString;
10
+ name: z.ZodString;
11
+ identity: z.ZodString;
12
+ type: z.ZodString;
13
+ roleId: z.ZodString;
14
+ isActive: z.ZodBoolean;
15
+ isArchived: z.ZodBoolean;
16
+ resetPassword: z.ZodBoolean;
17
+ createdAt: z.ZodString;
18
+ updatedAt: z.ZodString;
19
+ access: z.ZodArray<z.ZodString, "many">;
20
+ version: z.ZodDefault<z.ZodOptional<z.ZodString>>;
21
+ }, "strip", z.ZodTypeAny, {
22
+ type: string;
23
+ name: string;
3
24
  id: string;
25
+ version: string;
4
26
  profileId: string;
5
- name: string;
6
27
  identity: string;
28
+ roleId: string;
29
+ isActive: boolean;
30
+ isArchived: boolean;
31
+ resetPassword: boolean;
32
+ createdAt: string;
33
+ updatedAt: string;
34
+ access: string[];
35
+ }, {
7
36
  type: string;
37
+ name: string;
38
+ id: string;
39
+ profileId: string;
40
+ identity: string;
8
41
  roleId: string;
9
42
  isActive: boolean;
10
43
  isArchived: boolean;
@@ -12,7 +45,12 @@ export type LogInJson = {
12
45
  createdAt: string;
13
46
  updatedAt: string;
14
47
  access: string[];
15
- };
48
+ version?: string | undefined;
49
+ }>;
50
+ /**
51
+ * Infer the LogInJSON type from the schema
52
+ */
53
+ export type LogInJSON = z.infer<typeof LogInJSONSchema>;
16
54
  /**
17
55
  * Represents the authenticated user's identity and account information.
18
56
  *
@@ -63,18 +101,6 @@ export declare class LogIn {
63
101
  * ```
64
102
  */
65
103
  static from(dto: LogInDTO): LogIn;
66
- /**
67
- * Creates a LogIn instance from a JSON string.
68
- *
69
- * @param json - JSON string containing login data
70
- * @returns A new LogIn instance, or undefined if parsing failed
71
- *
72
- * @example
73
- * ```typescript
74
- * const logIn = LogIn.fromJson('{"id":"abc","name":"John",...}');
75
- * ```
76
- */
77
- static fromJson(json: string): LogIn | undefined;
78
104
  /**
79
105
  * Gets the user's unique identifier.
80
106
  */
@@ -124,15 +150,19 @@ export declare class LogIn {
124
150
  */
125
151
  get access(): ReadonlyArray<string>;
126
152
  /**
127
- * Converts the LogIn instance to a plain object.
128
- *
129
- * @returns A plain object representation
153
+ * Serializes the LogIn instance to a JSON-compatible object
154
+ */
155
+ toJSON(): LogInJSON;
156
+ /**
157
+ * Serializes the LogIn instance to a JSON string
130
158
  */
131
- toObject(): LogInJson;
159
+ toJSONString(): string;
132
160
  /**
133
- * Converts the LogIn instance to a JSON string.
134
- *
135
- * @returns A JSON string representation
161
+ * Creates a LogIn instance from a JSON-compatible object or string
162
+ */
163
+ static fromJSON(json: LogInJSON | string): LogIn | undefined;
164
+ /**
165
+ * Type guard using Zod schema validation
136
166
  */
137
- toJson(): string;
167
+ static isLogInJSON(obj: unknown): obj is LogInJSON;
138
168
  }