@cuenca-mx/cuenca-js 0.0.1-dev.2 → 0.0.1-dev.22

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,12 +1,23 @@
1
1
  import axios from 'axios';
2
- import { CuencaResponseException, CuencaException, NoResultFound, MultipleResultsFound, InvalidPassword } from './errors.js';
3
- export { CuencaException, CuencaResponseException, InvalidPassword, MalformedJwtToken, MultipleResultsFound, NoResultFound, ValidationError } from './errors.js';
4
- import { Jwt } from './jwt.js';
5
- export { Jwt } from './jwt.js';
6
- import { P as Phase, F as FileFormat, E as EntryType, T as TransactionStatus, C as CardIssuer, a as CardStatus, b as CardType, c as CardFundingType, d as CardErrorType, e as CardTransactionType, f as CommissionType, D as DepositNetwork, S as SavingCategory, g as ServiceProviderCategory, h as TransferNetwork, W as WalletTransactionType, A as AccountQuery, i as ApiKeyQuery, B as BalanceEntryQuery, j as BillPaymentQuery, k as CardsQuery, l as CardTransactionQuery, Q as QueryParams, m as DepositQuery, n as WalletQuery, o as StatementQuery, p as TransferQuery, q as WalletTransactionQuery } from './queries-716c6be4.js';
7
- export { A as AccountQuery, i as ApiKeyQuery, B as BalanceEntryQuery, j as BillPaymentQuery, d as CardErrorType, c as CardFundingType, C as CardIssuer, a as CardStatus, l as CardTransactionQuery, e as CardTransactionType, b as CardType, k as CardsQuery, f as CommissionType, D as DepositNetwork, m as DepositQuery, E as EntryType, F as FileFormat, s as PageSize, P as Phase, Q as QueryParams, S as SavingCategory, g as ServiceProviderCategory, o as StatementQuery, r as TrackDataMethod, T as TransactionStatus, h as TransferNetwork, p as TransferQuery, n as WalletQuery, q as WalletTransactionQuery, W as WalletTransactionType } from './queries-716c6be4.js';
8
- import { d as dateToUTC, e as enumValueFromString } from './data-7d3d5fcc.js';
9
- import { A as ApiKeyUpdateRequest, a as ArpcRequest, C as CardActivationRequest, b as CardRequest, c as CardUpdateRequest, d as CardValidationRequest, S as SavingRequest, T as TransferRequest, U as UserCredentialRequest, e as UserCredentialUpdateRequest, f as UserLoginRequest, W as WalletTransactionRequest } from './walletTransactionRequest-18aad4dc.js';
2
+ import Buffer from 'buffer';
3
+ import { CuencaResponseException, CuencaException, NoResultFound, MultipleResultsFound, InvalidPassword } from './errors/index.mjs';
4
+ import { Jwt } from './jwt/index.mjs';
5
+ import { P as Phase, F as FileFormat, d as dateToUTC, e as enumValueFromString, E as EntryType, T as TransactionStatus, C as CardIssuer, a as CardStatus, b as CardType, c as CardFundingType, f as CardErrorType, g as CardTransactionType, h as CommissionType, D as DepositNetwork, S as SavingCategory, i as ServiceProviderCategory, j as TransferNetwork, k as TOSAgreements, W as WalletTransactionType } from './identities-7fc7251d.mjs';
6
+ import { A as AccountQuery, a as ApiKeyQuery, B as BalanceEntryQuery, b as BillPaymentQuery, C as CardsQuery, c as CardTransactionQuery, Q as QueryParams, D as DepositQuery, W as WalletQuery, S as StatementQuery, T as TransferQuery, d as WalletTransactionQuery } from './queries-59c893b6.mjs';
7
+ import { A as ApiKeyUpdateRequest, a as ArpcRequest, C as CardActivationRequest, b as CardRequest, c as CardUpdateRequest, d as CardValidationRequest, S as SavingRequest, T as TransferRequest, U as UserCredentialRequest, e as UserCredentialUpdateRequest, f as UserLoginRequest, g as UserUpdateRequest, W as WalletTransactionRequest } from './walletTransactionRequest-7334f8c5.mjs';
8
+
9
+ /* global window */
10
+
11
+ const isBrowser =
12
+ typeof window !== 'undefined' && typeof window.document !== 'undefined';
13
+
14
+ const isNode =
15
+ typeof process !== 'undefined' &&
16
+ Object.prototype.toString.call(process) === '[object process]';
17
+
18
+ const runtimeEnv = { isBrowser, isNode };
19
+
20
+ const name="@cuenca-mx/cuenca-js";const version="0.0.1-dev.22";const description="Cuenca client for JS";const main="./build/index.cjs";const module="./build/index.mjs";const browser="./build/umd/cuenca.umd.js";const files=["build/**/*"];const exports={".":{"import":"./build/index.mjs",require:"./build/index.cjs"},"./errors":{"import":"./build/errors/index.mjs",require:"./build/errors/index.cjs"},"./jwt":{"import":"./build/jwt/index.mjs",require:"./build/jwt/index.cjs"},"./requests":{"import":"./build/requests/index.mjs",require:"./build/requests/index.cjs"},"./types":{"import":"./build/types/index.mjs",require:"./build/types/index.cjs"}};const packageManager="yarn@3.0.2";const type="module";const repository={type:"git",url:"https://github.com/cuenca-mx/cuenca-js.git",directory:"packages/cuenca-js"};const keywords=["cuenca"];const license="MIT";const bugs={url:"https://github.com/cuenca-mx/cuenca-js/issues"};const homepage="https://cuenca.com";const scripts={build:"rm -rf build/ && yarn rollup --config",test:"yarn node --experimental-vm-modules $(yarn bin jest)",publish:"yarn build && yarn npm publish"};const devDependencies={"@rollup/plugin-json":"^4.1.0","@rollup/plugin-node-resolve":"^13.1.1",jest:"^27.4.5",rollup:"^2.61.1","rollup-plugin-terser":"^7.0.2"};const dependencies={axios:"^0.24.0",buffer:"^6.0.3"};var pkg = {name:name,version:version,description:description,main:main,module:module,browser:browser,files:files,exports:exports,packageManager:packageManager,type:type,repository:repository,keywords:keywords,license:license,bugs:bugs,homepage:homepage,scripts:scripts,devDependencies:devDependencies,dependencies:dependencies};
10
21
 
