From 4b59010fa66b11e7f5ae41f2f528a0a67b11f60b Mon Sep 17 00:00:00 2001 From: "gp42-ops (GitHub)" <91820843+gp42-ops@users.noreply.github.com> Date: Wed, 6 May 2026 00:07:48 +0300 Subject: [PATCH] feat: reshape Account type to separate org and user scopes Account.name renamed to Account.orgName. Account.partition removed. New AccountUser type with homePartition; Account.user replaces the top-level partition field. Tracks the public-API GET /v1/account response shape change: the account's home partition now reflects the calling user (sourced from the user's org-membership row), not the org default. --- src/__tests__/account.test.ts | 23 +++++++++ src/__tests__/sites.test.ts | 89 +++++++++++++++++++++++++++++++++++ src/resources/sites.ts | 51 +++++++++++++++----- src/types.ts | 10 ++-- 4 files changed, 157 insertions(+), 16 deletions(-) diff --git a/src/__tests__/account.test.ts b/src/__tests__/account.test.ts index de17832..f077ba4 100644 --- a/src/__tests__/account.test.ts +++ b/src/__tests__/account.test.ts @@ -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 () => { @@ -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"); + }); }); diff --git a/src/__tests__/sites.test.ts b/src/__tests__/sites.test.ts index fc349b7..ebcf55a 100644 --- a/src/__tests__/sites.test.ts +++ b/src/__tests__/sites.test.ts @@ -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//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//uploads//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, + ); + }); + }); }); diff --git a/src/resources/sites.ts b/src/resources/sites.ts index 781d8e8..b26d4fc 100644 --- a/src/resources/sites.ts +++ b/src/resources/sites.ts @@ -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 { + return this.http.post( + `/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 { + const body = completions && completions.length > 0 + ? { completions } + : undefined; + return this.http.post( + `/v1/sites/${encodeURIComponent(siteId)}/uploads/${encodeURIComponent(versionId)}/finalize`, + body, + ); + } + async deploy( siteId: string, source: string | DeployFile[], @@ -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( - `/v1/sites/${encodedSiteId}/uploads`, - { files: manifest }, - ); + const upload = await this.startUpload(siteId, manifest); const completions: UploadCompletion[] = []; @@ -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( - `/v1/sites/${encodedSiteId}/uploads/${encodeURIComponent(upload.versionId)}/finalize`, - finalizeBody, - ); + const finalize = await this.finalizeUpload(siteId, upload.versionId, completions); return { versionId: upload.versionId, diff --git a/src/types.ts b/src/types.ts index 892ba66..2e750b2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; }