@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
34 lines (32 loc) • 1.25 kB
JavaScript
import { softrApiUrl } from "../softr-api.js";
import { fetch } from "../fetch.js";
import { sessionConfig } from "../auth/config.js";
import { assertCanChange, readSessionToken } from "../auth/session.js";
import { describeFailure } from "../datasource/record.js";
import { randomUUID } from "node:crypto";
//#region src/core/file-upload.ts
async function upload(action, file) {
assertCanChange(`Upload "${action.id}"`);
const { applicationId } = sessionConfig();
const baseUrl = softrApiUrl("SOFTR_PLATFORM_API_URL");
const uploadUrl = new URL(`${baseUrl}/v1/vibe/applications/${applicationId}/actions/${action.id}/uploads`);
uploadUrl.searchParams.set("fileName", file.name);
const formData = new FormData();
formData.append("file", file);
const token = readSessionToken();
const res = await fetch(uploadUrl.toString(), {
method: "POST",
headers: token ? { Authorization: `Bearer ${token}` } : {},
body: formData
});
if (!res.ok) throw new Error(`File upload failed: ${await describeFailure(res)}`);
const resultJSON = await res.json();
return {
url: resultJSON.url,
filename: resultJSON.filename || "Untitled file",
id: resultJSON.id || randomUUID(),
type: resultJSON.type ?? null
};
}
//#endregion
export { upload };