Skip to content

Commit

Permalink
feat: Add a listCollections method and requisite caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-coster committed Jun 25, 2021
1 parent b177680 commit 65c2126
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,9 @@ As environment variables:
- Find collection by id
- Create a collection
- Delete a collection
- ✔ List users
- ✔ Find user by name, id, or email
- List widgets
- Find widgets by name
- Create a widget
- ✔ List orgs
- ✔ Find org by name
- ✔ Set org by name or ID

## Usage

Expand Down
35 changes: 32 additions & 3 deletions src/lib/BravoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import {
FavroApiMethod,
FavroDataOrganization,
FavroDataOrganizationUser,
FavroDataOrganizationUserPartial,
FavroDataCollection,
} from '../types/FavroApi';
import { FavroResponse } from './FavroResponse';
import { findRequiredByField } from './utility.js';

type FavroDataOrganizationUserPartial =
FavroDataOrganization['sharedToUsers'][number];
export class FavroCollection {
private _data: FavroDataCollection;
constructor(data: FavroDataCollection) {
this._data = data;
}
}

export class FavroUser<
Data extends FavroDataOrganizationUser | FavroDataOrganizationUserPartial,
Expand Down Expand Up @@ -67,8 +73,8 @@ export class BravoClient {
private _backendId?: string;

private _organizations?: FavroDataOrganization[];

private _users?: FavroUser<FavroDataOrganizationUser>[];
private _collections?: FavroCollection[];

constructor(options?: {
token?: string;
Expand Down Expand Up @@ -190,6 +196,8 @@ export class BravoClient {
return favroRes;
}

//#region Organizations

async currentOrganization() {
if (!this._organizationId) {
return;
Expand Down Expand Up @@ -239,6 +247,10 @@ export class BravoClient {
this.organizationId = org.organizationId;
}

//#endregion

//#region Users

/**
* Full user info for the org (includes emails and names),
* requires an API request.
Expand Down Expand Up @@ -284,6 +296,22 @@ export class BravoClient {
return findRequiredByField(await this.listPartialUsers(), 'userId', userId);
}

//#endregion

//#region Collections

async listCollections() {
const org = await this.currentOrganization();
assertBravoClaim(org, 'Organization not set');
if (!this._collections) {
const res = await this.request<FavroDataCollection>('users');
this._collections = res.entities.map((u) => new FavroCollection(u));
}
return [...this._collections];
}

//#endregion

/**
* To reduce API calls (the rate limits are tight), things
* are generally cached. To ensure requests are up to date
Expand All @@ -292,6 +320,7 @@ export class BravoClient {
clearCache() {
this._users = undefined;
this._organizations = undefined;
this._collections = undefined;
}

static toBase64(string: string) {
Expand Down
39 changes: 37 additions & 2 deletions src/types/FavroApi.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
export type FavroApiMethod = 'get' | 'post' | 'put' | 'delete';
type FavroRole =
export type FavroRole =
| 'administrator'
| 'fullMember'
| 'externalMember'
| 'guest'
| 'disabled';
export type FavroCollectionBackground =
| 'purple'
| 'green'
| 'grape'
| 'red'
| 'pink'
| 'blue'
| 'solidPurple'
| 'solidGreen'
| 'solidGrape'
| 'solidRed'
| 'solidPink'
| 'solidGray';

/** {@link https://favro.com/developer/#collections} */
export interface FavroDataCollection {
/** The id of the collection. */
collectionId: string;
/** The id of the organization that this collection exists in. */
/** The name of the collection. */
organizationId: string;
name: string;
/** The array of collection members that the collection is shared to. */
sharedToUsers: FavroDataOrganizationUserPartial[];
/** The collection public sharing level. */
publicSharing: string;
/** The collection background. */
background: FavroCollectionBackground;
/** Whether or not the collection is archived. */
archived: boolean;
/** Whether or not full members shared this collection can create new widgets. */
fullMembersCanAddWidgets: boolean;
}

export interface FavroDataOrganization {
organizationId: string;
name: string;
sharedToUsers: {
userId: string;
role: FavroRole;
joinDate: string;
}[];
}

export type FavroDataOrganizationUserPartial =
FavroDataOrganization['sharedToUsers'][number];

export interface FavroDataOrganizationUser {
userId: string;
name: string;
Expand Down

0 comments on commit 65c2126

Please sign in to comment.