Skip to content

Commit

Permalink
Get apps by name as well
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdsgl committed Nov 16, 2023
1 parent ece40ef commit 8d5b5f4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fetch from "cross-fetch";

type RowIdentifiable<T extends ColumnSchema> = RowID | FullRow<T>;

type IDName = { id: string; name: string };

function rowID(row: RowIdentifiable<any>): RowID {
return typeof row === "string" ? row : row.$rowID;
}
Expand Down Expand Up @@ -215,6 +217,10 @@ class Table<T extends ColumnSchema> {
class App {
private client: Client;

public get name() {
return this.props.name;
}

constructor(private props: AppProps) {
this.client = makeClient({
token: process.env.GLIDE_TOKEN!,
Expand All @@ -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: {} }));
}

Expand All @@ -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<App[] | undefined> {
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<App | undefined> {
const apps = await getApps(props);
return apps?.find(a => a.name === name);
}

export function table<T extends ColumnSchema>(props: TableProps<T>) {
return new Table<T>(props);
}
2 changes: 1 addition & 1 deletion src/rest.ts
Original file line number Diff line number Diff line change
@@ -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}`, {
Expand Down
15 changes: 14 additions & 1 deletion src/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ export interface AppProps {
id: string;
token?: string;
endpoint?: string;
name?: string;
}

0 comments on commit 8d5b5f4

Please sign in to comment.