mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38: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 () => {
|
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([
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 { 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);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue