@temboplus/frontend-core 0.2.20-beta.0 → 0.2.20-beta.1

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.
@@ -4,6 +4,19 @@ import { CurrencyCode } from "../currency/index.js";
4
4
  * Supports both positive and negative amounts with optional comma separators and decimals
5
5
  */
6
6
  declare const AMOUNT_REGEX: RegExp;
7
+ /**
8
+ * JSON representation interface for Amount serialization
9
+ */
10
+ export interface AmountJSON {
11
+ /** The numeric value of the amount (can be positive, negative, or zero) */
12
+ value: number;
13
+ /** The formatted text representation of the amount */
14
+ text: string;
15
+ /** The ISO currency code */
16
+ currencyCode: string;
17
+ /** Version for future compatibility */
18
+ version?: string;
19
+ }
7
20
  /**
8
21
  * Represents a monetary amount with currency information
9
22
  *
@@ -562,18 +575,21 @@ declare class Amount {
562
575
  * ```
563
576
  */
564
577
  getWholeValue(): number;
578
+ /**
579
+ * Update these methods in the Amount class in amount.ts
580
+ */
565
581
  /**
566
582
  * Serializes the Amount instance to a JSON-compatible object
567
583
  *
568
584
  * This method creates a plain object representation that can be safely
569
585
  * serialized to JSON without losing any essential data.
570
586
  *
571
- * @returns {AmountJson} A plain object containing all necessary Amount data
587
+ * @returns {AmountJSON} A plain object containing all necessary Amount data
572
588
  *
573
589
  * @example
574
590
  * ```typescript
575
591
  * const amount = Amount.from(-1234.56, 'USD');
576
- * const json = amount.toJson();
592
+ * const json = amount.toJSON();
577
593
  * console.log(json);
578
594
  * // {
579
595
  * // value: -1234.56,
@@ -586,15 +602,36 @@ declare class Amount {
586
602
  * const jsonString = JSON.stringify(json);
587
603
  * ```
588
604
  */
589
- toJson(): AmountJson;
605
+ toJSON(): AmountJSON;
606
+ /**
607
+ * Serializes the Amount instance to a JSON string
608
+ *
609
+ * @returns {string} JSON string representation of the amount
610
+ *
611
+ * @example
612
+ * ```typescript
613
+ * const amount = Amount.from(1000, 'TZS');
614
+ * const jsonString = amount.toJSONString();
615
+ * console.log(jsonString);
616
+ * // '{"value":1000,"text":"1,000","currencyCode":"TZS","version":"1.0"}'
617
+ *
618
+ * // Store or transmit
619
+ * localStorage.setItem('paymentAmount', jsonString);
620
+ *
621
+ * // Later, retrieve and reconstruct
622
+ * const stored = localStorage.getItem('paymentAmount');
623
+ * const reconstructed = Amount.fromJSONString(stored!);
624
+ * ```
625
+ */
626
+ toJSONString(): string;
590
627
  /**
591
- * Creates an Amount instance from a JSON-compatible object
628
+ * Creates an Amount instance from a JSON-compatible object or string
592
629
  *
593
630
  * This static method reconstructs an Amount instance from data that was
594
- * previously serialized using toJson(). It validates the input data and
631
+ * previously serialized using toJSON(). It validates the input data and
595
632
  * ensures the resulting Amount is valid.
596
633
  *
597
- * @param {AmountJson | string} json - Either an AmountJson object or a JSON string
634
+ * @param {AmountJSON | string} json - Either an AmountJSON object or a JSON string
598
635
  * @returns {Amount | undefined} An Amount instance if valid, undefined otherwise
599
636
  *
600
637
  * @example
@@ -606,33 +643,49 @@ declare class Amount {
606
643
  * currencyCode: "USD",
607
644
  * version: "1.0"
608
645
  * };
609
- * const amount = Amount.fromJson(jsonData);
646
+ * const amount = Amount.fromJSON(jsonData);
610
647
  *
611
648
  * // From JSON string
612
649
  * const jsonString = '{"value":-1234.56,"text":"1,234.56","currencyCode":"USD","version":"1.0"}';
613
- * const amount2 = Amount.fromJson(jsonString);
650
+ * const amount2 = Amount.fromJSON(jsonString);
614
651
  *
615
652
  * // Both create equivalent Amount instances
616
653
  * console.log(amount?.label); // "-$1,234.56"
617
654
  * console.log(amount2?.label); // "-$1,234.56"
618
655
  * ```
619
656
  */
