|
| 1 | +import { authFetch } from "../authFetch.js"; |
| 2 | + |
| 3 | +type BaseOpts = { |
| 4 | + authServerUrl: string; |
| 5 | + authorization?: string; |
| 6 | +}; |
| 7 | + |
| 8 | +type WithQuery = BaseOpts & { |
| 9 | + query?: Record<string, any>; |
| 10 | +}; |
| 11 | + |
| 12 | +type WithBody = BaseOpts & { |
| 13 | + body?: any; |
| 14 | +}; |
| 15 | + |
| 16 | +type Result = { |
| 17 | + status: number; |
| 18 | + body?: any; |
| 19 | + error?: string; |
| 20 | +}; |
| 21 | + |
| 22 | +function buildUrl(base: string, query?: Record<string, any>) { |
| 23 | + if (!query) return base; |
| 24 | + |
| 25 | + const qs = new URLSearchParams( |
| 26 | + Object.entries(query) |
| 27 | + .filter(([, v]) => v !== undefined && v !== null) |
| 28 | + .map(([k, v]) => [k, String(v)]), |
| 29 | + ).toString(); |
| 30 | + |
| 31 | + return qs ? `${base}?${qs}` : base; |
| 32 | +} |
| 33 | + |
| 34 | +async function request( |
| 35 | + method: "GET" | "POST" | "PATCH" | "DELETE", |
| 36 | + path: string, |
| 37 | + opts: WithQuery & WithBody, |
| 38 | +): Promise<Result> { |
| 39 | + const up = await authFetch( |
| 40 | + buildUrl(`${opts.authServerUrl}${path}`, opts.query), |
| 41 | + { |
| 42 | + method, |
| 43 | + authorization: opts.authorization, |
| 44 | + body: opts.body, |
| 45 | + }, |
| 46 | + ); |
| 47 | + |
| 48 | + const data = await up.json(); |
| 49 | + |
| 50 | + if (!up.ok) { |
| 51 | + return { |
| 52 | + status: up.status, |
| 53 | + error: data?.error || "admin_request_failed", |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + return { |
| 58 | + status: up.status, |
| 59 | + body: data, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +export const getUsersHandler = (opts: BaseOpts) => |
| 64 | + request("GET", "/admin/users", opts); |
| 65 | + |
| 66 | +export const createUserHandler = (opts: WithBody) => |
| 67 | + request("POST", "/admin/users", opts); |
| 68 | + |
| 69 | +export const deleteUserHandler = (opts: BaseOpts) => |
| 70 | + request("DELETE", "/admin/users", opts); |
| 71 | + |
| 72 | +export const updateUserHandler = (userId: string, opts: WithBody) => |
| 73 | + request("PATCH", `/admin/users/${userId}`, opts); |
| 74 | + |
| 75 | +export const getUserDetailHandler = (userId: string, opts: BaseOpts) => |
| 76 | + request("GET", `/admin/users/${userId}`, opts); |
| 77 | + |
| 78 | +export const getUserAnomaliesHandler = (userId: string, opts: BaseOpts) => |
| 79 | + request("GET", `/admin/users/${userId}/anomalies`, opts); |
| 80 | + |
| 81 | +export const getAuthEventsHandler = (opts: WithQuery) => |
| 82 | + request("GET", "/admin/auth-events", opts); |
| 83 | + |
| 84 | +export const getCredentialCountHandler = (opts: BaseOpts) => |
| 85 | + request("GET", "/admin/credential-count", opts); |
| 86 | + |
| 87 | +export const listAllSessionsHandler = (opts: WithQuery) => |
| 88 | + request("GET", "/admin/sessions", opts); |
| 89 | + |
| 90 | +export const listUserSessionsHandler = (userId: string, opts: BaseOpts) => |
| 91 | + request("GET", `/admin/sessions/${userId}`, opts); |
| 92 | + |
| 93 | +export const revokeAllUserSessionsHandler = (userId: string, opts: BaseOpts) => |
| 94 | + request("DELETE", `/admin/sessions/${userId}/revoke-all`, opts); |
0 commit comments