n8n-nodes-smartsuite
Version:
n8n community node for SmartSuite
111 lines • 4.37 kB
JavaScript
;
// src/nodes/SmartSuite/transport/smartSuiteApi.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.apiRequest = apiRequest;
exports.paginatedRequest = paginatedRequest;
exports.apiRequestAllItems = paginatedRequest;
const n8n_workflow_1 = require("n8n-workflow");
/**
* Core HTTP request function.
* - customHeaders: extra headers to merge in
* - absolute-URL detection
*/
async function apiRequest(method, endpoint, body = {}, qs = {}, customHeaders = {}) {
const creds = await this.getCredentials('smartSuiteApi');
if (!creds.apiKey || !creds.accountId || !creds.baseUrl) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Missing required SmartSuite credentials. Please check API Key, Account ID, and Base URL.');
}
const originalEndpoint = endpoint;
let urlPath = endpoint;
if (urlPath.startsWith('/tables/')) {
urlPath = urlPath.replace(/^\/tables\//, '/applications/');
}
let finalUrl;
if (/^https?:\/\//.test(urlPath)) {
finalUrl = urlPath;
}
else {
if (!creds.baseUrl ||
typeof creds.baseUrl !== 'string' ||
!creds.baseUrl.startsWith('http')) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `SmartSuite baseUrl is missing or malformed: ${creds.baseUrl}`);
}
finalUrl = `${creds.baseUrl}${urlPath}`;
}
const options = {
method,
url: finalUrl,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Token ${creds.apiKey}`,
'Account-Id': creds.accountId,
...customHeaders,
},
qs,
json: true,
body: method === 'GET' ? undefined : body,
};
try {
const httpRequest = this.helpers.httpRequest;
if (typeof httpRequest !== 'function') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'HTTP Request helper not available');
}
return (await httpRequest(options));
}
catch (error) {
const status = error.response?.status;
const apiError = error.response?.data;
if (status === 400 &&
apiError &&
Object.values(apiError)[0] === 'Not allowed comparison.') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid comparison for that field type. See https://developers.smartsuite.com/docs/solution-data/records/sort-filter#operators-by-field-type for valid operators.');
}
if (apiError && typeof apiError === 'object') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `SmartSuite API Error (${status}): ${JSON.stringify(apiError)}`);
}
if (status === 404) {
let resourceType = 'Record';
if (originalEndpoint.startsWith('/solutions/')) {
resourceType = 'Solution';
}
else if (originalEndpoint.startsWith('/tables/')) {
resourceType = 'Table';
}
const parts = finalUrl.split('/');
const id = parts[parts.length - 1] || parts[parts.length - 2];
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The resource you are requesting could not be found
${id} is not a valid ${resourceType} ID`);
}
throw error;
}
}
/**
* Fetch all items from a paginated endpoint (limit/offset).
*/
async function paginatedRequest(method, endpoint, body = {}, qs = {}, pageSize = 100) {
const allItems = [];
let offset = 0;
while (true) {
const response = await apiRequest.call(this, method, endpoint, body, { ...qs, limit: pageSize, offset });
let pageArray = [];
if (Array.isArray(response)) {
pageArray = response;
}
else if (Array.isArray(response.data)) {
pageArray = response.data;
}
else if (Array.isArray(response.results)) {
pageArray = response.results;
}
else if (response != null && typeof response === 'object') {
pageArray = [response];
}
allItems.push(...pageArray);
if (pageArray.length < pageSize)
break;
offset += pageSize;
}
return allItems;
}
//# sourceMappingURL=smartSuiteApi.js.map