|
| 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 getSessions(params: { |
| 61 | + authToken: string; |
| 62 | +}) { |
| 63 | + const res = await fetchWithAuthToken({ |
| 64 | + method: "GET", |
| 65 | + endpoint: `${NEXT_PUBLIC_NEBULA_URL}/session/list`, |
| 66 | + authToken: params.authToken, |
| 67 | + }); |
| 68 | + |
| 69 | + if (!res.ok) { |
| 70 | + throw new Error("Failed to update session"); |
| 71 | + } |
| 72 | + const data = await res.json(); |
| 73 | + |
| 74 | + return data.result as SessionInfo[]; |
| 75 | +} |
| 76 | + |
| 77 | +export async function getSessionById(params: { |
| 78 | + authToken: string; |
| 79 | + sessionId: string; |
| 80 | +}) { |
| 81 | + const res = await fetchWithAuthToken({ |
| 82 | + method: "GET", |
| 83 | + endpoint: `${NEXT_PUBLIC_NEBULA_URL}/session/${params.sessionId}`, |
| 84 | + authToken: params.authToken, |
| 85 | + }); |
| 86 | + |
| 87 | + if (!res.ok) { |
| 88 | + throw new Error("Failed to update session"); |
| 89 | + } |
| 90 | + const data = await res.json(); |
| 91 | + |
| 92 | + return data.result as SessionInfo; |
| 93 | +} |
0 commit comments