@coinbase-sample/prime-sdk-ts 0.8.0 → 0.8.2

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.
Files changed (56) hide show
  1. package/dist/activities/index.js +14 -0
  2. package/dist/addressBooks/index.js +10 -0
  3. package/dist/allocations/index.js +12 -0
  4. package/dist/assets/index.js +4 -0
  5. package/dist/balances/index.js +15 -0
  6. package/dist/commission/index.js +4 -0
  7. package/dist/constants.js +1 -1
  8. package/dist/financing/index.js +52 -0
  9. package/dist/futures/index.js +33 -0
  10. package/dist/index.js +7 -1
  11. package/dist/invoices/index.js +4 -0
  12. package/dist/model/GetStakingStatusResponse.js +21 -0
  13. package/dist/model/StakingStatus.js +21 -0
  14. package/dist/model/SubmitDepositTravelRuleDataResponse.js +21 -0
  15. package/dist/model/TravelRuleData.js +21 -0
  16. package/dist/model/ValidatorStakingInfo.js +21 -0
  17. package/dist/model/enums/StakeType.js +28 -0
  18. package/dist/model/enums/index.js +4 -2
  19. package/dist/model/googleTypeDate.js +21 -0
  20. package/dist/model/requestToSubmitTravelRuleDataForAnExistingDepositTransaction.js +21 -0
  21. package/dist/onchainAddressBook/index.js +14 -0
  22. package/dist/orders/index.js +59 -0
  23. package/dist/paymentMethods/index.js +8 -0
  24. package/dist/portfolios/index.js +10 -0
  25. package/dist/positions/index.js +7 -0
  26. package/dist/products/index.js +11 -0
  27. package/dist/shared/__tests__/validation.test.js +184 -124
  28. package/dist/shared/validation.js +90 -260
  29. package/dist/staking/index.js +55 -0
  30. package/dist/transactions/index.js +58 -0
  31. package/dist/types/constants.d.ts +1 -1
  32. package/dist/types/index.d.ts +3 -0
  33. package/dist/types/model/Balance.d.ts +4 -0
  34. package/dist/types/model/CreatePortfolioAddressBookEntryRequest.d.ts +0 -2
  35. package/dist/types/model/CreateWalletWithdrawalRequest.d.ts +2 -0
  36. package/dist/types/model/GetStakingStatusResponse.d.ts +42 -0
  37. package/dist/types/model/StakingStatus.d.ts +39 -0
  38. package/dist/types/model/SubmitDepositTravelRuleDataResponse.d.ts +22 -0
  39. package/dist/types/model/TravelRuleData.d.ts +37 -0
  40. package/dist/types/model/TravelRuleParty.d.ts +19 -4
  41. package/dist/types/model/ValidatorStakingInfo.d.ts +30 -0
  42. package/dist/types/model/enums/StakeType.d.ts +24 -0
  43. package/dist/types/model/enums/index.d.ts +1 -0
  44. package/dist/types/model/googleTypeDate.d.ts +36 -0
  45. package/dist/types/model/index.d.ts +7 -0
  46. package/dist/types/model/requestToSubmitTravelRuleDataForAnExistingDepositTransaction.d.ts +26 -0
  47. package/dist/types/orders/types.d.ts +1 -1
  48. package/dist/types/shared/validation.d.ts +19 -107
  49. package/dist/types/staking/index.d.ts +3 -1
  50. package/dist/types/staking/types.d.ts +6 -1
  51. package/dist/types/transactions/index.d.ts +3 -1
  52. package/dist/types/transactions/types.d.ts +6 -1
  53. package/dist/types/wallets/types.d.ts +1 -1
  54. package/dist/users/index.js +7 -0
  55. package/dist/wallets/index.js +27 -0
  56. package/package.json +6 -2
@@ -29,19 +29,17 @@ describe('validation', () => {
29
29
  expect((0, validation_1.isValidUUID)('123e4567-e89b-12d3-a456')).toBe(false);
30
30
  expect((0, validation_1.isValidUUID)('123e4567e89b12d3a456426614174000')).toBe(false);
31
31
  expect((0, validation_1.isValidUUID)('')).toBe(false);
32
- expect((0, validation_1.isValidUUID)('12345678-1234-1234-1234-12345678901g')).toBe(false); // 'g' is not hex
32
+ expect((0, validation_1.isValidUUID)('12345678-1234-1234-1234-12345678901g')).toBe(false);
33
33
  });
