refactor(api): ♻️ Move all SSO account linking endpoint logic to OpenID plugin

This commit is contained in:
Jesse Wierzbinski 2024-09-25 12:31:35 +02:00
parent 6d4b4eb13b
commit 74ec563ba5
No known key found for this signature in database
9 changed files with 267 additions and 286 deletions

View file

@ -1,15 +1,13 @@
import { z } from "zod";
import { Hooks } from "./hooks";
import { Plugin, PluginConfigManager } from "./plugin";
import { Plugin } from "./plugin";
const configManager = new PluginConfigManager(
const myPlugin = new Plugin(
z.object({
apiKey: z.string(),
}),
);
const myPlugin = new Plugin(configManager);
myPlugin.registerHandler(Hooks.Response, (req) => {
console.info("Request received:", req);
return req;

View file

@ -1,6 +1,6 @@
import { Hooks } from "./hooks";
import { Plugin, PluginConfigManager } from "./plugin";
import { Plugin } from "./plugin";
import type { Manifest } from "./schema";
export type { Manifest };
export { Plugin, PluginConfigManager, Hooks };
export { Plugin, Hooks };

View file

@ -13,18 +13,19 @@ export type HonoPluginEnv<ConfigType extends z.ZodTypeAny> = HonoEnv & {
export class Plugin<ConfigSchema extends z.ZodTypeAny> {
private handlers: Partial<ServerHooks> = {};
private store: z.infer<ConfigSchema> | null = null;
private routes: {
path: string;
fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void;
}[] = [];
constructor(private configManager: PluginConfigManager<ConfigSchema>) {}
constructor(private configSchema: ConfigSchema) {}
get middleware() {
// Middleware that adds the plugin's configuration to the request object
return createMiddleware<HonoPluginEnv<ConfigSchema>>(
async (context, next) => {
context.set("pluginConfig", this.configManager.getConfig());
context.set("pluginConfig", this.getConfig());
await next();
},
);
@ -45,9 +46,12 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
* This will be called when the plugin is loaded.
* @param config Values the user has set in the configuration file.
*/
protected _loadConfig(config: z.input<ConfigSchema>): Promise<void> {
// biome-ignore lint/complexity/useLiteralKeys: Private method
return this.configManager["_load"](config);
protected async _loadConfig(config: z.input<ConfigSchema>): Promise<void> {
try {
this.store = await this.configSchema.parseAsync(config);
} catch (error) {
throw fromZodError(error as ZodError).message;
}
}
protected _addToApp(app: OpenAPIHono<HonoEnv>) {
@ -73,39 +77,11 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
"registerHandler" in instance
);
}
}
/**
* Handles loading, defining, and managing the plugin's configuration.
* Plugins can define their own configuration schema, which is then used to
* load it from the user's configuration file.
* @param schema The Zod schema that defines the configuration.
*/
export class PluginConfigManager<Schema extends z.ZodTypeAny> {
private store: z.infer<Schema> | null;
constructor(private schema: Schema) {
this.store = null;
}
/**
* Loads the configuration from the Versia Server configuration file.
* This will be called when the plugin is loaded.
* @param config Values the user has set in the configuration file.
*/
protected async _load(config: z.infer<Schema>) {
// Check if the configuration is valid
try {
this.store = await this.schema.parseAsync(config);
} catch (error) {
throw fromZodError(error as ZodError).message;
}
}
/**
* Returns the internal configuration object.
*/
public getConfig() {
private getConfig() {
if (!this.store) {
throw new Error("Configuration has not been loaded yet.");
}