n8n-nodes-lemonsqueezy 0.4.0 → 0.5.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/README.md +90 -1
- package/dist/nodes/LemonSqueezy/LemonSqueezy.node.js +34 -5
- package/dist/nodes/LemonSqueezy/LemonSqueezyTrigger.node.js +78 -33
- package/dist/nodes/LemonSqueezy/helpers.d.ts +312 -17
- package/dist/nodes/LemonSqueezy/helpers.js +360 -24
- package/dist/nodes/LemonSqueezy/resources/index.js +9 -0
- package/dist/nodes/LemonSqueezy/resources/shared.d.ts +64 -0
- package/dist/nodes/LemonSqueezy/resources/shared.js +196 -0
- package/dist/nodes/LemonSqueezy/types.d.ts +63 -7
- package/package.json +1 -1
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Lemon Squeezy API Helper Functions
|
|
4
|
+
*
|
|
5
|
+
* This module provides utility functions for:
|
|
6
|
+
* - Input validation (email, URL, date, etc.)
|
|
7
|
+
* - API request handling with retry logic
|
|
8
|
+
* - Webhook signature verification
|
|
9
|
+
* - JSON:API body construction
|
|
10
|
+
* - Query parameter building
|
|
11
|
+
*
|
|
12
|
+
* @module helpers
|
|
13
|
+
*/
|
|
2
14
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
15
|
if (k2 === undefined) k2 = k;
|
|
4
16
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -39,6 +51,9 @@ exports.isValidIsoDate = isValidIsoDate;
|
|
|
39
51
|
exports.isPositiveInteger = isPositiveInteger;
|
|
40
52
|
exports.validateField = validateField;
|
|
41
53
|
exports.safeJsonParse = safeJsonParse;
|
|
54
|
+
exports.sleep = sleep;
|
|
55
|
+
exports.isRateLimitError = isRateLimitError;
|
|
56
|
+
exports.isRetryableError = isRetryableError;
|
|
42
57
|
exports.lemonSqueezyApiRequest = lemonSqueezyApiRequest;
|
|
43
58
|
exports.lemonSqueezyApiRequestAllItems = lemonSqueezyApiRequestAllItems;
|
|
44
59
|
exports.validateRequiredFields = validateRequiredFields;
|
|
@@ -56,39 +71,146 @@ const constants_1 = require("./constants");
|
|
|
56
71
|
// Validation Helpers
|
|
57
72
|
// ============================================================================
|
|
58
73
|
/**
|
|
59
|
-
*
|
|
74
|
+
* Validates email format using RFC 5322 compliant regex.
|
|
75
|
+
*
|
|
76
|
+
* The validation checks for:
|
|
77
|
+
* - Valid local part characters (alphanumeric and special chars)
|
|
78
|
+
* - Single @ symbol
|
|
79
|
+
* - Valid domain with proper TLD (at least one dot)
|
|
80
|
+
*
|
|
81
|
+
* @param email - The email address to validate
|
|
82
|
+
* @returns True if the email format is valid, false otherwise
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* isValidEmail('user@example.com') // true
|
|
86
|
+
* isValidEmail('invalid') // false
|
|
87
|
+
* isValidEmail('user@localhost') // false (no TLD)
|
|
60
88
|
*/
|
|
61
89
|
function isValidEmail(email) {
|
|
62
|
-
const emailRegex = /^[
|
|
90
|
+
const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$/;
|
|
63
91
|
return emailRegex.test(email);
|
|
64
92
|
}
|
|
65
93
|
/**
|
|
66
|
-
*
|
|
94
|
+
* Validates URL format and ensures it's a safe external URL.
|
|
95
|
+
*
|
|
96
|
+
* Security features:
|
|
97
|
+
* - Only allows http:// and https:// protocols
|
|
98
|
+
* - Blocks localhost and loopback addresses (127.0.0.1, ::1, [::1])
|
|
99
|
+
* - Blocks private network ranges (10.x, 172.16-31.x, 192.168.x)
|
|
100
|
+
* - Blocks link-local addresses (169.254.x - AWS metadata endpoint)
|
|
101
|
+
*
|
|
102
|
+
* This prevents Server-Side Request Forgery (SSRF) attacks.
|
|
103
|
+
*
|
|
104
|
+
* @param url - The URL to validate
|
|
105
|
+
* @returns True if the URL is valid and safe, false otherwise
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* isValidUrl('https://example.com') // true
|
|
109
|
+
* isValidUrl('http://localhost:3000') // false (internal)
|
|
110
|
+
* isValidUrl('ftp://files.example.com') // false (non-http protocol)
|
|
111
|
+
* isValidUrl('http://169.254.169.254') // false (AWS metadata)
|
|
67
112
|
*/
|
|
68
113
|
function isValidUrl(url) {
|
|
69
114
|
try {
|
|
70
|
-
new URL(url);
|
|
115
|
+
const parsedUrl = new URL(url);
|
|
116
|
+
// Only allow http and https protocols (security: prevent file://, javascript:, etc.)
|
|
117
|
+
if (!['http:', 'https:'].includes(parsedUrl.protocol)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
// Block internal/private network URLs for SSRF protection
|
|
121
|
+
const hostname = parsedUrl.hostname.toLowerCase();
|
|
122
|
+
const blockedPatterns = [
|
|
123
|
+
'localhost', // Loopback hostname
|
|
124
|
+
'127.0.0.1', // IPv4 loopback
|
|
125
|
+
'0.0.0.0', // All interfaces
|
|
126
|
+
'::1', // IPv6 loopback
|
|
127
|
+
'[::1]', // IPv6 loopback (bracketed form)
|
|
128
|
+
'10.', // Private Class A (10.0.0.0/8)
|
|
129
|
+
'172.16.', // Private Class B start (172.16.0.0/12)
|
|
130
|
+
'172.17.',
|
|
131
|
+
'172.18.',
|
|
132
|
+
'172.19.',
|
|
133
|
+
'172.20.',
|
|
134
|
+
'172.21.',
|
|
135
|
+
'172.22.',
|
|
136
|
+
'172.23.',
|
|
137
|
+
'172.24.',
|
|
138
|
+
'172.25.',
|
|
139
|
+
'172.26.',
|
|
140
|
+
'172.27.',
|
|
141
|
+
'172.28.',
|
|
142
|
+
'172.29.',
|
|
143
|
+
'172.30.',
|
|
144
|
+
'172.31.', // Private Class B end
|
|
145
|
+
'192.168.', // Private Class C (192.168.0.0/16)
|
|
146
|
+
'169.254.', // Link-local / APIPA (includes AWS metadata endpoint)
|
|
147
|
+
];
|
|
148
|
+
for (const pattern of blockedPatterns) {
|
|
149
|
+
if (hostname === pattern || hostname.startsWith(pattern)) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
71
153
|
return true;
|
|
72
154
|
}
|
|
73
155
|
catch {
|
|
156
|
+
// URL parsing failed - invalid URL
|
|
74
157
|
return false;
|
|
75
158
|
}
|
|
76
159
|
}
|
|
77
160
|
/**
|
|
78
|
-
*
|
|
161
|
+
* Validates ISO 8601 date format.
|
|
162
|
+
*
|
|
163
|
+
* Accepts dates in formats like:
|
|
164
|
+
* - 2024-01-15
|
|
165
|
+
* - 2024-01-15T10:30:00Z
|
|
166
|
+
* - 2024-01-15T10:30:00.000Z
|
|
167
|
+
*
|
|
168
|
+
* @param dateString - The date string to validate
|
|
169
|
+
* @returns True if the date is valid ISO 8601 format, false otherwise
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* isValidIsoDate('2024-01-15T10:30:00Z') // true
|
|
173
|
+
* isValidIsoDate('invalid') // false
|
|
174
|
+
* isValidIsoDate('01/15/2024') // false (no dash separator)
|
|
79
175
|
*/
|
|
80
176
|
function isValidIsoDate(dateString) {
|
|
81
177
|
const date = new Date(dateString);
|
|
82
178
|
return !isNaN(date.getTime()) && dateString.includes('-');
|
|
83
179
|
}
|
|
84
180
|
/**
|
|
85
|
-
*
|
|
181
|
+
* Validates that a value is a positive integer (greater than 0).
|
|
182
|
+
*
|
|
183
|
+
* @param value - The value to validate
|
|
184
|
+
* @returns True if the value is a positive integer, false otherwise
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* isPositiveInteger(5) // true
|
|
188
|
+
* isPositiveInteger(0) // false
|
|
189
|
+
* isPositiveInteger(-1) // false
|
|
190
|
+
* isPositiveInteger(3.14) // false
|
|
191
|
+
* isPositiveInteger('5') // false (string, not number)
|
|
86
192
|
*/
|
|
87
193
|
function isPositiveInteger(value) {
|
|
88
194
|
return typeof value === 'number' && Number.isInteger(value) && value > 0;
|
|
89
195
|
}
|
|
90
196
|
/**
|
|
91
|
-
*
|
|
197
|
+
* Validates a field value and throws a descriptive error if invalid.
|
|
198
|
+
*
|
|
199
|
+
* Supports multiple validation types:
|
|
200
|
+
* - 'required': Ensures value is not empty/null/undefined
|
|
201
|
+
* - 'email': RFC 5322 compliant email validation
|
|
202
|
+
* - 'url': Safe URL validation with SSRF protection
|
|
203
|
+
* - 'date': ISO 8601 date format validation
|
|
204
|
+
* - 'positiveInteger': Positive integer validation
|
|
205
|
+
*
|
|
206
|
+
* @param fieldName - The name of the field (used in error messages)
|
|
207
|
+
* @param value - The value to validate
|
|
208
|
+
* @param validationType - The type of validation to perform
|
|
209
|
+
* @throws Error with descriptive message if validation fails
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* validateField('email', 'user@example.com', 'email') // passes
|
|
213
|
+
* validateField('email', 'invalid', 'email') // throws "email must be a valid email address"
|
|
92
214
|
*/
|
|
93
215
|
function validateField(fieldName, value, validationType) {
|
|
94
216
|
if (validationType === 'required') {
|
|
@@ -125,7 +247,20 @@ function validateField(fieldName, value, validationType) {
|
|
|
125
247
|
}
|
|
126
248
|
}
|
|
127
249
|
/**
|
|
128
|
-
* Safely
|
|
250
|
+
* Safely parses a JSON string with descriptive error handling.
|
|
251
|
+
*
|
|
252
|
+
* @template T - The expected type of the parsed JSON
|
|
253
|
+
* @param jsonString - The JSON string to parse
|
|
254
|
+
* @param fieldName - The name of the field (used in error messages)
|
|
255
|
+
* @returns The parsed JSON object
|
|
256
|
+
* @throws Error if the JSON is invalid
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* const data = safeJsonParse<{name: string}>('{"name": "test"}', 'config')
|
|
260
|
+
* // Returns: {name: "test"}
|
|
261
|
+
*
|
|
262
|
+
* safeJsonParse('invalid json', 'config')
|
|
263
|
+
* // Throws: "config contains invalid JSON"
|
|
129
264
|
*/
|
|
130
265
|
function safeJsonParse(jsonString, fieldName) {
|
|
131
266
|
try {
|
|
@@ -136,13 +271,31 @@ function safeJsonParse(jsonString, fieldName) {
|
|
|
136
271
|
}
|
|
137
272
|
}
|
|
138
273
|
/**
|
|
139
|
-
*
|
|
274
|
+
* Pauses execution for a specified duration.
|
|
275
|
+
*
|
|
276
|
+
* Used for implementing retry delays and rate limit backoff.
|
|
277
|
+
*
|
|
278
|
+
* @param ms - The number of milliseconds to sleep
|
|
279
|
+
* @returns A promise that resolves after the specified duration
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* await sleep(1000) // Wait 1 second
|
|
140
283
|
*/
|
|
141
284
|
function sleep(ms) {
|
|
142
285
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
143
286
|
}
|
|
144
287
|
/**
|
|
145
|
-
*
|
|
288
|
+
* Checks if an error is a rate limit error (HTTP 429).
|
|
289
|
+
*
|
|
290
|
+
* Handles both direct statusCode and nested response.statusCode patterns.
|
|
291
|
+
*
|
|
292
|
+
* @param error - The error object to check
|
|
293
|
+
* @returns True if the error is a rate limit error, false otherwise
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* isRateLimitError({ statusCode: 429 }) // true
|
|
297
|
+
* isRateLimitError({ response: { statusCode: 429 } }) // true
|
|
298
|
+
* isRateLimitError({ statusCode: 500 }) // false
|
|
146
299
|
*/
|
|
147
300
|
function isRateLimitError(error) {
|
|
148
301
|
var _a;
|
|
@@ -153,7 +306,19 @@ function isRateLimitError(error) {
|
|
|
153
306
|
return false;
|
|
154
307
|
}
|
|
155
308
|
/**
|
|
156
|
-
*
|
|
309
|
+
* Checks if an error is retryable (5xx server errors or network errors).
|
|
310
|
+
*
|
|
311
|
+
* Retryable conditions:
|
|
312
|
+
* - HTTP 5xx status codes (500-599)
|
|
313
|
+
* - Network errors: ECONNRESET, ETIMEDOUT, ECONNREFUSED
|
|
314
|
+
*
|
|
315
|
+
* @param error - The error object to check
|
|
316
|
+
* @returns True if the error is retryable, false otherwise
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* isRetryableError({ statusCode: 503 }) // true (server error)
|
|
320
|
+
* isRetryableError({ code: 'ECONNRESET' }) // true (network error)
|
|
321
|
+
* isRetryableError({ statusCode: 404 }) // false (client error)
|
|
157
322
|
*/
|
|
158
323
|
function isRetryableError(error) {
|
|
159
324
|
var _a;
|
|
@@ -170,7 +335,30 @@ function isRetryableError(error) {
|
|
|
170
335
|
return false;
|
|
171
336
|
}
|
|
172
337
|
/**
|
|
173
|
-
*
|
|
338
|
+
* Makes an authenticated request to the Lemon Squeezy API with retry logic.
|
|
339
|
+
*
|
|
340
|
+
* Features:
|
|
341
|
+
* - Automatic authentication using stored credentials
|
|
342
|
+
* - Rate limit handling with automatic retry after delay
|
|
343
|
+
* - Exponential backoff for server errors (5xx)
|
|
344
|
+
* - Detailed error messages using NodeApiError
|
|
345
|
+
*
|
|
346
|
+
* @param this - The n8n execution context
|
|
347
|
+
* @param method - HTTP method (GET, POST, PATCH, DELETE)
|
|
348
|
+
* @param endpoint - API endpoint path (e.g., '/v1/products')
|
|
349
|
+
* @param body - Optional request body for POST/PATCH requests
|
|
350
|
+
* @param qs - Optional query string parameters
|
|
351
|
+
* @returns The API response data
|
|
352
|
+
* @throws NodeApiError if the request fails after all retries
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* // GET request
|
|
356
|
+
* const product = await lemonSqueezyApiRequest.call(this, 'GET', '/v1/products/123')
|
|
357
|
+
*
|
|
358
|
+
* // POST request with body
|
|
359
|
+
* const checkout = await lemonSqueezyApiRequest.call(this, 'POST', '/v1/checkouts', {
|
|
360
|
+
* data: { type: 'checkouts', attributes: { ... } }
|
|
361
|
+
* })
|
|
174
362
|
*/
|
|
175
363
|
async function lemonSqueezyApiRequest(method, endpoint, body, qs = {}) {
|
|
176
364
|
const options = {
|
|
@@ -211,14 +399,49 @@ async function lemonSqueezyApiRequest(method, endpoint, body, qs = {}) {
|
|
|
211
399
|
});
|
|
212
400
|
}
|
|
213
401
|
/**
|
|
214
|
-
*
|
|
402
|
+
* Makes paginated requests to fetch all items from a Lemon Squeezy API endpoint.
|
|
403
|
+
*
|
|
404
|
+
* Automatically handles pagination by following 'next' links until all items
|
|
405
|
+
* are retrieved. Includes rate limit handling for long-running fetches.
|
|
406
|
+
*
|
|
407
|
+
* Features:
|
|
408
|
+
* - Optional maxItems limit to prevent memory issues with large datasets
|
|
409
|
+
* - Optional timeout to prevent long-running requests
|
|
410
|
+
* - Rate limit handling with automatic retry
|
|
411
|
+
*
|
|
412
|
+
* @param this - The n8n execution context
|
|
413
|
+
* @param method - HTTP method (typically 'GET')
|
|
414
|
+
* @param endpoint - API endpoint path (e.g., '/v1/products')
|
|
415
|
+
* @param qs - Optional query string parameters (filters, sorting, etc.)
|
|
416
|
+
* @param paginationOptions - Optional pagination configuration
|
|
417
|
+
* @returns Array of all items from all pages (up to maxItems if specified)
|
|
418
|
+
* @throws NodeApiError if any request fails or timeout is exceeded
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* // Fetch all products with filtering
|
|
422
|
+
* const products = await lemonSqueezyApiRequestAllItems.call(
|
|
423
|
+
* this, 'GET', '/v1/products', { 'filter[store_id]': 123 }
|
|
424
|
+
* )
|
|
425
|
+
*
|
|
426
|
+
* // Fetch with limits
|
|
427
|
+
* const products = await lemonSqueezyApiRequestAllItems.call(
|
|
428
|
+
* this, 'GET', '/v1/products', {}, { maxItems: 1000, timeout: 60000 }
|
|
429
|
+
* )
|
|
215
430
|
*/
|
|
216
|
-
async function lemonSqueezyApiRequestAllItems(method, endpoint, qs = {}) {
|
|
431
|
+
async function lemonSqueezyApiRequestAllItems(method, endpoint, qs = {}, paginationOptions = {}) {
|
|
217
432
|
var _a;
|
|
218
433
|
const returnData = [];
|
|
219
434
|
let nextPageUrl = `${constants_1.API_BASE_URL}${endpoint}`;
|
|
220
|
-
|
|
435
|
+
const { maxItems, timeout = 300000, pageSize = constants_1.DEFAULT_PAGE_SIZE } = paginationOptions;
|
|
436
|
+
const startTime = Date.now();
|
|
437
|
+
qs['page[size]'] = pageSize;
|
|
221
438
|
do {
|
|
439
|
+
// Check timeout
|
|
440
|
+
if (Date.now() - startTime > timeout) {
|
|
441
|
+
throw new n8n_workflow_1.NodeApiError(this.getNode(), {}, {
|
|
442
|
+
message: `Pagination timeout exceeded (${timeout}ms). Retrieved ${returnData.length} items before timeout.`,
|
|
443
|
+
});
|
|
444
|
+
}
|
|
222
445
|
const options = {
|
|
223
446
|
method,
|
|
224
447
|
url: nextPageUrl,
|
|
@@ -238,7 +461,12 @@ async function lemonSqueezyApiRequestAllItems(method, endpoint, qs = {}) {
|
|
|
238
461
|
message: getErrorMessage(error),
|
|
239
462
|
});
|
|
240
463
|
}
|
|
241
|
-
|
|
464
|
+
const pageData = responseData.data;
|
|
465
|
+
returnData.push(...pageData);
|
|
466
|
+
// Check maxItems limit
|
|
467
|
+
if (maxItems && returnData.length >= maxItems) {
|
|
468
|
+
return returnData.slice(0, maxItems);
|
|
469
|
+
}
|
|
242
470
|
nextPageUrl = ((_a = responseData.links) === null || _a === void 0 ? void 0 : _a.next) || null;
|
|
243
471
|
} while (nextPageUrl);
|
|
244
472
|
return returnData;
|
|
@@ -293,7 +521,15 @@ function getErrorMessage(error) {
|
|
|
293
521
|
return 'An unknown error occurred';
|
|
294
522
|
}
|
|
295
523
|
/**
|
|
296
|
-
*
|
|
524
|
+
* Validates that all required fields are present and non-empty.
|
|
525
|
+
*
|
|
526
|
+
* @param fields - Object containing field values to validate
|
|
527
|
+
* @param requiredFields - Array of field names that are required
|
|
528
|
+
* @throws Error listing all missing fields if any are empty
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* validateRequiredFields({ name: 'Test', email: '' }, ['name', 'email'])
|
|
532
|
+
* // Throws: "Missing required fields: email"
|
|
297
533
|
*/
|
|
298
534
|
function validateRequiredFields(fields, requiredFields) {
|
|
299
535
|
const missingFields = [];
|
|
@@ -307,7 +543,17 @@ function validateRequiredFields(fields, requiredFields) {
|
|
|
307
543
|
}
|
|
308
544
|
}
|
|
309
545
|
/**
|
|
310
|
-
*
|
|
546
|
+
* Builds filter query string parameters for Lemon Squeezy API.
|
|
547
|
+
*
|
|
548
|
+
* Converts camelCase field names to snake_case and wraps them in
|
|
549
|
+
* filter[] syntax as required by the API.
|
|
550
|
+
*
|
|
551
|
+
* @param filters - Object containing filter key-value pairs
|
|
552
|
+
* @returns Query string parameters object for API request
|
|
553
|
+
*
|
|
554
|
+
* @example
|
|
555
|
+
* buildFilterParams({ storeId: 123, status: 'active' })
|
|
556
|
+
* // Returns: { 'filter[store_id]': 123, 'filter[status]': 'active' }
|
|
311
557
|
*/
|
|
312
558
|
function buildFilterParams(filters) {
|
|
313
559
|
const qs = {};
|
|
@@ -321,7 +567,24 @@ function buildFilterParams(filters) {
|
|
|
321
567
|
return qs;
|
|
322
568
|
}
|
|
323
569
|
/**
|
|
324
|
-
*
|
|
570
|
+
* Builds a JSON:API compliant request body for create/update operations.
|
|
571
|
+
*
|
|
572
|
+
* Constructs the proper structure expected by Lemon Squeezy API:
|
|
573
|
+
* - data.type: Resource type (e.g., 'checkouts', 'customers')
|
|
574
|
+
* - data.attributes: Resource attributes
|
|
575
|
+
* - data.relationships: Optional related resource references
|
|
576
|
+
* - data.id: Optional resource ID (for updates)
|
|
577
|
+
*
|
|
578
|
+
* @param type - The JSON:API resource type
|
|
579
|
+
* @param attributes - Resource attributes to include
|
|
580
|
+
* @param relationships - Optional relationships to other resources
|
|
581
|
+
* @param id - Optional resource ID (required for updates)
|
|
582
|
+
* @returns Properly structured JSON:API request body
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
* buildJsonApiBody('customers', { name: 'John', email: 'john@example.com' },
|
|
586
|
+
* { store: { type: 'stores', id: '123' } })
|
|
587
|
+
* // Returns: { data: { type: 'customers', attributes: {...}, relationships: {...} } }
|
|
325
588
|
*/
|
|
326
589
|
function buildJsonApiBody(type, attributes, relationships, id) {
|
|
327
590
|
const body = {
|
|
@@ -348,7 +611,21 @@ function buildJsonApiBody(type, attributes, relationships, id) {
|
|
|
348
611
|
return body;
|
|
349
612
|
}
|
|
350
613
|
/**
|
|
351
|
-
*
|
|
614
|
+
* Verifies a webhook signature using HMAC-SHA256.
|
|
615
|
+
*
|
|
616
|
+
* Uses timing-safe comparison to prevent timing attacks.
|
|
617
|
+
*
|
|
618
|
+
* @param payload - The raw webhook payload string
|
|
619
|
+
* @param signature - The signature from X-Signature header
|
|
620
|
+
* @param secret - The webhook signing secret
|
|
621
|
+
* @returns True if signature is valid, false otherwise
|
|
622
|
+
*
|
|
623
|
+
* @example
|
|
624
|
+
* const isValid = verifyWebhookSignature(
|
|
625
|
+
* '{"data": {...}}',
|
|
626
|
+
* 'abc123signature',
|
|
627
|
+
* 'webhook_secret_key'
|
|
628
|
+
* )
|
|
352
629
|
*/
|
|
353
630
|
function verifyWebhookSignature(payload, signature, secret) {
|
|
354
631
|
const hmac = crypto.createHmac('sha256', secret);
|
|
@@ -364,7 +641,20 @@ function verifyWebhookSignature(payload, signature, secret) {
|
|
|
364
641
|
// Advanced Query Helpers
|
|
365
642
|
// ============================================================================
|
|
366
643
|
/**
|
|
367
|
-
*
|
|
644
|
+
* Builds query parameters for including related resources in API responses.
|
|
645
|
+
*
|
|
646
|
+
* Uses the JSON:API include parameter to fetch related resources in a single
|
|
647
|
+
* request, reducing the number of API calls needed.
|
|
648
|
+
*
|
|
649
|
+
* @param includes - Array of relationship names to include
|
|
650
|
+
* @returns Query string parameters object with 'include' key
|
|
651
|
+
*
|
|
652
|
+
* @example
|
|
653
|
+
* buildIncludeParams(['store', 'customer', 'order-items'])
|
|
654
|
+
* // Returns: { include: 'store,customer,order-items' }
|
|
655
|
+
*
|
|
656
|
+
* buildIncludeParams([])
|
|
657
|
+
* // Returns: {}
|
|
368
658
|
*/
|
|
369
659
|
function buildIncludeParams(includes) {
|
|
370
660
|
if (includes.length === 0) {
|
|
@@ -373,7 +663,27 @@ function buildIncludeParams(includes) {
|
|
|
373
663
|
return { include: includes.join(',') };
|
|
374
664
|
}
|
|
375
665
|
/**
|
|
376
|
-
*
|
|
666
|
+
* Builds advanced filter parameters with support for date ranges and sorting.
|
|
667
|
+
*
|
|
668
|
+
* Features:
|
|
669
|
+
* - Converts camelCase to snake_case for API compatibility
|
|
670
|
+
* - Handles date range filters with _after/_before suffixes
|
|
671
|
+
* - Adds sorting with ascending/descending direction
|
|
672
|
+
*
|
|
673
|
+
* @param filters - Object containing filter key-value pairs
|
|
674
|
+
* @param options - Optional configuration for date fields and sorting
|
|
675
|
+
* @param options.dateFields - Array of field names that are date ranges
|
|
676
|
+
* @param options.sortField - Field name to sort by
|
|
677
|
+
* @param options.sortDirection - Sort direction ('asc' or 'desc')
|
|
678
|
+
* @returns Query string parameters object for API request
|
|
679
|
+
*
|
|
680
|
+
* @example
|
|
681
|
+
* buildAdvancedFilterParams(
|
|
682
|
+
* { status: 'active', createdAt: { from: '2024-01-01', to: '2024-12-31' } },
|
|
683
|
+
* { dateFields: ['createdAt'], sortField: 'created_at', sortDirection: 'desc' }
|
|
684
|
+
* )
|
|
685
|
+
* // Returns: { 'filter[status]': 'active', 'filter[created_at_after]': '2024-01-01',
|
|
686
|
+
* // 'filter[created_at_before]': '2024-12-31', sort: '-created_at' }
|
|
377
687
|
*/
|
|
378
688
|
function buildAdvancedFilterParams(filters, options) {
|
|
379
689
|
var _a;
|
|
@@ -412,7 +722,19 @@ function buildAdvancedFilterParams(filters, options) {
|
|
|
412
722
|
return qs;
|
|
413
723
|
}
|
|
414
724
|
/**
|
|
415
|
-
*
|
|
725
|
+
* Extracts the 'data' field from a JSON:API response with proper typing.
|
|
726
|
+
*
|
|
727
|
+
* JSON:API responses wrap the actual resource data in a 'data' field.
|
|
728
|
+
* This helper extracts it while preserving type information.
|
|
729
|
+
*
|
|
730
|
+
* @template T - The expected type of the extracted data
|
|
731
|
+
* @param response - The full JSON:API response object
|
|
732
|
+
* @returns The extracted data, or undefined if not present
|
|
733
|
+
*
|
|
734
|
+
* @example
|
|
735
|
+
* const response = { data: { id: '1', type: 'products', attributes: {...} } }
|
|
736
|
+
* const product = extractResponseData<Product>(response)
|
|
737
|
+
* // Returns: { id: '1', type: 'products', attributes: {...} }
|
|
416
738
|
*/
|
|
417
739
|
function extractResponseData(response) {
|
|
418
740
|
if (!response || typeof response !== 'object') {
|
|
@@ -421,7 +743,21 @@ function extractResponseData(response) {
|
|
|
421
743
|
return response.data;
|
|
422
744
|
}
|
|
423
745
|
/**
|
|
424
|
-
*
|
|
746
|
+
* Extracts included related resources from a JSON:API response.
|
|
747
|
+
*
|
|
748
|
+
* When using the 'include' query parameter, related resources are returned
|
|
749
|
+
* in the 'included' array of the response. This helper extracts them.
|
|
750
|
+
*
|
|
751
|
+
* @param response - The full JSON:API response object
|
|
752
|
+
* @returns Array of included resources, or empty array if none
|
|
753
|
+
*
|
|
754
|
+
* @example
|
|
755
|
+
* const response = {
|
|
756
|
+
* data: {...},
|
|
757
|
+
* included: [{ id: '1', type: 'stores', attributes: {...} }]
|
|
758
|
+
* }
|
|
759
|
+
* const stores = extractIncludedResources(response)
|
|
760
|
+
* // Returns: [{ id: '1', type: 'stores', attributes: {...} }]
|
|
425
761
|
*/
|
|
426
762
|
function extractIncludedResources(response) {
|
|
427
763
|
if (!response || typeof response !== 'object') {
|
|
@@ -49,6 +49,7 @@ Object.defineProperty(exports, "usageRecordFields", { enumerable: true, get: fun
|
|
|
49
49
|
const user_1 = require("./user");
|
|
50
50
|
Object.defineProperty(exports, "userOperations", { enumerable: true, get: function () { return user_1.userOperations; } });
|
|
51
51
|
Object.defineProperty(exports, "userFields", { enumerable: true, get: function () { return user_1.userFields; } });
|
|
52
|
+
const shared_1 = require("./shared");
|
|
52
53
|
exports.resourceProperty = {
|
|
53
54
|
displayName: 'Resource',
|
|
54
55
|
name: 'resource',
|
|
@@ -94,18 +95,26 @@ exports.allOperations = [
|
|
|
94
95
|
];
|
|
95
96
|
exports.allFields = [
|
|
96
97
|
...product_1.productFields,
|
|
98
|
+
shared_1.productAdvancedOptions,
|
|
97
99
|
...order_1.orderFields,
|
|
100
|
+
shared_1.orderAdvancedOptions,
|
|
98
101
|
...orderItem_1.orderItemFields,
|
|
99
102
|
...subscription_1.subscriptionFields,
|
|
103
|
+
shared_1.subscriptionAdvancedOptions,
|
|
100
104
|
...subscriptionInvoice_1.subscriptionInvoiceFields,
|
|
101
105
|
...customer_1.customerFields,
|
|
106
|
+
shared_1.customerAdvancedOptions,
|
|
102
107
|
...licenseKey_1.licenseKeyFields,
|
|
108
|
+
shared_1.licenseKeyAdvancedOptions,
|
|
103
109
|
...licenseKeyInstance_1.licenseKeyInstanceFields,
|
|
104
110
|
...discount_1.discountFields,
|
|
111
|
+
shared_1.discountAdvancedOptions,
|
|
105
112
|
...discountRedemption_1.discountRedemptionFields,
|
|
106
113
|
...store_1.storeFields,
|
|
107
114
|
...variant_1.variantFields,
|
|
115
|
+
shared_1.variantAdvancedOptions,
|
|
108
116
|
...checkout_1.checkoutFields,
|
|
117
|
+
shared_1.checkoutAdvancedOptions,
|
|
109
118
|
...webhook_1.webhookFields,
|
|
110
119
|
...usageRecord_1.usageRecordFields,
|
|
111
120
|
...user_1.userFields,
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { INodeProperties } from 'n8n-workflow';
|
|
2
|
+
/**
|
|
3
|
+
* Shared field definitions for advanced query options
|
|
4
|
+
* Used across multiple resources for consistent sorting and relationship expansion
|
|
5
|
+
*
|
|
6
|
+
* This module reduces code duplication by providing reusable field definitions
|
|
7
|
+
* for common patterns like pagination (returnAll, limit), sorting, and filtering.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Creates a 'Return All' toggle field for a resource
|
|
11
|
+
* @param resource - The resource name (e.g., 'product', 'order')
|
|
12
|
+
* @param operations - Operations where this field applies (default: ['getAll'])
|
|
13
|
+
*/
|
|
14
|
+
export declare function createReturnAllField(resource: string, operations?: string[]): INodeProperties;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a 'Limit' number field for a resource
|
|
17
|
+
* @param resource - The resource name
|
|
18
|
+
* @param operations - Operations where this field applies
|
|
19
|
+
*/
|
|
20
|
+
export declare function createLimitField(resource: string, operations?: string[]): INodeProperties;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a resource ID field
|
|
23
|
+
* @param resource - The resource name
|
|
24
|
+
* @param idName - The parameter name for the ID (e.g., 'productId')
|
|
25
|
+
* @param displayName - Display name shown in UI
|
|
26
|
+
* @param operations - Operations where this field applies
|
|
27
|
+
*/
|
|
28
|
+
export declare function createIdField(resource: string, idName: string, displayName: string, operations?: string[]): INodeProperties;
|
|
29
|
+
/**
|
|
30
|
+
* Sort direction options for API queries
|
|
31
|
+
*/
|
|
32
|
+
export declare const SORT_DIRECTIONS: {
|
|
33
|
+
name: string;
|
|
34
|
+
value: string;
|
|
35
|
+
}[];
|
|
36
|
+
/**
|
|
37
|
+
* Common sortable fields across resources
|
|
38
|
+
*/
|
|
39
|
+
export declare const COMMON_SORT_FIELDS: {
|
|
40
|
+
name: string;
|
|
41
|
+
value: string;
|
|
42
|
+
}[];
|
|
43
|
+
/**
|
|
44
|
+
* Resource-specific includable relationships
|
|
45
|
+
*/
|
|
46
|
+
export declare const RESOURCE_INCLUDES: Record<string, Array<{
|
|
47
|
+
name: string;
|
|
48
|
+
value: string;
|
|
49
|
+
}>>;
|
|
50
|
+
/**
|
|
51
|
+
* Generate advanced options field for a specific resource
|
|
52
|
+
*/
|
|
53
|
+
export declare function createAdvancedOptionsField(resource: string, operations?: string[]): INodeProperties;
|
|
54
|
+
/**
|
|
55
|
+
* Pre-built advanced options fields for each resource
|
|
56
|
+
*/
|
|
57
|
+
export declare const orderAdvancedOptions: INodeProperties;
|
|
58
|
+
export declare const subscriptionAdvancedOptions: INodeProperties;
|
|
59
|
+
export declare const customerAdvancedOptions: INodeProperties;
|
|
60
|
+
export declare const licenseKeyAdvancedOptions: INodeProperties;
|
|
61
|
+
export declare const productAdvancedOptions: INodeProperties;
|
|
62
|
+
export declare const variantAdvancedOptions: INodeProperties;
|
|
63
|
+
export declare const checkoutAdvancedOptions: INodeProperties;
|
|
64
|
+
export declare const discountAdvancedOptions: INodeProperties;
|