11
22
  class Client {
12
23
  constructor({ apiKey, apiSecret, phase = Phase.Sandbox } = {}) {
@@ -27,8 +38,9 @@ class Client {
27
38
  get authHeader() {
28
39
  const { apiKey, apiSecret } = this.basicAuth;
29
40
  if (!apiKey || !apiSecret) return '';
30
- return `Basic ${Buffer.from(
31
- Buffer.from(`${apiKey}:${apiSecret}`).toString('utf-8'),
41
+ return `Basic ${Buffer.Buffer.from(
42
+ `${apiKey}:${apiSecret}`,
43
+ 'utf-8',
32
44
  ).toString('base64')}`;
33
45
  }
34
46
 
@@ -114,11 +126,18 @@ class Client {
114
126
  }) {
115
127
  const headers = {
116
128
  Authorization: this.authHeader,
117
- 'X-User-Agent': `cuenca-js/0.0.1`, // TODO: Change for client version
118
129
  'Content-Type': 'application/json',
119
130
  Accept: `application/${format.value}`,
120
131
  };
121
132
 
133
+ if (runtimeEnv.isNode) {
134
+ headers['User-Agent'] = `cuenca-js/${pkg.version}`;
135
+ } else if (runtimeEnv.isBrowser) {
136
+ // Cannot set User-Agent header on browsers
137
+ // https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name
138
+ headers['X-User-Agent'] = `cuenca-js/${pkg.version}`;
139
+ }
140
+
122
141
  if (this.jwtToken) {
123
142
  if (this.jwtToken.isExpired) {
124
143
  this.jwtToken = await Jwt.create(this);
@@ -293,7 +312,13 @@ class BillPayment extends Transaction {
293
312
  status,
294
313
  userId,
295
314
  }) {
296
- super({ amount, createdAt, descriptor, status, userId });
315
+ super({
316
+ amount,
317
+ createdAt,
318
+ descriptor,
319
+ status,
320
+ userId,
321
+ });
297
322
  this.accountNumber = accountNumber;
298
323
  this.id = id;
299
324
  this.providerUri = providerUri;
@@ -407,7 +432,13 @@ class CardTransaction extends Transaction {
407
432
  type,
408
433
  userId,
409
434
  }) {
410
- super({ amount, createdAt, descriptor, status, userId });
435
+ super({
436
+ amount,
437
+ createdAt,
438
+ descriptor,
439
+ status,
440
+ userId,
441
+ });
411
442
  this.cardErrorType = enumValueFromString(CardErrorType, cardErrorType);
412
443
  this.cardLastFour = cardLastFour;
413
444
  this.cardType = enumValueFromString(CardType, cardType);
@@ -504,7 +535,13 @@ class Commission extends Transaction {
504
535
  type,
505
536
  userId,
506
537
  }) {
507
- super({ amount, createdAt, descriptor, status, userId });
538
+ super({
539
+ amount,
540
+ createdAt,
541
+ descriptor,
542
+ status,
543
+ userId,
544
+ });
508
545
  this.relatedTransactionUri = relatedTransactionUri;
509
546
  this.type = enumValueFromString(CommissionType, type);
510
547
  }
@@ -533,7 +570,13 @@ class Deposit extends Transaction {
533
570
  trackingKey,
534
571
  userId,
535
572
  }) {
536
- super({ amount, createdAt, descriptor, status, userId });
573
+ super({
574
+ amount,
575
+ createdAt,
576
+ descriptor,
577
+ status,
578
+ userId,
579
+ });
537
580
  this.id = id;
538
581
  this.network = enumValueFromString(DepositNetwork, network);
539
582
  this.sourceUri = sourceUri;
@@ -586,7 +629,14 @@ class Saving extends Wallet {
586
629
  userId,
587
630
  updatedAt,
588
631
  }) {
589
- super({ balance, createdAt, deactivatedAt, id, userId, updatedAt });
632
+ super({
633
+ balance,
634
+ createdAt,
635
+ deactivatedAt,
636
+ id,
637
+ userId,
638
+ updatedAt,
639
+ });
590
640
  this.category = enumValueFromString(SavingCategory, category);
591
641
  this.goalAmount = goalAmount;
592
642
  this.goalDate = dateToUTC(goalDate);
@@ -665,7 +715,13 @@ class Transfer extends Transaction {
665
715
  updatedAt,
666
716
  userId,
667
717
  }) {
668
- super({ amount, createdAt, descriptor, status, userId });
718
+ super({
719
+ amount,
720
+ createdAt,
721
+ descriptor,
722
+ status,
723
+ userId,
724
+ });
669
725
  this.accountNumber = accountNumber;
670
726
  this.destinationUri = destinationUri;
671
727
  this.id = id;
@@ -694,6 +750,17 @@ class Transfer extends Transaction {
694
750
  });
695
751
  }
696
752
 
753
+ class User {
754
+ constructor({ termsOfService }) {
755
+ this.termsOfService = TOSAgreements.fromObject(termsOfService);
756
+ }
757
+
758
+ static fromObject = ({ ...obj }) =>
759
+ new User({
760
+ termsOfService: obj.terms_of_service,
761
+ });
762
+ }
763
+
697
764
  class UserCredential {
698
765
  constructor({ createdAt, id, isActive, updatedAt }) {
699
766
  this.createdAt = dateToUTC(createdAt);
@@ -737,7 +804,13 @@ class WalletTransaction extends Transaction {
737
804
  userId,
738
805
  walletUri,
739
806
  }) {
740
- super({ amount, createdAt, descriptor, status, userId });
807
+ super({
808
+ amount,
809
+ createdAt,
810
+ descriptor,
811
+ status,
812
+ userId,
813
+ });
741
814
  this.id = id;
742
815
  this.transactionType = enumValueFromString(
743
816
  WalletTransactionType,
@@ -776,7 +849,13 @@ class WhatsAppTransfer extends Transaction {
776
849
  updatedAt,
777
850
  userId,
778
851
  }) {
779
- super({ amount, createdAt, descriptor, status, userId });
852
+ super({
853
+ amount,
854
+ createdAt,
855
+ descriptor,
856
+ status,
857
+ userId,
858
+ });
780
859
  this.claimUrl = claimUrl;
781
860
  this.destinationUri = destinationUri;
782
861
  this.id = id;
@@ -837,6 +916,7 @@ const getModelFromPath = (path, obj) => {
837
916
  service_providers: () => ServiceProvider.fromObject(obj),
838
917
  statements: () => Statement.fromObject(obj),
839
918
  transfers: () => Transfer.fromObject(obj),
919
+ users: () => User.fromObject(obj),
840
920
  user_credentials: () => UserCredential.fromObject(obj),
841
921
  user_logins: () => UserLogin.fromObject(obj),
842
922
  wallet_transactions: () => WalletTransaction.fromObject(obj),
@@ -1398,6 +1478,18 @@ class UserLoginResource extends mix(Resource).with(Creatable, Deactivable) {
1398
1478
  }
1399
1479
  }
1400
1480
 
1481
+ class UserResourse extends mix(Resource).with(Updateable) {
1482
+ constructor(client) {
1483
+ super('user', Object, client);
1484
+ }
1485
+
1486
+ async update({ termsOfService, userId = 'me' }) {
1487
+ const request = new UserUpdateRequest({ termsOfService });
1488
+ const user = await this._update(userId, request.toCleanObject());
1489
+ return user;
1490
+ }
1491
+ }
1492
+
1401
1493
  class WalletTransactionsResource extends mix(Resource).with(
1402
1494
  Creatable,
1403
1495
  Queryable,
@@ -2,7 +2,12 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var errors = require('./errors.js');
5
+ var Buffer = require('buffer');
6
+ var errors_index = require('../errors/index.cjs');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ var Buffer__default = /*#__PURE__*/_interopDefaultLegacy(Buffer);
6
11
 
7
12
  class Jwt {
8
13
  constructor(expiresAt, token) {
@@ -20,10 +25,10 @@ class Jwt {
20
25
  try {
21
26
  const [, payloadEncoded] = token.split('.');
22
27
  payload = JSON.parse(
23
- Buffer.from(`${payloadEncoded}==`, 'base64').toString(),
28
+ Buffer__default["default"].Buffer.from(`${payloadEncoded}==`, 'base64').toString(),
24
29
  );
25
30
  } catch (error) {
26
- throw new errors.MalformedJwtToken();
31
+ throw new errors_index.MalformedJwtToken();
27
32
  }
28
33
 
29
34
  const { exp } = payload;
@@ -1,4 +1,5 @@
1
- import { MalformedJwtToken } from './errors.js';
1
+ import Buffer from 'buffer';
2
+ import { MalformedJwtToken } from '../errors/index.mjs';
2
3
 
3
4
  class Jwt {
4
5
  constructor(expiresAt, token) {
@@ -16,7 +17,7 @@ class Jwt {
16
17
  try {
17
18
  const [, payloadEncoded] = token.split('.');
18
19
  payload = JSON.parse(
19
- Buffer.from(`${payloadEncoded}==`, 'base64').toString(),
20
+ Buffer.Buffer.from(`${payloadEncoded}==`, 'base64').toString(),
20
21
  );
21
22
  } catch (error) {
22
23
  throw new MalformedJwtToken();
@@ -0,0 +1,282 @@
1
+ import { ValidationError } from './errors/index.mjs';
2
+ import { d as dateToUTC } from './identities-7fc7251d.mjs';
3
+
4
+ class PageSize {
5
+ constructor(size) {
6
+ this.maxSize = 100;
7
+ this._ps = size;
8
+ }
9
+
10
+ get size() {
11
+ return this._pageSize;
12
+ }
13
+
14
+ set _ps(value) {
15
+ let validatedPageSize = this.maxSize;
16
+ if (value && value > 0 && value <= this.maxSize) {
17
+ validatedPageSize = value;
18
+ }
19
+ this._pageSize = validatedPageSize;
20
+ }
21
+ }
22
+
23
+ class QueryParams {
24
+ constructor({
25
+ createdAfter,
26
+ createdBefore,
27
+ limit,
28
+ pageSize,
29
+ relatedTransaction,
30
+ userId,
31
+ count = false,
32
+ }) {
33
+ this.createdAfter = createdAfter;
34
+ this.createdBefore = createdBefore;
35
+ this._lmt = limit;
36
+ this.relatedTransaction = relatedTransaction;
37
+ this.userId = userId;
38
+ this.count = count;
39
+ this.pageSize = pageSize;
40
+ }
41
+
42
+ get limit() {
43
+ return this._limit;
44
+ }
45
+
46
+ // Validator for limit
47
+ set _lmt(value) {
48
+ let validatedValue = null;
49
+ if (value && value >= 0) {
50
+ validatedValue = value;
51
+ }
52
+ this._limit = validatedValue;
53
+ }
54
+
55
+ toObject() {
56
+ return {
57
+ created_after: this.createdAfter,
58
+ created_before: this.createdBefore,
59
+ limit: this.limit,
60
+ related_transaction: this.relatedTransaction,
61
+ user_id: this.userId,
62
+ page_size: this.pageSize && this.pageSize.size,
63
+ };
64
+ }
65
+
66
+ toParams() {
67
+ const helper = { ...this.toObject() };
68
+ if (this.count) helper.count = 1;
69
+ Object.keys(helper).forEach((k) => {
70
+ if (helper[k] == null) delete helper[k];
71
+ });
72
+ return helper;
73
+ }
74
+
75
+ toQueryString() {
76
+ return new URLSearchParams(this.toParams()).toString();
77
+ }
78
+ }
79
+
80
+ class AccountQuery extends QueryParams {
81
+ constructor({ accountNumber, ...args }) {
82
+ super(args);
83
+ this.accountNumber = accountNumber;
84
+ }
85
+
86
+ toObject() {
87
+ return Object.assign(super.toObject(), {
88
+ account_number: this.accountNumber,
89
+ });
90
+ }
91
+ }
92
+
93
+ class ApiKeyQuery extends QueryParams {
94
+ constructor({ active, ...args }) {
95
+ super(args);
96
+ this.active = active;
97
+ }
98
+
99
+ toObject() {
100
+ return Object.assign(super.toObject(), {
101
+ active: this.active,
102
+ });
103
+ }
104
+ }
105
+
106
+ class TransactionQuery extends QueryParams {
107
+ constructor({ status, ...args }) {
108
+ super(args);
109
+ this.status = status;
110
+ }
111
+
112
+ toObject() {
113
+ return Object.assign(super.toObject(), {
114
+ status: this.status,
115
+ });
116
+ }
117
+ }
118
+
119
+ class DepositQuery extends TransactionQuery {
120
+ constructor({ trackingKey, network, ...args }) {
121
+ super(args);
122
+ this.trackingKey = trackingKey;
123
+ this.network = network;
124
+ }
125
+
126
+ toObject() {
127
+ return Object.assign(super.toObject(), {
128
+ tracking_key: this.trackingKey,
129
+ network: this.network,
130
+ });
131
+ }
132
+ }
133
+
134
+ class TransferQuery extends TransactionQuery {
135
+ constructor({
136
+ accountNumber,
137
+ idempotencyKey,
138
+ trackingKey,
139
+ network,
140
+ ...args
141
+ }) {
142
+ super(args);
143
+ this.accountNumber = accountNumber;
144
+ this.idempotencyKey = idempotencyKey;
145
+ this.trackingKey = trackingKey;
146
+ this.network = network;
147
+ }
148
+
149
+ toObject() {
150
+ return Object.assign(super.toObject(), {
151
+ account_number: this.accountNumber,
152
+ idempotency_key: this.idempotencyKey,
153
+ tracking_key: this.trackingKey,
154
+ network: this.network,
155
+ });
156
+ }
157
+ }
158
+
159
+ class BalanceEntryQuery extends QueryParams {
160
+ constructor({
161
+ fundingInstrumentUri,
162
+ count = false,
163
+ walletId = 'default',
164
+ ...args
165
+ }) {
166
+ super(args);
167
+ this.fundingInstrumentUri = fundingInstrumentUri;
168
+ this.count = count;
169
+ this.walletId = walletId;
170
+ }
171
+
172
+ toObject() {
173
+ return Object.assign(super.toObject(), {
174
+ wallet_id: this.walletId,
175
+ funding_instrument_uri: this.fundingInstrumentUri,
176
+ count: this.count,
177
+ });
178
+ }
179
+ }
180
+
181
+ class BillPaymentQuery extends QueryParams {
182
+ constructor({ accountNumber, ...args }) {
183
+ super(args);
184
+ this.accountNumber = accountNumber;
185
+ }
186
+
187
+ toObject() {
188
+ return Object.assign(super.toObject(), {
189
+ account_number: this.accountNumber,
190
+ });
191
+ }
192
+ }
193
+
194
+ class CardTransactionQuery extends QueryParams {
195
+ constructor({ cardUri, ...args }) {
196
+ super(args);
197
+ this.cardUri = cardUri;
198
+ }
199
+
200
+ toObject() {
201
+ return Object.assign(super.toObject(), {
202
+ card_uri: this.cardUri,
203
+ });
204
+ }
205
+ }
206
+
207
+ class CardsQuery extends QueryParams {
208
+ constructor({ cardUri, count = false, ...args }) {
209
+ super(args);
210
+ this.cardUri = cardUri;
211
+ this.count = count;
212
+ }
213
+
214
+ toObject() {
215
+ return Object.assign(super.toObject(), {
216
+ card_uri: this.cardUri,
217
+ count: this.count,
218
+ });
219
+ }
220
+ }
221
+
222
+ class WalletTransactionQuery extends QueryParams {
223
+ constructor({ walletUri, ...args }) {
224
+ super(args);
225
+ this.walletUri = walletUri;
226
+ }
227
+
228
+ toObject() {
229
+ return Object.assign(super.toObject(), {
230
+ wallet_uri: this.walletUri,
231
+ });
232
+ }
233
+ }
234
+
235
+ class WalletQuery extends QueryParams {
236
+ constructor({ active, ...args }) {
237
+ super(args);
238
+ this.active = active;
239
+ }
240
+
241
+ toObject() {
242
+ return Object.assign(super.toObject(), {
243
+ active: this.active,
244
+ });
245
+ }
246
+ }
247
+
248
+ class StatementQuery extends QueryParams {
249
+ constructor({ month, year, ...args }) {
250
+ super(args);
251
+ this.d = { month, year };
252
+ }
253
+
254
+ get month() {
255
+ return this._date.month;
256
+ }
257
+
258
+ get year() {
259
+ return this._date.year;
260
+ }
261
+
262
+ set d({ month, year }) {
263
+ const now = dateToUTC(Date.now());
264
+ now.setUTCDate(1);
265
+ const date = dateToUTC(`${year}-${month}-01`);
266
+ if (date.getTime() >= now.getTime()) {
267
+ throw new ValidationError(
268
+ `${year}-${month} is not a valid year-month pair`,
269
+ );
270
+ }
271
+ this._date = { month, year };
272
+ }
273
+
274
+ toObject() {
275
+ return Object.assign(super.toObject(), {
276
+ month: this.month,
277
+ year: this.year,
278
+ });
279
+ }
280
+ }
281
+
282
+ export { AccountQuery as A, BalanceEntryQuery as B, CardsQuery as C, DepositQuery as D, PageSize as P, QueryParams as Q, StatementQuery as S, TransferQuery as T, WalletQuery as W, ApiKeyQuery as a, BillPaymentQuery as b, CardTransactionQuery as c, WalletTransactionQuery as d };