Skip to content

Commit 9792c72

Browse files
committed
Cleanup
1 parent 3338836 commit 9792c72

File tree

8 files changed

+48
-15
lines changed

8 files changed

+48
-15
lines changed

.env

-1
This file was deleted.

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ tsconfig.tsbuildinfo
1818
*.njsproj
1919
*.sln
2020
*.sw?
21+
22+
# .env files
23+
.env
24+
.env.*

src/rest/getFirefoxScreenshot.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { FirefoxService } from "../services/firefox-service";
2+
import { RouteHandler } from "../utils/rest-router";
3+
4+
export const getFirefoxScreenshot =
5+
(firefox: FirefoxService): RouteHandler<{ id: string; index: string }> =>
6+
async (params) => {
7+
const addon = await firefox.getAddon(params.id);
8+
const index = Number(params.index);
9+
const screenshot = addon?.screenshots.find(
10+
(screenshot) => screenshot.index == index,
11+
);
12+
13+
if (screenshot == null) return new Response(null, { status: 404 });
14+
return Response.redirect(screenshot.rawUrl);
15+
};

src/server.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { createChromeService } from "./services/chrome-service";
77
import { createFirefoxService } from "./services/firefox-service";
88
import { createRestRouter } from "./utils/rest-router";
99
import { getChromeScreenshot } from "./rest/getChromeScreenshot";
10+
import { getFirefoxScreenshot } from "./rest/getFirefoxScreenshot";
11+
import { SERVER_ORIGIN } from "./utils/urls";
1012

1113
const playgroundHtml = playgroundHtmlTemplate.replace(
1214
"{{VERSION}}",
@@ -24,10 +26,12 @@ export function createServer(config?: ServerConfig) {
2426
firefox,
2527
});
2628

27-
const restRouter = createRestRouter().get(
28-
"/api/rest/chrome/:id/screenshots/:index",
29-
getChromeScreenshot(chrome),
30-
);
29+
const restRouter = createRestRouter()
30+
.get("/api/rest/chrome/:id/screenshots/:index", getChromeScreenshot(chrome))
31+
.get(
32+
"/api/rest/firefox/:id/screenshots/:index",
33+
getFirefoxScreenshot(firefox),
34+
);
3135

3236
const httpServer = Bun.serve({
3337
port,
@@ -39,7 +43,7 @@ export function createServer(config?: ServerConfig) {
3943
return createResponse(undefined, { status: 204 });
4044
}
4145

42-
const url = new URL(req.url, process.env.SERVER_ORIGIN);
46+
const url = new URL(req.url, SERVER_ORIGIN);
4347

4448
// REST
4549

src/services/chrome-service.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ export function createChromeService() {
1616
});
1717

1818
return {
19-
getExtension: (id: string) => loader.load(id),
20-
getExtensions: async (ids: string[]) => {
19+
getExtension: (id: string): Promise<Gql.ChromeExtension | undefined> =>
20+
loader.load(id),
21+
getExtensions: async (
22+
ids: string[],
23+
): Promise<Array<Gql.ChromeExtension | undefined>> => {
2124
const result = await loader.loadMany(ids);
2225
return result.map((item, index) => {
2326
if (item instanceof Error) {

src/services/firefox-service.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ export function createFirefoxService() {
1111
>(HOUR_MS, (ids) => Promise.all(ids.map((id) => firefox.getAddon(id))));
1212

1313
return {
14-
getAddon: (id: string | number) => loader.load(id),
15-
getAddons: async (ids: Array<string | number>) => {
14+
getAddon: (id: string | number): Promise<Gql.FirefoxAddon | undefined> =>
15+
loader.load(id),
16+
getAddons: async (
17+
ids: Array<string | number>,
18+
): Promise<Array<Gql.FirefoxAddon | undefined>> => {
1619
const result = await loader.loadMany(ids);
1720
return result.map((item) => {
1821
if (item == null) return undefined;
@@ -25,3 +28,5 @@ export function createFirefoxService() {
2528
},
2629
};
2730
}
31+
32+
export type FirefoxService = ReturnType<typeof createFirefoxService>;

src/utils/rest-router.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ export interface Route {
1414
export function createRestRouter() {
1515
const r = radix3.createRouter<Route>();
1616
const router = {
17-
get(path: string, handler: RouteHandler) {
17+
get(path: string, handler: RouteHandler<any>) {
1818
r.insert(path, { method: "GET", handler });
1919
return router;
2020
},
21-
post(path: string, handler: RouteHandler) {
21+
post(path: string, handler: RouteHandler<any>) {
2222
r.insert(path, { method: "POST", handler });
2323
return router;
2424
},
25-
any(path: string, handler: RouteHandler) {
25+
any(path: string, handler: RouteHandler<any>) {
2626
r.insert(path, { method: "ANY", handler });
2727
return router;
2828
},
29-
on(method: string, path: string, handler: RouteHandler) {
29+
on(method: string, path: string, handler: RouteHandler<any>) {
3030
r.insert(path, { method, handler });
3131
return router;
3232
},

src/utils/urls.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
export const SERVER_ORIGIN =
2+
process.env.SERVER_ORIGIN ?? "http://localhost:3000";
3+
14
export function buildScreenshotUrl(
25
type: "chrome" | "firefox",
36
id: string,
47
index: number,
58
) {
6-
return `${process.env.SERVER_ORIGIN}/api/rest/${type}/${id}/screenshots/${index}`;
9+
return `${SERVER_ORIGIN}/api/rest/${type}/${id}/screenshots/${index}`;
710
}

0 commit comments

Comments
 (0)