Skip to content

Commit

Permalink
Improve optional token passing (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdsgl authored Nov 26, 2023
1 parent 7fbeae0 commit 1a57389
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
/* ... */
});
Expand Down
10 changes: 5 additions & 5 deletions src/Glide.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<App[] | undefined> {
const response = await this.get(`/apps`);
public async getApps(props: Tokened = {}): Promise<App[] | undefined> {
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 }));
Expand All @@ -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<App | undefined> {
const apps = await this.getApps();
public async getAppNamed(name: string, props: Tokened = {}): Promise<App | undefined> {
const apps = await this.getApps(props);
return apps?.find(a => a.name === name);
}
}
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ export type NullableFullRow<T extends ColumnSchema> = Pretty<
} & NullableRow<T>
>;

export interface TableProps<T = {}> extends Partial<GlideProps> {
export type Tokened = { token?: string };

export interface TableProps<T = {}> extends Tokened {
name?: string;
app: string;
table: string;
columns: T;
}

export interface AppProps extends Partial<GlideProps> {
export interface AppProps extends Tokened {
id: string;
name?: string;
}
Expand Down

0 comments on commit 1a57389

Please sign in to comment.