-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathroute.ts
More file actions
66 lines (58 loc) · 1.85 KB
/
Copy pathroute.ts
File metadata and controls
66 lines (58 loc) · 1.85 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
import { NextRequest } from 'next/server';
import { KBDevboxTypeV2 } from '@/types/k8s';
import { devboxDB } from '@/services/db/init';
import { adaptDevboxListItemV2 } from '@/utils/adapt';
import { authSession } from '@/services/backend/auth';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
export const dynamic = 'force-dynamic';
export async function GET(req: NextRequest) {
try {
const headerList = req.headers;
const { k8sCustomObjects, namespace } = await getK8s({
kubeconfig: await authSession(headerList)
});
const devboxResponse = await k8sCustomObjects.listNamespacedCustomObject(
'devbox.sealos.io',
'v1alpha2',
namespace,
'devboxes'
);
const devboxBody = devboxResponse.body as { items: KBDevboxTypeV2[] };
const uidList = devboxBody.items.map((item) => item.spec.templateID);
const templateResultList = await devboxDB.template.findMany({
where: {
uid: {
in: uidList
}
},
select: {
uid: true,
templateRepository: {
select: {
iconId: true,
icon: true
}
},
name: true
}
});
// match template with devbox
const resp = devboxBody.items.flatMap((item) => {
const templateItem = templateResultList.find(
(templateResult) => templateResult.uid === item.spec.templateID
);
if (!templateItem) return [];
return [[item, templateItem] as [KBDevboxTypeV2, typeof templateItem]];
});
const adaptedData = resp.map(adaptDevboxListItemV2).sort((a, b) => {
return new Date(b.createTime).getTime() - new Date(a.createTime).getTime();
});
return jsonRes({ data: adaptedData });
} catch (err: any) {
return jsonRes({
code: 500,
error: err
});
}
}