620
- static fromJson(json: AmountJson | string): Amount | undefined;
657
+ static fromJSON(json: AmountJSON | string): Amount | undefined;
658
+ /**
659
+ * Creates an Amount instance from a JSON string
660
+ *
661
+ * Convenience method that calls fromJSON() with a string parameter.
662
+ *
663
+ * @param {string} jsonString - JSON string containing amount data
664
+ * @returns {Amount | undefined} An Amount instance if valid, undefined otherwise
665
+ *
666
+ * @example
667
+ * ```typescript
668
+ * const jsonString = '{"value":1000,"text":"1,000","currencyCode":"TZS","version":"1.0"}';
669
+ * const amount = Amount.fromJSONString(jsonString);
670
+ * console.log(amount?.label); // "TSh 1,000"
671
+ * ```
672
+ */
673
+ static fromJSONString(jsonString: string): Amount | undefined;
621
674
  /**
622
- * Type guard to check if an object is a valid AmountJson
675
+ * Type guard to check if an object is a valid AmountJSON
623
676
  *
624
677
  * @param obj - The object to validate
625
- * @returns True if the object conforms to AmountJson structure
678
+ * @returns True if the object conforms to AmountJSON structure
626
679
  *
627
680
  * @example
628
681
  * ```typescript
629
682
  * const data = { value: 100, text: "100.00", currencyCode: "USD" };
630
- * if (Amount.isAmountJson(data)) {
631
- * const amount = Amount.fromJson(data);
683
+ * if (Amount.isAmountJSON(data)) {
684
+ * const amount = Amount.fromJSON(data);
632
685
  * }
633
686
  * ```
634
687
  */
