-
Notifications
You must be signed in to change notification settings - Fork 2k
/
listTemplate.ts
46 lines (42 loc) · 1.62 KB
/
listTemplate.ts
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
import { jsonRes } from '@/services/backend/response';
import { ApiResp } from '@/services/kubernet';
import { TemplateType } from '@/types/app';
import { parseGithubUrl } from '@/utils/tools';
import fs from 'fs';
import type { NextApiRequest, NextApiResponse } from 'next';
import path from 'path';
export function replaceRawWithCDN(url: string, cdnUrl: string) {
let parsedUrl = parseGithubUrl(url);
if (!parsedUrl || !cdnUrl) return url;
if (parsedUrl.hostname === 'raw.githubusercontent.com') {
const newUrl = `https://${cdnUrl}/gh/${parsedUrl.organization}/${parsedUrl.repository}@${parsedUrl.branch}/${parsedUrl.remainingPath}`;
return newUrl;
}
return url;
}
export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
const originalPath = process.cwd();
const jsonPath = path.resolve(originalPath, 'fast_deploy_template.json');
const cdnUrl = process.env.CDN_URL;
try {
if (fs.existsSync(jsonPath)) {
const jsonData = fs.readFileSync(jsonPath, 'utf8');
const _templates: TemplateType[] = JSON.parse(jsonData);
const templates = _templates
.filter((item) => item?.spec?.draft !== true)
.map((item) => {
if (!!cdnUrl) {
item.spec.readme = replaceRawWithCDN(item.spec.readme, cdnUrl);
item.spec.icon = replaceRawWithCDN(item.spec.icon, cdnUrl);
}
return item;
});
return jsonRes(res, { data: templates, code: 200 });
} else {
return jsonRes(res, { data: [], code: 200 });
}
} catch (error) {
console.log(error);
jsonRes(res, { code: 500, data: 'error' });
}
}