Skip to content
File

Blob: src/client/lib/live-api.ts

typescript178 lines
1import {
2 CreateWebhookRequest,
3 CreateProjectRequest,
4 UpdateProjectRequest,
5 GetMeResponse,
6 PublicAppConfigResponse,
7 GetProjectRunsResponse,
8 GetProjectsResponse,
9 GetProjectWebhooksResponse,
10 LogStreamTicketResponse,
11 ProjectDetail,
12 ProjectResponse,
13 RotateWebhookSecretResponse,
14 RunDetail,
15 TriggerRunAcceptedResponse,
16 TriggerRunRequest,
17 UpdateWebhookRequest,
18 UpsertWebhookResponse,
19} from "@/contracts";
20import type { ApiClient } from "@/client/lib/api-contract";
21import { request } from "@/client/lib/live-api-request";
22 
23export const createLiveApiClient = (): ApiClient => ({
24 getAppConfig() {
25 return request({
26 path: "/api/public/app-config",
27 method: "GET",
28 decode: (value) => PublicAppConfigResponse.assertDecode(value),
29 });
30 },
31 
32 async logout() {
33 await request({
34 path: "/api/public/auth/logout",
35 method: "POST",
36 });
37 },
38 
39 getMe() {
40 return request({
41 path: "/api/private/me",
42 method: "GET",
43 decode: (value) => GetMeResponse.assertDecode(value),
44 });
45 },
46 
47 getProjects() {
48 return request({
49 path: "/api/private/projects",
50 method: "GET",
51 decode: (value) => GetProjectsResponse.assertDecode(value),
52 });
53 },
54 
55 createProject(payload) {
56 const body = CreateProjectRequest.assertDecode(payload);
57 
58 return request({
59 path: "/api/private/projects",
60 method: "POST",
61 body,
62 decode: (value) => ProjectResponse.assertDecode(value),
63 });
64 },
65 
66 updateProject(projectId, payload) {
67 const body = UpdateProjectRequest.assertDecode(payload);
68 
69 return request({
70 path: `/api/private/projects/${encodeURIComponent(projectId)}`,
71 method: "PATCH",
72 body,
73 decode: (value) => ProjectResponse.assertDecode(value),
74 });
75 },
76 
77 getProjectDetail(projectId) {
78 return request({
79 path: `/api/private/projects/${encodeURIComponent(projectId)}`,
80 method: "GET",
81 decode: (value) => ProjectDetail.assertDecode(value),
82 });
83 },
84 
85 getProjectRuns(projectId, query) {
86 const params = new URLSearchParams();
87 if (query?.limit !== undefined) params.set("limit", String(query.limit));
88 if (query?.cursor) params.set("cursor", query.cursor);
89 const qs = params.toString();
90 
91 return request({
92 path: `/api/private/projects/${encodeURIComponent(projectId)}/runs${qs ? `?${qs}` : ""}`,
93 method: "GET",
94 decode: (value) => GetProjectRunsResponse.assertDecode(value),
95 });
96 },
97 
98 triggerRun(projectId, payload) {
99 const body = TriggerRunRequest.assertDecode(payload ?? {});
100 
101 return request({
102 path: `/api/private/projects/${encodeURIComponent(projectId)}/runs`,
103 method: "POST",
104 body,
105 decode: (value) => TriggerRunAcceptedResponse.assertDecode(value),
106 });
107 },
108 
109 getRunDetail(runId) {
110 return request({
111 path: `/api/private/runs/${encodeURIComponent(runId)}`,
112 method: "GET",
113 decode: (value) => RunDetail.assertDecode(value),
114 });
115 },
116 
117 cancelRun(runId) {
118 return request({
119 path: `/api/private/runs/${encodeURIComponent(runId)}/cancel`,
120 method: "POST",
121 decode: (value) => RunDetail.assertDecode(value),
122 });
123 },
124 
125 getLogStreamTicket(runId) {
126 return request({
127 path: `/api/private/runs/${encodeURIComponent(runId)}/log-ticket`,
128 method: "POST",
129 decode: (value) => LogStreamTicketResponse.assertDecode(value),
130 });
131 },
132 
133 getProjectWebhooks(projectId) {
134 return request({
135 path: `/api/private/projects/${encodeURIComponent(projectId)}/webhooks`,
136 method: "GET",
137 decode: (value) => GetProjectWebhooksResponse.assertDecode(value),
138 });
139 },
140 
141 createWebhook(projectId, provider, payload) {
142 const body = CreateWebhookRequest.assertDecode(payload);
143 
144 return request({
145 path: `/api/private/projects/${encodeURIComponent(projectId)}/webhooks/${encodeURIComponent(provider)}`,
146 method: "PUT",
147 body,
148 decode: (value) => UpsertWebhookResponse.assertDecode(value),
149 });
150 },
151 
152 updateWebhook(projectId, provider, payload) {
153 const body = UpdateWebhookRequest.assertDecode(payload);
154 
155 return request({
156 path: `/api/private/projects/${encodeURIComponent(projectId)}/webhooks/${encodeURIComponent(provider)}`,
157 method: "PUT",
158 body,
159 decode: (value) => UpsertWebhookResponse.assertDecode(value),
160 });
161 },
162 
163 rotateWebhookSecret(projectId, provider) {
164 return request({
165 path: `/api/private/projects/${encodeURIComponent(projectId)}/webhooks/${encodeURIComponent(provider)}/rotate-secret`,
166 method: "POST",
167 decode: (value) => RotateWebhookSecretResponse.assertDecode(value),
168 });
169 },
170 
171 async deleteWebhook(projectId, provider) {
172 await request({
173 path: `/api/private/projects/${encodeURIComponent(projectId)}/webhooks/${encodeURIComponent(provider)}`,
174 method: "DELETE",
175 });
176 },
177});