api.ts (1165B)
1 'use server' 2 import createClient from "openapi-fetch"; 3 import type { paths } from "./api/v1"; 4 import { cacheLife } from "next/dist/server/use-cache/cache-life"; 5 6 export interface DraupnirBot { 7 id: string, 8 managementRoom: string, 9 ownerID: string, 10 displayName: string, 11 protectedRooms: { 12 room: string, 13 displayName?: string, 14 }[], 15 subscribedLists: string[], 16 } 17 18 export interface ListResponse { 19 bots: DraupnirBot[] 20 } 21 22 const client = createClient<paths>({ baseUrl: `${process.env.NEXT_PUBLIC_D4ALL_INSTANCE_ADDRESS}/api` }); 23 24 export async function listBots(mxid: string, token: string): Promise<ListResponse | undefined> { 25 'use cache' 26 cacheLife("minutes") 27 28 console.log("Fetching teams from Draupnir4All instance for user:", mxid); 29 30 const { data, error } = await client.GET("/1/appservice/list", { 31 params: { 32 header: { 33 "X-Draupnir-UserID": mxid, 34 Authorization: `Bearer ${token}`, 35 } 36 } 37 }); 38 if (error) { 39 console.error("Failed to fetch teams:", error); 40 throw new Error("Failed to fetch teams"); 41 } 42 return data; 43 }