-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
90 lines (67 loc) · 1.87 KB
/
mod.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
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
81
82
83
84
85
86
87
88
89
90
export interface ReleaseInfo {
id: string
version: string
short_version: string
}
export interface DownloadInfo {
download_url: string
}
export interface ErrorResponse {
code: number | string
message: string
}
const NOT_FOUND = 404
export async function appcenter(req: Request): Promise<Response> {
let url = new URL(req.url).pathname
if (!url || url === '/') {
url = '/index.html'
}
if (/\.[a-z]+[a-z\d]*$/.test(url)) {
return new Response(await Deno.readFile(`./public${url}`))
}
const matched = /\/([\w-]+)\/([\w-]+)\/([.\w]+)/.exec(url)
if (!matched) {
return Response.json({
code: NOT_FOUND,
message: 'Not Found',
}, {
status: NOT_FOUND,
})
}
const [, owner, app, version] = matched
const releasesUrl =
`https://install.appcenter.ms/api/v0.1/apps/${owner}/${app}/distribution_groups/public/public_releases`
console.log(`Fetching ${releasesUrl}`)
const releasesRes = await fetch(releasesUrl)
if (!releasesRes.ok) {
return releasesRes
}
const releases = await releasesRes.json() as ReleaseInfo[]
const found = releases.find(
(it) => it.version === version || it.short_version === version,
)
if (!found) {
return Response.json(
{
code: NOT_FOUND,
message: `No matched version ${version} found for ${owner}/${app}`,
} satisfies ErrorResponse,
{
status: NOT_FOUND,
},
)
}
const releaseUrl =
`https://install.appcenter.ms/api/v0.1/apps/${owner}/${app}/distribution_groups/public/releases/${found.id}`
console.log(`Fetching ${releaseUrl}`)
const downloadUrlRes = await fetch(
releaseUrl,
)
if (!downloadUrlRes.ok) {
return downloadUrlRes
}
const { download_url: downloadUrl } = await downloadUrlRes
.json() as DownloadInfo
console.log(`Redirect to ${downloadUrl}`)
return Response.redirect(downloadUrl)
}