Skip to content

Commit

Permalink
Add support for publish targets. (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoseley authored Jul 29, 2024
1 parent 6118072 commit 91b2eec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
38 changes: 36 additions & 2 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`);
Expand All @@ -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<string, unknown>) {
this._commands = body.commands as ContentCommand[];
this._content = body.content as string;
Expand Down

0 comments on commit 91b2eec

Please sign in to comment.