sheetiq
Version:
<div align="center"> <img src="https://docapi.datafetchpro.com/featured_google_api.png" width="60%" /> <br /> <a href="https://discord.gg/ZkMMxZQf"><img src="https://img.shields.io/discord/1397785576253423616?color=5865F2&logo=discord&logoColo
134 lines (131 loc) • 3.55 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
SheetIQ: () => SheetIQ
});
module.exports = __toCommonJS(index_exports);
// src/functions.ts
var SheetIQ = class {
token;
baseUrl = "https://docapi.datafetchpro.com";
sheet = [];
/**
*
* @param p
* ```ts
* const sheet=new SheetIQ({token:"YOUR_BEARER_TOKEN"})
* sheet.sheet=["SHEET_ID","SHEET_NAME"]
* ```
*/
constructor(p) {
this.token = p.token;
}
checkParameter() {
if (!this.token || this.token == void 0) throw Error("Bearer Token not defined");
if (this.sheet.length < 2) throw Error("Your sheet array is not fine");
}
/**
* Fetches data from a Google Sheet.
*
* @param params - An object containing the sheet ID and range
* @returns A Promise that resolves to the sheet data
*
* @example
* ```ts
* await sheet.getSheet();
* ```
* @returns [{}] Array of Object
*
*
* @example
* ```ts
* await sheet.getSheet(
* key:false
* })
* ```
* @returns [[]] 2D Array
*/
async getSheet(params) {
this.checkParameter();
const { key = true } = params || {};
const data = await fetch(`${this.baseUrl}/api/v1/googlesheet/get`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ id: this.sheet[0], key, range: this.sheet[1] })
});
if (data.status != 200) throw Error;
return await data.json();
}
/**
* Push Data on a Google Sheet.
*
* @param params - An object containing the sheet ID and range
* @returns A Promise that resolves to the sheet data
*
* @example
* It'll append your data at the end of sheet
* ```ts
* await sheet.getSheet({
* type:"append",
* data:[["example@gmail.com"]]
* });
* ```
* @returns `{range}`
*
*
* @example
* It'll update sheet range
*
* ```ts
* await sheet.getSheet({
* type:"update",
* data:[["example@gmail.com"]]
* });
* ```
* @returns `{range}`
*/
async updateSheet(params) {
this.checkParameter();
const { data, type } = params;
const datac = await fetch(`${this.baseUrl}/api/v1/googlesheet/get`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
id: this.sheet[0],
range: this.sheet[1],
data,
type
})
});
if (datac.status != 200) throw Error(`message: ${JSON.stringify(await datac.json())}`);
return await datac.json();
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SheetIQ
});