Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/workspace-cards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pdfmonkey": minor
---

Adding `client.workspaceCards` to call the `workspace_cards` endpoint: `list()`/`listAll()` return workspace summaries including the admin's `current_plan`, and `update()` renames a workspace.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ if (page.hasNextPage()) {
}
```

Workspaces, document templates, snippets and template folders also expose `listAll()`, which fetches every item in a single request (`page=all`) and returns a plain array. Document cards do not support it, so iterate with `for await (const card of page)` instead.
Workspaces, workspace cards, document templates, snippets and template folders also expose `listAll()`, which fetches every item in a single request (`page=all`) and returns a plain array. Document cards do not support it, so iterate with `for await (const card of page)` instead.

```ts
const workspaces = await client.workspaces.listAll();
Expand Down Expand Up @@ -295,6 +295,10 @@ const folders = await client.templateFolders.list({ workspace_id: 'ws_xxx' });
// Workspaces
const workspaces = await client.workspaces.list();

// Workspace Cards (includes the admin's plan)
const cards = await client.workspaceCards.list();
await client.workspaceCards.update('ws_xxx', { identifier: 'New name' });

// Current User
const user = await client.currentUser.get();
```
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/api.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ describe.runIf(enabled)('live API', () => {
});
});

describe('workspaceCards', () => {
it('list', async () => {
const page = await client.workspaceCards.list();
const card = page.data.find((c) => c.id === workspaceId);
expect(card?.current_plan).toBeTruthy();
});

it('listAll', async () => {
const cards = await client.workspaceCards.listAll();
expect(cards.map((c) => c.id)).toContain(workspaceId);
});
});

it('pdfEngines.list', async () => {
const engines = await client.pdfEngines.list();
expect(engines.length).toBeGreaterThan(0);
Expand Down
47 changes: 47 additions & 0 deletions src/__tests__/resources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,53 @@ describe('Workspaces', () => {
});
});

// ── Workspace Cards ────────────────────────────────────────────────────────

describe('WorkspaceCards', () => {
it('lists workspace cards with page param', async () => {
const { client, fetch } = createClient([
{
status: 200,
body: {
workspace_cards: [{ id: 'ws_1', identifier: 'main', current_plan: 'free' }],
meta: { current_page: 2, total_pages: 2, next_page: null, prev_page: 1 },
},
},
]);

const page = await client.workspaceCards.list({ page: 2 });

expect(page.data[0]?.current_plan).toBe('free');
const parsed = new URL(fetch.mock.calls[0]?.[0] as string);
expect(parsed.pathname).toMatch(/\/workspace_cards$/);
expect(parsed.searchParams.get('page[number]')).toBe('2');
});

it('lists all workspace cards in a single request', async () => {
const { client, fetch } = createClient([
{ status: 200, body: { workspace_cards: [{ id: 'ws_1' }, { id: 'ws_2' }] } },
]);

const cards = await client.workspaceCards.listAll();
expect(cards).toHaveLength(2);
expect(new URL(fetch.mock.calls[0]?.[0] as string).searchParams.get('page')).toBe('all');
});

it('updates a workspace card with PATCH', async () => {
const { client, fetch } = createClient([
{ status: 200, body: { workspace_card: { id: 'ws_1', identifier: 'renamed' } } },
]);

const card = await client.workspaceCards.update('ws_1', { identifier: 'renamed' });

expect(card.identifier).toBe('renamed');
const [url, init] = fetch.mock.calls[0] as [string, RequestInit];
expect(url).toContain('/workspace_cards/ws_1');
expect(init.method).toBe('PATCH');
expect(JSON.parse(init.body as string)).toEqual({ workspace_card: { identifier: 'renamed' } });
});
});

// ── Snippets ───────────────────────────────────────────────────────────────

describe('Snippets', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PdfEngines } from './resources/pdf-engines.js';
import { RestHooks } from './resources/rest-hooks.js';
import { Snippets } from './resources/snippets.js';
import { TemplateFolders } from './resources/template-folders.js';
import { WorkspaceCards } from './resources/workspace-cards.js';
import { Workspaces } from './resources/workspaces.js';
import { VERSION } from './version.js';

Expand Down Expand Up @@ -143,6 +144,7 @@ export class PDFMonkey {
readonly restHooks: RestHooks;
readonly templateFolders: TemplateFolders;
readonly workspaces: Workspaces;
readonly workspaceCards: WorkspaceCards;
readonly snippets: Snippets;
readonly currentUser: CurrentUserResource;

Expand Down Expand Up @@ -188,6 +190,7 @@ export class PDFMonkey {
this.restHooks = new RestHooks(this);
this.templateFolders = new TemplateFolders(this);
this.workspaces = new Workspaces(this);
this.workspaceCards = new WorkspaceCards(this);
this.snippets = new Snippets(this);
this.currentUser = new CurrentUserResource(this);
}
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export type {
TemplateFolderUpdateParams,
} from './resources/template-folders.js';
export { TemplateFolders } from './resources/template-folders.js';
export type {
WorkspaceCard,
WorkspaceCardListParams,
WorkspaceCardUpdateParams,
} from './resources/workspace-cards.js';
export { WorkspaceCards } from './resources/workspace-cards.js';
export type { Workspace, WorkspaceListParams } from './resources/workspaces.js';
export { Workspaces } from './resources/workspaces.js';
export { VERSION } from './version.js';
Expand Down
61 changes: 61 additions & 0 deletions src/resources/workspace-cards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { ResourceRequestOptions } from '../client.js';
import { buildListQuery, fetchAll, fetchPage, type Page } from '../pagination.js';
import { APIResource } from '../resource.js';

// ── Types ──────────────────────────────────────────────────────────────────

export interface WorkspaceCard {
readonly id: string;
readonly identifier: string;
readonly invite_token: string;
readonly updated_at: string;
/** Plan of the workspace's admin. */
readonly current_plan: string;
}

export interface WorkspaceCardListParams {
page?: number;
}

export interface WorkspaceCardUpdateParams {
identifier?: string;
}

interface WorkspaceCardResponse {
workspace_card: WorkspaceCard;
}

// ── Resource ───────────────────────────────────────────────────────────────

/** Workspace summaries, including the admin's plan. */
export class WorkspaceCards extends APIResource {
/** List workspace cards. Returns a paginated result. */
async list(
params?: WorkspaceCardListParams,
options?: ResourceRequestOptions,
): Promise<Page<WorkspaceCard>> {
const query = buildListQuery({}, { page: params?.page });
return fetchPage<WorkspaceCard>(this._client, '/workspace_cards', 'workspace_cards', {
...options,
query,
});
}

/** List all workspace cards in a single request. */
async listAll(options?: ResourceRequestOptions): Promise<WorkspaceCard[]> {
return fetchAll<WorkspaceCard>(this._client, '/workspace_cards', 'workspace_cards', options);
}

/** Update a workspace by ID. Uses PATCH. */
async update(
id: string,
params: WorkspaceCardUpdateParams,
options?: ResourceRequestOptions,
): Promise<WorkspaceCard> {
const response = await this._client.patch<WorkspaceCardResponse>(
`/workspace_cards/${encodeURIComponent(id)}`,
{ ...options, body: { workspace_card: params } },
);
return response.workspace_card;
}
}
Loading