100 lines
2.5 KiB
JavaScript
100 lines
2.5 KiB
JavaScript
// jobs_api.js — client for the SQLite-backed recurring jobs sidecar.
|
|
|
|
const params = new URLSearchParams(location.search);
|
|
const QUERY_API = params.get("jobsApi");
|
|
const DEFAULT_PORT = "5581";
|
|
const API_BASE = QUERY_API
|
|
? QUERY_API.replace(/\/$/, "")
|
|
: `${location.protocol}//${location.hostname || "127.0.0.1"}:${DEFAULT_PORT}`;
|
|
|
|
export function jobsApiURL() {
|
|
return API_BASE;
|
|
}
|
|
|
|
export async function listRecurringJobs() {
|
|
const res = await request("/api/jobs");
|
|
return (res && res.jobs) || [];
|
|
}
|
|
|
|
export async function createRecurringJob(job) {
|
|
const res = await request("/api/jobs", {
|
|
method: "POST",
|
|
body: job,
|
|
});
|
|
return res && res.job;
|
|
}
|
|
|
|
export async function runRecurringJobNow(id) {
|
|
const res = await request(`/api/jobs/${encodeURIComponent(id)}/run`, {
|
|
method: "POST",
|
|
});
|
|
return res && res.job;
|
|
}
|
|
|
|
export async function stopRecurringJob(id) {
|
|
const res = await request(`/api/jobs/${encodeURIComponent(id)}/stop`, {
|
|
method: "POST",
|
|
});
|
|
return res && res.job;
|
|
}
|
|
|
|
export async function deleteRecurringJob(id) {
|
|
return request(`/api/jobs/${encodeURIComponent(id)}`, {
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
|
|
export async function listOneTimeJobs() {
|
|
const res = await request("/api/one-time-jobs");
|
|
return (res && res.jobs) || [];
|
|
}
|
|
|
|
export async function createOneTimeJob(job) {
|
|
const res = await request("/api/one-time-jobs", {
|
|
method: "POST",
|
|
body: job,
|
|
});
|
|
return res && res.job;
|
|
}
|
|
|
|
export async function runOneTimeJobNow(id) {
|
|
const res = await request(`/api/one-time-jobs/${encodeURIComponent(id)}/run`, {
|
|
method: "POST",
|
|
});
|
|
return res && res.job;
|
|
}
|
|
|
|
export async function stopOneTimeJob(id) {
|
|
const res = await request(`/api/one-time-jobs/${encodeURIComponent(id)}/stop`, {
|
|
method: "POST",
|
|
});
|
|
return res && res.job;
|
|
}
|
|
|
|
export async function deleteOneTimeJob(id) {
|
|
return request(`/api/one-time-jobs/${encodeURIComponent(id)}`, {
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
|
|
async function request(path, { method = "GET", body } = {}) {
|
|
const headers = { "Content-Type": "application/json" };
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
|
method,
|
|
headers,
|
|
body: body == null ? undefined : JSON.stringify(body),
|
|
});
|
|
let data = {};
|
|
const ct = res.headers.get("Content-Type") || "";
|
|
if (ct.includes("application/json")) {
|
|
data = await res.json();
|
|
} else {
|
|
const text = await res.text();
|
|
data = text ? { error: text } : {};
|
|
}
|
|
if (!res.ok) {
|
|
throw new Error(data.error || `HTTP ${res.status}`);
|
|
}
|
|
return data;
|
|
}
|