-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathapi.v1.workers.ts
More file actions
80 lines (73 loc) · 2.41 KB
/
Copy pathapi.v1.workers.ts
File metadata and controls
80 lines (73 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { json, TypedResponse } from "@remix-run/server-runtime";
import {
WorkersCreateRequestBody,
WorkersCreateResponseBody,
WorkersListResponseBody,
} from "@trigger.dev/core/v3";
import {
createActionApiRoute,
createLoaderApiRoute,
} from "~/services/routeBuilders/apiBuilder.server";
import { WorkerGroupService } from "~/v3/services/worker/workerGroupService.server";
export const loader = createLoaderApiRoute(
{
corsStrategy: "all",
findResource: async () => 1, // This is a dummy function, we don't need to find a resource
},
async ({
authentication,
}): Promise<TypedResponse<WorkersListResponseBody | { error: string }>> => {
if (authentication.environment.project.engine !== "V2") {
return json({ error: "Not supported for V1 projects" }, { status: 400 });
}
const service = new WorkerGroupService();
const workers = await service.listWorkerGroups({
projectId: authentication.environment.projectId,
});
// Reuse the trigger path's resolution so isDefault matches where runs
// actually route: env default -> project default -> global default.
const defaultWorkerGroup = await service.getDefaultWorkerGroupForProject({
projectId: authentication.environment.projectId,
environmentDefaultWorkerGroupId: authentication.environment.defaultWorkerGroupId,
});
return json(
workers.map((w) => ({
type: w.type,
name: w.name,
description: w.description,
isDefault: w.id === defaultWorkerGroup?.id,
updatedAt: w.updatedAt,
}))
);
}
);
export const { action } = createActionApiRoute(
{
corsStrategy: "all",
body: WorkersCreateRequestBody,
},
async ({
authentication,
body,
}): Promise<TypedResponse<WorkersCreateResponseBody | { error: string }>> => {
if (authentication.environment.project.engine !== "V2") {
return json({ error: "Not supported" }, { status: 400 });
}
const service = new WorkerGroupService();
const { workerGroup, token } = await service.createWorkerGroup({
projectId: authentication.environment.projectId,
organizationId: authentication.environment.organizationId,
name: body.name,
description: body.description,
});
return json({
token: {
plaintext: token.plaintext,
},
workerGroup: {
name: workerGroup.name,
description: workerGroup.description,
},
});
}
);