|
| 1 | +import { NEXT_PUBLIC_NEBULA_URL } from "@/constants/env"; |
| 2 | +import { fetchWithAuthToken } from "./fetchWithAuthToken"; |
| 3 | +import type { ExecuteConfig, SessionInfo } from "./types"; |
| 4 | + |
| 5 | +// TODO - get the spec for return types on /session POST and PUT |
| 6 | + |
| 7 | +export async function createSession(params: { |
| 8 | + authToken: string; |
| 9 | + config: ExecuteConfig | null; |
| 10 | +}) { |
| 11 | + const res = await fetchWithAuthToken({ |
| 12 | + method: "POST", |
| 13 | + endpoint: `${NEXT_PUBLIC_NEBULA_URL}/session`, |
| 14 | + body: { |
| 15 | + can_execute: !!params.config, |
| 16 | + config: params.config, |
| 17 | + }, |
| 18 | + authToken: params.authToken, |
| 19 | + }); |
| 20 | + |
| 21 | + if (!res.ok) { |
| 22 | + throw new Error("Failed to create session"); |
| 23 | + } |
| 24 | + const data = await res.json(); |
| 25 | + |
| 26 | + // TODO - need better type |
| 27 | + return data.result as { |
| 28 | + id: string; |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +export async function updateSession(params: { |
| 33 | + authToken: string; |
| 34 | + config: ExecuteConfig | null; |
| 35 | + sessionId: string; |
| 36 | +}) { |
| 37 | + const res = await fetchWithAuthToken({ |
| 38 | + method: "PUT", |
| 39 | + endpoint: `${NEXT_PUBLIC_NEBULA_URL}/session/${params.sessionId}`, |
| 40 | + body: { |
| 41 | + can_execute: !!params.config, |
| 42 | + config: params.config, |
| 43 | + }, |
| 44 | + authToken: params.authToken, |
| 45 | + }); |
| 46 | + |
| 47 | + if (!res.ok) { |
| 48 | + throw new Error("Failed to update session"); |
| 49 | + } |
| 50 | + const data = await res.json(); |
| 51 | + |
| 52 | + // TODO - need proper types |
| 53 | + return data.result as |
| 54 | + | { |
| 55 | + session_id: string | undefined; |
| 56 | + } |
| 57 | + | undefined; |
| 58 | +} |
| 59 | + |
| 60 | +export async function deleteSession(params: { |
| 61 | + authToken: string; |
| 62 | + sessionId: string; |
| 63 | +}) { |
| 64 | + const res = await fetchWithAuthToken({ |
| 65 | + method: "DELETE", |
| 66 | + endpoint: `${NEXT_PUBLIC_NEBULA_URL}/session/${params.sessionId}`, |
| 67 | + authToken: params.authToken, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!res.ok) { |
| 71 | + throw new Error("Failed to update session"); |
| 72 | + } |
| 73 | + const data = await res.json(); |
| 74 | + |
| 75 | + return data.result as |
| 76 | + | { |
| 77 | + session_id: string | undefined; |
| 78 | + } |
| 79 | + | undefined; |
| 80 | +} |
| 81 | + |
| 82 | +export async function getSessions(params: { |
| 83 | + authToken: string; |
| 84 | +}) { |
| 85 | + const res = await fetchWithAuthToken({ |
| 86 | + method: "GET", |
| 87 | + endpoint: `${NEXT_PUBLIC_NEBULA_URL}/session/list`, |
| 88 | + authToken: params.authToken, |
| 89 | + }); |
| 90 | + |
| 91 | + if (!res.ok) { |
| 92 | + throw new Error("Failed to update session"); |
| 93 | + } |
| 94 | + const data = await res.json(); |
| 95 | + |
| 96 | + return data.result as SessionInfo[]; |
| 97 | +} |
| 98 | + |
| 99 | +export async function getSessionById(params: { |
| 100 | + authToken: string; |
| 101 | + sessionId: string; |
| 102 | +}) { |
| 103 | + const res = await fetchWithAuthToken({ |
| 104 | + method: "GET", |
| 105 | + endpoint: `${NEXT_PUBLIC_NEBULA_URL}/session/${params.sessionId}`, |
| 106 | + authToken: params.authToken, |
| 107 | + }); |
| 108 | + |
| 109 | + if (!res.ok) { |
| 110 | + throw new Error("Failed to update session"); |
| 111 | + } |
| 112 | + const data = await res.json(); |
| 113 | + |
| 114 | + return data.result as SessionInfo; |
| 115 | +} |
0 commit comments