ser pos 请求接口 HTTP POST #45
This commit is contained in:
@ -27,7 +27,7 @@
|
|||||||
- `que.ts` 查询接口 `query` `HTTP GET` 对数据组合做请求
|
- `que.ts` 查询接口 `query` `HTTP GET` 对数据组合做请求
|
||||||
- `pas.ts` 用户登陆 `pass` (的操作组合)
|
- `pas.ts` 用户登陆 `pass` (的操作组合)
|
||||||
- `pre.ts` 创建操作 `pre-data` `prepare` (的操作组合)
|
- `pre.ts` 创建操作 `pre-data` `prepare` (的操作组合)
|
||||||
- `put.ts` 数据操作 `put-data`(的操作组合)
|
- `put.ts` 更新操作 `put-data`(的操作组合)
|
||||||
- `pos.ts` 请求接口 `post` `HTTP POST` 对操作组合做请求
|
- `pos.ts` 请求接口 `post` `HTTP POST` 对操作组合做请求
|
||||||
* `ser.ts` 服务接口 `serve`
|
* `ser.ts` 服务接口 `serve`
|
||||||
* `tst` 测试代码 `tests`
|
* `tst` 测试代码 `tests`
|
||||||
|
@ -7,23 +7,24 @@ export type { PreRet } from "./pre.ts"
|
|||||||
|
|
||||||
export type Pos = Psg | Pre
|
export type Pos = Psg | Pre
|
||||||
export type PosRet = {
|
export type PosRet = {
|
||||||
psg: Ret<typeof psg>,
|
ret: Ret<typeof psg>["ret"] | Ret<typeof pre>,
|
||||||
pre: { ret: Ret<typeof pre> },
|
jwt?: string | null,
|
||||||
|
etag?: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function pos(
|
export async function pos(
|
||||||
b: string,
|
b: string,
|
||||||
jwt?: string,
|
jwt?: string,
|
||||||
) {
|
): Promise<PosRet> {
|
||||||
let json
|
let json
|
||||||
try { json = b.length > 0 ? JSON.parse(b) as Pos : {} }
|
try { json = b.length > 0 ? JSON.parse(b) as Pos : {} }
|
||||||
catch { return { ret: null } }
|
catch { return { ret: null } }
|
||||||
|
|
||||||
const p = jwt ? await pas(jwt) : null
|
const p = jwt ? await pas(jwt) : null
|
||||||
|
let ret = null
|
||||||
if ("psg" in json) return psg(p, json)
|
if ("psg" in json) return psg(p, json)
|
||||||
else if (!p) return { ret: null }
|
else if (!p) return { ret: null, jwt: null }
|
||||||
else if ("pre" in json) return { ret: await pre(p, json) }
|
else if ("pre" in json) ret = await pre(p, json)
|
||||||
|
|
||||||
return { ret: null }
|
return ret ? { ret, etag: "" } : { ret }
|
||||||
}
|
}
|
||||||
|
15
src/ser.ts
15
src/ser.ts
@ -1,5 +1,6 @@
|
|||||||
import { db } from "./eid/db.ts"
|
import { db } from "./eid/db.ts"
|
||||||
import { utc_dt, utc_etag, utc_h } from "./ont/utc.ts"
|
import { utc_dt, utc_etag, utc_h } from "./ont/utc.ts"
|
||||||
|
import { pos } from "./pra/pos.ts"
|
||||||
import { que } from "./pra/que.ts"
|
import { que } from "./pra/que.ts"
|
||||||
|
|
||||||
db("ismism")
|
db("ismism")
|
||||||
@ -40,6 +41,20 @@ async function handler(
|
|||||||
log(utc, `${r}${s}`, 200)
|
log(utc, `${r}${s}`, 200)
|
||||||
const q = await que(s)
|
const q = await que(s)
|
||||||
return new Response(JSON.stringify(q), { status: 200, headers: { etag } })
|
return new Response(JSON.stringify(q), { status: 200, headers: { etag } })
|
||||||
|
} case "p": {
|
||||||
|
const [cookie] = req.headers.get("cookie")?.split(";").filter(c => c.startsWith("pp=")) ?? []
|
||||||
|
const jwt = cookie ? cookie.substring(3) : undefined
|
||||||
|
const b = await req.text()
|
||||||
|
const r = await pos(b, jwt)
|
||||||
|
const headers: Headers = new Headers()
|
||||||
|
if ("jwt" in r) {
|
||||||
|
const [pp, ma] = r.jwt ? [r.jwt, 31728728] : ["", 0]
|
||||||
|
headers.set("set-cookie", `pp=${pp}; Path=/p; SameSite=Strict; Secure; HttpOnly; Max-Age=${ma}`)
|
||||||
|
}
|
||||||
|
if ("etag" in r) etag = r.etag ?? ""
|
||||||
|
const s = JSON.stringify(r.ret)
|
||||||
|
log(utc, `${b} => ${s}`, 200)
|
||||||
|
return new Response(s, { status: 200, headers })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new Response(null, { status: 400 })
|
return new Response(null, { status: 400 })
|
||||||
|
@ -19,7 +19,7 @@ Deno.test("pas", async () => {
|
|||||||
const usr = await usr_c(nbr, "四川", "成都")
|
const usr = await usr_c(nbr, "四川", "成都")
|
||||||
assertEquals(1, usr)
|
assertEquals(1, usr)
|
||||||
assertEquals([
|
assertEquals([
|
||||||
{ ret: null }, { ret: null }, { ret: null }
|
{ ret: null, jwt: null }, { ret: null, jwt: null }, { ret: null }
|
||||||
], await Promise.all([
|
], await Promise.all([
|
||||||
pos(""), pos("", ""), pos(json({ psg: "pas" }), "invalidkey")
|
pos(""), pos("", ""), pos(json({ psg: "pas" }), "invalidkey")
|
||||||
]))
|
]))
|
||||||
@ -27,7 +27,7 @@ Deno.test("pas", async () => {
|
|||||||
const { ret: sms } = await pos(json({ psg: "sms", nbr, sms: false })) as { ret: PsgRet["sms"] }
|
const { ret: sms } = await pos(json({ psg: "sms", nbr, sms: false })) as { ret: PsgRet["sms"] }
|
||||||
assertEquals(true, sms && !sms.sms && is_utc(sms.utc!))
|
assertEquals(true, sms && !sms.sms && is_utc(sms.utc!))
|
||||||
const code = await usr_r({ nbr }, { sms: 1 })
|
const code = await usr_r({ nbr }, { sms: 1 })
|
||||||
const { ret: pas, jwt } = await pos(json({ psg: "code", nbr, code: code?.sms?.code! })) as PosRet["psg"] & { ret: PsgRet["code"] }
|
const { ret: pas, jwt } = await pos(json({ psg: "code", nbr, code: code?.sms?.code! })) as PosRet & { ret: PsgRet["code"] }
|
||||||
assertEquals(true, pas && pas.usr == usr && jwt!.length > 0)
|
assertEquals(true, pas && pas.usr == usr && jwt!.length > 0)
|
||||||
assertEquals({ ret: pas }, await pos(json({ psg: "pas" }), jwt!))
|
assertEquals({ ret: pas }, await pos(json({ psg: "pas" }), jwt!))
|
||||||
assertEquals({ ret: 1, jwt: null }, await pos(json({ psg: "clr", usr: pas!.usr }), jwt!))
|
assertEquals({ ret: 1, jwt: null }, await pos(json({ psg: "clr", usr: pas!.usr }), jwt!))
|
||||||
@ -53,7 +53,7 @@ Deno.test("pos", async () => {
|
|||||||
const u2 = (await usr_c(nbr[1], adm1, adm2))!
|
const u2 = (await usr_c(nbr[1], adm1, adm2))!
|
||||||
await pos(json({ psg: "sms", nbr: nbr[1], sms: false }))
|
await pos(json({ psg: "sms", nbr: nbr[1], sms: false }))
|
||||||
const { sms } = (await usr_r({ _id: u2 }, { sms: 1 }))!
|
const { sms } = (await usr_r({ _id: u2 }, { sms: 1 }))!
|
||||||
const { jwt } = (await pos(json({ psg: "code", nbr: nbr[1], code: sms!.code })))! as PosRet["psg"]
|
const { jwt } = (await pos(json({ psg: "code", nbr: nbr[1], code: sms!.code })))! as PosRet
|
||||||
assertEquals([
|
assertEquals([
|
||||||
3, 1,
|
3, 1,
|
||||||
null, null, null, null, null, null,
|
null, null, null, null, null, null,
|
||||||
|
Reference in New Issue
Block a user