UNPKG

n8n-nodes-anywebsites

Version:
121 lines (120 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.anyWebsitesApiRequestAllItems = exports.anyWebsitesApiRequest = void 0; const n8n_workflow_1 = require("n8n-workflow"); // Cache for JWT tokens to avoid repeated logins const tokenCache = new Map(); /** * Login to AnyWebsites and get JWT token */ async function getJWTToken(context, credentials) { const cacheKey = `${credentials.baseUrl}:${credentials.username}`; const cached = tokenCache.get(cacheKey); // Check if we have a valid cached token (expires in 1 hour, check 5 minutes early) if (cached && cached.expires > Date.now() + 5 * 60 * 1000) { return cached.token; } // Login to get new token const loginOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: { username: credentials.username, password: credentials.password, }, url: `${credentials.baseUrl}/api/auth/login`, json: true, skipSslCertificateValidation: credentials.allowUnauthorizedCerts, }; try { const response = await context.helpers.httpRequest(loginOptions); if (!response.access_token) { throw new Error('Login failed: No access token received'); } // Cache the token (assume 1 hour expiry) tokenCache.set(cacheKey, { token: response.access_token, expires: Date.now() + 60 * 60 * 1000, // 1 hour }); return response.access_token; } catch (error) { throw new n8n_workflow_1.NodeApiError(context.getNode(), error, { message: 'Login failed. Please check your username and password.', }); } } /** * Make an API request to AnyWebsites */ async function anyWebsitesApiRequest(method, endpoint, body = {}, qs = {}) { var _a; const credentials = await this.getCredentials('anyWebsitesApi'); // Get JWT token const token = await getJWTToken(this, credentials); const options = { method, headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, body, qs, url: `${credentials.baseUrl}${endpoint}`, json: true, skipSslCertificateValidation: credentials.allowUnauthorizedCerts, }; // Remove empty body for GET and DELETE requests if (method === 'GET' || method === 'DELETE') { delete options.body; } try { return await this.helpers.httpRequest(options); } catch (error) { // If we get 401, clear the cached token and try once more if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) { const cacheKey = `${credentials.baseUrl}:${credentials.username}`; tokenCache.delete(cacheKey); // Try once more with fresh token const newToken = await getJWTToken(this, credentials); options.headers['Authorization'] = `Bearer ${newToken}`; try { return await this.helpers.httpRequest(options); } catch (retryError) { throw new n8n_workflow_1.NodeApiError(this.getNode(), retryError); } } throw new n8n_workflow_1.NodeApiError(this.getNode(), error); } } exports.anyWebsitesApiRequest = anyWebsitesApiRequest; /** * Make an API request to AnyWebsites and return all results * by handling pagination automatically */ async function anyWebsitesApiRequestAllItems(method, endpoint, body = {}, qs = {}) { const returnData = []; let responseData; qs.page = 1; qs.limit = qs.limit || 100; do { responseData = await anyWebsitesApiRequest.call(this, method, endpoint, body, qs); if (responseData.contents) { returnData.push.apply(returnData, responseData.contents); } else if (Array.isArray(responseData)) { returnData.push.apply(returnData, responseData); } else { returnData.push(responseData); } qs.page++; } while (responseData.contents && responseData.contents.length === qs.limit); return returnData; } exports.anyWebsitesApiRequestAllItems = anyWebsitesApiRequestAllItems;