Skip to content

client

The typed client and the upload workflow.

@softzweig/suitcase-client is transport-agnostic. It turns a definition into typed operations and runs the multi-step upload flow on top of them.

suitcase/client.ts
import type { SuitcaseClient } from "./definition.js";
import { createSuitcaseClient } from "@softzweig/suitcase-client";
import { createSuitcaseOperations } from "@softzweig/suitcase-core/contracts";
import { createHttpTransport } from "@softzweig/suitcase-http";
const operations = createSuitcaseOperations<SuitcaseClient>({
transport: createHttpTransport({
url: "/api/suitcase",
configure: () => ({ headers: { "x-user": currentUser() } }),
}),
});
export const client = createSuitcaseClient<SuitcaseClient>({
operations,
transfer: async (target, bytes, signal) => {
const response = await fetch(target.url, {
method: target.method,
headers: target.headers,
body: bytes,
signal,
});
if (!response.ok) throw new Error(`Upload refused (${response.status})`);
},
});

SuitcaseClient is the alias exported from the definition module. Import it with import type so the server package never enters the browser bundle. See Export the client type.

Dependency Required Meaning
operations yes The typed operations, usually from createSuitcaseOperations
transfer yes Sends bytes to a presigned target
digest no Defaults to SHA-256 over the bytes
processors no Rules applied before the file is claimed
client.operations; // every operation, raw
client.resources; // resolve, update, delete
client.upload.constraints({ kind });
client.upload.start({ kind, file, intent?, metadata?, signal? });
client.usage.total({ kinds, owner });

upload.start is the workflow, not one call. It reads constraints, processes the file, checks it, claims it, reuses or transfers, and finalizes. It returns { resource, reused, upload }. The metadata argument is typed by the kind’s metadata schema: required when the schema does not allow undefined, and refused when the kind declares none.

The package does not assume fetch exists. You pass the function that moves bytes:

transfer: async (target, bytes, signal) => {
const response = await fetch(target.url, {
method: target.method,
headers: target.headers,
body: bytes,
signal,
});
if (!response.ok) throw new Error(`Upload refused (${response.status})`);
};

Refusals are SuitcaseClientError instances with a SpindleErrorCode, whether the server or the client produced them. A network failure or a malformed response is refused as unknown.

  • It does not retry uploads.
  • It does not cache resolves.
  • It does not pick files, read cookies, or know about React.
  • It does not read configuration from globals.

Those belong to the host, the transport, or the React bindings.