635
- static isAmountJson(obj: unknown): obj is AmountJson;
688
+ static isAmountJSON(obj: unknown): obj is AmountJSON;
636
689
  /**
637
690
  * Returns the sum of multiple amounts
638
691
  *
@@ -1,5 +1,16 @@
1
1
  import { ISO2CountryCode } from "../country/country.types.js";
2
2
  import type { BankSwiftCode, KEBankSwiftCode, TZBankSwiftCode } from "./bank.types.js";
3
+ /**
4
+ * JSON representation interface for Bank serialization
5
+ */
6
+ export interface BankJSON {
7
+ /** The SWIFT/BIC code for the bank's head office */
8
+ swiftCode: string;
9
+ /** The ISO 3166-1 alpha-2 country code */
10
+ countryCode: string;
11
+ /** Version for future compatibility */
12
+ version?: string;
13
+ }
3
14
  export declare class Bank {
4
15
  /**
5
16
  * The full registered name of the bank.
@@ -32,30 +43,6 @@ export declare class Bank {
32
43
  */
33
44
  toString(): string;
34
45
  static from(swiftCode: BankSwiftCode, countryCode: ISO2CountryCode): Bank;
35
- /**
36
- * Creates a Bank instance from a JSON string.
37
- *
38
- * The JSON should contain swiftCode and countryCode fields.
39
- * The bank data is then looked up from the bank service to ensure validity.
40
- *
41
- * @static
42
- * @param {string} jsonString - JSON string containing bank data
43
- * @returns {Bank | undefined} A new Bank instance if successful, undefined if parsing fails
44
- *
45
- * @example
46
- * ```typescript
47
- * const jsonString = '{"swiftCode":"CORUTZTZ","countryCode":"TZ"}';
48
- * const bank = Bank.fromJSON(jsonString);
49
- * if (bank) {
50
- * console.log(bank.fullName); // "CRDB BANK PLC"
51
- * }
52
- *
53
- * // Also works with full bank data
54
- * const fullJson = '{"fullName":"CRDB BANK PLC","shortName":"CRDB","swiftCode":"CORUTZTZ","countryCode":"TZ"}';
55
- * const bank2 = Bank.fromJSON(fullJson);
56
- * ```
57
- */
58
- static fromJSON(jsonString: string): Bank | undefined;
59
46
  /**
60
47
  * Converts the Bank instance to a plain JavaScript object.
61
48
  * Suitable for serialization or data transfer.
@@ -85,27 +72,6 @@ export declare class Bank {
85
72
  swiftCode: TZBankSwiftCode | KEBankSwiftCode;
86
73
  countryCode: ISO2CountryCode;
87
74
  };
88
- /**
89
- * Serializes the Bank instance to a JSON string.
90
- *
91
- * @returns {string} JSON string representation of the bank
92
- *
93
- * @example
94
- * ```typescript
95
- * const bank = Bank.from("CORUTZTZ", "TZ");
96
- * const jsonString = bank.toJSON();
97
- * console.log(jsonString);
98
- * // '{"fullName":"CRDB BANK PLC","shortName":"CRDB","swiftCode":"CORUTZTZ","countryCode":"TZ"}'
99
- *
100
- * // Store or transmit
101
- * localStorage.setItem('selectedBank', jsonString);
102
- *
103
- * // Later, retrieve and reconstruct
104
- * const stored = localStorage.getItem('selectedBank');
105
- * const reconstructed = Bank.fromJSON(stored);
106
- * ```
107
- */
108
- toJSON(): string;
109
75
  /**
110
76
  * Static method to determine if an unknown object is a valid Bank object.
111
77
  *
@@ -116,6 +82,26 @@ export declare class Bank {
116
82
  * @returns {obj is Bank} - Returns true if the object is a valid Bank, false otherwise.
117
83
  */
118
84
  static is(obj: unknown): obj is Bank;
85
+ /**
86
+ * Serializes the Bank instance to a JSON-compatible object
87
+ */
88
+ toJSON(): BankJSON;
89
+ /**
90
+ * Serializes the Bank instance to a JSON string
91
+ */
92
+ toJSONString(): string;
93
+ /**
94
+ * Creates a Bank instance from a JSON-compatible object or string
95
+ */
96
+ static fromJSON(json: BankJSON | string): Bank | undefined;
97
+ /**
98
+ * Creates a Bank instance from a JSON string
99
+ */
100
+ static fromJSONString(jsonString: string): Bank | undefined;
101
+ /**
102
+ * Type guard to check if an object is a valid BankJSON
103
+ */
104
+ static isBankJSON(obj: unknown): obj is BankJSON;
119
105
  }
120
106
  /**
121
107
  * Service for managing bank data, validation, and instance creation
@@ -27,6 +27,15 @@
27
27
  import { Currency } from "@models/currency/currency.js";
28
28
  import { CurrencyCode } from "@models/currency/currency.types.js";
29
29
  import { ISO2CountryCode, ISO3CountryCode, CountryCode } from "./country.types.js";
30
+ /**
31
+ * JSON representation interface for Country serialization
32
+ */
33
+ export interface CountryJSON {
34
+ /** The ISO 3166-1 alpha-2 country code */
35
+ code: string;
36
+ /** Version for future compatibility */
37
+ version?: string;
38
+ }
30
39
  /**
31
40
  * Enum for continents
32
41
  */
@@ -716,6 +725,26 @@ export declare class Country {
716
725
  * @returns Type predicate indicating if the value is a valid Country
717
726
  */
718
727
  static is(obj: unknown): obj is Country;
728
+ /**
729
+ * Serializes the Country instance to a JSON-compatible object
730
+ */
731
+ toJSON(): CountryJSON;
732
+ /**
733
+ * Serializes the Country instance to a JSON string
734
+ */
735
+ toJSONString(): string;
736
+ /**
737
+ * Creates a Country instance from a JSON-compatible object or string
738
+ */
739
+ static fromJSON(json: CountryJSON | string): Country | undefined;
740
+ /**
741
+ * Creates a Country instance from a JSON string
742
+ */
743
+ static fromJSONString(jsonString: string): Country | undefined;
744
+ /**
745
+ * Type guard to check if an object is a valid CountryJSON
746
+ */
747
+ static isCountryJSON(obj: unknown): obj is CountryJSON;
719
748
  }
