UNPKG

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

107 lines (106 loc) 2.53 kB
// 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(); } }; export { SheetIQ };