http api actually works

This commit is contained in:
Seth Trowbridge 2025-11-01 09:58:06 -04:00
parent 810e530bd1
commit b6c80af059
2 changed files with 42 additions and 22 deletions

View File

@ -1,5 +1,4 @@
/** @import * as TYPES from "./types.ts" */ /** @import * as TYPES from "./types.ts" */
import * as FSAccess from "../store-directory-handle.js";
/** @type {TYPES.GraphBuilder} */ /** @type {TYPES.GraphBuilder} */
export function Room({user, role, part, desk, pass}) export function Room({user, role, part, desk, pass})
@ -103,21 +102,12 @@ export function Room({user, role, part, desk, pass})
{ {
this.time = Date.now(); this.time = Date.now();
const handle = await FSAccess.getDirectoryHandle(); const body = JSON.stringify({
const path = ["store", context.Path, passID, user.id+".json"]; [partObj.id]:[this.time, data]
let text = await FSAccess.Read(handle, path) || "{}"; })
/** @type {TYPES.UserPassFile} */
const json = JSON.parse(text);
let field = json[partObj.id]; const http = await fetch(`./store/${context.Path}/${passID}/${user.id}.json`, {method:"PUT", body});
if(!field) console.log(http);
{
field = [];
json[partObj.id] = field;
}
field.push([this.time, data]);
text = JSON.stringify(json, null, 2);
await FSAccess.Write(handle, path, text);
this.work.push(/** @type {TYPES.Work}*/([this.time, data, user])); this.work.push(/** @type {TYPES.Work}*/([this.time, data, user]));
partObj.make.forEach((arg)=>{Scan(arg, passObj)}); partObj.make.forEach((arg)=>{Scan(arg, passObj)});
@ -137,10 +127,9 @@ export function Room({user, role, part, desk, pass})
const [userID, userObject] = userData[i]; const [userID, userObject] = userData[i];
try try
{ {
const handle = await FSAccess.getDirectoryHandle(); const http = await fetch(`./store/${context.Path}/${passID}/${userID}.json`);
const text = await FSAccess.Read(handle, ["store", context.Path, passID, userID+".json"]);
/** @type {TYPES.UserPassFile} */ /** @type {TYPES.UserPassFile} */
const json = JSON.parse(text); const json = await http.json();
Object.entries(json).forEach(([partID, payload])=>{ Object.entries(json).forEach(([partID, payload])=>{

View File

@ -21,15 +21,46 @@ async function Interceptor(event)
const pathname = url.pathname.substring(1); const pathname = url.pathname.substring(1);
let parts = pathname.split("/"); let parts = pathname.split("/");
if(parts[0] == "graph") if(parts[0] == "graph" || parts[0] == "store")
{ {
console.log("intercept:", pathname); console.log("intercept:", pathname);
const handle = await FSAccess.getDirectoryHandle(); const handle = await FSAccess.getDirectoryHandle();
if(event.request.method == "PUT")
{
console.log("put operation");
const payloadJSON = await event.request.clone().json();
let text = await FSAccess.Read(handle, parts) || "{}";
const fileJSON = JSON.parse(text);
for(const key in payloadJSON)
{
const value = payloadJSON[key];
let field = fileJSON[key];
if(!field)
{
field = [];
fileJSON[key] = field;
}
field.push(value);
}
console.log(`incoming:`, payloadJSON);
console.log(`outgoing:`, fileJSON);
text = JSON.stringify(fileJSON, null, 2);
const result = await FSAccess.Write(handle, parts, text);
return new Response(result+"", options);
}
const text = await FSAccess.Read(handle, parts); const text = await FSAccess.Read(handle, parts);
if(text) if(text)
{ {
return new Response(text, options); return new Response(text, options);
} }
} }