@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.
@@ -1,6 +1,5 @@
1
1
  import { beforeEach, jest } from "@jest/globals";
2
2
 
3
- import type { Client as WsClient } from "graphql-ws";
4
3
  import type AuthProvider from "../../auth/AuthProvider.js";
5
4
  import type { CryptoInterface } from "../../crypto/crypto.js";
6
5
  import type NodeKeyCache from "../../crypto/NodeKeyCache.js";
@@ -16,7 +15,7 @@ await jest.unstable_mockModule("graphql-ws", () => ({
16
15
  createClient: jest.fn(),
17
16
  }));
18
17
  /* Since Requester uses graphql-ws we need a dynamic import after the above mock */
19
- const { Requester } = await import("../index.js");
18
+ const { default: Requester } = await import("../Requester.js");
20
19
 
21
20
  describe("Requester", () => {
22
21
  const schemaEndpoint = "graphql";
@@ -257,26 +256,7 @@ describe("Requester", () => {
257
256
  expect(() => requester.subscribe("invalid")).toThrow(LightsparkException);
258
257
  });
259
258
 
260
- it("returns an Observable for a valid subscription", async () => {
261
- // Mock wsClient and its subscribe method
262
- const wsClient = {
263
- subscribe: jest.fn(
264
- (
265
- _body,
266
- handlers: { next?: (data: unknown) => void; complete?: () => void },
267
- ) => {
268
- setTimeout(() => {
269
- handlers.next?.({ data: { foo: "bar" } });
270
- handlers.complete?.();
271
- }, 10);
272
- return jest.fn();
273
- },
274
- ),
275
- } as unknown as WsClient;
276
-
277
- const { createClient } = await import("graphql-ws");
278
- (createClient as jest.Mock).mockReturnValue(wsClient);
279
-
259
+ it("emits error when wsClient is not initialized", async () => {
280
260
  const requester = new Requester(
281
261
  nodeKeyCache,
282
262
  schemaEndpoint,
@@ -287,25 +267,36 @@ describe("Requester", () => {
287
267
  signingKey,
288
268
  fetchImpl,
289
269
  );
270
+ // Resolve internal wsClient promise to null so the observable emits an error.
271
+ (
272
+ requester as unknown as {
273
+ resolveWsClient: ((v: unknown) => void) | null;
274
+ }
275
+ ).resolveWsClient?.(null);
290
276
 
291
- const observable = requester.subscribe<{ foo: string }>(
292
- "subscription TestSub { foo }",
293
- );
277
+ const observable = requester.subscribe("subscription TestSub { foo }");
294
278
 
295
- const results: { foo: string }[] = [];
296
279
  await new Promise<void>((resolve) => {
297
280
  observable.subscribe({
298
- next: (data) => {
299
- results.push(data.data);
281
+ next: () => {
282
+ throw new Error(
283
+ "Should not emit next when wsClient is uninitialized",
284
+ );
300
285
  },
301
- complete: () => {
302
- expect(results).toEqual([{ foo: "bar" }]);
286
+ error: (err) => {
287
+ expect(err).toBeInstanceOf(LightsparkException);
288
+ expect(String((err as Error).message)).toMatch(
289
+ /WebSocket client is not initialized/,
290
+ );
303
291
  resolve();
304
292
  },
293
+ complete: () => {
294
+ throw new Error(
295
+ "Should not complete when wsClient is uninitialized",
296
+ );
297
+ },
305
298
  });
306
299
  });
307
-
308
- expect(wsClient.subscribe).toHaveBeenCalled();
309
300
  });
310
301
  });
311
302
 
package/src/shared.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from "./auth/index.js";
2
+ export * from "./constants/index.js";
3
+ export * from "./crypto/index.js";
4
+ export { default as LightsparkException } from "./LightsparkException.js";
5
+ export { Logger, LoggingLevel, logger } from "./Logger.js";
6
+ export {
7
+ default as ServerEnvironment,
8
+ apiDomainForEnvironment,
9
+ } from "./ServerEnvironment.js";
10
+ export * from "./utils/index.js";
@@ -23,6 +23,7 @@ export const CurrencyUnit = {
23
23
  PHP: "PHP",
24
24
  EUR: "EUR",
25
25
  GBP: "GBP",
26
+ INR: "INR",
26
27
 
27
28
  Bitcoin: "BITCOIN",
28
29
  Microbitcoin: "MICROBITCOIN",
@@ -34,6 +35,7 @@ export const CurrencyUnit = {
34
35
  Mxn: "MXN",
35
36
  Php: "PHP",
36
37
  Gbp: "GBP",
38
+ Inr: "INR",
37
39
  } as const;
38
40
 
39
41
  export type CurrencyUnitType = (typeof CurrencyUnit)[keyof typeof CurrencyUnit];
@@ -64,6 +66,7 @@ const standardUnitConversionObj = {
64
66
  [CurrencyUnit.PHP]: (v: number) => v,
65
67
  [CurrencyUnit.EUR]: (v: number) => v,
66
68
  [CurrencyUnit.GBP]: (v: number) => v,
69
+ [CurrencyUnit.INR]: (v: number) => v,
67
70
  };
68
71
 
69
72
  /* Round without decimals since we're returning cents: */
@@ -93,6 +96,7 @@ const CONVERSION_MAP = {
93
96
  [CurrencyUnit.PHP]: toBitcoinConversion,
94
97
  [CurrencyUnit.EUR]: toBitcoinConversion,
95
98
  [CurrencyUnit.GBP]: toBitcoinConversion,
99
+ [CurrencyUnit.INR]: toBitcoinConversion,
96
100
  },
97
101
  [CurrencyUnit.MICROBITCOIN]: {
98
102
  [CurrencyUnit.BITCOIN]: (v: number) => v / 1_000_000,
@@ -106,6 +110,7 @@ const CONVERSION_MAP = {
106
110
  [CurrencyUnit.PHP]: toMicrobitcoinConversion,
107
111
  [CurrencyUnit.EUR]: toMicrobitcoinConversion,
108
112
  [CurrencyUnit.GBP]: toMicrobitcoinConversion,
113
+ [CurrencyUnit.INR]: toMicrobitcoinConversion,
109
114
  },
110
115
  [CurrencyUnit.MILLIBITCOIN]: {
111
116
  [CurrencyUnit.BITCOIN]: (v: number) => v / 1_000,
@@ -119,6 +124,7 @@ const CONVERSION_MAP = {
119
124
  [CurrencyUnit.PHP]: toMillibitcoinConversion,
120
125
  [CurrencyUnit.EUR]: toMillibitcoinConversion,
121
126
  [CurrencyUnit.GBP]: toMillibitcoinConversion,
127
+ [CurrencyUnit.INR]: toMillibitcoinConversion,
122
128
  },
123
129
  [CurrencyUnit.MILLISATOSHI]: {
124
130
  [CurrencyUnit.BITCOIN]: (v: number) => v / 100_000_000_000,
@@ -132,6 +138,7 @@ const CONVERSION_MAP = {
132
138
  [CurrencyUnit.PHP]: toMillisatoshiConversion,
133
139
  [CurrencyUnit.EUR]: toMillisatoshiConversion,
134
140
  [CurrencyUnit.GBP]: toMillisatoshiConversion,
141
+ [CurrencyUnit.INR]: toMillisatoshiConversion,
135
142
  },
136
143
  [CurrencyUnit.NANOBITCOIN]: {
137
144
  [CurrencyUnit.BITCOIN]: (v: number) => v / 1_000_000_000,
@@ -145,6 +152,7 @@ const CONVERSION_MAP = {
145
152
  [CurrencyUnit.PHP]: toNanobitcoinConversion,
146
153
  [CurrencyUnit.EUR]: toNanobitcoinConversion,
147
154
  [CurrencyUnit.GBP]: toNanobitcoinConversion,
155
+ [CurrencyUnit.INR]: toNanobitcoinConversion,
148
156
  },
149
157
  [CurrencyUnit.SATOSHI]: {
150
158
  [CurrencyUnit.BITCOIN]: (v: number) => v / 100_000_000,
@@ -158,12 +166,14 @@ const CONVERSION_MAP = {
158
166
  [CurrencyUnit.PHP]: toSatoshiConversion,
159
167
  [CurrencyUnit.EUR]: toSatoshiConversion,
160
168
  [CurrencyUnit.GBP]: toSatoshiConversion,
169
+ [CurrencyUnit.INR]: toSatoshiConversion,
161
170
  },
162
171
  [CurrencyUnit.USD]: standardUnitConversionObj,
163
172
  [CurrencyUnit.MXN]: standardUnitConversionObj,
164
173
  [CurrencyUnit.PHP]: standardUnitConversionObj,
165
174
  [CurrencyUnit.EUR]: standardUnitConversionObj,
166
175
  [CurrencyUnit.GBP]: standardUnitConversionObj,
176
+ [CurrencyUnit.INR]: standardUnitConversionObj,
167
177
  };
168
178
 
169
179
  export function convertCurrencyAmountValue(
@@ -230,6 +240,7 @@ export type CurrencyMap = {
230
240
  [CurrencyUnit.PHP]: number;
231
241
  [CurrencyUnit.EUR]: number;
232
242
  [CurrencyUnit.GBP]: number;
243
+ [CurrencyUnit.INR]: number;
233
244
  [CurrencyUnit.FUTURE_VALUE]: number;
234
245
  formatted: {
235
246
  sats: string;
@@ -246,6 +257,7 @@ export type CurrencyMap = {
246
257
  [CurrencyUnit.PHP]: string;
247
258
  [CurrencyUnit.EUR]: string;
248
259
  [CurrencyUnit.GBP]: string;
260
+ [CurrencyUnit.INR]: string;
249
261
  [CurrencyUnit.FUTURE_VALUE]: string;
250
262
  };
251
263
  isZero: boolean;
@@ -443,6 +455,7 @@ function convertCurrencyAmountValues(
443
455
  php: CurrencyUnit.PHP,
444
456
  eur: CurrencyUnit.EUR,
445
457
  gbp: CurrencyUnit.GBP,
458
+ inr: CurrencyUnit.INR,
446
459
  mibtc: CurrencyUnit.MICROBITCOIN,
447
460
  mlbtc: CurrencyUnit.MILLIBITCOIN,
448
461
  nbtc: CurrencyUnit.NANOBITCOIN,
@@ -492,7 +505,7 @@ export function mapCurrencyAmount(
492
505
  * preferred_currency_unit on CurrencyAmount types: */
493
506
  const conversionOverride = getPreferredConversionOverride(currencyAmountArg);
494
507
 
495
- const { sats, msats, btc, usd, mxn, php, mibtc, mlbtc, nbtc, eur, gbp } =
508
+ const { sats, msats, btc, usd, mxn, php, mibtc, mlbtc, nbtc, eur, gbp, inr } =
496
509
  convertCurrencyAmountValues(unit, value, unitsPerBtc, conversionOverride);
497
510
 
498
511
  const mapWithCurrencyUnits = {
@@ -504,6 +517,7 @@ export function mapCurrencyAmount(
504
517
  [CurrencyUnit.PHP]: php,
505
518
  [CurrencyUnit.EUR]: eur,
506
519
  [CurrencyUnit.GBP]: gbp,
520
+ [CurrencyUnit.INR]: inr,
507
521
  [CurrencyUnit.MICROBITCOIN]: mibtc,
508
522
  [CurrencyUnit.MILLIBITCOIN]: mlbtc,
509
523
  [CurrencyUnit.NANOBITCOIN]: nbtc,
@@ -553,6 +567,10 @@ export function mapCurrencyAmount(
553
567
  value: gbp,
554
568
  unit: CurrencyUnit.GBP,
555
569
  }),
570
+ [CurrencyUnit.INR]: formatCurrencyStr({
571
+ value: inr,
572
+ unit: CurrencyUnit.INR,
573
+ }),
556
574
  [CurrencyUnit.FUTURE_VALUE]: "-",
557
575
  },
558
576
  };
@@ -631,6 +649,8 @@ export const abbrCurrencyUnit = (unit: CurrencyUnitType) => {
631
649
  return "EUR";
632
650
  case CurrencyUnit.GBP:
633
651
  return "GBP";
652
+ case CurrencyUnit.INR:
653
+ return "INR";
634
654
  }
635
655
  return "Unsupported CurrencyUnit";
636
656
  };
@@ -825,3 +845,24 @@ export function localeToCurrencySymbol(locale: string) {
825
845
  const { symbol } = separateCurrencyStrParts(formatted);
826
846
  return symbol;
827
847
  }
848
+
849
+ /**
850
+ * Formats an amount from the smallest currency unit to a display string
851
+ * @param amount - The amount in the smallest currency unit (No decimals)
852
+ * @param currency - The currency object with code and decimals
853
+ * @returns Formatted string like "12.50 USD" or empty string if invalid
854
+ */
855
+ export function formatInviteAmount(
856
+ amount: number | null | undefined,
857
+ currency: UmaCurrency | null | undefined,
858
+ ): string {
859
+ if (!amount || !currency) {
860
+ return "";
861
+ }
862
+
863
+ const displayAmount = (amount / Math.pow(10, currency.decimals)).toFixed(
864
+ currency.decimals,
865
+ );
866
+
867
+ return `${displayAmount} ${currency.code}`;
868
+ }
@@ -1,3 +1,7 @@
1
+ declare global {
2
+ const Bare: unknown;
3
+ }
4
+
1
5
  export const isBrowser =
2
6
  typeof window !== "undefined" && typeof window.document !== "undefined";
3
7
 
@@ -7,3 +11,9 @@ export const isNode =
7
11
  process.versions.node != null;
8
12
 
9
13
  export const isTest = isNode && process.env.NODE_ENV === "test";
14
+
15
+ /* https://github.com/holepunchto/which-runtime/blob/main/index.js */
16
+ export const isBare = typeof Bare !== "undefined";
17
+
18
+ export const isReactNative =
19
+ typeof navigator !== "undefined" && navigator.product === "ReactNative";