@kalamba/sdk 0.4.1 → 0.6.0

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/dist/index.js CHANGED
@@ -2,26 +2,120 @@
2
2
  * BSD 3-Clause License
3
3
  * Copyright (c) 2024, Kalamba Games Limited
4
4
  */
5
- var b = Object.defineProperty;
6
- var M = (r, i, t) => i in r ? b(r, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[i] = t;
7
- var l = (r, i, t) => (M(r, typeof i != "symbol" ? i + "" : i, t), t), d = (r, i, t) => {
8
- if (!i.has(r))
5
+ var b = (n, i, t) => {
6
+ if (!i.has(n))
9
7
  throw TypeError("Cannot " + t);
10
8
  };
11
- var o = (r, i, t) => (d(r, i, "read from private field"), t ? t.call(r) : i.get(r)), F = (r, i, t) => {
12
- if (i.has(r))
9
+ var m = (n, i, t) => (b(n, i, "read from private field"), t ? t.call(n) : i.get(n)), e = (n, i, t) => {
10
+ if (i.has(n))
13
11
  throw TypeError("Cannot add the same private member more than once");
14
- i instanceof WeakSet ? i.add(r) : i.set(r, t);
15
- }, y = (r, i, t, m) => (d(r, i, "write to private field"), m ? m.call(r, t) : i.set(r, t), t);
16
- function A(r, i) {
17
- return r * i / 100;
12
+ i instanceof WeakSet ? i.add(n) : i.set(n, t);
13
+ }, u = (n, i, t, r) => (b(n, i, "write to private field"), r ? r.call(n, t) : i.set(n, t), t);
14
+ import { T as E, O as H, P as U } from "./errors-96eb18f7.js";
15
+ import { a as ti, b as ni } from "./errors-96eb18f7.js";
16
+ function O(n) {
17
+ const i = Object.prototype.toString.call(n);
18
+ return n instanceof Date || typeof n == "object" && i === "[object Date]" ? new n.constructor(+n) : typeof n == "number" || i === "[object Number]" || typeof n == "string" || i === "[object String]" ? new Date(n) : /* @__PURE__ */ new Date(NaN);
18
19
  }
19
- function w(r, i, t) {
20
+ function B(n) {
21
+ const i = O(n);
22
+ return i.setHours(23, 59, 59, 999), i;
23
+ }
24
+ function P() {
25
+ return B(Date.now());
26
+ }
27
+ function k(n) {
28
+ const i = O(n);
29
+ return i.setHours(0, 0, 0, 0), i;
30
+ }
31
+ function L() {
32
+ return k(Date.now());
33
+ }
34
+ const $ = 3e4;
35
+ var D, x;
36
+ class G {
37
+ constructor(i) {
38
+ e(this, D, void 0);
39
+ e(this, x, $);
40
+ u(this, D, i);
41
+ }
42
+ buildHeaders() {
43
+ const i = {};
44
+ return m(this, D).token && (i.Authorization = `Bearer ${m(this, D).token}`), i;
45
+ }
46
+ async request(i, t) {
47
+ const r = new URL(i, m(this, D).apiUrl ?? window.location.origin);
48
+ return r.search = new URLSearchParams({
49
+ ...t
50
+ }).toString(), await (await fetch(r, {
51
+ signal: AbortSignal.timeout(m(this, x)),
52
+ headers: this.buildHeaders()
53
+ })).json();
54
+ }
55
+ async getRounds(i) {
56
+ return this.request("/rounds", i);
57
+ }
58
+ async getRound(i, t) {
59
+ return this.request(`/rounds/${i}`, t);
60
+ }
61
+ }
62
+ D = new WeakMap(), x = new WeakMap();
63
+ const R = {
64
+ limit: 100,
65
+ page: 0
66
+ };
67
+ var a, f;
68
+ class _ {
69
+ constructor({
70
+ config: i
71
+ }) {
72
+ e(this, a, void 0);
73
+ e(this, f, void 0);
74
+ u(this, a, i), u(this, f, new G({
75
+ apiUrl: i.api.gameHistoryUrl
76
+ }));
77
+ }
78
+ async getRound(i, {
79
+ pagination: t = R
80
+ } = {}) {
81
+ return m(this, f).getRound(i, {
82
+ gameCode: m(this, a).api.game,
83
+ operatorCode: m(this, a).api.brand,
84
+ username: m(this, a).api.user,
85
+ ...t
86
+ });
87
+ }
88
+ async getLastRound() {
89
+ return m(this, a).api.lastRoundId ? this.getRound(m(this, a).api.lastRoundId) : null;
90
+ }
91
+ async getRounds({
92
+ searchPeriod: i = {
93
+ fromDateTime: L(),
94
+ toDateTime: P()
95
+ },
96
+ pagination: t = R
97
+ } = {}) {
98
+ const r = m(this, a).api.user.replace(/^user_/, "").replace(new RegExp(`_${m(this, a).api.integration}$`), "");
99
+ return m(this, f).getRounds({
100
+ gameCode: m(this, a).api.game,
101
+ operatorCode: m(this, a).api.integration,
102
+ username: r,
103
+ fromDataTime: i.fromDateTime.toISOString(),
104
+ toDateTime: i.toDateTime.toISOString(),
105
+ ...t
106
+ });
107
+ }
108
+ }
109
+ a = new WeakMap(), f = new WeakMap();
110
+ function j(n, i) {
111
+ return n * i / 100;
112
+ }
113
+ function I(n, i, t) {
20
114
  if (!["string", "number"].includes(typeof i))
21
115
  return "NaN";
22
- const { locales: m } = r;
116
+ const { locales: r } = n;
23
117
  try {
24
- return new Intl.NumberFormat(m, {
118
+ return new Intl.NumberFormat(r, {
25
119
  ...t,
26
120
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
27
121
  // @ts-ignore
@@ -32,9 +126,9 @@ function w(r, i, t) {
32
126
  return `${i}`;
33
127
  }
34
128
  }
35
- const x = "XTS";
36
- var u = /* @__PURE__ */ ((r) => (r.Currency = "Currency", r.Number = "Number", r))(u || {}), c = /* @__PURE__ */ ((r) => (r.Coins = "Coins", r.Money = "Money", r))(c || {});
37
- const h = {
129
+ const w = "XTS";
130
+ var F = /* @__PURE__ */ ((n) => (n.Currency = "Currency", n.Number = "Number", n))(F || {}), y = /* @__PURE__ */ ((n) => (n.Coins = "Coins", n.Money = "Money", n))(y || {});
131
+ const S = {
38
132
  /* eslint-disable sort-keys-fix/sort-keys-fix */
39
133
  // ARSB is the internal currency relevant for invoicing purposes, players should see it as ARS
40
134
  ARSB: { displayAs: "ARS", maximumFractionDigits: 2, minimumFractionDigits: 2 },
@@ -128,146 +222,135 @@ const h = {
128
222
  FTN: { maximumFractionDigits: 4, minimumFractionDigits: 4 }
129
223
  /* eslint-enable sort-keys-fix/sort-keys-fix */
130
224
  };
131
- function T(r) {
132
- return r in h;
225
+ function A(n) {
226
+ return n in S;
133
227
  }
134
- function O(r) {
135
- return T(r) ? {
136
- ...h[r],
137
- currency: x
228
+ function q(n) {
229
+ return A(n) ? {
230
+ ...S[n],
231
+ currency: w
138
232
  } : {
139
- currency: r
233
+ currency: n
140
234
  };
141
235
  }
142
- function B(r, i, t) {
143
- const m = i.format(+r);
144
- return T(t) ? m.replace(x, h[t].displayAs ?? t) : m;
236
+ function V(n, i, t) {
237
+ const r = i.format(+n);
238
+ return A(t) ? r.replace(w, S[t].displayAs ?? t) : r;
145
239
  }
146
- function P(r, i, t) {
147
- const { maximumFractionDigits: m, minimumFractionDigits: n } = t.resolvedOptions();
148
- return w(r, i, { maximumFractionDigits: m, minimumFractionDigits: n });
240
+ function X(n, i, t) {
241
+ const { maximumFractionDigits: r, minimumFractionDigits: o } = t.resolvedOptions();
242
+ return I(n, i, { maximumFractionDigits: r, minimumFractionDigits: o });
149
243
  }
150
- function I(r, i, { as: t } = {
244
+ function v(n, i, { as: t } = {
151
245
  as: "Currency"
152
246
  /* Currency */
153
247
  }) {
154
- const { canShowCurrency: m, currency: n } = r;
155
- return t === "Number" || !m ? `${i}` : `${i} ${n}`;
248
+ const { canShowCurrency: r, currency: o } = n;
249
+ return t === "Number" || !r ? `${i}` : `${i} ${o}`;
156
250
  }
157
- function R(r, i, { as: t, from: m } = {
251
+ function z(n, i, { as: t, from: r } = {
158
252
  as: "Currency",
159
253
  from: "Money"
160
254
  /* Money */
161
- }, n) {
255
+ }, o) {
162
256
  if (!["string", "number"].includes(typeof i))
163
257
  return "NaN";
164
- const { canShowCurrency: s = !0, coinValueInCents: a, currency: f, locales: p } = r;
165
- m === "Coins" && (i = A(+i, a));
258
+ const { canShowCurrency: s = !0, coinValueInCents: g, currency: C, locales: T } = n;
259
+ r === "Coins" && (i = j(+i, g));
166
260
  try {
167
- const N = O(f), S = new Intl.NumberFormat(p, {
168
- ...N,
169
- ...n,
261
+ const M = q(C), N = new Intl.NumberFormat(T, {
262
+ ...M,
263
+ ...o,
170
264
  currencyDisplay: "symbol",
171
265
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
172
266
  // @ts-ignore
173
267
  numberingSystem: "latn",
174
268
  style: "currency"
175
269
  });
176
- return t === "Number" || !s ? P(r, i, S) : B(i, S, f);
270
+ return t === "Number" || !s ? X(n, i, N) : V(i, N, C);
177
271
  } catch {
178
- return I(r, i, { as: t });
272
+ return v(n, i, { as: t });
179
273
  }
180
274
  }
181
- var e;
182
- class k {
275
+ var c;
276
+ class K {
183
277
  constructor({ config: i }) {
184
- F(this, e, void 0);
185
- y(this, e, i);
278
+ e(this, c, void 0);
279
+ u(this, c, i);
186
280
  }
187
281
  formatNumber(i, t) {
188
- const m = { locales: [o(this, e).ui.language] };
189
- return w(m, i, t);
282
+ const r = { locales: [m(this, c).ui.language] };
283
+ return I(r, i, t);
190
284
  }
191
- formatCurrency(i, t = { as: u.Currency, from: c.Money }, m) {
192
- const n = o(this, e).ui.feature.showCurrency, s = o(this, e).api.coinValueInCents, a = o(this, e).api.currency || x, f = [o(this, e).ui.language];
193
- return R({
194
- canShowCurrency: n,
285
+ formatCurrency(i, t = { as: F.Currency, from: y.Money }, r) {
286
+ const o = m(this, c).ui.feature.showCurrency, s = m(this, c).api.coinValueInCents, g = m(this, c).api.currency || w, C = [m(this, c).ui.language];
287
+ return z({
288
+ canShowCurrency: o,
195
289
  coinValueInCents: s,
196
- currency: a,
197
- locales: f
198
- }, i, t, m);
290
+ currency: g,
291
+ locales: C
292
+ }, i, t, r);
199
293
  }
200
294
  formatCoins(i, t) {
201
- return this.formatCurrency(i, { as: u.Number, from: c.Coins }, t);
295
+ return this.formatCurrency(i, { as: F.Number, from: y.Coins }, t);
202
296
  }
203
297
  formatCoinsWithCurrency(i, t) {
204
- return this.formatCurrency(i, { as: u.Currency, from: c.Coins }, t);
298
+ return this.formatCurrency(i, { as: F.Currency, from: y.Coins }, t);
205
299
  }
206
300
  formatMoney(i, t) {
207
- return this.formatCurrency(i, { as: u.Number, from: c.Money }, t);
301
+ return this.formatCurrency(i, { as: F.Number, from: y.Money }, t);
208
302
  }
209
303
  formatMoneyWithCurrency(i, t) {
210
- return this.formatCurrency(i, { as: u.Currency, from: c.Money }, t);
304
+ return this.formatCurrency(i, { as: F.Currency, from: y.Money }, t);
211
305
  }
212
306
  }
213
- e = new WeakMap();
214
- const G = () => {
307
+ c = new WeakMap();
308
+ const W = () => {
215
309
  };
216
- class H extends Error {
217
- constructor(t) {
218
- super("OpenGameError");
219
- l(this, "error");
220
- this.name = "OpenGameError", this.error = t;
221
- }
222
- }
223
- class U extends Error {
224
- constructor(t) {
225
- super("PlayError");
226
- l(this, "error");
227
- this.name = "PlayError", this.error = t;
228
- }
229
- }
230
- class E extends Error {
231
- constructor() {
232
- super("TimeoutError"), this.name = "TimeoutError";
233
- }
234
- }
235
- var g, D, C;
236
- class L {
310
+ var l, h, p, d;
311
+ class Q {
237
312
  constructor({ messagePort: i }) {
238
- F(this, g, void 0);
239
- F(this, D, void 0);
240
- F(this, C, void 0);
241
- y(this, C, i), this.on("configured", (t) => {
242
- y(this, g, t), y(this, D, new k({ config: t }));
313
+ e(this, l, void 0);
314
+ e(this, h, void 0);
315
+ e(this, p, void 0);
316
+ e(this, d, void 0);
317
+ u(this, d, i), this.on("configured", (t) => {
318
+ u(this, l, t), u(this, p, new K({ config: t })), u(this, h, new _({ config: t }));
319
+ }), this.on("playResponse", (t) => {
320
+ this.config.api.lastRoundId = t.contract.roundId;
243
321
  });
244
322
  }
245
323
  get config() {
246
- if (!o(this, g))
324
+ if (!m(this, l))
325
+ throw new Error("config is not initialized, you must open game first");
326
+ return m(this, l);
327
+ }
328
+ get history() {
329
+ if (!m(this, h))
247
330
  throw new Error("config is not initialized, you must open game first");
248
- return o(this, g);
331
+ return m(this, h);
249
332
  }
250
333
  get i18n() {
251
- if (!o(this, D))
334
+ if (!m(this, p))
252
335
  throw new Error("i18n is not initialized, you must open game first");
253
- return o(this, D);
336
+ return m(this, p);
254
337
  }
255
338
  request(i, ...[t]) {
256
- return new Promise((m) => {
257
- this.on(`${i}Response`, (n) => m({ type: "response", response: n }), {
339
+ return new Promise((r) => {
340
+ this.on(`${i}Response`, (o) => r({ type: "response", response: o }), {
258
341
  once: !0
259
- }), this.on(`${i}Error`, (n) => m({ type: "error", error: n }), { once: !0 }), this.send(i, t);
342
+ }), this.on(`${i}Error`, (o) => r({ type: "error", error: o }), { once: !0 }), this.send(i, t);
260
343
  });
261
344
  }
262
- on(i, t, m) {
263
- const n = function s(a) {
264
- a.data.message === `kalamba:wrapper:${i}` && (G("on", a.data.message.replace(/^kalamba:/, ""), a.data.payload), t(a.data.payload), m != null && m.once && window.removeEventListener("message", s));
345
+ on(i, t, r) {
346
+ const o = function s(g) {
347
+ g.data.message === `kalamba:wrapper:${i}` && (W("on", g.data.message.replace(/^kalamba:/, ""), g.data.payload), t(g.data.payload), r != null && r.once && window.removeEventListener("message", s));
265
348
  };
266
- window.addEventListener("message", n);
349
+ window.addEventListener("message", o);
267
350
  }
268
351
  // TODO error messages
269
352
  send(i, ...[t]) {
270
- o(this, C).postMessage(
353
+ m(this, d).postMessage(
271
354
  {
272
355
  message: `kalamba:sdk:${i}`,
273
356
  payload: t
@@ -276,29 +359,30 @@ class L {
276
359
  );
277
360
  }
278
361
  async openGame(i) {
279
- var m;
280
362
  const [, t] = await Promise.all([
281
363
  // TODO: return promise from call to this.on if listener is not provided
282
- new Promise((n) => this.on("configured", n, { once: !0 })),
364
+ new Promise((r) => this.on("configured", r, { once: !0 })),
283
365
  this.request("openGame", i)
284
366
  ]);
285
367
  if (t.type === "response")
286
368
  return t.response;
287
- throw ((m = t.error) == null ? void 0 : m.type) === "timeout" ? new E() : new H(t.error);
369
+ throw t.error.type === "timeout" ? new E() : new H(t.error.data);
288
370
  }
289
- async play(i, t, m = "SPIN") {
290
- var a;
291
- const n = { contract: i, extra: t, actionType: m }, s = await this.request("play", n);
371
+ async play(i, t, r = "SPIN") {
372
+ const o = { contract: i, extra: t, actionType: r }, s = await this.request("play", o);
292
373
  if (s.type === "response")
293
374
  return s.response;
294
- throw ((a = s.error) == null ? void 0 : a.type) === "timeout" ? new E() : new U(s.error);
375
+ throw s.error.type === "timeout" ? new E() : new U(s.error.data);
295
376
  }
296
377
  }
297
- g = new WeakMap(), D = new WeakMap(), C = new WeakMap();
378
+ l = new WeakMap(), h = new WeakMap(), p = new WeakMap(), d = new WeakMap();
298
379
  export {
299
- L as KalambaSdk,
380
+ _ as History,
381
+ K as I18n,
382
+ Q as KalambaSdk,
300
383
  H as OpenGameError,
384
+ ti as OpenGameErrorCode,
301
385
  U as PlayError,
302
- E as TimeoutError,
303
- k as i18n
386
+ ni as PlayErrorCode,
387
+ E as TimeoutError
304
388
  };