n8n-nodes-rd-station-crm
Version:
n8n community node for RD Station CRM: API v1 (private token) and API v2 (OAuth2) — contacts, deals, organizations, tasks, notes, pipelines, stages, products, custom fields, sources, campaigns, loss reasons, users, teams and a real webhook trigger.
73 lines • 2.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RD_CRM_BASE_URL = void 0;
exports.rdCrmApiRequest = rdCrmApiRequest;
exports.rdCrmApiRequestAllItems = rdCrmApiRequestAllItems;
const n8n_workflow_1 = require("n8n-workflow");
exports.RD_CRM_BASE_URL = 'https://crm.rdstation.com/api/v1';
/**
* Make a single authenticated request to the RD Station CRM v1 API.
* The private token is injected as a query-string param by the credential.
*/
async function rdCrmApiRequest(method, endpoint, body = {}, qs = {}) {
const options = {
method,
baseURL: exports.RD_CRM_BASE_URL,
url: endpoint,
qs,
body,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
try {
return await this.helpers.httpRequestWithAuthentication.call(this, 'rdStationCrmApi', options);
}
catch (error) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
/**
* Fetch every page of a paginated RD Station CRM list endpoint.
*
* Handles the two response shapes the API uses:
* - `{ [propertyName]: [...], has_more: boolean, total: number }`
* - a bare array `[...]`
*
* @param propertyName key that holds the array in wrapped responses (e.g. "deals", "contacts").
*/
async function rdCrmApiRequestAllItems(propertyName, method, endpoint, body = {}, qs = {}) {
const returnData = [];
const query = { ...qs };
query.page = query.page || 1;
query.limit = query.limit || 200;
let hasMore = true;
let guard = 0;
const maxPages = 1000;
while (hasMore && guard < maxPages) {
guard += 1;
const responseData = await rdCrmApiRequest.call(this, method, endpoint, body, query);
let items;
if (Array.isArray(responseData)) {
items = responseData;
hasMore = items.length === query.limit;
}
else {
items = responseData?.[propertyName] ?? [];
hasMore =
responseData?.has_more === true ||
(items.length > 0 && items.length === query.limit && responseData?.has_more === undefined);
}
returnData.push(...items);
query.page = query.page + 1;
if (!items.length) {
break;
}
}
return returnData;
}
//# sourceMappingURL=index.js.map