Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 6ec88a1

Browse files
author
prostarz
committed
refactor(plugin): restructure plugin creation and route handling
This commit introduces significant refactoring to the plugin creation process and route handling. The `buildPlugin.ts` file has been removed, and its functionality has been integrated into `Plugin.ts`. The `createPlugin` function now supports additional options like `handleSetup` and `routeOptions`, allowing for more flexible configuration. The `setupRoute.ts` has been updated to handle different types of configuration (`query` or `params`), improving the plugin's adaptability. These changes aim to enhance maintainability and provide a more robust plugin architecture.
1 parent 9a94b48 commit 6ec88a1

5 files changed

Lines changed: 126 additions & 249 deletions

File tree

src/plugin/Plugin.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import cors from "@elysiajs/cors";
22
import Elysia from "elysia";
33
import type {
44
CreatePluginExtraOptions,
5+
CustomHandleSetupFunction,
56
HandleReturnFunction,
67
HandleSearchFunction,
78
PluginSetupWithoutConfig,
9+
SetupJsonOptions,
810
} from "../types";
911
import {
1012
registerReturnRoute,
@@ -16,8 +18,12 @@ import { Colors } from "./utils/colors";
1618
export class Plugin {
1719
private app = new Elysia().use(cors());
1820
private debug = false;
21+
private routeOptions: SetupJsonOptions = {
22+
typeOfConfig: "query",
23+
};
1924
private searchHandler: HandleSearchFunction | null = null;
2025
private returnHandler: HandleReturnFunction | null = null;
26+
private customSetupHandler: CustomHandleSetupFunction | null = null;
2127
private setupData: PluginSetupWithoutConfig | null = null;
2228
private started = false;
2329

@@ -37,19 +43,27 @@ export class Plugin {
3743
public set handlers({
3844
search,
3945
return: ret,
46+
setup,
4047
}: {
4148
search: HandleSearchFunction;
4249
return?: HandleReturnFunction;
50+
setup?: CustomHandleSetupFunction;
4351
}) {
4452
this.searchHandler = search;
4553
this.returnHandler = ret ?? null;
54+
this.customSetupHandler = setup ?? null;
4655
}
4756

4857
/** Extra options (e.g. debug) */
4958
public set extraOptions(opts: CreatePluginExtraOptions) {
5059
this.debug = opts.debug ?? false;
5160
}
5261

62+
/** Route options */
63+
public set routeConfig(opts: SetupJsonOptions) {
64+
this.routeOptions = opts;
65+
}
66+
5367
/** Kick off the HTTP server */
5468
public listen(port: number): void {
5569
if (this.started) {
@@ -62,7 +76,9 @@ export class Plugin {
6276
this.started = true;
6377
this.registerRoutes();
6478
this.app.listen(port, (ctx) => {
65-
const url = this.setupData!.api_url ?? ctx.url.href;
79+
const url = this.setupData?.api_url?.length
80+
? this.setupData?.api_url
81+
: ctx.url.href;
6682
if (this.debug) {
6783
console.info(
6884
Colors.create("[plugin] Listening on ")
@@ -92,7 +108,13 @@ export class Plugin {
92108
throw new Error("Missing setupData; call setupApp first");
93109
}
94110

95-
registerSetupRoute(this.app, this.setupData, this.debug);
111+
registerSetupRoute(
112+
this.app,
113+
this.setupData,
114+
this.debug,
115+
this.customSetupHandler,
116+
this.routeOptions
117+
);
96118
registerSearchRoute(this.app, () => this.searchHandler, this.debug);
97119
registerReturnRoute(this.app, () => this.returnHandler, this.debug);
98120
}

src/plugin/routes/setupRoute.ts

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,84 @@
11
import { PluginSetupJSON } from "@team-falkor/shared-types";
22
import Elysia, { t } from "elysia";
3-
import { PluginSetupWithoutConfig } from "../../types";
3+
import {
4+
CustomHandleSetupFunction,
5+
PluginSetupWithoutConfig,
6+
SetupJsonOptions,
7+
} from "../../types";
48
import { Colors } from "../utils/colors";
59

610
export function registerSetupRoute(
711
app: Elysia,
812
setupData: PluginSetupWithoutConfig,
9-
debug: boolean
13+
debug: boolean,
14+
getHandler: CustomHandleSetupFunction | null,
15+
options: SetupJsonOptions = {
16+
typeOfConfig: "query",
17+
}
1018
) {
11-
app.get(
12-
"/setup.json",
13-
({ query }) => {
14-
if (debug) {
15-
console.log(
16-
Colors.create("[plugin] GET /setup.json").blue.toString(),
17-
query?.search
18-
? Colors.create(
19-
`filters: ${query.search.join(", ")}`
20-
).magenta.toString()
21-
: ""
22-
);
19+
if (options.typeOfConfig === "query")
20+
app.get(
21+
"/setup.json",
22+
({ query }) => {
23+
if (debug) {
24+
console.log(
25+
Colors.create("[plugin] GET /setup.json").blue.toString(),
26+
query?.search
27+
? Colors.create(
28+
`filters: ${query.search.join(", ")}`
29+
).magenta.toString()
30+
: ""
31+
);
32+
}
33+
34+
if (getHandler) {
35+
const response = getHandler(query?.search);
36+
return response;
37+
}
38+
39+
const response: PluginSetupJSON = {
40+
...setupData,
41+
config: query?.search ? { search: query.search } : false,
42+
};
43+
return response;
44+
},
45+
{
46+
query: t.Optional(
47+
t.Object({
48+
search: t.Array(t.String()),
49+
})
50+
),
2351
}
52+
);
53+
54+
if (options.typeOfConfig === "params")
55+
app.get(
56+
":search/setup.json",
57+
({ params }) => {
58+
const search = params.search?.split("%20");
2459

25-
const response: PluginSetupJSON = {
26-
...setupData,
27-
config: query?.search ? { search: query.search } : false,
28-
};
29-
return response;
30-
},
31-
{
32-
query: t.Optional(
33-
t.Object({
34-
search: t.Array(t.String()),
35-
})
36-
),
37-
}
38-
);
60+
if (debug) {
61+
console.log(
62+
Colors.create("[plugin] GET /setup.json").blue.toString(),
63+
search ? Colors.create(`filters: ${search}`).magenta.toString() : ""
64+
);
65+
}
66+
67+
if (getHandler) {
68+
const response = getHandler(search);
69+
return response;
70+
}
71+
72+
const response: PluginSetupJSON = {
73+
...setupData,
74+
config: search ? { search: search } : false,
75+
};
76+
return response;
77+
},
78+
{
79+
params: t.Object({
80+
search: t.String(),
81+
}),
82+
}
83+
);
3984
}

0 commit comments

Comments
 (0)