Skip to content

Commit be6d4cb

Browse files
committed
refactor: extract appcenter mod.ts
1 parent 1853c67 commit be6d4cb

File tree

3 files changed

+77
-75
lines changed

3 files changed

+77
-75
lines changed

main.ts

+2-75
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,5 @@
11
import { serve } from 'https://deno.land/std@0.137.0/http/server.ts';
22

3-
export interface ReleaseInfo {
4-
id: string;
5-
version: string;
6-
short_version: string;
7-
}
3+
import { appcenter } from './mod.ts';
84

9-
export interface DownloadInfo {
10-
download_url: string;
11-
}
12-
13-
const REDIRECT = 302;
14-
const NOT_FOUND = 404;
15-
16-
async function handler(req: Request): Promise<Response> {
17-
let url = new URL(req.url).pathname;
18-
19-
if (!url || url === '/') {
20-
url = '/index.html';
21-
}
22-
23-
if (/\.[a-z]+[a-z\d]*$/.test(url)) {
24-
return new Response(await Deno.readFile(`./public${url}`));
25-
}
26-
27-
const matched = /\/([\w-]+)\/([\w-]+)\/([.\w]+)/.exec(url);
28-
29-
if (!matched) {
30-
return new Response('Not Found', {
31-
status: NOT_FOUND,
32-
});
33-
}
34-
35-
const [, owner, app, version] = matched;
36-
37-
const releasesUrl =
38-
`https://install.appcenter.ms/api/v0.1/apps/${owner}/${app}/distribution_groups/public/public_releases`;
39-
40-
console.log(`Fetching ${releasesUrl}`);
41-
42-
const releases: ReleaseInfo[] = await fetch(releasesUrl).then((res) =>
43-
res.ok ? res.json() : []
44-
);
45-
46-
const found = releases.find(
47-
(it) => it.version === version || it.short_version === version,
48-
);
49-
50-
if (!found) {
51-
return new Response(
52-
`No matched version ${version} found for ${owner}/${app}`,
53-
{
54-
status: NOT_FOUND,
55-
},
56-
);
57-
}
58-
59-
const releaseUrl =
60-
`https://install.appcenter.ms/api/v0.1/apps/${owner}/${app}/distribution_groups/public/releases/${found.id}`;
61-
62-
console.log(`Fetching ${releaseUrl}`);
63-
64-
const { download_url: downloadUrl }: DownloadInfo = await fetch(
65-
releaseUrl,
66-
).then((res) => res.json());
67-
68-
console.log(`Redirect to ${downloadUrl}`);
69-
70-
return new Response(null, {
71-
status: REDIRECT,
72-
headers: {
73-
Location: downloadUrl,
74-
},
75-
});
76-
}
77-
78-
serve(handler);
5+
serve(appcenter);

mod.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
export interface ReleaseInfo {
2+
id: string;
3+
version: string;
4+
short_version: string;
5+
}
6+
7+
export interface DownloadInfo {
8+
download_url: string;
9+
}
10+
11+
export const REDIRECT = 302;
12+
export const NOT_FOUND = 404;
13+
14+
export async function appcenter(req: Request): Promise<Response> {
15+
let url = new URL(req.url).pathname;
16+
17+
if (!url || url === '/') {
18+
url = '/index.html';
19+
}
20+
21+
if (/\.[a-z]+[a-z\d]*$/.test(url)) {
22+
return new Response(await Deno.readFile(`./public${url}`));
23+
}
24+
25+
const matched = /\/([\w-]+)\/([\w-]+)\/([.\w]+)/.exec(url);
26+
27+
if (!matched) {
28+
return new Response('Not Found', {
29+
status: NOT_FOUND,
30+
});
31+
}
32+
33+
const [, owner, app, version] = matched;
34+
35+
const releasesUrl =
36+
`https://install.appcenter.ms/api/v0.1/apps/${owner}/${app}/distribution_groups/public/public_releases`;
37+
38+
console.log(`Fetching ${releasesUrl}`);
39+
40+
const releases: ReleaseInfo[] = await fetch(releasesUrl).then((res) =>
41+
res.ok ? res.json() : []
42+
);
43+
44+
const found = releases.find(
45+
(it) => it.version === version || it.short_version === version,
46+
);
47+
48+
if (!found) {
49+
return new Response(
50+
`No matched version ${version} found for ${owner}/${app}`,
51+
{
52+
status: NOT_FOUND,
53+
},
54+
);
55+
}
56+
57+
const releaseUrl =
58+
`https://install.appcenter.ms/api/v0.1/apps/${owner}/${app}/distribution_groups/public/releases/${found.id}`;
59+
60+
console.log(`Fetching ${releaseUrl}`);
61+
62+
const { download_url: downloadUrl }: DownloadInfo = await fetch(
63+
releaseUrl,
64+
).then((res) => res.json());
65+
66+
console.log(`Redirect to ${downloadUrl}`);
67+
68+
return new Response(null, {
69+
status: REDIRECT,
70+
headers: {
71+
Location: downloadUrl,
72+
},
73+
});
74+
}

version.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const VERSION = '0.0.0';

0 commit comments

Comments
 (0)