From 91b2eecbb42945220cce9b5be86635a146aa6d19 Mon Sep 17 00:00:00 2001 From: Jeremy Moseley Date: Mon, 29 Jul 2024 15:19:42 -0700 Subject: [PATCH] Add support for publish targets. (#41) --- src/content.test.ts | 9 ++++++++- src/content.ts | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/content.test.ts b/src/content.test.ts index 14b84c6..744658e 100644 --- a/src/content.test.ts +++ b/src/content.test.ts @@ -331,9 +331,16 @@ test(`test content status and publishing`, { timeout: 180000 }, async () => { }); expect(publishedContent2.content.length).toBe(0); - await content.publish(); + const publishTargets = await content.getPublishTargets(); + expect(publishTargets.length).toBe(0); + + await content.publish({ id: "none", type: "none" }); expect(content.status).toBe(ContentStatus.Published); expect(content.publishedVersion).toBe(0); + await content.unpublish(); + expect(content.status).toBe(ContentStatus.Draft); + expect(content.publishedVersion).toBe(undefined); + await cortex.delete(); }); diff --git a/src/content.ts b/src/content.ts index 49f00dc..aa5eb72 100644 --- a/src/content.ts +++ b/src/content.ts @@ -102,6 +102,21 @@ export type ContentMetadata = { publishedVersion: number | null; }; +export type ContentPublishTarget = { + id: string; + name: string; + type: "github_repo"; +}; + +export type ContentFulfilledPublishTarget = + | { + id: string; + name: string; + type: "github_repo"; + path: string; + } + | { id: "none"; type: "none" }; + export class Content { get id() { return this._id; @@ -386,8 +401,12 @@ export class Content { return this; } - async publish() { - const res = await this.apiClient.POST(`/content/${this._id}/publish`); + async publish( + publishTarget: ContentFulfilledPublishTarget = { id: "none", type: "none" }, + ) { + const res = await this.apiClient.POST(`/content/${this._id}/publish`, { + publishTarget, + }); if (res.status !== 200) { throw new Error(`Failed to publish content: ${res.statusText}`); @@ -412,6 +431,21 @@ export class Content { return this; } + async getPublishTargets() { + const res = await this.apiClient.GET( + `/content/${this._id}/publish-targets`, + ); + + if (res.status !== 200) { + throw new Error( + `Failed to get content publish targets: ${res.statusText}`, + ); + } + + const result = (await res.json()) as { targets: ContentPublishTarget[] }; + return result.targets; + } + private updateFromResponseBody(body: Record) { this._commands = body.commands as ContentCommand[]; this._content = body.content as string;