digital-tools 2.0.2 → 2.1.1
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 +17 -0
- package/package.json +3 -4
- package/src/define.js +267 -0
- package/src/entities/advertising.js +999 -0
- package/src/entities/ai.js +756 -0
- package/src/entities/analytics.js +1588 -0
- package/src/entities/automation.js +601 -0
- package/src/entities/communication.js +1150 -0
- package/src/entities/crm.js +1386 -0
- package/src/entities/design.js +546 -0
- package/src/entities/development.js +2212 -0
- package/src/entities/document.js +874 -0
- package/src/entities/ecommerce.js +1429 -0
- package/src/entities/experiment.js +1039 -0
- package/src/entities/finance.js +3478 -0
- package/src/entities/forms.js +1892 -0
- package/src/entities/hr.js +661 -0
- package/src/entities/identity.js +997 -0
- package/src/entities/index.js +282 -0
- package/src/entities/infrastructure.js +1153 -0
- package/src/entities/knowledge.js +1438 -0
- package/src/entities/marketing.js +1610 -0
- package/src/entities/media.js +1634 -0
- package/src/entities/notification.js +1199 -0
- package/src/entities/presentation.js +1274 -0
- package/src/entities/productivity.js +1317 -0
- package/src/entities/project-management.js +1136 -0
- package/src/entities/recruiting.js +736 -0
- package/src/entities/shipping.js +509 -0
- package/src/entities/signature.js +1102 -0
- package/src/entities/site.js +222 -0
- package/src/entities/spreadsheet.js +1341 -0
- package/src/entities/storage.js +1198 -0
- package/src/entities/support.js +1166 -0
- package/src/entities/video-conferencing.js +1750 -0
- package/src/entities/video.js +950 -0
- package/src/entities.js +1663 -0
- package/src/index.js +74 -0
- package/src/providers/analytics/index.js +17 -0
- package/src/providers/analytics/mixpanel.js +255 -0
- package/src/providers/calendar/cal-com.js +303 -0
- package/src/providers/calendar/google-calendar.js +335 -0
- package/src/providers/calendar/index.js +20 -0
- package/src/providers/crm/hubspot.js +566 -0
- package/src/providers/crm/index.js +17 -0
- package/src/providers/development/github.js +472 -0
- package/src/providers/development/index.js +17 -0
- package/src/providers/ecommerce/index.js +17 -0
- package/src/providers/ecommerce/shopify.js +378 -0
- package/src/providers/email/index.js +20 -0
- package/src/providers/email/resend.js +258 -0
- package/src/providers/email/sendgrid.js +161 -0
- package/src/providers/finance/index.js +17 -0
- package/src/providers/finance/stripe.js +549 -0
- package/src/providers/forms/index.js +17 -0
- package/src/providers/forms/typeform.js +500 -0
- package/src/providers/index.js +123 -0
- package/src/providers/knowledge/index.js +17 -0
- package/src/providers/knowledge/notion.js +389 -0
- package/src/providers/marketing/index.js +17 -0
- package/src/providers/marketing/mailchimp.js +443 -0
- package/src/providers/media/cloudinary.js +318 -0
- package/src/providers/media/index.js +17 -0
- package/src/providers/messaging/index.js +20 -0
- package/src/providers/messaging/slack.js +393 -0
- package/src/providers/messaging/twilio-sms.js +249 -0
- package/src/providers/project-management/index.js +17 -0
- package/src/providers/project-management/linear.js +575 -0
- package/src/providers/registry.js +86 -0
- package/src/providers/spreadsheet/google-sheets.js +375 -0
- package/src/providers/spreadsheet/index.js +20 -0
- package/src/providers/spreadsheet/xlsx.js +423 -0
- package/src/providers/storage/index.js +24 -0
- package/src/providers/storage/s3.js +419 -0
- package/src/providers/support/index.js +17 -0
- package/src/providers/support/zendesk.js +373 -0
- package/src/providers/tasks/index.js +17 -0
- package/src/providers/tasks/todoist.js +286 -0
- package/src/providers/types.js +9 -0
- package/src/providers/video-conferencing/google-meet.js +286 -0
- package/src/providers/video-conferencing/index.js +31 -0
- package/src/providers/video-conferencing/jitsi.js +254 -0
- package/src/providers/video-conferencing/teams.js +270 -0
- package/src/providers/video-conferencing/zoom.js +332 -0
- package/src/registry.js +128 -0
- package/src/tools/communication.js +184 -0
- package/src/tools/data.js +205 -0
- package/src/tools/index.js +11 -0
- package/src/tools/web.js +137 -0
- package/src/types.js +10 -0
- package/test/define.test.js +306 -0
- package/test/registry.test.js +357 -0
- package/test/tools.test.js +363 -0
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shopify E-commerce Provider
|
|
3
|
+
*
|
|
4
|
+
* Concrete implementation of EcommerceProvider using Shopify Admin REST API.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import { defineProvider } from '../registry.js';
|
|
9
|
+
const API_VERSION = '2023-10';
|
|
10
|
+
/**
|
|
11
|
+
* Shopify provider info
|
|
12
|
+
*/
|
|
13
|
+
export const shopifyInfo = {
|
|
14
|
+
id: 'ecommerce.shopify',
|
|
15
|
+
name: 'Shopify',
|
|
16
|
+
description: 'Shopify e-commerce platform',
|
|
17
|
+
category: 'ecommerce',
|
|
18
|
+
website: 'https://www.shopify.com',
|
|
19
|
+
docsUrl: 'https://shopify.dev/docs/api/admin-rest',
|
|
20
|
+
requiredConfig: ['shopDomain', 'accessToken'],
|
|
21
|
+
optionalConfig: [],
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Create Shopify e-commerce provider
|
|
25
|
+
*/
|
|
26
|
+
export function createShopifyProvider(config) {
|
|
27
|
+
let shopDomain;
|
|
28
|
+
let accessToken;
|
|
29
|
+
let baseUrl;
|
|
30
|
+
function getHeaders() {
|
|
31
|
+
return {
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
'X-Shopify-Access-Token': accessToken,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async function makeRequest(endpoint, method = 'GET', body) {
|
|
37
|
+
const response = await fetch(`${baseUrl}${endpoint}`, {
|
|
38
|
+
method,
|
|
39
|
+
headers: getHeaders(),
|
|
40
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const errorData = await response.json().catch(() => ({}));
|
|
44
|
+
throw new Error(`Shopify API error: ${response.status} - ${errorData?.errors || response.statusText}`);
|
|
45
|
+
}
|
|
46
|
+
return response.json();
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
info: shopifyInfo,
|
|
50
|
+
async initialize(cfg) {
|
|
51
|
+
shopDomain = cfg.shopDomain;
|
|
52
|
+
accessToken = cfg.accessToken;
|
|
53
|
+
if (!shopDomain) {
|
|
54
|
+
throw new Error('Shopify shop domain is required');
|
|
55
|
+
}
|
|
56
|
+
if (!accessToken) {
|
|
57
|
+
throw new Error('Shopify access token is required');
|
|
58
|
+
}
|
|
59
|
+
// Ensure domain has proper format
|
|
60
|
+
const domain = shopDomain.includes('.myshopify.com')
|
|
61
|
+
? shopDomain
|
|
62
|
+
: `${shopDomain}.myshopify.com`;
|
|
63
|
+
baseUrl = `https://${domain}/admin/api/${API_VERSION}`;
|
|
64
|
+
},
|
|
65
|
+
async healthCheck() {
|
|
66
|
+
const start = Date.now();
|
|
67
|
+
try {
|
|
68
|
+
await makeRequest('/shop.json', 'GET');
|
|
69
|
+
return {
|
|
70
|
+
healthy: true,
|
|
71
|
+
latencyMs: Date.now() - start,
|
|
72
|
+
message: 'Connected',
|
|
73
|
+
checkedAt: new Date(),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
return {
|
|
78
|
+
healthy: false,
|
|
79
|
+
latencyMs: Date.now() - start,
|
|
80
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
81
|
+
checkedAt: new Date(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
async dispose() {
|
|
86
|
+
// No cleanup needed
|
|
87
|
+
},
|
|
88
|
+
async createProduct(product) {
|
|
89
|
+
const shopifyProduct = {
|
|
90
|
+
title: product.title,
|
|
91
|
+
body_html: product.description,
|
|
92
|
+
vendor: 'Default',
|
|
93
|
+
product_type: '',
|
|
94
|
+
tags: product.tags?.join(', ') || '',
|
|
95
|
+
status: product.status || 'active',
|
|
96
|
+
};
|
|
97
|
+
// Add variants
|
|
98
|
+
if (product.variants && product.variants.length > 0) {
|
|
99
|
+
shopifyProduct.variants = product.variants.map((v) => ({
|
|
100
|
+
title: v.title,
|
|
101
|
+
price: v.price.toString(),
|
|
102
|
+
sku: v.sku,
|
|
103
|
+
inventory_quantity: v.inventory || 0,
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
// Single variant
|
|
108
|
+
shopifyProduct.variants = [
|
|
109
|
+
{
|
|
110
|
+
title: 'Default Title',
|
|
111
|
+
price: product.price.toString(),
|
|
112
|
+
sku: product.sku,
|
|
113
|
+
inventory_quantity: product.inventory || 0,
|
|
114
|
+
},
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
// Add images
|
|
118
|
+
if (product.images && product.images.length > 0) {
|
|
119
|
+
shopifyProduct.images = product.images.map((url) => ({ src: url }));
|
|
120
|
+
}
|
|
121
|
+
const response = await makeRequest('/products.json', 'POST', {
|
|
122
|
+
product: shopifyProduct,
|
|
123
|
+
});
|
|
124
|
+
return mapShopifyProduct(response.product);
|
|
125
|
+
},
|
|
126
|
+
async getProduct(productId) {
|
|
127
|
+
try {
|
|
128
|
+
const response = await makeRequest(`/products/${productId}.json`, 'GET');
|
|
129
|
+
return mapShopifyProduct(response.product);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
async updateProduct(productId, updates) {
|
|
136
|
+
const shopifyUpdates = {};
|
|
137
|
+
if (updates.title)
|
|
138
|
+
shopifyUpdates.title = updates.title;
|
|
139
|
+
if (updates.description)
|
|
140
|
+
shopifyUpdates.body_html = updates.description;
|
|
141
|
+
if (updates.tags)
|
|
142
|
+
shopifyUpdates.tags = updates.tags.join(', ');
|
|
143
|
+
if (updates.status)
|
|
144
|
+
shopifyUpdates.status = updates.status;
|
|
145
|
+
// Handle images update
|
|
146
|
+
if (updates.images) {
|
|
147
|
+
shopifyUpdates.images = updates.images.map((url) => ({ src: url }));
|
|
148
|
+
}
|
|
149
|
+
const response = await makeRequest(`/products/${productId}.json`, 'PUT', {
|
|
150
|
+
product: shopifyUpdates,
|
|
151
|
+
});
|
|
152
|
+
return mapShopifyProduct(response.product);
|
|
153
|
+
},
|
|
154
|
+
async deleteProduct(productId) {
|
|
155
|
+
try {
|
|
156
|
+
await makeRequest(`/products/${productId}.json`, 'DELETE');
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
async listProducts(options) {
|
|
164
|
+
const params = new URLSearchParams();
|
|
165
|
+
if (options?.limit)
|
|
166
|
+
params.append('limit', options.limit.toString());
|
|
167
|
+
if (options?.status)
|
|
168
|
+
params.append('status', options.status);
|
|
169
|
+
if (options?.vendor)
|
|
170
|
+
params.append('vendor', options.vendor);
|
|
171
|
+
if (options?.cursor)
|
|
172
|
+
params.append('page_info', options.cursor);
|
|
173
|
+
const queryString = params.toString();
|
|
174
|
+
const endpoint = `/products.json${queryString ? `?${queryString}` : ''}`;
|
|
175
|
+
const response = await makeRequest(endpoint, 'GET');
|
|
176
|
+
return {
|
|
177
|
+
items: response.products.map(mapShopifyProduct),
|
|
178
|
+
hasMore: response.products.length === (options?.limit || 50),
|
|
179
|
+
total: undefined, // Shopify doesn't provide total count in products endpoint
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
async getOrder(orderId) {
|
|
183
|
+
try {
|
|
184
|
+
const response = await makeRequest(`/orders/${orderId}.json`, 'GET');
|
|
185
|
+
return mapShopifyOrder(response.order);
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
async listOrders(options) {
|
|
192
|
+
const params = new URLSearchParams();
|
|
193
|
+
if (options?.limit)
|
|
194
|
+
params.append('limit', options.limit.toString());
|
|
195
|
+
if (options?.status)
|
|
196
|
+
params.append('status', options.status);
|
|
197
|
+
if (options?.financialStatus)
|
|
198
|
+
params.append('financial_status', options.financialStatus);
|
|
199
|
+
if (options?.fulfillmentStatus)
|
|
200
|
+
params.append('fulfillment_status', options.fulfillmentStatus);
|
|
201
|
+
if (options?.customerId)
|
|
202
|
+
params.append('customer_id', options.customerId);
|
|
203
|
+
if (options?.since)
|
|
204
|
+
params.append('created_at_min', options.since.toISOString());
|
|
205
|
+
if (options?.until)
|
|
206
|
+
params.append('created_at_max', options.until.toISOString());
|
|
207
|
+
if (options?.cursor)
|
|
208
|
+
params.append('page_info', options.cursor);
|
|
209
|
+
const queryString = params.toString();
|
|
210
|
+
const endpoint = `/orders.json${queryString ? `?${queryString}` : ''}`;
|
|
211
|
+
const response = await makeRequest(endpoint, 'GET');
|
|
212
|
+
return {
|
|
213
|
+
items: response.orders.map(mapShopifyOrder),
|
|
214
|
+
hasMore: response.orders.length === (options?.limit || 50),
|
|
215
|
+
total: undefined,
|
|
216
|
+
};
|
|
217
|
+
},
|
|
218
|
+
async updateOrderStatus(orderId, status) {
|
|
219
|
+
const response = await makeRequest(`/orders/${orderId}.json`, 'PUT', {
|
|
220
|
+
order: {
|
|
221
|
+
id: orderId,
|
|
222
|
+
tags: status,
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
return mapShopifyOrder(response.order);
|
|
226
|
+
},
|
|
227
|
+
async getEcommerceCustomer(customerId) {
|
|
228
|
+
try {
|
|
229
|
+
const response = await makeRequest(`/customers/${customerId}.json`, 'GET');
|
|
230
|
+
return mapShopifyCustomer(response.customer);
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
async listEcommerceCustomers(options) {
|
|
237
|
+
const params = new URLSearchParams();
|
|
238
|
+
if (options?.limit)
|
|
239
|
+
params.append('limit', options.limit.toString());
|
|
240
|
+
if (options?.cursor)
|
|
241
|
+
params.append('page_info', options.cursor);
|
|
242
|
+
const queryString = params.toString();
|
|
243
|
+
const endpoint = `/customers.json${queryString ? `?${queryString}` : ''}`;
|
|
244
|
+
const response = await makeRequest(endpoint, 'GET');
|
|
245
|
+
return {
|
|
246
|
+
items: response.customers.map(mapShopifyCustomer),
|
|
247
|
+
hasMore: response.customers.length === (options?.limit || 50),
|
|
248
|
+
total: undefined,
|
|
249
|
+
};
|
|
250
|
+
},
|
|
251
|
+
async updateInventory(productId, variantId, quantity) {
|
|
252
|
+
try {
|
|
253
|
+
// First get the inventory item ID from the variant
|
|
254
|
+
const variantResponse = await makeRequest(`/variants/${variantId}.json`, 'GET');
|
|
255
|
+
const inventoryItemId = variantResponse.variant.inventory_item_id;
|
|
256
|
+
// Get inventory levels
|
|
257
|
+
const levelsResponse = await makeRequest(`/inventory_levels.json?inventory_item_ids=${inventoryItemId}`, 'GET');
|
|
258
|
+
if (levelsResponse.inventory_levels.length === 0) {
|
|
259
|
+
throw new Error('No inventory levels found for this variant');
|
|
260
|
+
}
|
|
261
|
+
const locationId = levelsResponse.inventory_levels[0].location_id;
|
|
262
|
+
// Set the inventory level
|
|
263
|
+
await makeRequest('/inventory_levels/set.json', 'POST', {
|
|
264
|
+
location_id: locationId,
|
|
265
|
+
inventory_item_id: inventoryItemId,
|
|
266
|
+
available: quantity,
|
|
267
|
+
});
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Map Shopify product to our format
|
|
278
|
+
*/
|
|
279
|
+
function mapShopifyProduct(product) {
|
|
280
|
+
const firstVariant = product.variants?.[0];
|
|
281
|
+
return {
|
|
282
|
+
id: product.id.toString(),
|
|
283
|
+
title: product.title,
|
|
284
|
+
description: product.body_html,
|
|
285
|
+
price: parseFloat(firstVariant?.price || '0'),
|
|
286
|
+
compareAtPrice: firstVariant?.compare_at_price
|
|
287
|
+
? parseFloat(firstVariant.compare_at_price)
|
|
288
|
+
: undefined,
|
|
289
|
+
sku: firstVariant?.sku,
|
|
290
|
+
inventory: firstVariant?.inventory_quantity,
|
|
291
|
+
images: product.images?.map((img) => img.src) || [],
|
|
292
|
+
variants: product.variants?.map((v) => ({
|
|
293
|
+
id: v.id.toString(),
|
|
294
|
+
title: v.title,
|
|
295
|
+
price: parseFloat(v.price),
|
|
296
|
+
sku: v.sku,
|
|
297
|
+
inventory: v.inventory_quantity,
|
|
298
|
+
})) || [],
|
|
299
|
+
tags: product.tags ? product.tags.split(', ') : [],
|
|
300
|
+
status: product.status,
|
|
301
|
+
url: product.admin_graphql_api_id,
|
|
302
|
+
createdAt: new Date(product.created_at),
|
|
303
|
+
updatedAt: new Date(product.updated_at),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Map Shopify order to our format
|
|
308
|
+
*/
|
|
309
|
+
function mapShopifyOrder(order) {
|
|
310
|
+
return {
|
|
311
|
+
id: order.id.toString(),
|
|
312
|
+
orderNumber: order.name || order.order_number?.toString(),
|
|
313
|
+
status: order.cancelled_at ? 'cancelled' : order.closed_at ? 'closed' : 'open',
|
|
314
|
+
financialStatus: order.financial_status,
|
|
315
|
+
fulfillmentStatus: order.fulfillment_status || 'unfulfilled',
|
|
316
|
+
customerId: order.customer?.id?.toString(),
|
|
317
|
+
email: order.email || order.customer?.email || '',
|
|
318
|
+
lineItems: order.line_items?.map((item) => ({
|
|
319
|
+
productId: item.product_id?.toString() || '',
|
|
320
|
+
variantId: item.variant_id?.toString(),
|
|
321
|
+
title: item.title,
|
|
322
|
+
quantity: item.quantity,
|
|
323
|
+
price: parseFloat(item.price),
|
|
324
|
+
})) || [],
|
|
325
|
+
subtotal: parseFloat(order.subtotal_price || '0'),
|
|
326
|
+
tax: parseFloat(order.total_tax || '0'),
|
|
327
|
+
shipping: parseFloat(order.total_shipping_price_set?.shop_money?.amount || '0'),
|
|
328
|
+
total: parseFloat(order.total_price || '0'),
|
|
329
|
+
currency: order.currency,
|
|
330
|
+
shippingAddress: order.shipping_address
|
|
331
|
+
? {
|
|
332
|
+
firstName: order.shipping_address.first_name,
|
|
333
|
+
lastName: order.shipping_address.last_name,
|
|
334
|
+
address1: order.shipping_address.address1,
|
|
335
|
+
address2: order.shipping_address.address2,
|
|
336
|
+
city: order.shipping_address.city,
|
|
337
|
+
province: order.shipping_address.province,
|
|
338
|
+
postalCode: order.shipping_address.zip,
|
|
339
|
+
country: order.shipping_address.country,
|
|
340
|
+
phone: order.shipping_address.phone,
|
|
341
|
+
}
|
|
342
|
+
: undefined,
|
|
343
|
+
billingAddress: order.billing_address
|
|
344
|
+
? {
|
|
345
|
+
firstName: order.billing_address.first_name,
|
|
346
|
+
lastName: order.billing_address.last_name,
|
|
347
|
+
address1: order.billing_address.address1,
|
|
348
|
+
address2: order.billing_address.address2,
|
|
349
|
+
city: order.billing_address.city,
|
|
350
|
+
province: order.billing_address.province,
|
|
351
|
+
postalCode: order.billing_address.zip,
|
|
352
|
+
country: order.billing_address.country,
|
|
353
|
+
phone: order.billing_address.phone,
|
|
354
|
+
}
|
|
355
|
+
: undefined,
|
|
356
|
+
createdAt: new Date(order.created_at),
|
|
357
|
+
updatedAt: new Date(order.updated_at),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Map Shopify customer to our format
|
|
362
|
+
*/
|
|
363
|
+
function mapShopifyCustomer(customer) {
|
|
364
|
+
return {
|
|
365
|
+
id: customer.id.toString(),
|
|
366
|
+
email: customer.email,
|
|
367
|
+
firstName: customer.first_name,
|
|
368
|
+
lastName: customer.last_name,
|
|
369
|
+
phone: customer.phone,
|
|
370
|
+
ordersCount: customer.orders_count || 0,
|
|
371
|
+
totalSpent: parseFloat(customer.total_spent || '0'),
|
|
372
|
+
createdAt: new Date(customer.created_at),
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Shopify provider definition
|
|
377
|
+
*/
|
|
378
|
+
export const shopifyProvider = defineProvider(shopifyInfo, async (config) => createShopifyProvider(config));
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Email Providers
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
export { sendgridInfo, sendgridProvider, createSendGridProvider } from './sendgrid.js';
|
|
7
|
+
export { resendInfo, resendProvider, createResendProvider } from './resend.js';
|
|
8
|
+
import { sendgridProvider } from './sendgrid.js';
|
|
9
|
+
import { resendProvider } from './resend.js';
|
|
10
|
+
/**
|
|
11
|
+
* Register all email providers
|
|
12
|
+
*/
|
|
13
|
+
export function registerEmailProviders() {
|
|
14
|
+
sendgridProvider.register();
|
|
15
|
+
resendProvider.register();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* All email providers
|
|
19
|
+
*/
|
|
20
|
+
export const emailProviders = [sendgridProvider, resendProvider];
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resend Email Provider
|
|
3
|
+
*
|
|
4
|
+
* Concrete implementation of EmailProvider using Resend API.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import { defineProvider } from '../registry.js';
|
|
9
|
+
const RESEND_API_URL = 'https://api.resend.com';
|
|
10
|
+
/**
|
|
11
|
+
* Resend provider info
|
|
12
|
+
*/
|
|
13
|
+
export const resendInfo = {
|
|
14
|
+
id: 'email.resend',
|
|
15
|
+
name: 'Resend',
|
|
16
|
+
description: 'Modern email API for developers',
|
|
17
|
+
category: 'email',
|
|
18
|
+
website: 'https://resend.com',
|
|
19
|
+
docsUrl: 'https://resend.com/docs',
|
|
20
|
+
requiredConfig: ['apiKey'],
|
|
21
|
+
optionalConfig: ['defaultFrom'],
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Create Resend email provider
|
|
25
|
+
*/
|
|
26
|
+
export function createResendProvider(config) {
|
|
27
|
+
let apiKey;
|
|
28
|
+
let defaultFrom;
|
|
29
|
+
return {
|
|
30
|
+
info: resendInfo,
|
|
31
|
+
async initialize(cfg) {
|
|
32
|
+
apiKey = cfg.apiKey;
|
|
33
|
+
defaultFrom = cfg.defaultFrom;
|
|
34
|
+
if (!apiKey) {
|
|
35
|
+
throw new Error('Resend API key is required');
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
async healthCheck() {
|
|
39
|
+
const start = Date.now();
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(`${RESEND_API_URL}/domains`, {
|
|
42
|
+
headers: {
|
|
43
|
+
Authorization: `Bearer ${apiKey}`,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
healthy: response.ok,
|
|
48
|
+
latencyMs: Date.now() - start,
|
|
49
|
+
message: response.ok ? 'Connected' : `HTTP ${response.status}`,
|
|
50
|
+
checkedAt: new Date(),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
return {
|
|
55
|
+
healthy: false,
|
|
56
|
+
latencyMs: Date.now() - start,
|
|
57
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
58
|
+
checkedAt: new Date(),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
async dispose() {
|
|
63
|
+
// No cleanup needed
|
|
64
|
+
},
|
|
65
|
+
async send(options) {
|
|
66
|
+
const from = options.from || defaultFrom;
|
|
67
|
+
if (!from) {
|
|
68
|
+
return {
|
|
69
|
+
success: false,
|
|
70
|
+
error: { code: 'MISSING_FROM', message: 'From address is required' },
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const body = {
|
|
74
|
+
from,
|
|
75
|
+
to: options.to,
|
|
76
|
+
subject: options.subject,
|
|
77
|
+
...(options.cc && { cc: options.cc }),
|
|
78
|
+
...(options.bcc && { bcc: options.bcc }),
|
|
79
|
+
...(options.replyTo && { reply_to: options.replyTo }),
|
|
80
|
+
...(options.text && { text: options.text }),
|
|
81
|
+
...(options.html && { html: options.html }),
|
|
82
|
+
...(options.headers && { headers: options.headers }),
|
|
83
|
+
...(options.tags && { tags: options.tags.map((name) => ({ name })) }),
|
|
84
|
+
};
|
|
85
|
+
if (options.attachments?.length) {
|
|
86
|
+
body.attachments = options.attachments.map((att) => ({
|
|
87
|
+
filename: att.filename,
|
|
88
|
+
content: typeof att.content === 'string' ? att.content : att.content.toString('base64'),
|
|
89
|
+
content_type: att.contentType,
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
if (options.sendAt) {
|
|
93
|
+
body.scheduled_at = options.sendAt.toISOString();
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const response = await fetch(`${RESEND_API_URL}/emails`, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers: {
|
|
99
|
+
'Content-Type': 'application/json',
|
|
100
|
+
Authorization: `Bearer ${apiKey}`,
|
|
101
|
+
},
|
|
102
|
+
body: JSON.stringify(body),
|
|
103
|
+
});
|
|
104
|
+
const data = await response.json();
|
|
105
|
+
if (response.ok) {
|
|
106
|
+
return {
|
|
107
|
+
success: true,
|
|
108
|
+
messageId: data.id,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
success: false,
|
|
113
|
+
error: {
|
|
114
|
+
code: data.name || `HTTP_${response.status}`,
|
|
115
|
+
message: data.message || response.statusText,
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
return {
|
|
121
|
+
success: false,
|
|
122
|
+
error: {
|
|
123
|
+
code: 'NETWORK_ERROR',
|
|
124
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
async sendBatch(emails) {
|
|
130
|
+
// Resend has a batch endpoint
|
|
131
|
+
const from = defaultFrom;
|
|
132
|
+
const batch = emails.map((options) => ({
|
|
133
|
+
from: options.from || from,
|
|
134
|
+
to: options.to,
|
|
135
|
+
subject: options.subject,
|
|
136
|
+
...(options.cc && { cc: options.cc }),
|
|
137
|
+
...(options.bcc && { bcc: options.bcc }),
|
|
138
|
+
...(options.replyTo && { reply_to: options.replyTo }),
|
|
139
|
+
...(options.text && { text: options.text }),
|
|
140
|
+
...(options.html && { html: options.html }),
|
|
141
|
+
}));
|
|
142
|
+
try {
|
|
143
|
+
const response = await fetch(`${RESEND_API_URL}/emails/batch`, {
|
|
144
|
+
method: 'POST',
|
|
145
|
+
headers: {
|
|
146
|
+
'Content-Type': 'application/json',
|
|
147
|
+
Authorization: `Bearer ${apiKey}`,
|
|
148
|
+
},
|
|
149
|
+
body: JSON.stringify(batch),
|
|
150
|
+
});
|
|
151
|
+
const data = await response.json();
|
|
152
|
+
if (response.ok) {
|
|
153
|
+
return data.data.map((item) => ({
|
|
154
|
+
success: true,
|
|
155
|
+
messageId: item.id,
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
// If batch fails, return error for all
|
|
159
|
+
return emails.map(() => ({
|
|
160
|
+
success: false,
|
|
161
|
+
error: {
|
|
162
|
+
code: data.name || `HTTP_${response.status}`,
|
|
163
|
+
message: data.message || response.statusText,
|
|
164
|
+
},
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
return emails.map(() => ({
|
|
169
|
+
success: false,
|
|
170
|
+
error: {
|
|
171
|
+
code: 'NETWORK_ERROR',
|
|
172
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
173
|
+
},
|
|
174
|
+
}));
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
async get(messageId) {
|
|
178
|
+
try {
|
|
179
|
+
const response = await fetch(`${RESEND_API_URL}/emails/${messageId}`, {
|
|
180
|
+
headers: {
|
|
181
|
+
Authorization: `Bearer ${apiKey}`,
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
if (!response.ok) {
|
|
185
|
+
if (response.status === 404)
|
|
186
|
+
return null;
|
|
187
|
+
throw new Error(`HTTP ${response.status}`);
|
|
188
|
+
}
|
|
189
|
+
const data = (await response.json());
|
|
190
|
+
return {
|
|
191
|
+
id: data.id,
|
|
192
|
+
from: data.from,
|
|
193
|
+
to: Array.isArray(data.to) ? data.to : [data.to],
|
|
194
|
+
cc: data.cc,
|
|
195
|
+
bcc: data.bcc,
|
|
196
|
+
subject: data.subject,
|
|
197
|
+
text: data.text,
|
|
198
|
+
html: data.html,
|
|
199
|
+
status: mapResendStatus(data.last_event),
|
|
200
|
+
sentAt: data.created_at ? new Date(data.created_at) : undefined,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
async verifyDomain(domain) {
|
|
208
|
+
const response = await fetch(`${RESEND_API_URL}/domains`, {
|
|
209
|
+
method: 'POST',
|
|
210
|
+
headers: {
|
|
211
|
+
'Content-Type': 'application/json',
|
|
212
|
+
Authorization: `Bearer ${apiKey}`,
|
|
213
|
+
},
|
|
214
|
+
body: JSON.stringify({ name: domain }),
|
|
215
|
+
});
|
|
216
|
+
const data = (await response.json());
|
|
217
|
+
return {
|
|
218
|
+
domain: data.name,
|
|
219
|
+
verified: data.status === 'verified',
|
|
220
|
+
dnsRecords: data.records?.map((r) => ({
|
|
221
|
+
type: r.type,
|
|
222
|
+
name: r.name,
|
|
223
|
+
value: r.value,
|
|
224
|
+
verified: r.status === 'verified',
|
|
225
|
+
})) || [],
|
|
226
|
+
};
|
|
227
|
+
},
|
|
228
|
+
async listDomains() {
|
|
229
|
+
const response = await fetch(`${RESEND_API_URL}/domains`, {
|
|
230
|
+
headers: {
|
|
231
|
+
Authorization: `Bearer ${apiKey}`,
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
const data = (await response.json());
|
|
235
|
+
return data.data?.map((d) => ({
|
|
236
|
+
domain: d.name,
|
|
237
|
+
verified: d.status === 'verified',
|
|
238
|
+
createdAt: new Date(d.created_at),
|
|
239
|
+
})) || [];
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function mapResendStatus(event) {
|
|
244
|
+
switch (event) {
|
|
245
|
+
case 'delivered':
|
|
246
|
+
return 'delivered';
|
|
247
|
+
case 'bounced':
|
|
248
|
+
return 'bounced';
|
|
249
|
+
case 'complained':
|
|
250
|
+
return 'failed';
|
|
251
|
+
default:
|
|
252
|
+
return 'sent';
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Resend provider definition
|
|
257
|
+
*/
|
|
258
|
+
export const resendProvider = defineProvider(resendInfo, async (config) => createResendProvider(config));
|