This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from franciscop/drive-db
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.min.js
1 lines (1 loc) · 1.71 KB
/
index.min.js
1
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=global||self,global.drive=factory())})(this,function(){"use strict";const getKeys=row=>Object.keys(row).filter(key=>/^gsx\$/.test(key));const parseRow=row=>{return getKeys(row).reduce((obj,key)=>{obj[key.slice(4)]=row[key].$t;return obj},{})};const get=async url=>{if(typeof fetch!=="undefined"){const res=await fetch(url);if(!res.ok)throw new Error(`Error ${res.status} retrieving ${url}`);return res.json()}if(typeof require!=="undefined"){return new Promise((resolve,reject)=>{const handler=res=>{res.setEncoding("utf8");if(res.statusCode<200||res.statusCode>=300){return reject(new Error(`Error ${res.statusCode} retrieving ${url}`))}let data="";res.on("data",chunk=>data+=chunk);res.on("end",()=>resolve(JSON.parse(data)))};require("https").get(url,handler).on("error",reject)})}throw new Error("fetch() is not available, please polyfill it")};const retrieve=async({sheet:sheet,tab:tab})=>{const raw=await get(`https://spreadsheets.google.com/feeds/list/${sheet}/${tab}/public/values?alt=json`);return raw.feed.entry.map(parseRow)};const memo=(cb,map={})=>async(options,timeout)=>{const key=JSON.stringify(options);const time=new Date;if(map[key]&&time-map[key].time<timeout){return map[key].value}map[key]={value:await cb(options),time:time};return map[key].value};const getSheet=memo(retrieve);var index=async options=>{const optObject=typeof options==="object"?options:{sheet:options};const{sheet:sheet="",tab:tab="default",cache:cache=3600}=optObject;if(!sheet)throw new Error("Need a Google Drive sheet id to load");return getSheet({sheet:sheet,tab:tab},cache)};return index});