-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
68 lines (50 loc) · 1.53 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
export interface ReleaseInfo {
id: string
version: string
short_version: string
}
export interface DownloadInfo {
download_url: 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 new Response('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 releases: ReleaseInfo[] = await fetch(releasesUrl).then((res) =>
res.ok ? res.json() : []
)
const found = releases.find(
(it) => it.version === version || it.short_version === version,
)
if (!found) {
return new Response(
`No matched version ${version} found for ${owner}/${app}`,
{
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 { download_url: downloadUrl }: DownloadInfo = await fetch(
releaseUrl,
).then((res) => res.json())
console.log(`Redirect to ${downloadUrl}`)
return Response.redirect(downloadUrl)
}