From 1a573890900683530cb7d15c2388799590a7410c Mon Sep 17 00:00:00 2001 From: David Siegel Date: Sun, 26 Nov 2023 14:35:27 -0600 Subject: [PATCH] Improve optional token passing (#41) --- README.md | 4 ++-- src/Glide.ts | 10 +++++----- src/types.ts | 6 ++++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9ac3bec..a47f5dc 100644 --- a/README.md +++ b/README.md @@ -136,10 +136,10 @@ npm t ## Advanced Options -You can specify an alternate endpoint to use Glide's staging environment (for internal testing by Glide). +You can specify an alternate Glide environment (for internal testing by Glide). ```ts -const inventoryStaging = glide.table({ +const staging = new Glide({ endpoint: "https://staging.heyglide.com/api/container", /* ... */ }); diff --git a/src/Glide.ts b/src/Glide.ts index 9590371..e721032 100644 --- a/src/Glide.ts +++ b/src/Glide.ts @@ -1,7 +1,7 @@ import { App } from "./App"; import { Table } from "./Table"; import { defaultEndpoint, defaultEndpointREST } from "./constants"; -import type { TableProps, ColumnSchema, AppProps, IDName, GlideProps } from "./types"; +import type { TableProps, ColumnSchema, AppProps, IDName, GlideProps, Tokened } from "./types"; import fetch from "cross-fetch"; export class Glide { @@ -85,8 +85,8 @@ export class Glide { * @param props.token An optional token for authentication. * @returns A promise that resolves to an array of applications if successful, or undefined. */ - public async getApps(): Promise { - const response = await this.get(`/apps`); + public async getApps(props: Tokened = {}): Promise { + const response = await this.with(props).get(`/apps`); if (response.status !== 200) return undefined; const { data: apps }: { data: IDName[] } = await response.json(); return apps.map(idName => this.app({ ...idName })); @@ -100,8 +100,8 @@ export class Glide { * @param props.token An optional token for authentication. * @returns A promise that resolves to the application if found, or undefined. */ - public async getAppNamed(name: string): Promise { - const apps = await this.getApps(); + public async getAppNamed(name: string, props: Tokened = {}): Promise { + const apps = await this.getApps(props); return apps?.find(a => a.name === name); } } diff --git a/src/types.ts b/src/types.ts index ff55024..4ab6e6c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -74,14 +74,16 @@ export type NullableFullRow = Pretty< } & NullableRow >; -export interface TableProps extends Partial { +export type Tokened = { token?: string }; + +export interface TableProps extends Tokened { name?: string; app: string; table: string; columns: T; } -export interface AppProps extends Partial { +export interface AppProps extends Tokened { id: string; name?: string; }