@lightsparkdev/core 1.4.2 → 1.4.4
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/CHANGELOG.md +14 -0
- package/README.md +0 -2
- package/dist/{chunk-CP4LQTTD.js → chunk-ADHQHZNM.js} +50 -18
- package/dist/chunk-VY7NND44.js +713 -0
- package/dist/{index-BCTAeaWD.d.cts → index-CFQtMxrx.d.cts} +18 -2
- package/dist/{index-BCTAeaWD.d.ts → index-CFQtMxrx.d.ts} +18 -2
- package/dist/index.cjs +2586 -2532
- package/dist/index.d.cts +8 -171
- package/dist/index.d.ts +8 -171
- package/dist/index.js +34 -674
- package/dist/react-native/index.cjs +3111 -0
- package/dist/react-native/index.d.cts +175 -0
- package/dist/react-native/index.d.ts +175 -0
- package/dist/react-native/index.js +154 -0
- package/dist/utils/index.cjs +45 -10
- package/dist/utils/index.d.cts +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +7 -1
- package/package.json +15 -1
- package/src/Logger.ts +2 -1
- package/src/crypto/SigningKey.ts +4 -2
- package/src/index.ts +3 -11
- package/src/react-native/index.ts +2 -0
- package/src/requester/DefaultRequester.ts +46 -0
- package/src/requester/Requester.ts +21 -37
- package/src/requester/tests/DefaultRequester.test.ts +129 -0
- package/src/requester/tests/Requester.test.ts +23 -32
- package/src/shared.ts +10 -0
- package/src/utils/currency.ts +42 -1
- package/src/utils/environment.ts +10 -0
package/dist/utils/index.cjs
CHANGED
|
@@ -45,12 +45,14 @@ __export(utils_exports, {
|
|
|
45
45
|
ensureArray: () => ensureArray,
|
|
46
46
|
errorToJSON: () => errorToJSON,
|
|
47
47
|
formatCurrencyStr: () => formatCurrencyStr,
|
|
48
|
+
formatInviteAmount: () => formatInviteAmount,
|
|
48
49
|
getCurrencyAmount: () => getCurrencyAmount,
|
|
49
50
|
getCurrentLocale: () => getCurrentLocale,
|
|
50
51
|
getErrorMsg: () => getErrorMsg,
|
|
51
52
|
getLocalStorageBoolean: () => getLocalStorageBoolean,
|
|
52
53
|
getLocalStorageConfigItem: () => getLocalStorageConfigItem,
|
|
53
54
|
hexToBytes: () => hexToBytes,
|
|
55
|
+
isBare: () => isBare,
|
|
54
56
|
isBrowser: () => isBrowser,
|
|
55
57
|
isCurrencyAmountInputObj: () => isCurrencyAmountInputObj,
|
|
56
58
|
isCurrencyAmountObj: () => isCurrencyAmountObj,
|
|
@@ -63,6 +65,7 @@ __export(utils_exports, {
|
|
|
63
65
|
isNode: () => isNode,
|
|
64
66
|
isNumber: () => isNumber,
|
|
65
67
|
isObject: () => isObject,
|
|
68
|
+
isReactNative: () => isReactNative,
|
|
66
69
|
isRecord: () => isRecord,
|
|
67
70
|
isSDKCurrencyAmount: () => isSDKCurrencyAmount,
|
|
68
71
|
isTest: () => isTest,
|
|
@@ -138,6 +141,8 @@ var b64encode = (data) => {
|
|
|
138
141
|
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
139
142
|
var isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
140
143
|
var isTest = isNode && process.env.NODE_ENV === "test";
|
|
144
|
+
var isBare = typeof Bare !== "undefined";
|
|
145
|
+
var isReactNative = typeof navigator !== "undefined" && navigator.product === "ReactNative";
|
|
141
146
|
|
|
142
147
|
// src/utils/hex.ts
|
|
143
148
|
var bytesToHex = (bytes) => {
|
|
@@ -498,6 +503,7 @@ var CurrencyUnit = {
|
|
|
498
503
|
PHP: "PHP",
|
|
499
504
|
EUR: "EUR",
|
|
500
505
|
GBP: "GBP",
|
|
506
|
+
INR: "INR",
|
|
501
507
|
Bitcoin: "BITCOIN",
|
|
502
508
|
Microbitcoin: "MICROBITCOIN",
|
|
503
509
|
Millibitcoin: "MILLIBITCOIN",
|
|
@@ -507,7 +513,8 @@ var CurrencyUnit = {
|
|
|
507
513
|
Usd: "USD",
|
|
508
514
|
Mxn: "MXN",
|
|
509
515
|
Php: "PHP",
|
|
510
|
-
Gbp: "GBP"
|
|
516
|
+
Gbp: "GBP",
|
|
517
|
+
Inr: "INR"
|
|
511
518
|
};
|
|
512
519
|
var standardUnitConversionObj = {
|
|
513
520
|
[CurrencyUnit.BITCOIN]: (v, unitsPerBtc = 1) => v / unitsPerBtc,
|
|
@@ -521,7 +528,8 @@ var standardUnitConversionObj = {
|
|
|
521
528
|
[CurrencyUnit.MXN]: (v) => v,
|
|
522
529
|
[CurrencyUnit.PHP]: (v) => v,
|
|
523
530
|
[CurrencyUnit.EUR]: (v) => v,
|
|
524
|
-
[CurrencyUnit.GBP]: (v) => v
|
|
531
|
+
[CurrencyUnit.GBP]: (v) => v,
|
|
532
|
+
[CurrencyUnit.INR]: (v) => v
|
|
525
533
|
};
|
|
526
534
|
var toBitcoinConversion = (v, unitsPerBtc = 1) => round(v * unitsPerBtc);
|
|
527
535
|
var toMicrobitcoinConversion = (v, unitsPerBtc = 1) => round(v / 1e6 * unitsPerBtc);
|
|
@@ -541,7 +549,8 @@ var CONVERSION_MAP = {
|
|
|
541
549
|
[CurrencyUnit.MXN]: toBitcoinConversion,
|
|
542
550
|
[CurrencyUnit.PHP]: toBitcoinConversion,
|
|
543
551
|
[CurrencyUnit.EUR]: toBitcoinConversion,
|
|
544
|
-
[CurrencyUnit.GBP]: toBitcoinConversion
|
|
552
|
+
[CurrencyUnit.GBP]: toBitcoinConversion,
|
|
553
|
+
[CurrencyUnit.INR]: toBitcoinConversion
|
|
545
554
|
},
|
|
546
555
|
[CurrencyUnit.MICROBITCOIN]: {
|
|
547
556
|
[CurrencyUnit.BITCOIN]: (v) => v / 1e6,
|
|
@@ -554,7 +563,8 @@ var CONVERSION_MAP = {
|
|
|
554
563
|
[CurrencyUnit.MXN]: toMicrobitcoinConversion,
|
|
555
564
|
[CurrencyUnit.PHP]: toMicrobitcoinConversion,
|
|
556
565
|
[CurrencyUnit.EUR]: toMicrobitcoinConversion,
|
|
557
|
-
[CurrencyUnit.GBP]: toMicrobitcoinConversion
|
|
566
|
+
[CurrencyUnit.GBP]: toMicrobitcoinConversion,
|
|
567
|
+
[CurrencyUnit.INR]: toMicrobitcoinConversion
|
|
558
568
|
},
|
|
559
569
|
[CurrencyUnit.MILLIBITCOIN]: {
|
|
560
570
|
[CurrencyUnit.BITCOIN]: (v) => v / 1e3,
|
|
@@ -567,7 +577,8 @@ var CONVERSION_MAP = {
|
|
|
567
577
|
[CurrencyUnit.MXN]: toMillibitcoinConversion,
|
|
568
578
|
[CurrencyUnit.PHP]: toMillibitcoinConversion,
|
|
569
579
|
[CurrencyUnit.EUR]: toMillibitcoinConversion,
|
|
570
|
-
[CurrencyUnit.GBP]: toMillibitcoinConversion
|
|
580
|
+
[CurrencyUnit.GBP]: toMillibitcoinConversion,
|
|
581
|
+
[CurrencyUnit.INR]: toMillibitcoinConversion
|
|
571
582
|
},
|
|
572
583
|
[CurrencyUnit.MILLISATOSHI]: {
|
|
573
584
|
[CurrencyUnit.BITCOIN]: (v) => v / 1e11,
|
|
@@ -580,7 +591,8 @@ var CONVERSION_MAP = {
|
|
|
580
591
|
[CurrencyUnit.MXN]: toMillisatoshiConversion,
|
|
581
592
|
[CurrencyUnit.PHP]: toMillisatoshiConversion,
|
|
582
593
|
[CurrencyUnit.EUR]: toMillisatoshiConversion,
|
|
583
|
-
[CurrencyUnit.GBP]: toMillisatoshiConversion
|
|
594
|
+
[CurrencyUnit.GBP]: toMillisatoshiConversion,
|
|
595
|
+
[CurrencyUnit.INR]: toMillisatoshiConversion
|
|
584
596
|
},
|
|
585
597
|
[CurrencyUnit.NANOBITCOIN]: {
|
|
586
598
|
[CurrencyUnit.BITCOIN]: (v) => v / 1e9,
|
|
@@ -593,7 +605,8 @@ var CONVERSION_MAP = {
|
|
|
593
605
|
[CurrencyUnit.MXN]: toNanobitcoinConversion,
|
|
594
606
|
[CurrencyUnit.PHP]: toNanobitcoinConversion,
|
|
595
607
|
[CurrencyUnit.EUR]: toNanobitcoinConversion,
|
|
596
|
-
[CurrencyUnit.GBP]: toNanobitcoinConversion
|
|
608
|
+
[CurrencyUnit.GBP]: toNanobitcoinConversion,
|
|
609
|
+
[CurrencyUnit.INR]: toNanobitcoinConversion
|
|
597
610
|
},
|
|
598
611
|
[CurrencyUnit.SATOSHI]: {
|
|
599
612
|
[CurrencyUnit.BITCOIN]: (v) => v / 1e8,
|
|
@@ -606,13 +619,15 @@ var CONVERSION_MAP = {
|
|
|
606
619
|
[CurrencyUnit.MXN]: toSatoshiConversion,
|
|
607
620
|
[CurrencyUnit.PHP]: toSatoshiConversion,
|
|
608
621
|
[CurrencyUnit.EUR]: toSatoshiConversion,
|
|
609
|
-
[CurrencyUnit.GBP]: toSatoshiConversion
|
|
622
|
+
[CurrencyUnit.GBP]: toSatoshiConversion,
|
|
623
|
+
[CurrencyUnit.INR]: toSatoshiConversion
|
|
610
624
|
},
|
|
611
625
|
[CurrencyUnit.USD]: standardUnitConversionObj,
|
|
612
626
|
[CurrencyUnit.MXN]: standardUnitConversionObj,
|
|
613
627
|
[CurrencyUnit.PHP]: standardUnitConversionObj,
|
|
614
628
|
[CurrencyUnit.EUR]: standardUnitConversionObj,
|
|
615
|
-
[CurrencyUnit.GBP]: standardUnitConversionObj
|
|
629
|
+
[CurrencyUnit.GBP]: standardUnitConversionObj,
|
|
630
|
+
[CurrencyUnit.INR]: standardUnitConversionObj
|
|
616
631
|
};
|
|
617
632
|
function convertCurrencyAmountValue(fromUnit, toUnit, amount, unitsPerBtc = 1) {
|
|
618
633
|
if (fromUnit === CurrencyUnit.FUTURE_VALUE || toUnit === CurrencyUnit.FUTURE_VALUE) {
|
|
@@ -697,6 +712,7 @@ function convertCurrencyAmountValues(fromUnit, amount, unitsPerBtc = 1, conversi
|
|
|
697
712
|
php: CurrencyUnit.PHP,
|
|
698
713
|
eur: CurrencyUnit.EUR,
|
|
699
714
|
gbp: CurrencyUnit.GBP,
|
|
715
|
+
inr: CurrencyUnit.INR,
|
|
700
716
|
mibtc: CurrencyUnit.MICROBITCOIN,
|
|
701
717
|
mlbtc: CurrencyUnit.MILLIBITCOIN,
|
|
702
718
|
nbtc: CurrencyUnit.NANOBITCOIN
|
|
@@ -735,7 +751,7 @@ function getPreferredConversionOverride(currencyAmountArg) {
|
|
|
735
751
|
function mapCurrencyAmount(currencyAmountArg, unitsPerBtc = 1) {
|
|
736
752
|
const { value, unit } = getCurrencyAmount(currencyAmountArg);
|
|
737
753
|
const conversionOverride = getPreferredConversionOverride(currencyAmountArg);
|
|
738
|
-
const { sats, msats, btc, usd, mxn, php, mibtc, mlbtc, nbtc, eur, gbp } = convertCurrencyAmountValues(unit, value, unitsPerBtc, conversionOverride);
|
|
754
|
+
const { sats, msats, btc, usd, mxn, php, mibtc, mlbtc, nbtc, eur, gbp, inr } = convertCurrencyAmountValues(unit, value, unitsPerBtc, conversionOverride);
|
|
739
755
|
const mapWithCurrencyUnits = {
|
|
740
756
|
[CurrencyUnit.BITCOIN]: btc,
|
|
741
757
|
[CurrencyUnit.SATOSHI]: sats,
|
|
@@ -745,6 +761,7 @@ function mapCurrencyAmount(currencyAmountArg, unitsPerBtc = 1) {
|
|
|
745
761
|
[CurrencyUnit.PHP]: php,
|
|
746
762
|
[CurrencyUnit.EUR]: eur,
|
|
747
763
|
[CurrencyUnit.GBP]: gbp,
|
|
764
|
+
[CurrencyUnit.INR]: inr,
|
|
748
765
|
[CurrencyUnit.MICROBITCOIN]: mibtc,
|
|
749
766
|
[CurrencyUnit.MILLIBITCOIN]: mlbtc,
|
|
750
767
|
[CurrencyUnit.NANOBITCOIN]: nbtc,
|
|
@@ -794,6 +811,10 @@ function mapCurrencyAmount(currencyAmountArg, unitsPerBtc = 1) {
|
|
|
794
811
|
value: gbp,
|
|
795
812
|
unit: CurrencyUnit.GBP
|
|
796
813
|
}),
|
|
814
|
+
[CurrencyUnit.INR]: formatCurrencyStr({
|
|
815
|
+
value: inr,
|
|
816
|
+
unit: CurrencyUnit.INR
|
|
817
|
+
}),
|
|
797
818
|
[CurrencyUnit.FUTURE_VALUE]: "-"
|
|
798
819
|
}
|
|
799
820
|
};
|
|
@@ -862,6 +883,8 @@ var abbrCurrencyUnit = (unit) => {
|
|
|
862
883
|
return "EUR";
|
|
863
884
|
case CurrencyUnit.GBP:
|
|
864
885
|
return "GBP";
|
|
886
|
+
case CurrencyUnit.INR:
|
|
887
|
+
return "INR";
|
|
865
888
|
}
|
|
866
889
|
return "Unsupported CurrencyUnit";
|
|
867
890
|
};
|
|
@@ -994,6 +1017,15 @@ function localeToCurrencySymbol(locale) {
|
|
|
994
1017
|
const { symbol } = separateCurrencyStrParts(formatted);
|
|
995
1018
|
return symbol;
|
|
996
1019
|
}
|
|
1020
|
+
function formatInviteAmount(amount, currency) {
|
|
1021
|
+
if (!amount || !currency) {
|
|
1022
|
+
return "";
|
|
1023
|
+
}
|
|
1024
|
+
const displayAmount = (amount / Math.pow(10, currency.decimals)).toFixed(
|
|
1025
|
+
currency.decimals
|
|
1026
|
+
);
|
|
1027
|
+
return `${displayAmount} ${currency.code}`;
|
|
1028
|
+
}
|
|
997
1029
|
|
|
998
1030
|
// src/utils/typeGuards.ts
|
|
999
1031
|
function isUint8Array(value) {
|
|
@@ -2300,12 +2332,14 @@ function zipcodeToState(zipcode) {
|
|
|
2300
2332
|
ensureArray,
|
|
2301
2333
|
errorToJSON,
|
|
2302
2334
|
formatCurrencyStr,
|
|
2335
|
+
formatInviteAmount,
|
|
2303
2336
|
getCurrencyAmount,
|
|
2304
2337
|
getCurrentLocale,
|
|
2305
2338
|
getErrorMsg,
|
|
2306
2339
|
getLocalStorageBoolean,
|
|
2307
2340
|
getLocalStorageConfigItem,
|
|
2308
2341
|
hexToBytes,
|
|
2342
|
+
isBare,
|
|
2309
2343
|
isBrowser,
|
|
2310
2344
|
isCurrencyAmountInputObj,
|
|
2311
2345
|
isCurrencyAmountObj,
|
|
@@ -2318,6 +2352,7 @@ function zipcodeToState(zipcode) {
|
|
|
2318
2352
|
isNode,
|
|
2319
2353
|
isNumber,
|
|
2320
2354
|
isObject,
|
|
2355
|
+
isReactNative,
|
|
2321
2356
|
isRecord,
|
|
2322
2357
|
isSDKCurrencyAmount,
|
|
2323
2358
|
isTest,
|
package/dist/utils/index.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { A as AppendUnitsOptions,
|
|
1
|
+
export { A as AppendUnitsOptions, ae as ById, ap as Complete, o as CurrencyAmountArg, k as CurrencyAmountInputObj, m as CurrencyAmountObj, n as CurrencyAmountPreferenceObj, Y as CurrencyCodes, X as CurrencyLocales, j as CurrencyMap, f as CurrencyUnit, g as CurrencyUnitType, ai as DeepPartial, D as DeprecatedCurrencyAmountObj, ad as ExpandRecursively, ah as ExtractByTypename, aj as JSONLiteral, al as JSONObject, ak as JSONType, ac as Maybe, am as NN, af as OmitTypename, ao as PartialBy, aq as RequiredKeys, S as SDKCurrencyAmountType, ar as StateCode, U as UmaCurrency, l as UmaCurrencyAmount, z as abbrCurrencyUnit, b as b64decode, a as b64encode, R as bytesToHex, a2 as clamp, i as convertCurrencyAmount, h as convertCurrencyAmountValue, W as countryCodesToCurrencyCodes, c as createSha256Hash, d as defaultCurrencyCode, a1 as deleteLocalStorageItem, e as ensureArray, Q as errorToJSON, B as formatCurrencyStr, G as formatInviteAmount, w as getCurrencyAmount, V as getCurrentLocale, O as getErrorMsg, $ as getLocalStorageBoolean, _ as getLocalStorageConfigItem, T as hexToBytes, K as isBare, H as isBrowser, p as isCurrencyAmountInputObj, s as isCurrencyAmountObj, t as isCurrencyAmountPreferenceObj, y as isCurrencyMap, r as isDeprecatedCurrencyAmountObj, M as isError, P as isErrorMsg, N as isErrorWithMessage, I as isNode, a5 as isNumber, aa as isObject, L as isReactNative, ab as isRecord, v as isSDKCurrencyAmount, J as isTest, ag as isType, a9 as isUint8Array, q as isUmaCurrencyAmount, a3 as linearInterpolate, Z as localeToCurrencyCode, F as localeToCurrencySymbol, a8 as lsidToUUID, x as mapCurrencyAmount, an as notNullUndefined, a6 as pollUntil, a4 as round, E as separateCurrencyStrParts, a0 as setLocalStorageBoolean, a7 as sleep, u as urlsafe_b64decode, at as zipcodeToState, as as zipcodeToStateCode } from '../index-CFQtMxrx.cjs';
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { A as AppendUnitsOptions,
|
|
1
|
+
export { A as AppendUnitsOptions, ae as ById, ap as Complete, o as CurrencyAmountArg, k as CurrencyAmountInputObj, m as CurrencyAmountObj, n as CurrencyAmountPreferenceObj, Y as CurrencyCodes, X as CurrencyLocales, j as CurrencyMap, f as CurrencyUnit, g as CurrencyUnitType, ai as DeepPartial, D as DeprecatedCurrencyAmountObj, ad as ExpandRecursively, ah as ExtractByTypename, aj as JSONLiteral, al as JSONObject, ak as JSONType, ac as Maybe, am as NN, af as OmitTypename, ao as PartialBy, aq as RequiredKeys, S as SDKCurrencyAmountType, ar as StateCode, U as UmaCurrency, l as UmaCurrencyAmount, z as abbrCurrencyUnit, b as b64decode, a as b64encode, R as bytesToHex, a2 as clamp, i as convertCurrencyAmount, h as convertCurrencyAmountValue, W as countryCodesToCurrencyCodes, c as createSha256Hash, d as defaultCurrencyCode, a1 as deleteLocalStorageItem, e as ensureArray, Q as errorToJSON, B as formatCurrencyStr, G as formatInviteAmount, w as getCurrencyAmount, V as getCurrentLocale, O as getErrorMsg, $ as getLocalStorageBoolean, _ as getLocalStorageConfigItem, T as hexToBytes, K as isBare, H as isBrowser, p as isCurrencyAmountInputObj, s as isCurrencyAmountObj, t as isCurrencyAmountPreferenceObj, y as isCurrencyMap, r as isDeprecatedCurrencyAmountObj, M as isError, P as isErrorMsg, N as isErrorWithMessage, I as isNode, a5 as isNumber, aa as isObject, L as isReactNative, ab as isRecord, v as isSDKCurrencyAmount, J as isTest, ag as isType, a9 as isUint8Array, q as isUmaCurrencyAmount, a3 as linearInterpolate, Z as localeToCurrencyCode, F as localeToCurrencySymbol, a8 as lsidToUUID, x as mapCurrencyAmount, an as notNullUndefined, a6 as pollUntil, a4 as round, E as separateCurrencyStrParts, a0 as setLocalStorageBoolean, a7 as sleep, u as urlsafe_b64decode, at as zipcodeToState, as as zipcodeToStateCode } from '../index-CFQtMxrx.js';
|
package/dist/utils/index.js
CHANGED
|
@@ -14,12 +14,14 @@ import {
|
|
|
14
14
|
ensureArray,
|
|
15
15
|
errorToJSON,
|
|
16
16
|
formatCurrencyStr,
|
|
17
|
+
formatInviteAmount,
|
|
17
18
|
getCurrencyAmount,
|
|
18
19
|
getCurrentLocale,
|
|
19
20
|
getErrorMsg,
|
|
20
21
|
getLocalStorageBoolean,
|
|
21
22
|
getLocalStorageConfigItem,
|
|
22
23
|
hexToBytes,
|
|
24
|
+
isBare,
|
|
23
25
|
isBrowser,
|
|
24
26
|
isCurrencyAmountInputObj,
|
|
25
27
|
isCurrencyAmountObj,
|
|
@@ -32,6 +34,7 @@ import {
|
|
|
32
34
|
isNode,
|
|
33
35
|
isNumber,
|
|
34
36
|
isObject,
|
|
37
|
+
isReactNative,
|
|
35
38
|
isRecord,
|
|
36
39
|
isSDKCurrencyAmount,
|
|
37
40
|
isTest,
|
|
@@ -52,7 +55,7 @@ import {
|
|
|
52
55
|
urlsafe_b64decode,
|
|
53
56
|
zipcodeToState,
|
|
54
57
|
zipcodeToStateCode
|
|
55
|
-
} from "../chunk-
|
|
58
|
+
} from "../chunk-ADHQHZNM.js";
|
|
56
59
|
export {
|
|
57
60
|
CurrencyUnit,
|
|
58
61
|
abbrCurrencyUnit,
|
|
@@ -69,12 +72,14 @@ export {
|
|
|
69
72
|
ensureArray,
|
|
70
73
|
errorToJSON,
|
|
71
74
|
formatCurrencyStr,
|
|
75
|
+
formatInviteAmount,
|
|
72
76
|
getCurrencyAmount,
|
|
73
77
|
getCurrentLocale,
|
|
74
78
|
getErrorMsg,
|
|
75
79
|
getLocalStorageBoolean,
|
|
76
80
|
getLocalStorageConfigItem,
|
|
77
81
|
hexToBytes,
|
|
82
|
+
isBare,
|
|
78
83
|
isBrowser,
|
|
79
84
|
isCurrencyAmountInputObj,
|
|
80
85
|
isCurrencyAmountObj,
|
|
@@ -87,6 +92,7 @@ export {
|
|
|
87
92
|
isNode,
|
|
88
93
|
isNumber,
|
|
89
94
|
isObject,
|
|
95
|
+
isReactNative,
|
|
90
96
|
isRecord,
|
|
91
97
|
isSDKCurrencyAmount,
|
|
92
98
|
isTest,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lightsparkdev/core",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "Lightspark JS SDK",
|
|
5
5
|
"author": "Lightspark Inc.",
|
|
6
6
|
"keywords": [
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
25
25
|
"exports": {
|
|
26
26
|
".": {
|
|
27
|
+
"react-native": "./dist/react-native/index.js",
|
|
27
28
|
"import": "./dist/index.js",
|
|
28
29
|
"require": "./dist/index.cjs"
|
|
29
30
|
},
|
|
@@ -39,6 +40,7 @@
|
|
|
39
40
|
"src/*",
|
|
40
41
|
"dist/*",
|
|
41
42
|
"dist/utils/*",
|
|
43
|
+
"dist/react-native/*",
|
|
42
44
|
"CHANGELOG.md"
|
|
43
45
|
],
|
|
44
46
|
"scripts": {
|
|
@@ -51,6 +53,7 @@
|
|
|
51
53
|
"lint:fix": "eslint --fix .",
|
|
52
54
|
"lint:watch": "esw ./src -w --ext .ts,.tsx,.js --color",
|
|
53
55
|
"lint": "eslint .",
|
|
56
|
+
"circular-deps": "madge --circular --extensions ts,tsx src",
|
|
54
57
|
"package:checks": "yarn publint && yarn attw --pack .",
|
|
55
58
|
"postversion": "yarn build",
|
|
56
59
|
"test-cmd": "node --experimental-vm-modules $(yarn bin jest) --no-cache --runInBand --bail",
|
|
@@ -81,6 +84,7 @@
|
|
|
81
84
|
"eslint-watch": "^8.0.0",
|
|
82
85
|
"jest": "^29.6.2",
|
|
83
86
|
"lodash-es": "^4.17.21",
|
|
87
|
+
"madge": "^6.1.0",
|
|
84
88
|
"prettier": "3.0.3",
|
|
85
89
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
86
90
|
"publint": "^0.3.9",
|
|
@@ -89,6 +93,16 @@
|
|
|
89
93
|
"tsup": "^8.2.4",
|
|
90
94
|
"typescript": "^5.6.2"
|
|
91
95
|
},
|
|
96
|
+
"madge": {
|
|
97
|
+
"detectiveOptions": {
|
|
98
|
+
"ts": {
|
|
99
|
+
"skipTypeImports": true
|
|
100
|
+
},
|
|
101
|
+
"tsx": {
|
|
102
|
+
"skipTypeImports": true
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
},
|
|
92
106
|
"engines": {
|
|
93
107
|
"node": ">=18"
|
|
94
108
|
}
|
package/src/Logger.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ConfigKeys
|
|
1
|
+
import { ConfigKeys } from "./constants/index.js";
|
|
2
2
|
import { isBrowser, isTest } from "./utils/environment.js";
|
|
3
|
+
import { getLocalStorageConfigItem } from "./utils/localStorage.js";
|
|
3
4
|
|
|
4
5
|
type GetLoggingEnabled = (() => Promise<boolean> | boolean) | undefined;
|
|
5
6
|
|
package/src/crypto/SigningKey.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import secp256k1 from "secp256k1";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { createSha256Hash } from "../utils/createHash.js";
|
|
3
|
+
import { hexToBytes } from "../utils/hex.js";
|
|
4
|
+
import type { CryptoInterface } from "./crypto.js";
|
|
5
|
+
import { SigningKeyType } from "./types.js";
|
|
4
6
|
|
|
5
7
|
interface Alias {
|
|
6
8
|
alias: string;
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export * from "./
|
|
6
|
-
export { default as LightsparkException } from "./LightsparkException.js";
|
|
7
|
-
export { Logger, LoggingLevel, logger } from "./Logger.js";
|
|
8
|
-
export * from "./requester/index.js";
|
|
9
|
-
export {
|
|
10
|
-
default as ServerEnvironment,
|
|
11
|
-
apiDomainForEnvironment,
|
|
12
|
-
} from "./ServerEnvironment.js";
|
|
13
|
-
export * from "./utils/index.js";
|
|
3
|
+
export { DefaultRequester as Requester } from "./requester/DefaultRequester.js";
|
|
4
|
+
export { default as Query } from "./requester/Query.js";
|
|
5
|
+
export * from "./shared.js";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type AuthProvider from "../auth/AuthProvider.js";
|
|
2
|
+
import { isBare, isNode } from "../utils/environment.js";
|
|
3
|
+
import Requester from "./Requester.js";
|
|
4
|
+
|
|
5
|
+
export class DefaultRequester extends Requester {
|
|
6
|
+
protected async initWsClient(baseUrl: string, authProvider: AuthProvider) {
|
|
7
|
+
if (!this.resolveWsClient) {
|
|
8
|
+
/* If resolveWsClient is null assume already initialized: */
|
|
9
|
+
return this.wsClient;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (isBare) {
|
|
13
|
+
/* graphql-ws library is currently not supported in Bare environment, see LIG-7942 */
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let websocketImpl;
|
|
18
|
+
if (isNode && typeof WebSocket === "undefined") {
|
|
19
|
+
const wsModule = await import("ws");
|
|
20
|
+
websocketImpl = wsModule.default;
|
|
21
|
+
}
|
|
22
|
+
let websocketProtocol = "wss";
|
|
23
|
+
if (baseUrl.startsWith("http://")) {
|
|
24
|
+
websocketProtocol = "ws";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const graphqlWsModule = await import("graphql-ws");
|
|
28
|
+
const { createClient } = graphqlWsModule;
|
|
29
|
+
|
|
30
|
+
const wsClient = createClient({
|
|
31
|
+
url: `${websocketProtocol}://${this.stripProtocol(this.baseUrl)}/${
|
|
32
|
+
this.schemaEndpoint
|
|
33
|
+
}`,
|
|
34
|
+
connectionParams: () => authProvider.addWsConnectionParams({}),
|
|
35
|
+
webSocketImpl: websocketImpl,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (this.resolveWsClient) {
|
|
39
|
+
this.resolveWsClient(wsClient);
|
|
40
|
+
this.resolveWsClient = null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return wsClient;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export default DefaultRequester;
|
|
@@ -4,7 +4,6 @@ import autoBind from "auto-bind";
|
|
|
4
4
|
import dayjs from "dayjs";
|
|
5
5
|
import utc from "dayjs/plugin/utc.js";
|
|
6
6
|
import type { Client as WsClient } from "graphql-ws";
|
|
7
|
-
import { createClient } from "graphql-ws";
|
|
8
7
|
import { Observable } from "zen-observable-ts";
|
|
9
8
|
|
|
10
9
|
import type Query from "./Query.js";
|
|
@@ -32,55 +31,30 @@ type BodyData = {
|
|
|
32
31
|
};
|
|
33
32
|
|
|
34
33
|
class Requester {
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
protected wsClient: Promise<WsClient | null>;
|
|
35
|
+
protected resolveWsClient: ((value: WsClient | null) => void) | null = null;
|
|
37
36
|
constructor(
|
|
38
37
|
private readonly nodeKeyCache: NodeKeyCache,
|
|
39
|
-
|
|
38
|
+
protected readonly schemaEndpoint: string,
|
|
40
39
|
private readonly sdkUserAgent: string,
|
|
41
40
|
private readonly authProvider: AuthProvider = new StubAuthProvider(),
|
|
42
|
-
|
|
41
|
+
protected readonly baseUrl: string = DEFAULT_BASE_URL,
|
|
43
42
|
private readonly cryptoImpl: CryptoInterface = DefaultCrypto,
|
|
44
43
|
private readonly signingKey?: SigningKey,
|
|
45
44
|
private readonly fetchImpl: typeof fetch = fetch,
|
|
46
45
|
) {
|
|
47
|
-
this.wsClient = new Promise<WsClient>((resolve) => {
|
|
46
|
+
this.wsClient = new Promise<WsClient | null>((resolve) => {
|
|
48
47
|
this.resolveWsClient = resolve;
|
|
49
48
|
});
|
|
50
49
|
void this.initWsClient(baseUrl, authProvider);
|
|
51
50
|
autoBind(this);
|
|
52
51
|
}
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
let websocketImpl;
|
|
61
|
-
if (isNode && typeof WebSocket === "undefined") {
|
|
62
|
-
const wsModule = await import("ws");
|
|
63
|
-
websocketImpl = wsModule.default;
|
|
64
|
-
}
|
|
65
|
-
let websocketProtocol = "wss";
|
|
66
|
-
if (baseUrl.startsWith("http://")) {
|
|
67
|
-
websocketProtocol = "ws";
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const wsClient = createClient({
|
|
71
|
-
url: `${websocketProtocol}://${this.stripProtocol(this.baseUrl)}/${
|
|
72
|
-
this.schemaEndpoint
|
|
73
|
-
}`,
|
|
74
|
-
connectionParams: () => authProvider.addWsConnectionParams({}),
|
|
75
|
-
webSocketImpl: websocketImpl,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
if (this.resolveWsClient) {
|
|
79
|
-
this.resolveWsClient(wsClient);
|
|
80
|
-
this.resolveWsClient = null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return wsClient;
|
|
53
|
+
protected initWsClient(
|
|
54
|
+
baseUrl: string,
|
|
55
|
+
authProvider: AuthProvider,
|
|
56
|
+
): Promise<WsClient | null> {
|
|
57
|
+
return Promise.resolve(null);
|
|
84
58
|
}
|
|
85
59
|
|
|
86
60
|
public async executeQuery<T>(query: Query<T>): Promise<T | null> {
|
|
@@ -99,6 +73,7 @@ class Requester {
|
|
|
99
73
|
variables: { [key: string]: unknown } = {},
|
|
100
74
|
) {
|
|
101
75
|
logger.trace(`Requester.subscribe variables`, variables);
|
|
76
|
+
|
|
102
77
|
const operationNameRegex = /^\s*(query|mutation|subscription)\s+(\w+)/i;
|
|
103
78
|
const operationMatch = queryPayload.match(operationNameRegex);
|
|
104
79
|
if (!operationMatch || operationMatch.length < 3) {
|
|
@@ -134,6 +109,15 @@ class Requester {
|
|
|
134
109
|
void (async () => {
|
|
135
110
|
try {
|
|
136
111
|
const wsClient = await this.wsClient;
|
|
112
|
+
|
|
113
|
+
if (!wsClient) {
|
|
114
|
+
/* graphql-ws library is currently not supported in Bare environment, see LIG-7942 */
|
|
115
|
+
throw new LightsparkException(
|
|
116
|
+
"WebSocketNotInitialized",
|
|
117
|
+
"WebSocket client is not initialized or is not available in the current environment.",
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
137
121
|
if (!canceled) {
|
|
138
122
|
cleanup = wsClient.subscribe(bodyData, {
|
|
139
123
|
next: (data) => observer.next(data as { data: T }),
|
|
@@ -276,7 +260,7 @@ class Requester {
|
|
|
276
260
|
return `${this.sdkUserAgent} ${platform}/${platformVersion}`;
|
|
277
261
|
}
|
|
278
262
|
|
|
279
|
-
|
|
263
|
+
protected stripProtocol(url: string): string {
|
|
280
264
|
return url.replace(/.*?:\/\//g, "");
|
|
281
265
|
}
|
|
282
266
|
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { beforeEach, jest } from "@jest/globals";
|
|
2
|
+
|
|
3
|
+
import type { Client as WsClient } from "graphql-ws";
|
|
4
|
+
import type AuthProvider from "../../auth/AuthProvider.js";
|
|
5
|
+
import type { CryptoInterface } from "../../crypto/crypto.js";
|
|
6
|
+
import type NodeKeyCache from "../../crypto/NodeKeyCache.js";
|
|
7
|
+
import type { SigningKey } from "../../crypto/SigningKey.js";
|
|
8
|
+
import { SigningKeyType } from "../../crypto/types.js";
|
|
9
|
+
|
|
10
|
+
/* Mocking ESM modules (when running node with --experimental-vm-modules)
|
|
11
|
+
requires unstable_mockModule, see https://bit.ly/433nRV1 */
|
|
12
|
+
await jest.unstable_mockModule("graphql-ws", () => ({
|
|
13
|
+
__esModule: true,
|
|
14
|
+
createClient: jest.fn(),
|
|
15
|
+
}));
|
|
16
|
+
/* Since Requester uses graphql-ws we need a dynamic import after the above mock */
|
|
17
|
+
const { DefaultRequester } = await import("../DefaultRequester.js");
|
|
18
|
+
|
|
19
|
+
describe("DefaultRequester", () => {
|
|
20
|
+
const schemaEndpoint = "graphql";
|
|
21
|
+
const sdkUserAgent = "test-agent";
|
|
22
|
+
const baseUrl = "https://api.example.com";
|
|
23
|
+
|
|
24
|
+
let nodeKeyCache: NodeKeyCache;
|
|
25
|
+
let authProvider: AuthProvider;
|
|
26
|
+
let signingKey: SigningKey;
|
|
27
|
+
let cryptoImpl: CryptoInterface;
|
|
28
|
+
let fetchImpl: typeof fetch;
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
nodeKeyCache = {
|
|
32
|
+
getKey: jest.fn(),
|
|
33
|
+
hasKey: jest.fn(),
|
|
34
|
+
} as unknown as NodeKeyCache;
|
|
35
|
+
|
|
36
|
+
authProvider = {
|
|
37
|
+
addAuthHeaders: jest.fn(async (headers: Record<string, string>) => ({
|
|
38
|
+
...headers,
|
|
39
|
+
"X-Test": "1",
|
|
40
|
+
})),
|
|
41
|
+
isAuthorized: jest.fn(async () => true),
|
|
42
|
+
addWsConnectionParams: jest.fn(
|
|
43
|
+
async (params: Record<string, unknown>) => ({
|
|
44
|
+
...params,
|
|
45
|
+
ws: true,
|
|
46
|
+
}),
|
|
47
|
+
),
|
|
48
|
+
} satisfies AuthProvider;
|
|
49
|
+
|
|
50
|
+
signingKey = {
|
|
51
|
+
type: SigningKeyType.RSASigningKey,
|
|
52
|
+
sign: jest.fn(async (data: Uint8Array) => new Uint8Array([1, 2, 3])),
|
|
53
|
+
} satisfies SigningKey;
|
|
54
|
+
|
|
55
|
+
cryptoImpl = {
|
|
56
|
+
decryptSecretWithNodePassword: jest.fn(async () => new ArrayBuffer(0)),
|
|
57
|
+
generateSigningKeyPair: jest.fn(async () => ({
|
|
58
|
+
publicKey: "",
|
|
59
|
+
privateKey: "",
|
|
60
|
+
})),
|
|
61
|
+
serializeSigningKey: jest.fn(async () => new ArrayBuffer(0)),
|
|
62
|
+
getNonce: jest.fn(async () => 123),
|
|
63
|
+
sign: jest.fn(async () => new ArrayBuffer(0)),
|
|
64
|
+
importPrivateSigningKey: jest.fn(async () => ""),
|
|
65
|
+
} satisfies CryptoInterface;
|
|
66
|
+
|
|
67
|
+
fetchImpl = jest.fn(
|
|
68
|
+
async () =>
|
|
69
|
+
({
|
|
70
|
+
ok: true,
|
|
71
|
+
json: async () => ({ data: { foo: "bar" }, errors: undefined }),
|
|
72
|
+
statusText: "OK",
|
|
73
|
+
}) as Response,
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("subscribe", () => {
|
|
78
|
+
it("returns an Observable for a valid subscription", async () => {
|
|
79
|
+
// Mock wsClient and its subscribe method
|
|
80
|
+
const wsClient = {
|
|
81
|
+
subscribe: jest.fn(
|
|
82
|
+
(
|
|
83
|
+
_body,
|
|
84
|
+
handlers: { next?: (data: unknown) => void; complete?: () => void },
|
|
85
|
+
) => {
|
|
86
|
+
setTimeout(() => {
|
|
87
|
+
handlers.next?.({ data: { foo: "bar" } });
|
|
88
|
+
handlers.complete?.();
|
|
89
|
+
}, 10);
|
|
90
|
+
return jest.fn();
|
|
91
|
+
},
|
|
92
|
+
),
|
|
93
|
+
} as unknown as WsClient;
|
|
94
|
+
|
|
95
|
+
const { createClient } = await import("graphql-ws");
|
|
96
|
+
(createClient as jest.Mock).mockReturnValue(wsClient);
|
|
97
|
+
|
|
98
|
+
const requester = new DefaultRequester(
|
|
99
|
+
nodeKeyCache,
|
|
100
|
+
schemaEndpoint,
|
|
101
|
+
sdkUserAgent,
|
|
102
|
+
authProvider,
|
|
103
|
+
baseUrl,
|
|
104
|
+
cryptoImpl,
|
|
105
|
+
signingKey,
|
|
106
|
+
fetchImpl,
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const observable = requester.subscribe<{ foo: string }>(
|
|
110
|
+
"subscription TestSub { foo }",
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
const results: { foo: string }[] = [];
|
|
114
|
+
await new Promise<void>((resolve) => {
|
|
115
|
+
observable.subscribe({
|
|
116
|
+
next: (data: { data: { foo: string } }) => {
|
|
117
|
+
results.push(data.data);
|
|
118
|
+
},
|
|
119
|
+
complete: () => {
|
|
120
|
+
expect(results).toEqual([{ foo: "bar" }]);
|
|
121
|
+
resolve();
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(wsClient.subscribe).toHaveBeenCalled();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|