@vaadin/hilla-frontend
Advanced tools
Comparing version 24.7.0-alpha10 to 24.7.0-alpha11
import type { ReactiveControllerHost } from '@lit/reactive-element'; | ||
import { type ActionOnLostSubscription, FluxConnection, type FluxSubscriptionStateChangeEvent } from './FluxConnection.js'; | ||
export declare const BODY_PART_NAME = "hilla_body_part"; | ||
export type MaybePromise<T> = Promise<T> | T; | ||
@@ -4,0 +5,0 @@ /** |
@@ -19,2 +19,3 @@ import { ConnectionIndicator, ConnectionState } from "@vaadin/common-frontend"; | ||
}); | ||
const BODY_PART_NAME = "hilla_body_part"; | ||
const assertResponseIsOk = async (response) => { | ||
@@ -50,2 +51,27 @@ if (!response.ok) { | ||
} | ||
function extractFiles(obj) { | ||
const fileMap = /* @__PURE__ */ new Map(); | ||
function recursiveExtract(prop, path) { | ||
if (prop !== null && typeof prop === "object") { | ||
if (prop instanceof File) { | ||
fileMap.set(path, prop); | ||
return null; | ||
} | ||
if (Array.isArray(prop)) { | ||
return prop.map((item, index) => recursiveExtract(item, `${path}/${index}`)); | ||
} | ||
return Object.entries(prop).reduce((acc, [key, value]) => { | ||
const newPath = `${path}/${key}`; | ||
if (value instanceof File) { | ||
fileMap.set(newPath, value); | ||
} else { | ||
acc[key] = recursiveExtract(value, newPath); | ||
} | ||
return acc; | ||
}, {}); | ||
} | ||
return prop; | ||
} | ||
return [recursiveExtract(obj, ""), fileMap]; | ||
} | ||
class ConnectClient { | ||
@@ -118,7 +144,24 @@ /** | ||
Accept: "application/json", | ||
"Content-Type": "application/json", | ||
...csrfHeaders | ||
}; | ||
const [paramsWithoutFiles, files] = extractFiles(params ?? {}); | ||
let body; | ||
if (files.size > 0) { | ||
body = new FormData(); | ||
body.append( | ||
BODY_PART_NAME, | ||
JSON.stringify(paramsWithoutFiles, (_, value) => value === void 0 ? null : value) | ||
); | ||
for (const [path, file] of files) { | ||
body.append(path, file); | ||
} | ||
} else { | ||
headers["Content-Type"] = "application/json"; | ||
if (params) { | ||
body = JSON.stringify(params, (_, value) => value === void 0 ? null : value); | ||
} | ||
} | ||
const request = new Request(`${this.prefix}/${endpoint}/${method}`, { | ||
body: params !== void 0 ? JSON.stringify(params, (_, value) => value === void 0 ? null : value) : void 0, | ||
body, | ||
// automatically sets Content-Type header | ||
headers, | ||
@@ -188,4 +231,5 @@ method: "POST" | ||
export { | ||
BODY_PART_NAME, | ||
ConnectClient | ||
}; | ||
//# sourceMappingURL=Connect.js.map |
@@ -5,3 +5,3 @@ function __REGISTER__(feature, vaadinObj = window.Vaadin ??= {}) { | ||
is: feature ? `${"@vaadin/hilla-frontend"}/${feature}` : "@vaadin/hilla-frontend", | ||
version: "24.7.0-alpha10" | ||
version: "24.7.0-alpha11" | ||
}); | ||
@@ -8,0 +8,0 @@ } |
{ | ||
"name": "@vaadin/hilla-frontend", | ||
"version": "24.7.0-alpha10", | ||
"version": "24.7.0-alpha11", | ||
"description": "Hilla core frontend utils", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
129159
1327