From 8d5b5f45f9bf9785ed4771c306b1ac3a41efaf77 Mon Sep 17 00:00:00 2001 From: David Siegel Date: Wed, 15 Nov 2023 22:01:18 -0800 Subject: [PATCH] Get apps by name as well --- README.md | 18 +++++++++++++----- src/index.ts | 29 +++++++++++++++++++++++++++-- src/rest.ts | 2 +- src/table.test.ts | 15 ++++++++++++++- src/types.ts | 1 + 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 66fde39..9bc209b 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,28 @@ # Glide Tables Client +## Authorization + +Set `GLIDE_TOKEN` environment variable to your Glide token, or pass the token inline as props. + ## Apps ```ts import * as glide from "@glideapps/tables"; -const myApp = glide.app({ - token: process.env.GLIDE_TOKEN, - id: "bAFxpGXU1bHiBgUMcDgn", -}); +// Create an app with its ID +const myApp = glide.app("bAFxpGXU1bHiBgUMcDgn"); + +// Or get by name +const myApp = await glide.getAppNamed("Employee Directory"); // Get all tables const tables = await myApp.getTables(); // Get a table by name -const users = await myApp.getTableNamed("Users); +const users = await myApp.getTableNamed("Users"); + +// List all apps +const apps = await glide.getApps(); ``` ## Tables diff --git a/src/index.ts b/src/index.ts index 93cd022..189ac67 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,8 @@ import fetch from "cross-fetch"; type RowIdentifiable = RowID | FullRow; +type IDName = { id: string; name: string }; + function rowID(row: RowIdentifiable): RowID { return typeof row === "string" ? row : row.$rowID; } @@ -215,6 +217,10 @@ class Table { class App { private client: Client; + public get name() { + return this.props.name; + } + constructor(private props: AppProps) { this.client = makeClient({ token: process.env.GLIDE_TOKEN!, @@ -232,7 +238,7 @@ class App { if (result.status !== 200) return undefined; - const { data: tables }: { data: Array<{ id: string; name: string }> } = await result.json(); + const { data: tables }: { data: IDName[] } = await result.json(); return tables.map(t => this.table({ table: t.id, name: t.name, columns: {} })); } @@ -246,10 +252,29 @@ class App { } } -export function app(props: AppProps): App { +export function app(props: AppProps | string): App { + if (typeof props === "string") { + props = { id: props }; + } return new App(props); } +export async function getApps(props: { token?: string } = {}): Promise { + const client = makeClient(props); + const response = await client.get(`/apps`); + if (response.status !== 200) return undefined; + const { data: apps }: { data: IDName[] } = await response.json(); + return apps.map(idName => app({ ...props, ...idName })); +} + +export async function getAppNamed( + name: string, + props: { token?: string } = {} +): Promise { + const apps = await getApps(props); + return apps?.find(a => a.name === name); +} + export function table(props: TableProps) { return new Table(props); } diff --git a/src/rest.ts b/src/rest.ts index 8ed048b..fc6b4b8 100644 --- a/src/rest.ts +++ b/src/rest.ts @@ -1,6 +1,6 @@ import fetch from "cross-fetch"; -export function makeClient({ token }: { token: string }) { +export function makeClient({ token = process.env.GLIDE_TOKEN! }: { token?: string } = {}) { return { get(route: string, r: RequestInit = {}) { return fetch(`https://functions.prod.internal.glideapps.com/api${route}`, { diff --git a/src/table.test.ts b/src/table.test.ts index 03b89a1..e92246d 100644 --- a/src/table.test.ts +++ b/src/table.test.ts @@ -3,9 +3,11 @@ require("dotenv").config(); import * as glide from "."; import type { RowOf } from "."; +const token = process.env.GLIDE_TOKEN!; + const app = glide.app({ id: "bAFxpGXU1bHiBgUMcDgn", - token: process.env.GLIDE_TOKEN, + token, }); const inventory = app.table({ @@ -28,6 +30,17 @@ const inventoryStaging = glide.table({ }); describe("app", () => { + it("can get apps", async () => { + const apps = await glide.getApps(); + expect(apps).toBeDefined(); + expect(apps?.length).toBeGreaterThan(0); + }); + + it("can get an app by name", async () => { + const app = await glide.getAppNamed("API Testing"); + expect(app).toBeDefined(); + }); + it("can get tables", async () => { const tables = await app.getTables(); expect(tables).toBeDefined(); diff --git a/src/types.ts b/src/types.ts index f43272b..5869086 100644 --- a/src/types.ts +++ b/src/types.ts @@ -61,4 +61,5 @@ export interface AppProps { id: string; token?: string; endpoint?: string; + name?: string; }