mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
refactor(plugin): ♻️ Remove mandatory manifest inside Plugin constructor
This commit is contained in:
parent
d224d7b9b8
commit
c7221ae9d1
|
|
@ -154,14 +154,7 @@ describe("PluginLoader", () => {
|
|||
});
|
||||
|
||||
test("loadPlugin should load and return a Plugin instance", async () => {
|
||||
const mockPlugin = new Plugin(
|
||||
{
|
||||
name: "test-plugin",
|
||||
version: "1.1.0",
|
||||
description: "Doobaee",
|
||||
},
|
||||
new PluginConfigManager(z.object({})),
|
||||
);
|
||||
const mockPlugin = new Plugin(new PluginConfigManager(z.object({})));
|
||||
mock.module("/some/path/index.ts", () => ({
|
||||
default: mockPlugin,
|
||||
}));
|
||||
|
|
@ -186,10 +179,7 @@ describe("PluginLoader", () => {
|
|||
version: "1.1.0",
|
||||
description: "Doobaee",
|
||||
};
|
||||
const mockPlugin = new Plugin(
|
||||
manifestContent,
|
||||
new PluginConfigManager(z.object({})),
|
||||
);
|
||||
const mockPlugin = new Plugin(new PluginConfigManager(z.object({})));
|
||||
|
||||
mockReaddir
|
||||
.mockResolvedValueOnce([
|
||||
|
|
|
|||
|
|
@ -1,13 +1,6 @@
|
|||
import { z } from "zod";
|
||||
import { Hooks } from "./hooks";
|
||||
import { Plugin, PluginConfigManager } from "./plugin";
|
||||
import type { Manifest } from "./schema";
|
||||
|
||||
const myManifest: Manifest = {
|
||||
name: "my-plugin",
|
||||
description: "A plugin for my app",
|
||||
version: "1.0.0",
|
||||
};
|
||||
|
||||
const configManager = new PluginConfigManager(
|
||||
z.object({
|
||||
|
|
@ -15,7 +8,7 @@ const configManager = new PluginConfigManager(
|
|||
}),
|
||||
);
|
||||
|
||||
const myPlugin = new Plugin(myManifest, configManager);
|
||||
const myPlugin = new Plugin(configManager);
|
||||
|
||||
myPlugin.registerHandler(Hooks.Response, (req) => {
|
||||
console.info("Request received:", req);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import type { z } from "zod";
|
|||
import { type ZodError, fromZodError } from "zod-validation-error";
|
||||
import type { HonoEnv } from "~/types/api";
|
||||
import type { ServerHooks } from "./hooks";
|
||||
import { type Manifest, manifestSchema } from "./schema";
|
||||
|
||||
export type HonoPluginEnv<ConfigType extends z.ZodTypeAny> = HonoEnv & {
|
||||
Variables: {
|
||||
|
|
@ -19,12 +18,7 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void;
|
||||
}[] = [];
|
||||
|
||||
constructor(
|
||||
private manifest: Manifest,
|
||||
private configManager: PluginConfigManager<ConfigSchema>,
|
||||
) {
|
||||
this.validateManifest(manifest);
|
||||
}
|
||||
constructor(private configManager: PluginConfigManager<ConfigSchema>) {}
|
||||
|
||||
get middleware() {
|
||||
// Middleware that adds the plugin's configuration to the request object
|
||||
|
|
@ -36,10 +30,6 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
);
|
||||
}
|
||||
|
||||
public getManifest() {
|
||||
return this.manifest;
|
||||
}
|
||||
|
||||
public registerRoute(
|
||||
path: string,
|
||||
fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void,
|
||||
|
|
@ -76,19 +66,10 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
this.handlers[hook] = handler;
|
||||
}
|
||||
|
||||
private validateManifest(manifest: Manifest) {
|
||||
try {
|
||||
manifestSchema.parse(manifest);
|
||||
} catch (error) {
|
||||
throw fromZodError(error as ZodError);
|
||||
}
|
||||
}
|
||||
|
||||
static [Symbol.hasInstance](instance: unknown): boolean {
|
||||
return (
|
||||
typeof instance === "object" &&
|
||||
instance !== null &&
|
||||
"getManifest" in instance &&
|
||||
"registerHandler" in instance
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,49 +0,0 @@
|
|||
import { describe, expect, it } from "bun:test";
|
||||
import { z } from "zod";
|
||||
import { Plugin, PluginConfigManager } from "../plugin";
|
||||
import type { Manifest } from "../schema";
|
||||
|
||||
describe("Manifest parsing tests", () => {
|
||||
it("should parse a valid manifest", () => {
|
||||
const manifest: Manifest = {
|
||||
name: "plugin",
|
||||
version: "1.0.0",
|
||||
description: "A test plugin",
|
||||
authors: [
|
||||
{
|
||||
name: "Author",
|
||||
email: "bob@joe.com",
|
||||
url: "https://example.com",
|
||||
},
|
||||
],
|
||||
repository: {
|
||||
type: "git",
|
||||
url: "https://example.com",
|
||||
},
|
||||
};
|
||||
|
||||
const plugin = new Plugin(
|
||||
manifest,
|
||||
new PluginConfigManager(z.string()),
|
||||
);
|
||||
|
||||
expect(plugin.getManifest()).toEqual(manifest);
|
||||
});
|
||||
|
||||
it("should throw an error for an invalid manifest", () => {
|
||||
const manifest = {
|
||||
name: "plugin",
|
||||
silly: "Manifest",
|
||||
};
|
||||
|
||||
expect(
|
||||
() =>
|
||||
new Plugin(
|
||||
manifest as unknown as Manifest,
|
||||
new PluginConfigManager(z.string()),
|
||||
),
|
||||
).toThrowError(
|
||||
`Validation error: Required at "version"; Required at "description"`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,13 +1,7 @@
|
|||
import { Hooks, type Manifest, Plugin, PluginConfigManager } from "@versia/kit";
|
||||
import { Hooks, Plugin, PluginConfigManager } from "@versia/kit";
|
||||
import { z } from "zod";
|
||||
import authorizeRoute from "./routes/authorize";
|
||||
|
||||
const myManifest: Manifest = {
|
||||
name: "@versia/openid",
|
||||
description: "OpenID authentication.",
|
||||
version: "0.1.0",
|
||||
};
|
||||
|
||||
const configManager = new PluginConfigManager(
|
||||
z.object({
|
||||
forced: z.boolean().default(false),
|
||||
|
|
@ -65,7 +59,7 @@ const configManager = new PluginConfigManager(
|
|||
}),
|
||||
);
|
||||
|
||||
const plugin = new Plugin(myManifest, configManager);
|
||||
const plugin = new Plugin(configManager);
|
||||
|
||||
plugin.registerHandler(Hooks.Response, (req) => {
|
||||
console.info("Request received:", req);
|
||||
|
|
|
|||
Loading…
Reference in a new issue