@thorprovider/adapters 5.0.0 → 5.0.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.
- package/AGENTS.md +1 -1
- package/CHANGELOG.md +6 -0
- package/README.md +53 -631
- package/dist/index.d.mts +406 -194
- package/dist/index.d.ts +406 -194
- package/dist/index.js +3092 -604
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3081 -602
- package/dist/index.mjs.map +1 -1
- package/docs/RETRY_SOLUTION_SUMMARY.md +1 -1
- package/docs/authentication.md +0 -26
- package/docs/capabilities-system.md +66 -36
- package/docs/getting-started.md +1 -1
- package/docs/providers-overview.md +16 -13
- package/docs/update-documentation.md +10 -8
- package/package.json +6 -5
- package/quick-reference.md +0 -33
- package/src/backend/factory.ts +11 -1
- package/src/factory/index.ts +52 -74
- package/src/index.ts +17 -3
- package/src/providers/bigcommerce/bigcommerce.ts +6 -0
- package/src/providers/bigcommerce/index.ts +315 -0
- package/src/providers/bigcommerce/transforms.ts +227 -0
- package/src/providers/index.ts +16 -1
- package/src/providers/magento/index.ts +387 -0
- package/src/providers/magento/magento.ts +5 -0
- package/src/providers/magento/queries.ts +267 -0
- package/src/providers/magento/transforms.ts +249 -0
- package/src/providers/medusa/auth.test.ts +94 -92
- package/src/providers/medusa/auth.ts +72 -123
- package/src/providers/medusa/index.test.ts +392 -0
- package/src/providers/medusa/index.ts +1 -21
- package/src/providers/medusa/live-smoke.test.ts +412 -0
- package/src/providers/shopify/auth.ts +402 -0
- package/src/providers/shopify/index.ts +444 -0
- package/src/providers/shopify/mutations.ts +247 -0
- package/src/providers/shopify/queries.ts +324 -0
- package/src/providers/shopify/shopify.ts +7 -0
- package/src/providers/shopify/transforms.ts +275 -0
- package/src/providers/spree/index.ts +344 -0
- package/src/providers/spree/spree.ts +5 -0
- package/src/providers/spree/transforms.ts +254 -0
- package/src/providers/woocommerce/index.ts +310 -0
- package/src/providers/woocommerce/transforms.ts +254 -0
- package/src/providers/woocommerce/woocommerce.ts +5 -0
- package/src/types/capabilities.types.ts +250 -0
- package/vitest.live.config.ts +18 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Address,
|
|
3
|
+
AuthConfig,
|
|
4
|
+
AuthProvider,
|
|
5
|
+
AuthResponse,
|
|
6
|
+
CreateAddressData,
|
|
7
|
+
Customer,
|
|
8
|
+
LoginCredentials,
|
|
9
|
+
RegisterData,
|
|
10
|
+
UpdateCustomerData,
|
|
11
|
+
} from '@thorprovider/types';
|
|
12
|
+
import { createLogger } from '../../core/logger';
|
|
13
|
+
import { ProviderAPIError, extractErrorMessage } from '../../core/errors';
|
|
14
|
+
import { AuthStorage } from '../../lib/auth-storage';
|
|
15
|
+
import {
|
|
16
|
+
CUSTOMER_ACCESS_TOKEN_CREATE,
|
|
17
|
+
CUSTOMER_ACCESS_TOKEN_DELETE,
|
|
18
|
+
CUSTOMER_ADDRESS_CREATE,
|
|
19
|
+
CUSTOMER_ADDRESS_DELETE,
|
|
20
|
+
CUSTOMER_ADDRESS_UPDATE,
|
|
21
|
+
CUSTOMER_CREATE,
|
|
22
|
+
CUSTOMER_RECOVER,
|
|
23
|
+
CUSTOMER_RESET,
|
|
24
|
+
CUSTOMER_UPDATE,
|
|
25
|
+
} from './mutations';
|
|
26
|
+
import { GET_CUSTOMER } from './queries';
|
|
27
|
+
import { transformAddress, transformCustomer, transformCustomerAddresses } from './transforms';
|
|
28
|
+
|
|
29
|
+
type ShopifyGraphqlRequester = <T = any>(gql: string, variables?: Record<string, unknown>) => Promise<T>;
|
|
30
|
+
|
|
31
|
+
interface ShopifyCustomerToken {
|
|
32
|
+
accessToken: string;
|
|
33
|
+
expiresAt?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface ShopifyAuthDependencies {
|
|
37
|
+
query: ShopifyGraphqlRequester;
|
|
38
|
+
authConfig: AuthConfig;
|
|
39
|
+
debug?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class ShopifyAuthProvider implements AuthProvider {
|
|
43
|
+
private readonly logger;
|
|
44
|
+
private readonly storage: AuthStorage;
|
|
45
|
+
|
|
46
|
+
constructor(private readonly deps: ShopifyAuthDependencies) {
|
|
47
|
+
this.logger = createLogger('ShopifyAuthProvider', deps.debug);
|
|
48
|
+
this.storage = new AuthStorage(deps.authConfig, 'thor:shopify:auth');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private async readToken(): Promise<ShopifyCustomerToken | null> {
|
|
52
|
+
const token = await this.storage.getJson<ShopifyCustomerToken>('customer-access-token');
|
|
53
|
+
if (!token?.accessToken) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (token.expiresAt && new Date(token.expiresAt).getTime() <= Date.now()) {
|
|
58
|
+
await this.storage.remove('customer-access-token');
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return token;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async getStoredToken(): Promise<ShopifyCustomerToken | null> {
|
|
66
|
+
return this.readToken();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private async writeToken(token: ShopifyCustomerToken | null): Promise<void> {
|
|
70
|
+
if (!token) {
|
|
71
|
+
await this.storage.remove('customer-access-token');
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
await this.storage.setJson('customer-access-token', token);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private async requireToken(): Promise<ShopifyCustomerToken> {
|
|
79
|
+
const token = await this.readToken();
|
|
80
|
+
if (!token) {
|
|
81
|
+
throw new ProviderAPIError('Shopify customer access token is required.', 'shopify', 401);
|
|
82
|
+
}
|
|
83
|
+
return token;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private async fetchCurrentCustomer(): Promise<any | null> {
|
|
87
|
+
const token = await this.readToken();
|
|
88
|
+
if (!token) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
const data = await this.deps.query<{ customer: any }>(GET_CUSTOMER, {
|
|
94
|
+
customerAccessToken: token.accessToken,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return data.customer ?? null;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
await this.writeToken(null);
|
|
100
|
+
this.logger.warn('Failed to fetch current Shopify customer', extractErrorMessage(error));
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async login(credentials: LoginCredentials): Promise<AuthResponse> {
|
|
106
|
+
try {
|
|
107
|
+
const data = await this.deps.query<{
|
|
108
|
+
customerAccessTokenCreate: {
|
|
109
|
+
customerAccessToken?: ShopifyCustomerToken;
|
|
110
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
111
|
+
};
|
|
112
|
+
}>(CUSTOMER_ACCESS_TOKEN_CREATE, {
|
|
113
|
+
input: {
|
|
114
|
+
email: credentials.email,
|
|
115
|
+
password: credentials.password,
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const result = data.customerAccessTokenCreate;
|
|
120
|
+
if (result.customerUserErrors?.length || !result.customerAccessToken?.accessToken) {
|
|
121
|
+
throw new ProviderAPIError(
|
|
122
|
+
result.customerUserErrors?.map((entry) => entry.message).join('; ') || 'Shopify login failed.',
|
|
123
|
+
'shopify',
|
|
124
|
+
400,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await this.writeToken(result.customerAccessToken);
|
|
129
|
+
const customer = await this.getCurrentCustomer();
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
token: result.customerAccessToken.accessToken,
|
|
133
|
+
customer: customer ?? undefined,
|
|
134
|
+
};
|
|
135
|
+
} catch (error) {
|
|
136
|
+
throw new ProviderAPIError(`Shopify login failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async register(data: RegisterData): Promise<AuthResponse> {
|
|
141
|
+
try {
|
|
142
|
+
const createResult = await this.deps.query<{
|
|
143
|
+
customerCreate: {
|
|
144
|
+
customer?: any;
|
|
145
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
146
|
+
};
|
|
147
|
+
}>(CUSTOMER_CREATE, {
|
|
148
|
+
input: {
|
|
149
|
+
firstName: data.first_name,
|
|
150
|
+
lastName: data.last_name,
|
|
151
|
+
email: data.email,
|
|
152
|
+
phone: data.phone,
|
|
153
|
+
password: data.password,
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const result = createResult.customerCreate;
|
|
158
|
+
if (result.customerUserErrors?.length) {
|
|
159
|
+
throw new ProviderAPIError(
|
|
160
|
+
result.customerUserErrors.map((entry) => entry.message).join('; '),
|
|
161
|
+
'shopify',
|
|
162
|
+
400,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return this.login({ email: data.email, password: data.password });
|
|
167
|
+
} catch (error) {
|
|
168
|
+
throw new ProviderAPIError(`Shopify registration failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async logout(): Promise<void> {
|
|
173
|
+
try {
|
|
174
|
+
const token = await this.readToken();
|
|
175
|
+
if (token?.accessToken) {
|
|
176
|
+
await this.deps.query(CUSTOMER_ACCESS_TOKEN_DELETE, {
|
|
177
|
+
customerAccessToken: token.accessToken,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
this.logger.warn('Shopify logout returned an error', extractErrorMessage(error));
|
|
182
|
+
} finally {
|
|
183
|
+
await this.writeToken(null);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async requestPasswordReset(email: string): Promise<void> {
|
|
188
|
+
try {
|
|
189
|
+
const data = await this.deps.query<{
|
|
190
|
+
customerRecover: {
|
|
191
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
192
|
+
};
|
|
193
|
+
}>(CUSTOMER_RECOVER, { email });
|
|
194
|
+
|
|
195
|
+
if (data.customerRecover.customerUserErrors?.length) {
|
|
196
|
+
throw new ProviderAPIError(
|
|
197
|
+
data.customerRecover.customerUserErrors.map((entry) => entry.message).join('; '),
|
|
198
|
+
'shopify',
|
|
199
|
+
400,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
} catch (error) {
|
|
203
|
+
throw new ProviderAPIError(`Shopify password reset request failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async resetPassword(token: string, password: string, metadata?: Record<string, unknown>): Promise<void> {
|
|
208
|
+
const customerId = typeof metadata?.customerId === 'string' ? metadata.customerId : null;
|
|
209
|
+
if (!customerId) {
|
|
210
|
+
throw new ProviderAPIError('Shopify password reset requires metadata.customerId.', 'shopify', 400);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
const data = await this.deps.query<{
|
|
215
|
+
customerReset: {
|
|
216
|
+
customerAccessToken?: ShopifyCustomerToken;
|
|
217
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
218
|
+
};
|
|
219
|
+
}>(CUSTOMER_RESET, {
|
|
220
|
+
id: customerId,
|
|
221
|
+
input: {
|
|
222
|
+
resetToken: token,
|
|
223
|
+
password,
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
if (data.customerReset.customerUserErrors?.length) {
|
|
228
|
+
throw new ProviderAPIError(
|
|
229
|
+
data.customerReset.customerUserErrors.map((entry) => entry.message).join('; '),
|
|
230
|
+
'shopify',
|
|
231
|
+
400,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (data.customerReset.customerAccessToken?.accessToken) {
|
|
236
|
+
await this.writeToken(data.customerReset.customerAccessToken);
|
|
237
|
+
}
|
|
238
|
+
} catch (error) {
|
|
239
|
+
throw new ProviderAPIError(`Shopify password reset failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async getCurrentCustomer(): Promise<Customer | null> {
|
|
244
|
+
const customer = await this.fetchCurrentCustomer();
|
|
245
|
+
return customer ? transformCustomer(customer) : null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async updateCustomer(data: UpdateCustomerData): Promise<Customer> {
|
|
249
|
+
const token = await this.requireToken();
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
const payload: Record<string, unknown> = {};
|
|
253
|
+
if (data.first_name !== undefined) payload.firstName = data.first_name;
|
|
254
|
+
if (data.last_name !== undefined) payload.lastName = data.last_name;
|
|
255
|
+
if (data.email !== undefined) payload.email = data.email;
|
|
256
|
+
if (data.phone !== undefined && data.phone !== null) payload.phone = data.phone;
|
|
257
|
+
if (data.password !== undefined) payload.password = data.password;
|
|
258
|
+
|
|
259
|
+
const response = await this.deps.query<{
|
|
260
|
+
customerUpdate: {
|
|
261
|
+
customer?: any;
|
|
262
|
+
customerAccessToken?: ShopifyCustomerToken;
|
|
263
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
264
|
+
};
|
|
265
|
+
}>(CUSTOMER_UPDATE, {
|
|
266
|
+
customerAccessToken: token.accessToken,
|
|
267
|
+
customer: payload,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const result = response.customerUpdate;
|
|
271
|
+
if (result.customerUserErrors?.length || !result.customer) {
|
|
272
|
+
throw new ProviderAPIError(
|
|
273
|
+
result.customerUserErrors?.map((entry) => entry.message).join('; ') || 'Shopify customer update failed.',
|
|
274
|
+
'shopify',
|
|
275
|
+
400,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (result.customerAccessToken?.accessToken) {
|
|
280
|
+
await this.writeToken(result.customerAccessToken);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return transformCustomer(result.customer);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
throw new ProviderAPIError(`Shopify update customer failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async isAuthenticated(): Promise<boolean> {
|
|
290
|
+
return (await this.getCurrentCustomer()) !== null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
getAuthToken(): string | null {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
async getAddresses(): Promise<Address[]> {
|
|
298
|
+
const customer = await this.fetchCurrentCustomer();
|
|
299
|
+
return customer ? transformCustomerAddresses(customer) : [];
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private toMailingAddressInput(data: Partial<CreateAddressData>): Record<string, unknown> {
|
|
303
|
+
return {
|
|
304
|
+
firstName: data.firstName,
|
|
305
|
+
lastName: data.lastName,
|
|
306
|
+
company: data.company,
|
|
307
|
+
address1: data.address1,
|
|
308
|
+
address2: data.address2,
|
|
309
|
+
city: data.city,
|
|
310
|
+
province: data.province,
|
|
311
|
+
country: data.countryCode,
|
|
312
|
+
zip: data.postalCode,
|
|
313
|
+
phone: data.phone,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async createAddress(data: CreateAddressData): Promise<Address> {
|
|
318
|
+
const token = await this.requireToken();
|
|
319
|
+
|
|
320
|
+
try {
|
|
321
|
+
const response = await this.deps.query<{
|
|
322
|
+
customerAddressCreate: {
|
|
323
|
+
customerAddress?: any;
|
|
324
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
325
|
+
};
|
|
326
|
+
}>(CUSTOMER_ADDRESS_CREATE, {
|
|
327
|
+
customerAccessToken: token.accessToken,
|
|
328
|
+
address: this.toMailingAddressInput(data),
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
if (response.customerAddressCreate.customerUserErrors?.length || !response.customerAddressCreate.customerAddress) {
|
|
332
|
+
throw new ProviderAPIError(
|
|
333
|
+
response.customerAddressCreate.customerUserErrors?.map((entry) => entry.message).join('; ') || 'Shopify address create failed.',
|
|
334
|
+
'shopify',
|
|
335
|
+
400,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return transformAddress(response.customerAddressCreate.customerAddress);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
throw new ProviderAPIError(`Shopify create address failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async updateAddress(addressId: string, data: Partial<CreateAddressData>): Promise<Address> {
|
|
346
|
+
const token = await this.requireToken();
|
|
347
|
+
|
|
348
|
+
try {
|
|
349
|
+
const response = await this.deps.query<{
|
|
350
|
+
customerAddressUpdate: {
|
|
351
|
+
customerAddress?: any;
|
|
352
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
353
|
+
};
|
|
354
|
+
}>(CUSTOMER_ADDRESS_UPDATE, {
|
|
355
|
+
customerAccessToken: token.accessToken,
|
|
356
|
+
id: addressId,
|
|
357
|
+
address: this.toMailingAddressInput(data),
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
if (response.customerAddressUpdate.customerUserErrors?.length || !response.customerAddressUpdate.customerAddress) {
|
|
361
|
+
throw new ProviderAPIError(
|
|
362
|
+
response.customerAddressUpdate.customerUserErrors?.map((entry) => entry.message).join('; ') || 'Shopify address update failed.',
|
|
363
|
+
'shopify',
|
|
364
|
+
400,
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return transformAddress(response.customerAddressUpdate.customerAddress);
|
|
369
|
+
} catch (error) {
|
|
370
|
+
throw new ProviderAPIError(`Shopify update address failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
async deleteAddress(addressId: string): Promise<void> {
|
|
375
|
+
const token = await this.requireToken();
|
|
376
|
+
|
|
377
|
+
try {
|
|
378
|
+
const response = await this.deps.query<{
|
|
379
|
+
customerAddressDelete: {
|
|
380
|
+
customerUserErrors?: Array<{ message: string }>;
|
|
381
|
+
};
|
|
382
|
+
}>(CUSTOMER_ADDRESS_DELETE, {
|
|
383
|
+
customerAccessToken: token.accessToken,
|
|
384
|
+
id: addressId,
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
if (response.customerAddressDelete.customerUserErrors?.length) {
|
|
388
|
+
throw new ProviderAPIError(
|
|
389
|
+
response.customerAddressDelete.customerUserErrors.map((entry) => entry.message).join('; '),
|
|
390
|
+
'shopify',
|
|
391
|
+
400,
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
} catch (error) {
|
|
395
|
+
throw new ProviderAPIError(`Shopify delete address failed: ${extractErrorMessage(error)}`, 'shopify', 400);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async deleteCustomerAccount(): Promise<void> {
|
|
400
|
+
throw new ProviderAPIError('Shopify customer self-service deletion requires Admin API and is not supported.', 'shopify', 501);
|
|
401
|
+
}
|
|
402
|
+
}
|