n8n-nodes-octavehq
Version:
Octave is the AI-powered messaging brain for B2B go-to-market teams, helping articulate product value, synthesize customer interaction data, and deploy consistent, effective messaging across all channels.
83 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.octaveApiRequest = octaveApiRequest;
exports.octaveApiRequestListAll = octaveApiRequestListAll;
const n8n_workflow_1 = require("n8n-workflow");
async function octaveApiRequest(method, endpoint, body = {}, qs = {}) {
const credentials = await this.getCredentials('octaveApi');
if (!credentials) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Octave API credentials are not set!');
}
const baseUrl = credentials.baseUrl.replace(/\/$/, '');
const options = {
headers: {
'Accept': 'application/json',
'api_key': `${credentials.apiKey}`,
'x-request-source': 'n8n',
},
method,
qs,
uri: `${baseUrl}${endpoint}`,
json: true,
};
if (Object.keys(body).length !== 0) {
options.body = body;
}
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
options.headers['Content-Type'] = 'application/json';
}
try {
const responseData = await this.helpers.request(options);
return responseData;
}
catch (error) {
throw error;
}
}
async function octaveApiRequestListAll(method, endpoint, initialQs = {}) {
const allResults = [];
const queryParams = { ...initialQs };
queryParams.limit = initialQs.limit && initialQs.limit > 0 ? initialQs.limit : 100;
queryParams.offset = initialQs.offset && initialQs.offset >= 0 ? initialQs.offset : 0;
let responseData;
let hasNextPageInResponse = true;
while (hasNextPageInResponse) {
responseData = await octaveApiRequest.call(this, method, endpoint, {}, queryParams);
if (responseData && responseData.data && Array.isArray(responseData.data)) {
allResults.push(...responseData.data);
hasNextPageInResponse = responseData.hasNext === true;
if (hasNextPageInResponse) {
queryParams.offset += responseData.data.length;
if (responseData.data.length === 0) {
hasNextPageInResponse = false;
}
if (responseData.data.length < queryParams.limit) {
hasNextPageInResponse = false;
}
}
else {
if (responseData.data.length < queryParams.limit) {
hasNextPageInResponse = false;
}
else if (responseData.hasNext === undefined && responseData.data.length === queryParams.limit) {
queryParams.offset += responseData.data.length;
hasNextPageInResponse = true;
}
else {
hasNextPageInResponse = false;
}
}
}
else {
if (Array.isArray(responseData) && responseData.length > 0) {
allResults.push(...responseData);
}
else if (responseData && Object.keys(responseData).length > 0 && allResults.length === 0) {
allResults.push(responseData);
}
hasNextPageInResponse = false;
}
}
return allResults;
}
//# sourceMappingURL=OctaveApiRequest.js.map