Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 committed Oct 23, 2024
1 parent 3338836 commit 9792c72
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
1 change: 0 additions & 1 deletion .env

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ tsconfig.tsbuildinfo
*.njsproj
*.sln
*.sw?

# .env files
.env
.env.*
15 changes: 15 additions & 0 deletions src/rest/getFirefoxScreenshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { FirefoxService } from "../services/firefox-service";
import { RouteHandler } from "../utils/rest-router";

export const getFirefoxScreenshot =
(firefox: FirefoxService): RouteHandler<{ id: string; index: string }> =>
async (params) => {
const addon = await firefox.getAddon(params.id);
const index = Number(params.index);
const screenshot = addon?.screenshots.find(
(screenshot) => screenshot.index == index,
);

if (screenshot == null) return new Response(null, { status: 404 });
return Response.redirect(screenshot.rawUrl);
};
14 changes: 9 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { createChromeService } from "./services/chrome-service";
import { createFirefoxService } from "./services/firefox-service";
import { createRestRouter } from "./utils/rest-router";
import { getChromeScreenshot } from "./rest/getChromeScreenshot";
import { getFirefoxScreenshot } from "./rest/getFirefoxScreenshot";
import { SERVER_ORIGIN } from "./utils/urls";

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

const restRouter = createRestRouter().get(
"/api/rest/chrome/:id/screenshots/:index",
getChromeScreenshot(chrome),
);
const restRouter = createRestRouter()
.get("/api/rest/chrome/:id/screenshots/:index", getChromeScreenshot(chrome))
.get(
"/api/rest/firefox/:id/screenshots/:index",
getFirefoxScreenshot(firefox),
);

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

const url = new URL(req.url, process.env.SERVER_ORIGIN);
const url = new URL(req.url, SERVER_ORIGIN);

// REST

Expand Down
7 changes: 5 additions & 2 deletions src/services/chrome-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export function createChromeService() {
});

return {
getExtension: (id: string) => loader.load(id),
getExtensions: async (ids: string[]) => {
getExtension: (id: string): Promise<Gql.ChromeExtension | undefined> =>
loader.load(id),
getExtensions: async (
ids: string[],
): Promise<Array<Gql.ChromeExtension | undefined>> => {
const result = await loader.loadMany(ids);
return result.map((item, index) => {
if (item instanceof Error) {
Expand Down
9 changes: 7 additions & 2 deletions src/services/firefox-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export function createFirefoxService() {
>(HOUR_MS, (ids) => Promise.all(ids.map((id) => firefox.getAddon(id))));

return {
getAddon: (id: string | number) => loader.load(id),
getAddons: async (ids: Array<string | number>) => {
getAddon: (id: string | number): Promise<Gql.FirefoxAddon | undefined> =>
loader.load(id),
getAddons: async (
ids: Array<string | number>,
): Promise<Array<Gql.FirefoxAddon | undefined>> => {
const result = await loader.loadMany(ids);
return result.map((item) => {
if (item == null) return undefined;
Expand All @@ -25,3 +28,5 @@ export function createFirefoxService() {
},
};
}

export type FirefoxService = ReturnType<typeof createFirefoxService>;
8 changes: 4 additions & 4 deletions src/utils/rest-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ export interface Route {
export function createRestRouter() {
const r = radix3.createRouter<Route>();
const router = {
get(path: string, handler: RouteHandler) {
get(path: string, handler: RouteHandler<any>) {
r.insert(path, { method: "GET", handler });
return router;
},
post(path: string, handler: RouteHandler) {
post(path: string, handler: RouteHandler<any>) {
r.insert(path, { method: "POST", handler });
return router;
},
any(path: string, handler: RouteHandler) {
any(path: string, handler: RouteHandler<any>) {
r.insert(path, { method: "ANY", handler });
return router;
},
on(method: string, path: string, handler: RouteHandler) {
on(method: string, path: string, handler: RouteHandler<any>) {
r.insert(path, { method, handler });
return router;
},
Expand Down
5 changes: 4 additions & 1 deletion src/utils/urls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export const SERVER_ORIGIN =
process.env.SERVER_ORIGIN ?? "http://localhost:3000";

export function buildScreenshotUrl(
type: "chrome" | "firefox",
id: string,
index: number,
) {
return `${process.env.SERVER_ORIGIN}/api/rest/${type}/${id}/screenshots/${index}`;
return `${SERVER_ORIGIN}/api/rest/${type}/${id}/screenshots/${index}`;
}

0 comments on commit 9792c72

Please sign in to comment.