refactor(plugin): ♻️ Remove mandatory manifest inside Plugin constructor

This commit is contained in:
Jesse Wierzbinski 2024-09-23 11:54:42 +02:00
parent d224d7b9b8
commit c7221ae9d1
No known key found for this signature in database
5 changed files with 6 additions and 97 deletions

View file

@ -154,14 +154,7 @@ describe("PluginLoader", () => {
}); });
test("loadPlugin should load and return a Plugin instance", async () => { test("loadPlugin should load and return a Plugin instance", async () => {
const mockPlugin = new Plugin( const mockPlugin = new Plugin(new PluginConfigManager(z.object({})));
{
name: "test-plugin",
version: "1.1.0",
description: "Doobaee",
},
new PluginConfigManager(z.object({})),
);
mock.module("/some/path/index.ts", () => ({ mock.module("/some/path/index.ts", () => ({
default: mockPlugin, default: mockPlugin,
})); }));
@ -186,10 +179,7 @@ describe("PluginLoader", () => {
version: "1.1.0", version: "1.1.0",
description: "Doobaee", description: "Doobaee",
}; };
const mockPlugin = new Plugin( const mockPlugin = new Plugin(new PluginConfigManager(z.object({})));
manifestContent,
new PluginConfigManager(z.object({})),
);
mockReaddir mockReaddir
.mockResolvedValueOnce([ .mockResolvedValueOnce([

View file

@ -1,13 +1,6 @@
import { z } from "zod"; import { z } from "zod";
import { Hooks } from "./hooks"; import { Hooks } from "./hooks";
import { Plugin, PluginConfigManager } from "./plugin"; 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( const configManager = new PluginConfigManager(
z.object({ 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) => { myPlugin.registerHandler(Hooks.Response, (req) => {
console.info("Request received:", req); console.info("Request received:", req);

View file

@ -4,7 +4,6 @@ import type { z } from "zod";
import { type ZodError, fromZodError } from "zod-validation-error"; import { type ZodError, fromZodError } from "zod-validation-error";
import type { HonoEnv } from "~/types/api"; import type { HonoEnv } from "~/types/api";
import type { ServerHooks } from "./hooks"; import type { ServerHooks } from "./hooks";
import { type Manifest, manifestSchema } from "./schema";
export type HonoPluginEnv<ConfigType extends z.ZodTypeAny> = HonoEnv & { export type HonoPluginEnv<ConfigType extends z.ZodTypeAny> = HonoEnv & {
Variables: { Variables: {
@ -19,12 +18,7 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void; fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void;
}[] = []; }[] = [];
constructor( constructor(private configManager: PluginConfigManager<ConfigSchema>) {}
private manifest: Manifest,
private configManager: PluginConfigManager<ConfigSchema>,
) {
this.validateManifest(manifest);
}
get middleware() { get middleware() {
// Middleware that adds the plugin's configuration to the request object // 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( public registerRoute(
path: string, path: string,
fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void, fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void,
@ -76,19 +66,10 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
this.handlers[hook] = handler; 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 { static [Symbol.hasInstance](instance: unknown): boolean {
return ( return (
typeof instance === "object" && typeof instance === "object" &&
instance !== null && instance !== null &&
"getManifest" in instance &&
"registerHandler" in instance "registerHandler" in instance
); );
} }

View file

@ -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"`,
);
});
});

View file

@ -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 { z } from "zod";
import authorizeRoute from "./routes/authorize"; import authorizeRoute from "./routes/authorize";
const myManifest: Manifest = {
name: "@versia/openid",
description: "OpenID authentication.",
version: "0.1.0",
};
const configManager = new PluginConfigManager( const configManager = new PluginConfigManager(
z.object({ z.object({
forced: z.boolean().default(false), 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) => { plugin.registerHandler(Hooks.Response, (req) => {
console.info("Request received:", req); console.info("Request received:", req);