720
749
  /**
721
750
  * Service for managing country data.
@@ -27,6 +27,15 @@
27
27
  * dependencies are best managed in a unified module.
28
28
  */
29
29
  import type { CurrencyCode } from "./currency.types.js";
30
+ /**
31
+ * JSON representation interface for Currency serialization
32
+ */
33
+ export interface CurrencyJSON {
34
+ /** The ISO 4217 currency code */
35
+ code: string;
36
+ /** Version for future compatibility */
37
+ version?: string;
38
+ }
30
39
  /**
31
40
  * Represents a currency with essential details.
32
41
  * @class Currency
@@ -367,6 +376,26 @@ export declare class Currency {
367
376
  * @returns Type predicate indicating if the value is a valid Currency
368
377
  */
369
378
  static is(obj: unknown): obj is Currency;
379
+ /**
380
+ * Serializes the Currency instance to a JSON-compatible object
381
+ */
382
+ toJSON(): CurrencyJSON;
383
+ /**
384
+ * Serializes the Currency instance to a JSON string
385
+ */
386
+ toJSONString(): string;
387
+ /**
388
+ * Creates a Currency instance from a JSON-compatible object or string
389
+ */
390
+ static fromJSON(json: CurrencyJSON | string): Currency | undefined;
391
+ /**
392
+ * Creates a Currency instance from a JSON string
393
+ */
394
+ static fromJSONString(jsonString: string): Currency | undefined;
395
+ /**
396
+ * Type guard to check if an object is a valid CurrencyJSON
397
+ */
398
+ static isCurrencyJSON(obj: unknown): obj is CurrencyJSON;
370
399
  }
371
400
  /**
372
401
  * Service for managing currency data.
@@ -3,6 +3,15 @@ import type { PhoneNumber as LibPhoneNumberInstance } from "libphonenumber-js";
3
3
  import { MNOInfo } from "./mno/mno.types.js";
4
4
  import { PhoneNumberService, ParsedPhoneNumber } from "./phone-number.service.js";
5
5
  import { PhoneNumberContract, PhoneNumberFormat, PhoneNumberType, PhoneNumberParseOptions } from "./phone-number.types.js";
6
+ /**
7
+ * JSON representation interface for PhoneNumber serialization
8
+ */
9
+ export interface PhoneNumberJSON {
10
+ /** The phone number in E.164 format */
11
+ e164Format: string;
12
+ /** Version for future compatibility */
13
+ version?: string;
14
+ }
6
15
  /**
7
16
  * Represents a generic international phone number, validated using libphonenumber-js
8
17
  * via the PhoneNumberService. Implements the common PhoneNumberContract interface.
@@ -75,4 +84,117 @@ export declare class PhoneNumber implements PhoneNumberContract {
75
84
  * Robust type guard checking structure and validatability.
76
85
  */
77
86
  static is(obj: unknown): obj is PhoneNumberContract;
