diff --git a/src/doc/doc.ts b/src/doc/doc.ts index e1735fd..f384c09 100644 --- a/src/doc/doc.ts +++ b/src/doc/doc.ts @@ -1,5 +1,4 @@ import type { Agd, Id, Pub, Soc, Spc, Usr } from "../db/typ.ts" -import type { Has } from "../ser/nav.ts" import { usr_r } from "../db/usr.ts" import { pas_of_usr, type Pas } from "./pas.ts" import { pub_usr } from "./pub.ts" @@ -40,6 +39,15 @@ export type Doc = { }, } +export type Has = { + usr?: Usr["_id"], + soc?: Soc["_id"], + agd?: Agd["_id"], + spc?: Spc["_id"], + rec?: "cdt" | "dbt" | "ern" | "act", +} + + async function usr( pas: Pas | null, _id: Usr["_id"], diff --git a/src/frm/frm.ts b/src/frm/frm.ts new file mode 100644 index 0000000..195ce6e --- /dev/null +++ b/src/frm/frm.ts @@ -0,0 +1,7 @@ +import { Usr } from "../db/typ.ts" + +export type Frm = { + frm: "psg", + nbr?: Required["nbr"], + pas?: Required["pas"], +} diff --git a/src/frm/psg.tsx b/src/frm/psg.tsx new file mode 100644 index 0000000..02bc634 --- /dev/null +++ b/src/frm/psg.tsx @@ -0,0 +1,2 @@ +function Psg( +) diff --git a/src/nav/fet.ts b/src/nav/fet.ts new file mode 100644 index 0000000..458f169 --- /dev/null +++ b/src/nav/fet.ts @@ -0,0 +1,10 @@ +import type { Nav } from "../ser./nav/nav.ts" + +export async function pos( + h: Nav, +) { + const res = await fetch(`/p`, { method: "POST", body: JSON.stringify(h) }) + const r = await res.json() as T + if (r && !("psg" in h)) await navpas() + return r +} diff --git a/src/ser/nav.ts b/src/nav/nav.ts similarity index 54% rename from src/ser/nav.ts rename to src/nav/nav.ts index 3263ed8..50b112f 100644 --- a/src/ser/nav.ts +++ b/src/nav/nav.ts @@ -1,26 +1,18 @@ -import { Agd, Soc, Spc, Usr } from "../db/typ.ts" -import { Psg } from "../que/psg.ts" +import type { Has } from "../doc/doc.ts" +import type { Pas } from "../doc/pas.ts" +import { Frm } from "../frm/frm.ts" -export type Has = { - usr?: Usr["_id"], - soc?: Soc["_id"], - agd?: Agd["_id"], - spc?: Spc["_id"], - rec?: "cdt" | "dbt" | "ern" | "act", - que?: Que, +export type Nav = { + has: Has, + frm?: Frm, + pas?: Pas, } -export type Que = Psg | { - que: "sig", - sig: string, -} | { - que: "act", - act: "cdt" | "ern", -} +export const nav: Nav = { has: {} } -export function has( +export function hasque( s: string, -): Has { +) { const h: Has = {} const [p, q] = decodeURI(s).substring(1).split("?") if (p.startsWith("t")) h.spc = p @@ -31,7 +23,9 @@ export function has( else if (h.spc == undefined && p.startsWith("#")) h.spc = p.substring(1) else if ((h.usr || h.soc) && ["cdt", "dbt", "ern", "act"].includes(p)) h.rec = p as Has["rec"] }) - if (!q || q.length == 0) return h - else if (q.startsWith("s=")) h.que = { que: "sig", sig: q.substring(2) } - return h + nav.has = h + delete nav.frm + if (!q || q.length == 0) return nav + else if (q == "psg") nav.frm = { frm: "psg" } + return nav } diff --git a/src/ser/ser.ts b/src/ser/ser.ts index 17997b1..b74c642 100644 --- a/src/ser/ser.ts +++ b/src/ser/ser.ts @@ -1,5 +1,5 @@ import { utc_dt } from "../uti/utc.ts" -import { Has } from "./nav.ts" +import { Nav } from "../nav/nav.ts" function log( utc: number, @@ -31,9 +31,9 @@ async function ser( } case "q": { const _c = req.headers.get("cookie")?.split(";").find(c => c.startsWith("q=")) try { - const h: Has = await req.json() - log(utc, `${r}?${h.que ? h.que : ""}`, 200) - return new Response(JSON.stringify(h), { status: 200 }) + const nav: Nav = await req.json() + log(utc, `${r}?${JSON.stringify(nav.has)}`, 200) + return new Response(JSON.stringify(nav), { status: 200 }) } catch { log(utc, `${r}`, 400) return new Response(null, { status: 400 }) diff --git a/tst/doc.test.ts b/tst/doc.test.ts index 3af7215..1f4069d 100644 --- a/tst/doc.test.ts +++ b/tst/doc.test.ts @@ -1,8 +1,8 @@ import { db } from "../src/db/db.ts" import { usr_r } from "../src/db/usr.ts" -import { has } from "../src/ser/nav.ts" import { doc } from "../src/doc/doc.ts" import { pas_aut, pas_of_usr, pas_soc, pas_usr } from "../src/doc/pas.ts" +import { hasque } from "../src/nav/nav.ts"; import { fil } from "./fil.ts" import { assertEquals } from "@std/assert" @@ -22,17 +22,6 @@ Deno.test("pas", async () => { ], [false, false, false]) }) -Deno.test("nav", () => { - assertEquals(has(""), {}) - assertEquals(has("#2"), { usr: 2 }) - assertEquals(has("#s1-2-a3"), { soc: 1, usr: 2, agd: 3 }) - assertEquals(has("#a3-s1-2"), { soc: 1, usr: 2, agd: 3 }) - assertEquals(has("#s1-2-ern"), { soc: 1, usr: 2, rec: "ern" }) - assertEquals(has("##中文123"), { spc: "中文123" }) - assertEquals(has(encodeURI("##中文123")), { spc: "中文123" }) - assertEquals(has("##中文123?s=123abc"), { spc: "中文123", que: { que: "sig", sig: "123abc" } }) -}) - Deno.test("pas", async () => { const [u, s] = [fil.usr[2], fil.soc[0]] assertEquals( @@ -44,12 +33,12 @@ Deno.test("pas", async () => { Deno.test("doc", async () => { const [u, s] = [fil.usr[2], fil.soc[0]] assertEquals( - await doc(null, has(`#s${s._id}-${u._id}`)), + await doc(null, hasque(`#s${s._id}-${u._id}`).has), { usr: { _id: u._id } }, ) const p = await pas_of_usr({ _id: u._id, nam: u.nam }) assertEquals( - (await doc(p, has(`#s${s._id}-${u._id}`)))?.usr?.soc, + (await doc(p, hasque(`#s${s._id}-${u._id}`).has))?.usr?.soc, [{ soc: s._id, nam: s.nam, sec: true }], ) }) diff --git a/tst/nav.test.ts b/tst/nav.test.ts new file mode 100644 index 0000000..efbd7b0 --- /dev/null +++ b/tst/nav.test.ts @@ -0,0 +1,13 @@ +import { assertEquals } from "@std/assert/equals" +import { hasque } from "../src/nav/nav.ts" + +Deno.test("nav", () => { + assertEquals(hasque(""), { has: {} }) + assertEquals(hasque("#2"), { has: { usr: 2 } }) + assertEquals(hasque("#s1-2-a3"), { has: { soc: 1, usr: 2, agd: 3 } }) + assertEquals(hasque("#a3-s1-2"), { has: { soc: 1, usr: 2, agd: 3 } }) + assertEquals(hasque("#s1-2-ern"), { has: { soc: 1, usr: 2, rec: "ern" } }) + assertEquals(hasque("##中文123"), { has: { spc: "中文123" } }) + assertEquals(hasque(encodeURI("##中文123")), { has: { spc: "中文123" } }) + assertEquals(hasque("##中文123?psg"), { has: { spc: "中文123" }, frm: { frm: "psg" } }) +}) diff --git a/tst/ser.test.ts b/tst/ser.test.ts index 9d58b04..33572a8 100644 --- a/tst/ser.test.ts +++ b/tst/ser.test.ts @@ -1,16 +1,16 @@ import { db } from "../src/db/db.ts" -import { has } from "../src/ser/nav.ts" +import { hasque } from "../src/nav/nav.ts" import { assertEquals } from "@std/assert" await db("tst", true, true) Deno.test("ser", async () => { - const h = has("##中文123?s=123abc") + const n = hasque("##中文123?s=123abc") const res = await fetch("http://localhost:728/p", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify(h), + body: JSON.stringify(n), }) - assertEquals(await res.json(), h) + assertEquals(await res.json(), n) })