@thorprovider/adapters 4.2.0 → 4.3.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/CHANGELOG.md +6 -0
- package/PROVIDER_IMPLEMENTATION_ANALYSIS.md +659 -0
- package/README.md +19 -0
- package/dist/index.d.mts +249 -2
- package/dist/index.d.ts +249 -2
- package/dist/index.js +1450 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1448 -2
- package/dist/index.mjs.map +1 -1
- package/docs/RETRY_SOLUTION_SUMMARY.md +26 -11
- package/docs/authentication.md +115 -62
- package/docs/capabilities-system.md +34 -2
- package/docs/category-customization.md +37 -29
- package/docs/fetch-reliability-solution.md +127 -278
- package/docs/getting-started.md +43 -29
- package/docs/profile-management.md +68 -38
- package/docs/providers-overview.md +14 -0
- package/docs/services-overview.md +20 -2
- package/package.json +10 -6
- package/quick-reference.md +34 -0
- package/src/factory/index.ts +4 -0
- package/src/index.ts +3 -0
- package/src/providers/index.ts +3 -0
- package/src/providers/medusa/admin.test.ts +404 -0
- package/src/providers/medusa/admin.ts +442 -0
- package/src/providers/medusa/auth.test.ts +137 -0
- package/src/providers/medusa/auth.ts +96 -1
- package/src/providers/medusa/index.ts +21 -1
- package/src/providers/shopify/auth.ts +6 -0
- package/src/providers/stelorder/client.test-helpers.ts +3 -0
- package/src/providers/stelorder/client.test.ts +58 -0
- package/src/providers/stelorder/client.ts +326 -0
- package/src/providers/stelorder/index.test.ts +48 -0
- package/src/providers/stelorder/index.ts +685 -0
- package/src/providers/stelorder/live-smoke.test.ts +54 -0
- package/src/providers/stelorder/transforms.test.ts +103 -0
- package/src/providers/stelorder/transforms.ts +631 -0
- package/src/resilience/README.md +70 -48
- package/src/types/capabilities.types.ts +51 -0
- package/vitest.config.ts +13 -0
- package/vitest.setup.ts +6 -0
- package/.turbo/turbo-build.log +0 -18
- package/dist/.tsbuildinfo +0 -1
- package/src/core/errors.js +0 -161
- package/src/core/fetch-utils.js +0 -240
- package/src/core/logger.js +0 -63
- package/src/metadata/channel-scope-validation.js +0 -136
- package/src/metadata/channel-scope.js +0 -174
- package/src/metadata/index.js +0 -31
- package/src/providers/medusa/auth.js +0 -601
- package/src/providers/medusa/index.js +0 -2485
- package/src/providers/medusa/payment.js +0 -265
- package/src/providers/medusa/transforms.js +0 -505
- package/src/types/capabilities.types.js +0 -141
- package/src/types/provider.js +0 -9
- package/src/utils/payment-utils.js +0 -152
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @thorprovider/adapters - Payment Utilities
|
|
4
|
-
*
|
|
5
|
-
* Backend-agnostic utilities for extracting backend provider IDs from
|
|
6
|
-
* normalized PaymentMethod objects using metadata.
|
|
7
|
-
*
|
|
8
|
-
* This solves the problem of losing backend-specific provider IDs during
|
|
9
|
-
* transformation to frontend-friendly formats.
|
|
10
|
-
*
|
|
11
|
-
* @module payment-utils
|
|
12
|
-
* @since 2.0.0
|
|
13
|
-
*/
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.getBackendProviderId = getBackendProviderId;
|
|
16
|
-
exports.validatePaymentId = validatePaymentId;
|
|
17
|
-
/**
|
|
18
|
-
* Extract backend provider ID from PaymentMethod
|
|
19
|
-
*
|
|
20
|
-
* Uses the `_backendMapping` metadata to retrieve the original
|
|
21
|
-
* backend-specific provider ID that should be sent to API calls.
|
|
22
|
-
*
|
|
23
|
-
* **Why this exists:**
|
|
24
|
-
* During transformation, we normalize backend IDs to friendly types:
|
|
25
|
-
* - Backend: 'pp_stripe_stripe' → Frontend: type 'card'
|
|
26
|
-
* - Backend: 'pp_system_default' → Frontend: type 'cash_on_delivery'
|
|
27
|
-
*
|
|
28
|
-
* But when making API calls, we need the ORIGINAL backend ID, not the type.
|
|
29
|
-
* This function extracts it from metadata.
|
|
30
|
-
*
|
|
31
|
-
* @param method - PaymentMethod with _backendMapping metadata
|
|
32
|
-
* @returns Backend provider ID (e.g., 'pp_stripe_stripe', 'pp_system_default')
|
|
33
|
-
* @throws {Error} If _backendMapping metadata is missing or invalid
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```typescript
|
|
37
|
-
* // PaymentMethod from backend with metadata
|
|
38
|
-
* const method = {
|
|
39
|
-
* id: 'pp_system_default',
|
|
40
|
-
* type: 'cash_on_delivery',
|
|
41
|
-
* name: 'Cash on Delivery',
|
|
42
|
-
* metadata: {
|
|
43
|
-
* _backendMapping: {
|
|
44
|
-
* backend: 'medusa',
|
|
45
|
-
* originalId: 'pp_system_default',
|
|
46
|
-
* normalizedType: 'cash_on_delivery'
|
|
47
|
-
* }
|
|
48
|
-
* }
|
|
49
|
-
* };
|
|
50
|
-
*
|
|
51
|
-
* const providerId = getBackendProviderId(method);
|
|
52
|
-
* // Returns: 'pp_system_default'
|
|
53
|
-
*
|
|
54
|
-
* // Now safe to use with backend API:
|
|
55
|
-
* await commerce.initiatePaymentSession(cartId, providerId);
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* // Handling missing metadata
|
|
61
|
-
* try {
|
|
62
|
-
* const providerId = getBackendProviderId(methodWithoutMetadata);
|
|
63
|
-
* } catch (error) {
|
|
64
|
-
* console.error('Payment method missing backend mapping:', error.message);
|
|
65
|
-
* // Error: PaymentMethod 'stripe' is missing _backendMapping metadata.
|
|
66
|
-
* // This is required to extract the backend provider ID.
|
|
67
|
-
* }
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
function getBackendProviderId(method) {
|
|
71
|
-
var _a;
|
|
72
|
-
var mapping = (_a = method.metadata) === null || _a === void 0 ? void 0 : _a._backendMapping;
|
|
73
|
-
if (!mapping) {
|
|
74
|
-
throw new Error("PaymentMethod '".concat(method.id, "' is missing _backendMapping metadata. ") +
|
|
75
|
-
"This is required to extract the backend provider ID. " +
|
|
76
|
-
"Ensure the payment method was transformed with metadata preservation. " +
|
|
77
|
-
"Check transformPaymentMethod() implementation in your provider adapter.");
|
|
78
|
-
}
|
|
79
|
-
if (!mapping.originalId) {
|
|
80
|
-
throw new Error("PaymentMethod '".concat(method.id, "' has invalid _backendMapping metadata: ") +
|
|
81
|
-
"'originalId' field is missing or empty. " +
|
|
82
|
-
"Expected structure: { _backendMapping: { backend: 'medusa', originalId: 'pp_...', normalizedType: '...' } }");
|
|
83
|
-
}
|
|
84
|
-
return mapping.originalId;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Validate payment provider ID format
|
|
88
|
-
*
|
|
89
|
-
* Checks if the provider ID is a valid backend-specific identifier,
|
|
90
|
-
* not a normalized type like 'card', 'wallet', etc.
|
|
91
|
-
*
|
|
92
|
-
* **Why this exists:**
|
|
93
|
-
* Common bug: Accidentally sending normalized types to backend APIs:
|
|
94
|
-
* ❌ `initiatePaymentSession(cartId, 'card')` → Backend error
|
|
95
|
-
* ✅ `initiatePaymentSession(cartId, 'pp_stripe_stripe')` → Success
|
|
96
|
-
*
|
|
97
|
-
* This validation catches the bug at runtime with a helpful error message.
|
|
98
|
-
*
|
|
99
|
-
* @param id - Provider ID to validate
|
|
100
|
-
* @throws {Error} If ID appears to be a normalized type instead of backend ID
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* ```typescript
|
|
104
|
-
* // Valid backend IDs - pass validation:
|
|
105
|
-
* validatePaymentId('pp_stripe_stripe'); // ✅ OK (Medusa)
|
|
106
|
-
* validatePaymentId('pp_system_default'); // ✅ OK (Medusa)
|
|
107
|
-
* validatePaymentId('shopify_payments'); // ✅ OK (Shopify)
|
|
108
|
-
* validatePaymentId('stripe'); // ✅ OK (WooCommerce)
|
|
109
|
-
* validatePaymentId('paypal_express'); // ✅ OK (generic)
|
|
110
|
-
*
|
|
111
|
-
* // Invalid (normalized types) - throw error:
|
|
112
|
-
* validatePaymentId('card'); // ❌ Throws error
|
|
113
|
-
* validatePaymentId('wallet'); // ❌ Throws error
|
|
114
|
-
* validatePaymentId('cash_on_delivery'); // ❌ Throws error
|
|
115
|
-
* ```
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
* ```typescript
|
|
119
|
-
* // Use before API calls:
|
|
120
|
-
* async function initiatePayment(cartId: string, providerId: string) {
|
|
121
|
-
* // Validate before sending to backend
|
|
122
|
-
* validatePaymentId(providerId);
|
|
123
|
-
*
|
|
124
|
-
* return await sdk.payment.initiatePaymentSession(cart, {
|
|
125
|
-
* provider_id: providerId
|
|
126
|
-
* });
|
|
127
|
-
* }
|
|
128
|
-
*
|
|
129
|
-
* // Catches bugs early:
|
|
130
|
-
* await initiatePayment(cartId, 'card');
|
|
131
|
-
* // ❌ Error: Invalid payment provider ID: 'card'.
|
|
132
|
-
* // This appears to be a normalized payment type, not a backend provider ID.
|
|
133
|
-
* // Use getBackendProviderId() to extract the correct backend ID from PaymentMethod.
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
function validatePaymentId(id) {
|
|
137
|
-
var normalizedTypes = [
|
|
138
|
-
'card',
|
|
139
|
-
'wallet',
|
|
140
|
-
'bank_transfer',
|
|
141
|
-
'buy_now_pay_later',
|
|
142
|
-
'cash_on_delivery',
|
|
143
|
-
'crypto',
|
|
144
|
-
'other'
|
|
145
|
-
];
|
|
146
|
-
if (normalizedTypes.includes(id)) {
|
|
147
|
-
throw new Error("Invalid payment provider ID: '".concat(id, "'. ") +
|
|
148
|
-
"This appears to be a normalized payment type, not a backend provider ID. " +
|
|
149
|
-
"Use getBackendProviderId() to extract the correct backend ID from PaymentMethod. " +
|
|
150
|
-
"Expected format examples: 'pp_stripe_stripe', 'pp_system_default', 'shopify_payments', 'stripe'");
|
|
151
|
-
}
|
|
152
|
-
}
|