diff --git a/driver-pwa/frontend/src/api/driver.test.ts b/driver-pwa/frontend/src/api/driver.test.ts index fd1e274..8d957d9 100644 --- a/driver-pwa/frontend/src/api/driver.test.ts +++ b/driver-pwa/frontend/src/api/driver.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; -import { BalanceSchema, TopupSchema, PaymentsSchema, AuthRequestSchema } from "./driver"; +import { BalanceSchema, TopupSchema, PaymentsSchema, AuthRequestSchema, TgSessionSchema, TgPollSchema } from "./driver"; describe("driver api schemas", () => { it("parses balance", () => { @@ -24,3 +24,16 @@ describe("driver api schemas", () => { expect(v.payments[0].status).toBe("PAID"); }); }); + +describe("tg verified-login schemas", () => { + it("parses tg session", () => { + const v = TgSessionSchema.parse({ pair_session: "S", deep_link: "https://t.me/x?start=S" }); + expect(v.pair_session).toBe("S"); + }); + it("parses tg poll states", () => { + expect(TgPollSchema.parse({ status: "pending" }).status).toBe("pending"); + expect(TgPollSchema.parse({ status: "expired" }).status).toBe("expired"); + const a = TgPollSchema.parse({ status: "authenticated", token: "T", driver_id: 7 }); + expect(a.status === "authenticated" && a.token).toBe("T"); + }); +}); diff --git a/driver-pwa/frontend/src/api/driver.ts b/driver-pwa/frontend/src/api/driver.ts index fda7c37..45f8af5 100644 --- a/driver-pwa/frontend/src/api/driver.ts +++ b/driver-pwa/frontend/src/api/driver.ts @@ -24,6 +24,21 @@ export const PaymentsSchema = z.object({ })), }); +export const TgSessionSchema = z.object({ + pair_session: z.string(), + deep_link: z.string().nullable(), +}); +export const TgPollSchema = z.union([ + z.object({ status: z.literal("pending") }), + z.object({ status: z.literal("expired") }), + z.object({ status: z.literal("authenticated"), token: z.string(), driver_id: z.number() }), +]); + +export const tgSession = async () => + TgSessionSchema.parse(await api.post("driver/auth/tg/session").json()); +export const tgPoll = async (pairSession: string) => + TgPollSchema.parse(await api.get("driver/auth/tg/poll", { searchParams: { pair_session: pairSession } }).json()); + export const authRequest = async (phone: string) => AuthRequestSchema.parse(await api.post("driver/auth/request", { json: { phone } }).json()); export const authVerify = async (phone: string, code: string) =>