modules

ac-apps-minimal-api-airtable

api/airtable.js

Makes it easy to work with the Airtable API and perform tasks such as fetching and updating records. Fetches all records if page number not provided.

Example:
app.api.woo.getProducts(options);
app.api.woo.updateProduct(options, product);

A module for ac-bundle-app, published by ac-bundle-module.

Install from:

https://www.npmjs.com/package/ac-apps-minimal-api-airtable
https://github.com/animatedcreativity/ac-apps-minimal-api-airtable

Available Methods: getRecords

getRecords

async function(options, page, offset) {
  var fetchAll = !app.has(page) || page > 1;
  if (!app.has(page)) page = 1;
  var url = "https://api.airtable.com/v0/" + options.airtableBaseName + "/" + options.airtableTableName + "?view=" + options.airtableViewName + "&pageSize=100" + (app.has(offset) ? "&offset=" + offset : "");
  console.log(app.consoleColors.bgBlue, "Fetching airtable page:", page + " " + url.split(" ").join("+"));
  var result = await fetch(url, {
    headers: {
      "Authorization": "Bearer " + options.airtableApiKey
    }
  });
  if (result.status === 200) {
    var records = await result.json();
    var list = {};
    for (var i=0; i<=records.records.length-1; i++) {
      var record = records.records[i];
      if (app.has(record.fields) && app.has(record.fields[options.airtableTableKey]) && record.fields[options.airtableKeyName] === options.airtableKeyValue) {
        list[record.fields[options.airtableTableKey]] = record;
      }
    }
    if (!(!app.has(records.offset) || fetchAll !== true)) {
      var moreRecords = await mod.getRecords(options, page + 1, records.offset);
      for (var key in moreRecords) list[key] = moreRecords[key];
    }
    return list;
  }
}

Happy Coding!

MIT License: https://choosealicense.com/licenses/mit/

Copyright 2022 Animated Creativity

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Leave a Reply

Your email address will not be published. Required fields are marked *