34
34
  });
35
- describe('ValidationResult', () => {
36
- let validator;
37
- beforeEach(() => {
38
- validator = (0, validation_1.createValidator)();
39
- });
35
+ describe('PropertyValidator', () => {
40
36
  it('should start with no errors', () => {
37
+ const validator = (0, validation_1.validate)({});
41
38
  expect(validator.hasErrors()).toBe(false);
42
39
  expect(validator.getErrors()).toEqual([]);
43
40
  });
44
41
  it('should accumulate errors', () => {
42
+ const validator = (0, validation_1.validate)({});
45
43
  validator.addError('field1', 'is required');
46
44
  validator.addError('field2', 'must be a valid UUID', 'invalid-value');
47
45
  expect(validator.hasErrors()).toBe(true);
@@ -57,146 +55,208 @@ describe('validation', () => {
57
55
  });
58
56
  });
59
57
  it('should throw with formatted error message when there are errors', () => {
58
+ const validator = (0, validation_1.validate)({});
60
59
  validator.addError('portfolioId', 'must be a valid UUID', 'bad-id');
61
60
  validator.addError('orderId', 'is required');
62
- expect(() => validator.throwIfInvalid()).toThrow(errors_1.CoinbasePrimeClientException);
63
- expect(() => validator.throwIfInvalid()).toThrow(/Request validation failed:\n - portfolioId: must be a valid UUID \(received: 'bad-id'\)\n - orderId: is required/);
61
+ expect(() => validator.check()).toThrow(errors_1.CoinbasePrimeClientException);
62
+ expect(() => validator.check()).toThrow(/Request validation failed:\n - portfolioId: must be a valid UUID \(received: 'bad-id'\)\n - orderId: is required/);
64
63
  });
65
64
  it('should not throw when there are no errors', () => {
66
- expect(() => validator.throwIfInvalid()).not.toThrow();
65
+ const validator = (0, validation_1.validate)({});
66
+ expect(() => validator.check()).not.toThrow();
67
67
  });
68
68
  it('should include custom context message', () => {
69
+ const validator = (0, validation_1.validate)({});
69
70
  validator.addError('field1', 'is invalid');
70
- expect(() => validator.throwIfInvalid('Custom operation failed')).toThrow(/Custom operation failed:/);
71
+ expect(() => validator.check('Custom operation failed')).toThrow(/Custom operation failed:/);
71
72
  });
72
73
  });
73
- describe('validateRequiredUUID', () => {
74
- let validator;
75
- beforeEach(() => {
76
- validator = (0, validation_1.createValidator)();
77
- });
78
- it('should not add error for valid UUID', () => {
79
- (0, validation_1.validateRequiredUUID)(validator, '123e4567-e89b-12d3-a456-426614174000', 'testId');
80
- expect(validator.hasErrors()).toBe(false);
81
- });
82
- it('should add error for undefined', () => {
83
- (0, validation_1.validateRequiredUUID)(validator, undefined, 'testId');
84
- expect(validator.hasErrors()).toBe(true);
85
- expect(validator.getErrors()[0].message).toContain('required');
86
- });
87
- it('should add error for null', () => {
88
- (0, validation_1.validateRequiredUUID)(validator, null, 'testId');
89
- expect(validator.hasErrors()).toBe(true);
90
- expect(validator.getErrors()[0].message).toContain('required');
91
- });
92
- it('should add error for empty string', () => {
93
- (0, validation_1.validateRequiredUUID)(validator, '', 'testId');
94
- expect(validator.hasErrors()).toBe(true);
95
- expect(validator.getErrors()[0].message).toContain('required');
96
- });
97
- it('should add error for whitespace-only string', () => {
98
- (0, validation_1.validateRequiredUUID)(validator, ' ', 'testId');
99
- expect(validator.hasErrors()).toBe(true);
100
- expect(validator.getErrors()[0].message).toContain('whitespace');
101
- });
102
- it('should add error for invalid UUID format', () => {
103
- (0, validation_1.validateRequiredUUID)(validator, 'not-a-uuid', 'testId');
104
- expect(validator.hasErrors()).toBe(true);
105
- expect(validator.getErrors()[0].message).toContain('valid UUID');
74
+ describe('requiredUUID', () => {
75
+ it('should not throw for valid UUID', () => {
76
+ const request = { testId: '123e4567-e89b-12d3-a456-426614174000' };
77
+ expect(() => (0, validation_1.validate)(request)
78
+ .requiredUUID((r) => r.testId)
79
+ .check()).not.toThrow();
80
+ });
81
+ it('should throw for undefined', () => {
82
+ const request = { testId: undefined };
83
+ expect(() => (0, validation_1.validate)(request)
84
+ .requiredUUID((r) => r.testId)
85
+ .check()).toThrow(/required/);
86
+ });
87
+ it('should throw for null', () => {
88
+ const request = { testId: null };
89
+ expect(() => (0, validation_1.validate)(request)
90
+ .requiredUUID((r) => r.testId)
91
+ .check()).toThrow(/required/);
92
+ });
93
+ it('should throw for empty string', () => {
94
+ const request = { testId: '' };
95
+ expect(() => (0, validation_1.validate)(request)
96
+ .requiredUUID((r) => r.testId)
97
+ .check()).toThrow(/required/);
98
+ });
99
+ it('should throw for whitespace-only string', () => {
100
+ const request = { testId: ' ' };
101
+ expect(() => (0, validation_1.validate)(request)
102
+ .requiredUUID((r) => r.testId)
103
+ .check()).toThrow(/whitespace/);
104
+ });
105
+ it('should throw for invalid UUID format', () => {
106
+ const request = { testId: 'not-a-uuid' };
107
+ expect(() => (0, validation_1.validate)(request)
108
+ .requiredUUID((r) => r.testId)
109
+ .check()).toThrow(/valid UUID/);
106
110
  });
107
111
  });
108
- describe('validateOptionalUUID', () => {
109
- let validator;
110
- beforeEach(() => {
111
- validator = (0, validation_1.createValidator)();
112
- });
113
- it('should not add error for valid UUID', () => {
114
- (0, validation_1.validateOptionalUUID)(validator, '123e4567-e89b-12d3-a456-426614174000', 'testId');
115
- expect(validator.hasErrors()).toBe(false);
116
- });
117
- it('should not add error for undefined', () => {
118
- (0, validation_1.validateOptionalUUID)(validator, undefined, 'testId');
119
- expect(validator.hasErrors()).toBe(false);
120
- });
121
- it('should not add error for null', () => {
122
- (0, validation_1.validateOptionalUUID)(validator, null, 'testId');
123
- expect(validator.hasErrors()).toBe(false);
124
- });
125
- it('should not add error for empty string', () => {
126
- (0, validation_1.validateOptionalUUID)(validator, '', 'testId');
127
- expect(validator.hasErrors()).toBe(false);
128
- });
129
- it('should add error for invalid UUID format', () => {
130
- (0, validation_1.validateOptionalUUID)(validator, 'not-a-uuid', 'testId');
131
- expect(validator.hasErrors()).toBe(true);
132
- expect(validator.getErrors()[0].message).toContain('valid UUID');
112
+ describe('optionalUUID', () => {
113
+ it('should not throw for valid UUID', () => {
114
+ const request = { testId: '123e4567-e89b-12d3-a456-426614174000' };
115
+ expect(() => (0, validation_1.validate)(request)
116
+ .optionalUUID((r) => r.testId)
117
+ .check()).not.toThrow();
118
+ });
119
+ it('should not throw for undefined', () => {
120
+ const request = { testId: undefined };
121
+ expect(() => (0, validation_1.validate)(request)
122
+ .optionalUUID((r) => r.testId)
123
+ .check()).not.toThrow();
124
+ });
125
+ it('should not throw for null', () => {
126
+ const request = { testId: null };
127
+ expect(() => (0, validation_1.validate)(request)
128
+ .optionalUUID((r) => r.testId)
129
+ .check()).not.toThrow();
130
+ });
131
+ it('should not throw for empty string', () => {
132
+ const request = { testId: '' };
133
+ expect(() => (0, validation_1.validate)(request)
134
+ .optionalUUID((r) => r.testId)
135
+ .check()).not.toThrow();
136
+ });
137
+ it('should throw for invalid UUID format', () => {
138
+ const request = { testId: 'not-a-uuid' };
139
+ expect(() => (0, validation_1.validate)(request)
140
+ .optionalUUID((r) => r.testId)
141
+ .check()).toThrow(/valid UUID/);
133
142
  });
134
143
  });
135
- describe('validateRequiredString', () => {
136
- let validator;
137
- beforeEach(() => {
138
- validator = (0, validation_1.createValidator)();
139
- });
140
- it('should not add error for valid string', () => {
141
- (0, validation_1.validateRequiredString)(validator, 'valid-string', 'testField');
142
- expect(validator.hasErrors()).toBe(false);
143
- });
144
- it('should add error for undefined', () => {
145
- (0, validation_1.validateRequiredString)(validator, undefined, 'testField');
146
- expect(validator.hasErrors()).toBe(true);
147
- expect(validator.getErrors()[0].message).toContain('required');
148
- });
149
- it('should add error for null', () => {
150
- (0, validation_1.validateRequiredString)(validator, null, 'testField');
151
- expect(validator.hasErrors()).toBe(true);
152
- expect(validator.getErrors()[0].message).toContain('required');
153
- });
154
- it('should add error for empty string', () => {
155
- (0, validation_1.validateRequiredString)(validator, '', 'testField');
156
- expect(validator.hasErrors()).toBe(true);
157
- expect(validator.getErrors()[0].message).toContain('required');
158
- });
159
- it('should add error for whitespace-only string', () => {
160
- (0, validation_1.validateRequiredString)(validator, ' ', 'testField');
161
- expect(validator.hasErrors()).toBe(true);
162
- expect(validator.getErrors()[0].message).toContain('whitespace');
144
+ describe('requiredString', () => {
145
+ it('should not throw for valid string', () => {
146
+ const request = { testField: 'valid-string' };
147
+ expect(() => (0, validation_1.validate)(request)
148
+ .requiredString((r) => r.testField)
149
+ .check()).not.toThrow();
150
+ });
151
+ it('should throw for undefined', () => {
152
+ const request = { testField: undefined };
153
+ expect(() => (0, validation_1.validate)(request)
154
+ .requiredString((r) => r.testField)
155
+ .check()).toThrow(/required/);
156
+ });
157
+ it('should throw for null', () => {
158
+ const request = { testField: null };
159
+ expect(() => (0, validation_1.validate)(request)
160
+ .requiredString((r) => r.testField)
161
+ .check()).toThrow(/required/);
162
+ });
163
+ it('should throw for empty string', () => {
164
+ const request = { testField: '' };
165
+ expect(() => (0, validation_1.validate)(request)
166
+ .requiredString((r) => r.testField)
167
+ .check()).toThrow(/required/);
168
+ });
169
+ it('should throw for whitespace-only string', () => {
170
+ const request = { testField: ' ' };
171
+ expect(() => (0, validation_1.validate)(request)
172
+ .requiredString((r) => r.testField)
173
+ .check()).toThrow(/whitespace/);
163
174
  });
164
175
  });
165
- describe('validateOptionalString', () => {
166
- let validator;
167
- beforeEach(() => {
168
- validator = (0, validation_1.createValidator)();
169
- });
170
- it('should not add error for valid string', () => {
171
- (0, validation_1.validateOptionalString)(validator, 'valid-string', 'testField');
172
- expect(validator.hasErrors()).toBe(false);
173
- });
174
- it('should not add error for undefined', () => {
175
- (0, validation_1.validateOptionalString)(validator, undefined, 'testField');
176
- expect(validator.hasErrors()).toBe(false);
176
+ describe('optionalString', () => {
177
+ it('should not throw for valid string', () => {
178
+ const request = { testField: 'valid-string' };
179
+ expect(() => (0, validation_1.validate)(request)
180
+ .optionalString((r) => r.testField)
181
+ .check()).not.toThrow();
182
+ });
183
+ it('should not throw for undefined', () => {
184
+ const request = { testField: undefined };
185
+ expect(() => (0, validation_1.validate)(request)
186
+ .optionalString((r) => r.testField)
187
+ .check()).not.toThrow();
188
+ });
189
+ it('should not throw for null', () => {
190
+ const request = { testField: null };
191
+ expect(() => (0, validation_1.validate)(request)
192
+ .optionalString((r) => r.testField)
193
+ .check()).not.toThrow();
194
+ });
195
+ it('should throw for whitespace-only when not empty', () => {
196
+ const request = { testField: ' ' };
197
+ expect(() => (0, validation_1.validate)(request)
198
+ .optionalString((r) => r.testField)
199
+ .check()).toThrow(/whitespace/);
177
200
  });
178
- it('should not add error for null', () => {
179
- (0, validation_1.validateOptionalString)(validator, null, 'testField');
180
- expect(validator.hasErrors()).toBe(false);
201
+ });
202
+ describe('requiredArray', () => {
203
+ it('should not throw for valid array', () => {
204
+ const request = { items: ['a', 'b', 'c'] };
205
+ expect(() => (0, validation_1.validate)(request)
206
+ .requiredArray((r) => r.items)
207
+ .check()).not.toThrow();
208
+ });
209
+ it('should throw for undefined', () => {
210
+ const request = { items: undefined };
211
+ expect(() => (0, validation_1.validate)(request)
212
+ .requiredArray((r) => r.items)
213
+ .check()).toThrow(/required/);
214
+ });
215
+ it('should throw for empty array', () => {
216
+ const request = { items: [] };
217
+ expect(() => (0, validation_1.validate)(request)
218
+ .requiredArray((r) => r.items)
219
+ .check()).toThrow(/empty array/);
181
220
  });
182
- it('should add error for whitespace-only when not empty', () => {
183
- (0, validation_1.validateOptionalString)(validator, ' ', 'testField');
184
- expect(validator.hasErrors()).toBe(true);
185
- expect(validator.getErrors()[0].message).toContain('whitespace');
221
+ });
222
+ describe('requiredBoolean', () => {
223
+ it('should not throw for true', () => {
224
+ const request = { flag: true };
225
+ expect(() => (0, validation_1.validate)(request)
226
+ .requiredBoolean((r) => r.flag)
227
+ .check()).not.toThrow();
228
+ });
229
+ it('should not throw for false', () => {
230
+ const request = { flag: false };
231
+ expect(() => (0, validation_1.validate)(request)
232
+ .requiredBoolean((r) => r.flag)
233
+ .check()).not.toThrow();
234
+ });
235
+ it('should throw for undefined', () => {
236
+ const request = { flag: undefined };
237
+ expect(() => (0, validation_1.validate)(request)
238
+ .requiredBoolean((r) => r.flag)
239
+ .check()).toThrow(/required/);
186
240
  });
187
241
  });
188
242
  describe('multiple field validation', () => {
189
243
  it('should collect all validation errors before throwing', () => {
190
- const validator = (0, validation_1.createValidator)();
191
- // Simulate validating multiple fields at once
192
- (0, validation_1.validateRequiredUUID)(validator, undefined, 'portfolioId');
193
- (0, validation_1.validateRequiredUUID)(validator, 'invalid-uuid', 'orderId');
194
- (0, validation_1.validateRequiredString)(validator, '', 'productId');
195
- expect(validator.hasErrors()).toBe(true);
196
- expect(validator.getErrors()).toHaveLength(3);
197
- expect(() => validator.throwIfInvalid('getOrder request validation failed')).toThrow(errors_1.CoinbasePrimeClientException);
244
+ const request = {
245
+ portfolioId: undefined,
246
+ orderId: 'invalid-uuid',
247
+ productId: '',
248
+ };
249
+ expect(() => (0, validation_1.validate)(request)
250
+ .requiredUUID((r) => r.portfolioId)
251
+ .requiredUUID((r) => r.orderId)
252
+ .requiredString((r) => r.productId)
253
+ .check('getOrder request validation failed')).toThrow(errors_1.CoinbasePrimeClientException);
198
254
  try {
199
- validator.throwIfInvalid('getOrder request validation failed');
255
+ (0, validation_1.validate)(request)
256
+ .requiredUUID((r) => r.portfolioId)
257
+ .requiredUUID((r) => r.orderId)
258
+ .requiredString((r) => r.productId)
259
+ .check('getOrder request validation failed');
200
260
  }
201
261
  catch (error) {
202
262
  if (error instanceof errors_1.CoinbasePrimeClientException) {