Skip to content

Commit e21b85a

Browse files
committed
refactor: Standardize and share store definitions
1 parent e1625e2 commit e21b85a

29 files changed

+282
-340
lines changed

src/apis/extension-store-apis.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createApp } from "@aklinker1/zeta";
2+
import { z } from "zod";
3+
import { contextPlugin } from "../plugins/context-plugin";
4+
import { NotFoundHttpError } from "@aklinker1/zeta";
5+
import { HttpStatus } from "@aklinker1/zeta";
6+
import { ExtensionStoreName } from "../enums";
7+
8+
export const extensionStoreApis = createApp()
9+
.use(contextPlugin)
10+
.get(
11+
"/api/rest/:storeName/:id/screenshots/:index",
12+
{
13+
operationId: "redirectToScreenshot",
14+
tags: ["Chrome Extensions"],
15+
description:
16+
"Redirect to a screenshot's URL from the Chrome Web Store listing",
17+
params: z.object({
18+
storeName: z.enum(ExtensionStoreName),
19+
id: z.string(),
20+
index: z.coerce.number().int().min(0),
21+
}),
22+
},
23+
async ({ params, stores, set }) => {
24+
const screenshotUrl = await stores[params.storeName].getScreenshotUrl(
25+
params.id,
26+
params.index,
27+
);
28+
if (!screenshotUrl)
29+
throw new NotFoundHttpError("Extension or screenshot not found");
30+
31+
set.status = HttpStatus.Found;
32+
set.headers["Location"] = screenshotUrl;
33+
},
34+
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const PLAYGROUND_HTML = (PLAYGROUND_HTML_TEMPLATE as any as string).replace(
1212

1313
const graphql = createGraphql();
1414

15-
export const graphqlRoutes = createApp()
15+
export const graphqlApis = createApp()
1616
.post(
1717
"/api",
1818
{

src/apis/system-apis.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { createApp } from "@aklinker1/zeta";
2+
import z from "zod";
3+
import { version } from "../version";
4+
5+
export const systemApis = createApp()
6+
.get(
7+
"/",
8+
{
9+
summary: "API Docs Redirect",
10+
tags: ["System"],
11+
description: "Redirect to the API reference when visiting the root URL.",
12+
},
13+
({ set }) => {
14+
set.status = 302;
15+
set.headers.Location = "/scalar";
16+
},
17+
)
18+
.get(
19+
"/api/health",
20+
{
21+
summary: "Health Check",
22+
tags: ["System"],
23+
description: "Used to make sure the API is up and running.",
24+
responses: z.object({
25+
status: z.literal("ok"),
26+
version: z.string(),
27+
}),
28+
},
29+
() => ({ status: "ok" as const, version }),
30+
);

src/dependencies.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import { createIocContainer } from "@aklinker1/zero-ioc";
2-
import { createChromeService } from "./utils/chrome/chrome-service";
3-
import { createFirefoxService } from "./utils/firefox/firefox-service";
4-
import { createEdgeService } from "./utils/edge/edge-service";
2+
import { createChromeWebStore } from "./services/chrome-web-store";
3+
import { createFirefoxAddonStore } from "./services/firefox-addon-store";
4+
import { createEdgeAddonStore } from "./services/edge-addon-store";
5+
import type { ExtensionStores } from "./services/extension-stores";
6+
import { ExtensionStoreName } from "./enums";
57

68
export const dependencies = createIocContainer()
7-
.register("chrome", createChromeService)
8-
.register("firefox", createFirefoxService)
9-
.register("edge", createEdgeService);
9+
.register("chromeWebStore", createChromeWebStore)
10+
.register("firefoxAddonStore", createFirefoxAddonStore)
11+
.register("edgeAddonStore", createEdgeAddonStore)
12+
.register(
13+
"stores",
14+
(deps) =>
15+
({
16+
[ExtensionStoreName.ChromeExtensions]: deps.chromeWebStore,
17+
[ExtensionStoreName.ChromeWebStore]: deps.chromeWebStore,
18+
[ExtensionStoreName.FirefoxExtensions]: deps.firefoxAddonStore,
19+
[ExtensionStoreName.FirefoxAddonStore]: deps.firefoxAddonStore,
20+
[ExtensionStoreName.EdgeExtensions]: deps.edgeAddonStore,
21+
[ExtensionStoreName.EdgeAddonStore]: deps.edgeAddonStore,
22+
}) satisfies ExtensionStores,
23+
);

src/enums.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export enum ExtensionStoreName {
2+
/** @deprecated Use {@link ChromeWebStore} instead. */
3+
ChromeExtensions = "chrome-extensions",
4+
ChromeWebStore = "chrome-web-store",
5+
/** @deprecated Use {@link FirefoxAddonStore} instead. */
6+
FirefoxExtensions = "firefox-extensions",
7+
FirefoxAddonStore = "firefox-addon-store",
8+
/** @deprecated Use {@link EdgeAddonStore} instead. */
9+
EdgeExtensions = "edge-extensions",
10+
EdgeAddonStore = "edge-addon-store",
11+
}

src/routes/rest-routes.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/server.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import consola from "consola";
22
import { createApp } from "@aklinker1/zeta";
33
import { corsPlugin } from "./plugins/cors-plugin";
4-
import { graphqlRoutes } from "./routes/grpahql-routes";
5-
import { restRoutes } from "./routes/rest-routes";
4+
import { graphqlApis } from "./apis/graphql-apis";
5+
import { extensionStoreApis } from "./apis/extension-store-apis";
66
import { zodSchemaAdapter } from "@aklinker1/zeta/adapters/zod-schema-adapter";
7-
import { version } from "../package.json";
8-
import { z } from "zod";
7+
import { version } from "./version";
98
import dedent from "dedent";
9+
import { systemApis } from "./apis/system-apis";
1010

1111
const app = createApp({
1212
schemaAdapter: zodSchemaAdapter,
@@ -49,33 +49,9 @@ const app = createApp({
4949
})
5050
.onGlobalError(({ error }) => void consola.error(error))
5151
.use(corsPlugin)
52-
.use(restRoutes)
53-
.use(graphqlRoutes)
54-
.get(
55-
"/",
56-
{
57-
summary: "API Docs Redirect",
58-
tags: ["System"],
59-
description: "Redirect to the API reference when visiting the root URL.",
60-
},
61-
({ set }) => {
62-
set.status = 302;
63-
set.headers.Location = "/scalar";
64-
},
65-
)
66-
.get(
67-
"/api/health",
68-
{
69-
summary: "Health Check",
70-
tags: ["System"],
71-
description: "Used to make sure the API is up and running.",
72-
responses: z.object({
73-
status: z.literal("ok"),
74-
version: z.string(),
75-
}),
76-
},
77-
() => ({ status: "ok" as const, version }),
78-
);
52+
.use(systemApis)
53+
.use(extensionStoreApis)
54+
.use(graphqlApis);
7955

8056
export default app;
8157
export type App = typeof app;

src/utils/chrome/__tests__/__snapshots__/chrome-crawler.test.ts.snap renamed to src/services/__tests__/__snapshots__/chrome-crawler.test.ts.snap

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)