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
23 changes: 23 additions & 0 deletions src/__tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect, vi } from "vitest";
import { AccountResource } from "../resources/account.js";
import type { HttpClient } from "../http.js";
import type { Account } from "../types.js";

describe("AccountResource", () => {
it("get() calls GET /v1/account", async () => {
Expand All @@ -12,4 +13,26 @@ describe("AccountResource", () => {
await account.get();
expect(http.get).toHaveBeenCalledWith("/v1/account");
});

it("Account type has orgName, user.homePartition, and no top-level partition or name", () => {
const account: Account = {
orgId: "o1",
orgName: "Acme",
user: { homePartition: "eu" },
plan: {
title: "Standard",
sitesLimit: 5,
domainsLimit: 5,
uploadLimit: 1024,
storageLimit: 10240,
dataResidencySelector: false,
},
usage: { sites: 0, domains: 0, storage: 0 },
};

expect(account.orgName).toBe("Acme");
expect(account.user.homePartition).toBe("eu");
expect(account).not.toHaveProperty("partition");
expect(account).not.toHaveProperty("name");
});
});
89 changes: 89 additions & 0 deletions src/__tests__/sites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,93 @@ describe("SitesResource.deploy", () => {
expect(fileNames).toEqual(["index.html", "sub/style.css"]);
});
});

describe("startUpload / finalizeUpload (public primitives)", () => {
it("startUpload posts manifest to /v1/sites/<id>/uploads", async () => {
const http = createMockHttp();
const envelope = {
versionId: "v1",
files: {
"a.bin": {
uploadId: "u1",
key: "k1",
partUploadUrls: [{ part: 1, url: "https://s3.example.com/a/1" }],
},
},
};
http.post.mockResolvedValue(envelope);
const sites = new SitesResource(http);

const result = await sites.startUpload("s1", [
{ fileName: "a.bin", fileSize: 100, parts: 1 },
]);

expect(http.post).toHaveBeenCalledWith(
"/v1/sites/s1/uploads",
{ files: [{ fileName: "a.bin", fileSize: 100, parts: 1 }] },
);
expect(result).toBe(envelope);
});

it("startUpload URL-encodes siteId", async () => {
const http = createMockHttp();
http.post.mockResolvedValue({ versionId: "v1", files: {} });
const sites = new SitesResource(http);
await sites.startUpload("a/b", [{ fileName: "x", fileSize: 1, parts: 1 }]);
expect(http.post).toHaveBeenCalledWith(
"/v1/sites/a%2Fb/uploads",
expect.anything(),
);
});

it("finalizeUpload posts completions to /v1/sites/<id>/uploads/<v>/finalize", async () => {
const http = createMockHttp();
http.post.mockResolvedValue({ status: "processing" });
const sites = new SitesResource(http);

const completions = [
{
uploadId: "u1",
key: "k1",
parts: [{ ETag: '"e1"', PartNumber: 1 }],
},
];
const result = await sites.finalizeUpload("s1", "v1", completions);

expect(http.post).toHaveBeenCalledWith(
"/v1/sites/s1/uploads/v1/finalize",
{ completions },
);
expect(result).toEqual({ status: "processing" });
});

it("finalizeUpload sends undefined body when completions empty/omitted", async () => {
const http = createMockHttp();
http.post.mockResolvedValue({ status: "processing" });
const sites = new SitesResource(http);
await sites.finalizeUpload("s1", "v1");
expect(http.post).toHaveBeenCalledWith(
"/v1/sites/s1/uploads/v1/finalize",
undefined,
);

http.post.mockClear();
await sites.finalizeUpload("s1", "v1", []);
expect(http.post).toHaveBeenCalledWith(
"/v1/sites/s1/uploads/v1/finalize",
undefined,
);
});

it("finalizeUpload URL-encodes siteId and versionId", async () => {
const http = createMockHttp();
http.post.mockResolvedValue({ status: "processing" });
const sites = new SitesResource(http);
await sites.finalizeUpload("a/b", "v 1");
expect(http.post).toHaveBeenCalledWith(
"/v1/sites/a%2Fb/uploads/v%201/finalize",
undefined,
);
});
});
});
51 changes: 38 additions & 13 deletions src/resources/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ export class SitesResource {
);
}

/**
* Request presigned S3 PUT URLs for uploading file parts directly.
* Lower-level primitive used by `deploy()`; also exposed for callers
* that need to drive the PUT step themselves (e.g. an MCP server
* handing the URLs to an agent).
*/
async startUpload(
siteId: string,
files: UploadFileEntry[],
): Promise<StartUploadResponse> {
return this.http.post<StartUploadResponse>(
`/v1/sites/${encodeURIComponent(siteId)}/uploads`,
{ files },
);
}

/**
* Commit a previously-started upload using the agent-collected
* ETag + PartNumber pairs from each S3 PUT response. Pass
* `completions` for multi-part files; single-part uploads need no
* completion.
*/
async finalizeUpload(
siteId: string,
versionId: string,
completions?: UploadCompletion[],
): Promise<FinalizeUploadResponse> {
const body = completions && completions.length > 0
? { completions }
: undefined;
return this.http.post<FinalizeUploadResponse>(
`/v1/sites/${encodeURIComponent(siteId)}/uploads/${encodeURIComponent(versionId)}/finalize`,
body,
);
}

async deploy(
siteId: string,
source: string | DeployFile[],
Expand All @@ -58,11 +94,7 @@ export class SitesResource {
parts: Math.max(1, Math.ceil(f.content.length / PART_SIZE)),
}));

const encodedSiteId = encodeURIComponent(siteId);
const upload = await this.http.post<StartUploadResponse>(
`/v1/sites/${encodedSiteId}/uploads`,
{ files: manifest },
);
const upload = await this.startUpload(siteId, manifest);

const completions: UploadCompletion[] = [];

Expand Down Expand Up @@ -117,14 +149,7 @@ export class SitesResource {
completion.parts.sort((a, b) => a.PartNumber - b.PartNumber);
}

const finalizeBody = completions.length > 0
? { completions }
: undefined;

const finalize = await this.http.post<FinalizeUploadResponse>(
`/v1/sites/${encodedSiteId}/uploads/${encodeURIComponent(upload.versionId)}/finalize`,
finalizeBody,
);
const finalize = await this.finalizeUpload(siteId, upload.versionId, completions);

return {
versionId: upload.versionId,
Expand Down
10 changes: 7 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,15 @@ export interface AccountUsage {
storage: number;
}

export interface AccountUser {
/** Data partition (residency) the calling user is anchored to. */
homePartition: Partition | null;
}

export interface Account {
orgId: string;
name: string;
/** Data partition (residency) the account is anchored to. */
partition: Partition | null;
orgName: string;
user: AccountUser;
plan: AccountPlan;
usage: AccountUsage;
}
Expand Down
Loading