87
+ /**
88
+ * Serializes the PhoneNumber instance to a JSON-compatible object
89
+ *
90
+ * This method creates a minimal object representation. Only the E.164 format
91
+ * is stored as everything else (country code, compact number, number type,
92
+ * and MNO info) can be reconstructed from it.
93
+ *
94
+ * @returns {PhoneNumberJSON} A plain object containing minimal PhoneNumber data
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * const phone = PhoneNumber.from('+255712345678');
99
+ * const json = phone.toJSON();
100
+ * console.log(json);
101
+ * // {
102
+ * // e164Format: "+255712345678",
103
+ * // version: "1.0"
104
+ * // }
105
+ * ```
106
+ */
107
+ toJSON(): PhoneNumberJSON;
108
+ /**
109
+ * Serializes the PhoneNumber instance to a JSON string
110
+ *
111
+ * @returns {string} JSON string representation of the phone number
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const phone = PhoneNumber.from('+254712345678');
116
+ * const jsonString = phone.toJSONString();
117
+ * console.log(jsonString);
118
+ * // '{"e164Format":"+254712345678","version":"1.0"}'
119
+ *
120
+ * // Store or transmit
121
+ * localStorage.setItem('userPhone', jsonString);
122
+ *
123
+ * // Later, retrieve and reconstruct
124
+ * const stored = localStorage.getItem('userPhone');
125
+ * const reconstructed = PhoneNumber.fromJSONString(stored!);
126
+ * ```
127
+ */
128
+ toJSONString(): string;
129
+ /**
130
+ * Creates a PhoneNumber instance from a JSON-compatible object or string
131
+ *
132
+ * This static method reconstructs a PhoneNumber instance from data that was
133
+ * previously serialized using toJSON(). It uses PhoneNumberFactory to get
134
+ * the appropriate instance (generic or country-specific like TZMobileNumber).
135
+ * All properties including country code, compact number, MNO info, and number
136
+ * type are automatically reconstructed from the E.164 format.
137
+ *
138
+ * @param {PhoneNumberJSON | string} json - Either a PhoneNumberJSON object or a JSON string
139
+ * @returns {PhoneNumber | undefined} A PhoneNumber instance if valid, undefined otherwise
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * // From object
144
+ * const jsonData = {
145
+ * e164Format: "+255712345678",
146
+ * version: "1.0"
147
+ * };
148
+ * const phone = PhoneNumber.fromJSON(jsonData);
149
+ *
150
+ * // From JSON string
151
+ * const jsonString = '{"e164Format":"+254712345678","version":"1.0"}';
152
+ * const phone2 = PhoneNumber.fromJSON(jsonString);
153
+ *
154
+ * // Both create appropriate PhoneNumber instances with all properties reconstructed
155
+ * console.log(phone?.countryCode); // "TZ"
156
+ * console.log(phone?.compactNumber); // "712345678"
157
+ * console.log(phone?.getOperatorInfo()?.displayName); // "Vodacom" (reconstructed!)
158
+ * console.log(phone?.getNumberType()); // "MOBILE"
159
+ *
160
+ * console.log(phone2?.countryCode); // "KE"
161
+ * console.log(phone2?.compactNumber); // "712345678"
162
+ * ```
163
+ */
164
+ static fromJSON(json: PhoneNumberJSON | string): PhoneNumber | undefined;
165
+ /**
166
+ * Creates a PhoneNumber instance from a JSON string
167
+ *
168
+ * Convenience method that calls fromJSON() with a string parameter.
169
+ *
170
+ * @param {string} jsonString - JSON string containing phone number data
171
+ * @returns {PhoneNumber | undefined} A PhoneNumber instance if valid, undefined otherwise
172
+ *
173
+ * @example
174
+ * ```typescript
175
+ * const jsonString = '{"e164Format":"+255712345678","version":"1.0"}';
176
+ * const phone = PhoneNumber.fromJSONString(jsonString);
177
+ * console.log(phone?.label); // "+255 712 345 678"
178
+ * console.log(phone?.getOperatorInfo()?.mobileMoneyService); // "M-Pesa" (reconstructed!)
179
+ * ```
180
+ */
181
+ static fromJSONString(jsonString: string): PhoneNumber | undefined;
182
+ /**
183
+ * Type guard to check if an object is a valid PhoneNumberJSON
184
+ *
185
+ * @param obj - The object to validate
186
+ * @returns True if the object conforms to PhoneNumberJSON structure
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * const data = {
191
+ * e164Format: "+255712345678",
192
+ * version: "1.0"
193
+ * };
194
+ * if (PhoneNumber.isPhoneNumberJSON(data)) {
195
+ * const phone = PhoneNumber.fromJSON(data);
196
+ * }
197
+ * ```
198
+ */
199
+ static isPhoneNumberJSON(obj: unknown): obj is PhoneNumberJSON;
78
200
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temboplus/frontend-core",
3
- "version": "0.2.20-beta.0",
3
+ "version": "0.2.20-beta.1",
4
4
  "description": "A JavaScript/TypeScript package providing common utilities and logic shared across front-end TemboPlus projects.",
5
5
  "author": "Okello Gerald",
6
6
  "license": "MIT",