UNPKG

knack-queries

Version:
44 lines 1.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Knack = void 0; const axios_1 = __importDefault(require("axios")); class Knack { constructor(appId, apiKey, baseURL = 'https://api.knack.com/v1/') { this.knackAPI = axios_1.default.create({ baseURL, headers: { 'X-Knack-Application-Id': appId, 'X-Knack-REST-API-KEY': apiKey } }); } /** * It downloads all the records from a Knack object, and returns them as an array * @param filters - This is an array of objects that contain the filter parameters. * @returns An array of objects. */ async getAllData({ objectName, filters }) { const { data: { total_pages, total_records } } = await this.knackAPI.get(`objects/${objectName}/records?filters=${JSON.stringify(filters)}&rows_per_page=1000`); console.log(`Starting object ${objectName}`); console.log(`Total number of pages: ${total_pages}`); console.log(`Total records ${total_records}`); const allData = []; for (let i = 1; i <= total_pages; i++) { const { data: { records } } = await this.knackAPI.get(`objects/${objectName}/records?filters=${JSON.stringify(filters)}&rows_per_page=1000&page=${i}`); console.log(`Downloading page ${i}`); allData.push(...records); } console.log(`Download complete. Total number of records: ${allData.length}`); console.log(`Finishing object ${objectName}`); return allData; } async getDataById({ objectName, id }) { const { data } = await this.knackAPI.get(`objects/${objectName}/records/${id}`); return data; } } exports.Knack = Knack; //# sourceMappingURL=index.js.map