@kahitsan/ksui 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kahitsan/ksui",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "description": "ksui is a standalone set of SolidJS UI components for KahitSan/Hilinga and any SolidJS app. Published to the public npm registry and consumed as a normal dependency. Ships source under a `solid` export condition so the consumer's vite-plugin-solid compiles it with only solid-js externalized; it depends on nothing but solid-js + lucide-solid and injects its own CSS.",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/index.ts CHANGED
@@ -197,6 +197,12 @@ export { attachmentUrl, isResolvableAttachment } from "./utils/attachments";
197
197
 
198
198
  export { createObjectUrlResource } from "./utils/object-url-resource";
199
199
  export type { ObjectUrlOptions } from "./utils/object-url-resource";
200
+ export {
201
+ createPendingFile,
202
+ revokePendingFile,
203
+ uploadPendingFiles,
204
+ } from "./utils/pending-file";
205
+ export type { PendingFile, UploadPendingFilesOptions } from "./utils/pending-file";
200
206
 
201
207
  export { useAccountsIndex, resolveAccount, resolveAccountName } from "./utils/accounts-index";
202
208
 
@@ -0,0 +1,59 @@
1
+ // uploadPendingFiles: per-file best-effort POST to the transactions plugin's
2
+ // standard attachment route, returning the names that failed.
3
+ import { describe, expect, it, vi, afterEach } from "vitest";
4
+ import { uploadPendingFiles, type PendingFile } from "./pending-file";
5
+
6
+ function pf(name: string): PendingFile {
7
+ return { id: name, file: new File(["x"], name), previewUrl: null };
8
+ }
9
+
10
+ describe("uploadPendingFiles", () => {
11
+ afterEach(() => {
12
+ vi.unstubAllGlobals();
13
+ });
14
+
15
+ it("POSTs each file to the transaction's attachments route with credentials included", async () => {
16
+ const fetchMock = vi.fn().mockResolvedValue({ ok: true });
17
+ vi.stubGlobal("fetch", fetchMock);
18
+
19
+ const failed = await uploadPendingFiles(42, [pf("receipt.jpg")]);
20
+
21
+ expect(failed).toEqual([]);
22
+ expect(fetchMock).toHaveBeenCalledWith(
23
+ "/api/transactions/42/attachments",
24
+ expect.objectContaining({ method: "POST", credentials: "include" })
25
+ );
26
+ });
27
+
28
+ it("layers caller-supplied headers onto the request", async () => {
29
+ const fetchMock = vi.fn().mockResolvedValue({ ok: true });
30
+ vi.stubGlobal("fetch", fetchMock);
31
+
32
+ await uploadPendingFiles(42, [pf("receipt.jpg")], {
33
+ headers: { "X-Workspace-Id": "7" },
34
+ });
35
+
36
+ expect(fetchMock).toHaveBeenCalledWith(
37
+ "/api/transactions/42/attachments",
38
+ expect.objectContaining({ headers: { "X-Workspace-Id": "7" } })
39
+ );
40
+ });
41
+
42
+ it("collects failed file names without aborting the remaining uploads", async () => {
43
+ const fetchMock = vi
44
+ .fn()
45
+ .mockResolvedValueOnce({ ok: false })
46
+ .mockRejectedValueOnce(new Error("network error"))
47
+ .mockResolvedValueOnce({ ok: true });
48
+ vi.stubGlobal("fetch", fetchMock);
49
+
50
+ const failed = await uploadPendingFiles(42, [
51
+ pf("a.jpg"),
52
+ pf("b.jpg"),
53
+ pf("c.jpg"),
54
+ ]);
55
+
56
+ expect(failed).toEqual(["a.jpg", "b.jpg"]);
57
+ expect(fetchMock).toHaveBeenCalledTimes(3);
58
+ });
59
+ });
@@ -0,0 +1,68 @@
1
+ // Pre-upload pending file state — a file the user has picked or pasted but not
2
+ // yet uploaded. The plugins that support attachment upload (transactions,
3
+ // counter, timesheets/payroll) each defined this locally; extracted here so
4
+ // they share one canonical type + helpers and avoid drift.
5
+
6
+ /** A file the user picked or pasted but hasn't been uploaded yet. */
7
+ export interface PendingFile {
8
+ id: string;
9
+ file: File;
10
+ previewUrl: string | null;
11
+ }
12
+
13
+ let _counter = 0;
14
+
15
+ /** Wrap a picked/pasted File into a PendingFile with a stable id and optional
16
+ * image preview URL. Must be called from the client (browser) — relies on
17
+ * URL.createObjectURL. */
18
+ export function createPendingFile(file: File): PendingFile {
19
+ const isImage = file.type.startsWith("image/");
20
+ return {
21
+ id: `pf-${++_counter}-${Date.now()}`,
22
+ file,
23
+ previewUrl: isImage ? URL.createObjectURL(file) : null,
24
+ };
25
+ }
26
+
27
+ /** Release the object URL held by a pending file. Call when the file is removed
28
+ * from the list or after upload completes. */
29
+ export function revokePendingFile(pf: PendingFile): void {
30
+ if (pf.previewUrl) URL.revokeObjectURL(pf.previewUrl);
31
+ }
32
+
33
+ /** Extra fetch() init the caller needs layered onto every upload request —
34
+ * e.g. timesheets/payroll's X-Workspace-Id header for the fresh-login case
35
+ * where the host's fetch monkey-patch has no localStorage fallback yet. */
36
+ export interface UploadPendingFilesOptions {
37
+ headers?: Record<string, string>;
38
+ }
39
+
40
+ /** Upload each pending file to the transactions plugin's standard multipart
41
+ * POST /:id/attachments S3 route — the one place attachment bytes are ever
42
+ * written, whichever plugin's UI collected them. Best-effort per file: a
43
+ * failed upload doesn't stop the rest. Returns the file names that failed,
44
+ * in the file's own `name` (not a caller-relabeled `file_name`), for the
45
+ * caller to surface as a soft error. */
46
+ export async function uploadPendingFiles(
47
+ transactionId: number,
48
+ files: PendingFile[],
49
+ opts: UploadPendingFilesOptions = {}
50
+ ): Promise<string[]> {
51
+ const failed: string[] = [];
52
+ for (const pf of files) {
53
+ try {
54
+ const fd = new FormData();
55
+ fd.append("file", pf.file, pf.file.name);
56
+ const res = await fetch(`/api/transactions/${transactionId}/attachments`, {
57
+ method: "POST",
58
+ credentials: "include",
59
+ headers: opts.headers,
60
+ body: fd,
61
+ });
62
+ if (!res.ok) failed.push(pf.file.name);
63
+ } catch {
64
+ failed.push(pf.file.name);
65
+ }
66
+ }
67
+ return failed;
68
+ }