@coinbase-sample/prime-sdk-ts 0.6.2 → 0.6.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.
Files changed (77) hide show
  1. package/README.md +207 -67
  2. package/dist/addressBooks/index.js +2 -2
  3. package/dist/allocations/index.js +3 -3
  4. package/dist/client-manual.js +84 -0
  5. package/dist/client-modular.js +57 -0
  6. package/dist/client-only.js +31 -0
  7. package/dist/{client.js → clients/client.js} +12 -2
  8. package/dist/clients/clientWithServices.js +323 -0
  9. package/dist/{types/client.d.ts → clients/clientWithServicesTypes.js} +3 -6
  10. package/dist/clients/index.js +28 -0
  11. package/dist/clients/types.js +2 -0
  12. package/dist/constants.js +1 -1
  13. package/dist/futures/index.js +4 -4
  14. package/dist/index.js +27 -18
  15. package/dist/model/enumPrefixes.js +330 -0
  16. package/dist/onchainAddressBook/index.js +4 -4
  17. package/dist/orders/index.js +6 -6
  18. package/dist/paymentMethods/index.js +2 -1
  19. package/dist/services.js +75 -0
  20. package/dist/shared/dynamicEnumValidation.js +184 -0
  21. package/dist/shared/dynamicEnumValidation.old.js +746 -0
  22. package/dist/shared/enumHelpers.js +219 -0
  23. package/dist/shared/enumRegistry.js +153 -0
  24. package/dist/shared/enumValidationCore.js +194 -0
  25. package/dist/shared/enumValidators.js +115 -0
  26. package/dist/shared/envUtils.js +66 -0
  27. package/dist/shared/fieldMapping.js +242 -0
  28. package/dist/shared/serviceContext.js +157 -0
  29. package/dist/staking/index.js +5 -5
  30. package/dist/transactions/index.js +5 -5
  31. package/dist/types/activities/index.d.ts +2 -3
  32. package/dist/types/addressBooks/index.d.ts +2 -3
  33. package/dist/types/allocations/index.d.ts +2 -3
  34. package/dist/types/assets/index.d.ts +2 -3
  35. package/dist/types/balances/index.d.ts +2 -3
  36. package/dist/types/balances/types.d.ts +1 -1
  37. package/dist/types/client-manual.d.ts +58 -0
  38. package/dist/types/client-modular.d.ts +39 -0
  39. package/dist/types/client-only.d.ts +18 -0
  40. package/dist/types/clients/client.d.ts +27 -0
  41. package/dist/types/clients/clientWithServices.d.ts +229 -0
  42. package/dist/types/clients/clientWithServicesTypes.d.ts +115 -0
  43. package/dist/types/clients/index.d.ts +19 -0
  44. package/dist/types/clients/types.d.ts +48 -0
  45. package/dist/types/commission/index.d.ts +2 -3
  46. package/dist/types/constants.d.ts +1 -1
  47. package/dist/types/financing/index.d.ts +2 -3
  48. package/dist/types/futures/index.d.ts +2 -3
  49. package/dist/types/index.d.ts +2 -1
  50. package/dist/types/invoices/index.d.ts +2 -3
  51. package/dist/types/model/enumPrefixes.d.ts +206 -0
  52. package/dist/types/onchainAddressBook/index.d.ts +2 -3
  53. package/dist/types/orders/index.d.ts +2 -3
  54. package/dist/types/paymentMethods/index.d.ts +2 -3
  55. package/dist/types/paymentMethods/types.d.ts +1 -0
  56. package/dist/types/portfolios/index.d.ts +2 -3
  57. package/dist/types/positions/index.d.ts +2 -3
  58. package/dist/types/products/index.d.ts +2 -3
  59. package/dist/types/services.d.ts +39 -0
  60. package/dist/types/shared/dynamicEnumValidation.d.ts +48 -0
  61. package/dist/types/shared/dynamicEnumValidation.old.d.ts +143 -0
  62. package/dist/types/shared/enumHelpers.d.ts +135 -0
  63. package/dist/types/shared/enumRegistry.d.ts +74 -0
  64. package/dist/types/shared/enumValidationCore.d.ts +68 -0
  65. package/dist/types/shared/enumValidators.d.ts +117 -0
  66. package/dist/types/shared/envUtils.d.ts +36 -0
  67. package/dist/types/shared/fieldMapping.d.ts +35 -0
  68. package/dist/types/shared/paginatedResponse.d.ts +3 -4
  69. package/dist/types/shared/serviceContext.d.ts +46 -0
  70. package/dist/types/staking/index.d.ts +2 -3
  71. package/dist/types/transactions/index.d.ts +2 -3
  72. package/dist/types/types.d.ts +37 -0
  73. package/dist/types/users/index.d.ts +2 -3
  74. package/dist/types/wallets/index.d.ts +2 -3
  75. package/dist/types.js +39 -0
  76. package/dist/wallets/index.js +3 -3
  77. package/package.json +33 -2
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright 2025-present Coinbase Global, Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.enumRegistry = exports.DynamicEnumValidationError = void 0;
19
+ exports.setDynamicValidationConfig = setDynamicValidationConfig;
20
+ exports.getDynamicValidationConfig = getDynamicValidationConfig;
21
+ exports.dynamicValidateRequest = dynamicValidateRequest;
22
+ exports.getEnumInfo = getEnumInfo;
23
+ /**
24
+ * Dynamic enum validation system that automatically discovers and validates
25
+ * enum fields in request objects without manual service-specific configuration.
26
+ *
27
+ * This approach is more scalable and maintainable than manual validation rules.
28
+ */
29
+ const enumRegistry_1 = require("./enumRegistry");
30
+ Object.defineProperty(exports, "enumRegistry", { enumerable: true, get: function () { return enumRegistry_1.enumRegistry; } });
31
+ const enumValidationCore_1 = require("./enumValidationCore");
32
+ Object.defineProperty(exports, "DynamicEnumValidationError", { enumerable: true, get: function () { return enumValidationCore_1.DynamicEnumValidationError; } });
33
+ // Default configuration
34
+ const defaultConfig = {
35
+ enabled: true,
36
+ strict: true,
37
+ autoNormalize: true,
38
+ logWarnings: true,
39
+ };
40
+ let currentConfig = Object.assign({}, defaultConfig);
41
+ // Cache for request type analysis to improve performance
42
+ const requestTypeCache = new Map();
43
+ /**
44
+ * Set global configuration for dynamic enum validation
45
+ */
46
+ function setDynamicValidationConfig(config) {
47
+ currentConfig = Object.assign(Object.assign({}, currentConfig), config);
48
+ }
49
+ /**
50
+ * Get current dynamic validation configuration
51
+ */
52
+ function getDynamicValidationConfig() {
53
+ return Object.assign({}, currentConfig);
54
+ }
55
+ /**
56
+ * Analyze request structure to identify enum fields
57
+ */
58
+ function analyzeRequestType(request, serviceName) {
59
+ // Use object structure and service name as cache key
60
+ const cacheKey = JSON.stringify({
61
+ structure: Object.keys(request || {})
62
+ .sort()
63
+ .reduce((acc, key) => {
64
+ acc[key] = typeof request[key];
65
+ return acc;
66
+ }, {}),
67
+ serviceName: serviceName || 'unknown',
68
+ });
69
+ // Return cached result if available
70
+ const cached = requestTypeCache.get(cacheKey);
71
+ if (cached) {
72
+ return cached;
73
+ }
74
+ const enumFields = [];
75
+ if (!request || typeof request !== 'object') {
76
+ return enumFields;
77
+ }
78
+ Object.entries(request).forEach(([fieldName, value]) => {
79
+ const fieldInfo = (0, enumValidationCore_1.analyzeField)(fieldName, value, serviceName);
80
+ if (fieldInfo) {
81
+ enumFields.push(fieldInfo);
82
+ }
83
+ });
84
+ // Cache the result
85
+ requestTypeCache.set(cacheKey, enumFields);
86
+ return enumFields;
87
+ }
88
+ /**
89
+ * Validate request object and normalize enum values
90
+ */
91
+ function validateRequestEnums(request, config = currentConfig) {
92
+ const validatedRequest = Object.assign({}, request);
93
+ const errors = [];
94
+ if (!config.enabled) {
95
+ return { validatedRequest, errors };
96
+ }
97
+ const enumFields = analyzeRequestType(request, config.serviceName);
98
+ enumFields.forEach((fieldInfo) => {
99
+ const { fieldName, enumName, isArray } = fieldInfo;
100
+ const fieldValue = request[fieldName];
101
+ if (fieldValue === undefined || fieldValue === null) {
102
+ return;
103
+ }
104
+ try {
105
+ const enumObject = enumRegistry_1.enumRegistry.getEnum(enumName);
106
+ if (!enumObject) {
107
+ errors.push(`${fieldName}: Enum ${enumName} not found`);
108
+ return;
109
+ }
110
+ if (isArray && Array.isArray(fieldValue)) {
111
+ // Handle array values
112
+ const validatedArray = [];
113
+ for (const item of fieldValue) {
114
+ if (typeof item === 'string') {
115
+ const validation = (0, enumValidationCore_1.validateEnumValue)(item, enumObject, enumName, fieldName, config);
116
+ if (validation.isValid && validation.normalizedValue) {
117
+ validatedArray.push(validation.normalizedValue);
118
+ }
119
+ else if (validation.error) {
120
+ if (config.strict) {
121
+ throw new enumValidationCore_1.DynamicEnumValidationError(fieldName, item, enumName, Object.values(enumObject), Object.keys(enumObject));
122
+ }
123
+ else {
124
+ if (config.logWarnings) {
125
+ console.warn(`[Prime SDK] Dynamic enum validation warning: ${validation.error}`);
126
+ }
127
+ validatedArray.push(item); // Keep original value
128
+ }
129
+ }
130
+ }
131
+ }
132
+ validatedRequest[fieldName] = validatedArray;
133
+ }
134
+ else if (typeof fieldValue === 'string') {
135
+ // Handle single string values
136
+ const validation = (0, enumValidationCore_1.validateEnumValue)(fieldValue, enumObject, enumName, fieldName, config);
137
+ if (validation.isValid && validation.normalizedValue) {
138
+ validatedRequest[fieldName] = validation.normalizedValue;
139
+ }
140
+ else if (validation.error) {
141
+ if (config.strict) {
142
+ throw new enumValidationCore_1.DynamicEnumValidationError(fieldName, fieldValue, enumName, Object.values(enumObject), Object.keys(enumObject));
143
+ }
144
+ else {
145
+ if (config.logWarnings) {
146
+ console.warn(`[Prime SDK] Dynamic enum validation warning: ${validation.error}`);
147
+ }
148
+ // Keep original value in non-strict mode
149
+ }
150
+ }
151
+ }
152
+ }
153
+ catch (error) {
154
+ if (error instanceof enumValidationCore_1.DynamicEnumValidationError) {
155
+ throw error; // Re-throw enum validation errors
156
+ }
157
+ errors.push(`${fieldName}: Unexpected validation error: ${error}`);
158
+ }
159
+ });
160
+ return { validatedRequest, errors };
161
+ }
162
+ /**
163
+ * Main API: Validate and normalize request with type-safe generics
164
+ */
165
+ function dynamicValidateRequest(request, config) {
166
+ const validationConfig = config
167
+ ? Object.assign(Object.assign({}, currentConfig), config) : currentConfig;
168
+ const result = validateRequestEnums(request, validationConfig);
169
+ if (result.errors.length > 0) {
170
+ throw new enumValidationCore_1.DynamicEnumValidationError('request', 'multiple fields', 'various', [], result.errors);
171
+ }
172
+ return result.validatedRequest;
173
+ }
174
+ /**
175
+ * Get information about discovered enums
176
+ */
177
+ function getEnumInfo() {
178
+ const stats = enumRegistry_1.enumRegistry.getStats();
179
+ return {
180
+ totalEnums: stats.totalEnums,
181
+ enumNames: enumRegistry_1.enumRegistry.getAllEnumNames(),
182
+ enumDetails: enumRegistry_1.enumRegistry.getEnumDetails(),
183
+ };
184
+ }