Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/app/src/flow-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Env, OAuthProps } from "./types";
import type { JobInput, ManualInvocationInput } from "./flow-schemas";
import { nextOccurrence, validateScheduleConfig } from "./schedule";

export class ServiceError extends Error {
constructor(public status: number, public code: string, message: string) { super(message); }
Expand Down Expand Up @@ -57,4 +58,14 @@ export class ApiService {
setJobEnabled(jobId: string, enabled: boolean) { return this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}/${enabled ? "enable" : "disable"}`, { method: "POST" }); }
invokeJob(jobId: string, input: ManualInvocationInput) { return this.call("runs:write", `/v1/jobs/${encodeURIComponent(jobId)}/invocations`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) }); }
listExecutionTargets() { return this.call("flows:read", "/v1/execution-targets"); }
listJobTriggerAvailability() { return this.call("flows:read", "/v1/job-trigger-availability"); }
async previewSchedule(input: unknown) {
await this.authorize("flows:read");
try {
const config = validateScheduleConfig(input);
return { nextRunAt: nextOccurrence(config, new Date()).toISOString() };
} catch (error) {
throw new ServiceError(400, "invalid_request", error instanceof Error ? error.message : "Invalid schedule");
}
}
}
24 changes: 0 additions & 24 deletions packages/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { GitHubInstallationRegistry, GitHubInstallationRegistryV2 } from "./gith
import { createAppJwt, githubHeaders, readSetupState, signSetupState } from "./github";
import { apiKeysSettingsPage, jobDetailPage, jobPage, jobsPage, jobRunPage, landingPage, settingsPage } from "./ui";
import type { Env } from "./types";
import { nextOccurrence, validateScheduleConfig } from "./schedule";
import { ACCESS_SCOPES, accessTokenDigest, issueAccessToken } from "./access-tokens";

const app = new Hono<{ Bindings: Env; Variables: { cspNonce: string } }>();
Expand Down Expand Up @@ -142,29 +141,6 @@ app.post("/auth/logout", (c) => {
return c.redirect("/");
});

const jobApi = async (c: any, path: string, init?: RequestInit) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch(`https://tenant/v1${path}`, init);
};
app.get("/api/jobs", (c) => jobApi(c, "/jobs"));
app.post("/api/jobs", async (c) => jobApi(c, "/jobs", { method: "POST", headers: { "Content-Type": "application/json" }, body: await c.req.text() }));
app.get("/api/jobs/:id", (c) => jobApi(c, `/jobs/${encodeURIComponent(c.req.param("id"))}`));
app.put("/api/jobs/:id", async (c) => jobApi(c, `/jobs/${encodeURIComponent(c.req.param("id"))}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: await c.req.text() }));
app.delete("/api/jobs/:id", (c) => jobApi(c, `/jobs/${encodeURIComponent(c.req.param("id"))}`, { method: "DELETE" }));
app.post("/api/jobs/:id/enable", (c) => jobApi(c, `/jobs/${encodeURIComponent(c.req.param("id"))}/enable`, { method: "POST" }));
app.post("/api/jobs/:id/disable", (c) => jobApi(c, `/jobs/${encodeURIComponent(c.req.param("id"))}/disable`, { method: "POST" }));
app.post("/api/jobs/:id/invocations", async (c) => jobApi(c, `/jobs/${encodeURIComponent(c.req.param("id"))}/invocations`, { method: "POST", headers: { "Content-Type": "application/json" }, body: await c.req.text() }));
app.post("/api/job-handlers/test", async (c) => jobApi(c, "/job-handlers/test", { method: "POST", headers: { "Content-Type": "application/json" }, body: await c.req.text() }));
app.get("/api/job-runs", (c) => jobApi(c, `/runs?${new URL(c.req.url).searchParams.toString()}`));
app.get("/api/job-runs/:id", (c) => jobApi(c, `/runs/${encodeURIComponent(c.req.param("id"))}`));
app.post("/api/job-runs/:id/stop", (c) => jobApi(c, `/runs/${encodeURIComponent(c.req.param("id"))}/stop`, { method: "POST" }));
app.get("/api/execution-targets", (c) => jobApi(c, "/execution-targets"));
app.get("/api/job-trigger-availability", (c) => jobApi(c, "/job-trigger-availability"));
app.post("/api/schedules/preview", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
try { const config = validateScheduleConfig(await c.req.json()); return c.json({ nextRunAt: nextOccurrence(config, new Date()).toISOString() }); }
catch (error) { return c.json({ error: error instanceof Error ? error.message : "Invalid schedule" }, 400); }
});
app.get("/api/connections/status", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/connections/status");
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/protected-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export async function protectedApiFetch(request: Request, env: Env, auth: OAuthP
if (!path.startsWith("/api/v1")) return Response.json({ error: { code: "not_found", message: "Not found" } }, { status: 404 });
if (request.method === "GET" && path === "/api/v1/exe-connections") return Response.json(await service.listExeConnections());
if (request.method === "GET" && path === "/api/v1/execution-targets") return Response.json(await service.listExecutionTargets());
if (request.method === "GET" && path === "/api/v1/job-trigger-availability") return Response.json(await service.listJobTriggerAvailability());
if (request.method === "POST" && path === "/api/v1/schedules/preview") return Response.json(await service.previewSchedule(await request.json()));
if (request.method === "GET" && path === "/api/v1/integrations/cloudflare-tail") return Response.json(await service.listTailIntegrations());
if (request.method === "GET" && path === "/api/v1/jobs") return Response.json(await service.listJobs());
if (request.method === "POST" && path === "/api/v1/jobs") return Response.json(await service.createJob(jobInputSchema.parse(await request.json())), { status: 201 });
Expand Down
Loading