mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor: ♻️ Always use explicit types in every function
This commit is contained in:
parent
54cea29ce9
commit
c1dcdc78ae
62 changed files with 359 additions and 226 deletions
|
|
@ -8,6 +8,7 @@
|
|||
import { loadConfig, watchConfig } from "c12";
|
||||
import { fromZodError } from "zod-validation-error";
|
||||
import { type Config, configValidator } from "./config.type";
|
||||
export type { Config } from "./config.type";
|
||||
|
||||
const { config } = await watchConfig({
|
||||
configFile: "./config/config.toml",
|
||||
|
|
@ -29,4 +30,3 @@ if (!parsed.success) {
|
|||
const exportedConfig = parsed.data;
|
||||
|
||||
export { exportedConfig as config };
|
||||
export type { Config };
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
import { Hooks } from "./hooks.ts";
|
||||
import { Plugin } from "./plugin.ts";
|
||||
import type { Manifest } from "./schema.ts";
|
||||
|
||||
export type { Manifest };
|
||||
export { Plugin, Hooks };
|
||||
// biome-ignore lint/performance/noBarrelFile: <explanation>
|
||||
export { Hooks } from "./hooks.ts";
|
||||
export { Plugin } from "./plugin.ts";
|
||||
export type { Manifest } from "./schema.ts";
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { createMiddleware } from "@hono/hono/factory";
|
||||
import type { OpenAPIHono } from "@hono/zod-openapi";
|
||||
import type { MiddlewareHandler } from "hono";
|
||||
import type { z } from "zod";
|
||||
import { type ZodError, fromZodError } from "zod-validation-error";
|
||||
import type { HonoEnv } from "~/types/api";
|
||||
|
|
@ -21,7 +22,7 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
|
||||
public constructor(private configSchema: ConfigSchema) {}
|
||||
|
||||
public get middleware() {
|
||||
public get middleware(): MiddlewareHandler {
|
||||
// Middleware that adds the plugin's configuration to the request object
|
||||
return createMiddleware<HonoPluginEnv<ConfigSchema>>(
|
||||
async (context, next) => {
|
||||
|
|
@ -34,7 +35,7 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
public registerRoute(
|
||||
path: string,
|
||||
fn: (app: OpenAPIHono<HonoPluginEnv<ConfigSchema>>) => void,
|
||||
) {
|
||||
): void {
|
||||
this.routes.push({
|
||||
path,
|
||||
fn,
|
||||
|
|
@ -54,7 +55,7 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
}
|
||||
}
|
||||
|
||||
protected _addToApp(app: OpenAPIHono<HonoEnv>) {
|
||||
protected _addToApp(app: OpenAPIHono<HonoEnv>): void {
|
||||
for (const route of this.routes) {
|
||||
app.use(route.path, this.middleware);
|
||||
route.fn(
|
||||
|
|
@ -66,7 +67,7 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
public registerHandler<HookName extends keyof ServerHooks>(
|
||||
hook: HookName,
|
||||
handler: ServerHooks[HookName],
|
||||
) {
|
||||
): void {
|
||||
this.handlers[hook] = handler;
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +82,7 @@ export class Plugin<ConfigSchema extends z.ZodTypeAny> {
|
|||
/**
|
||||
* Returns the internal configuration object.
|
||||
*/
|
||||
private getConfig() {
|
||||
private getConfig(): z.infer<ConfigSchema> {
|
||||
if (!this.store) {
|
||||
throw new Error("Configuration has not been loaded yet.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,6 +94,7 @@ export type Manifest = {
|
|||
};
|
||||
|
||||
// This is a type guard to ensure that the schema and the type are in sync
|
||||
// biome-ignore lint/nursery/useExplicitType: <explanation>
|
||||
function assert<_T extends never>() {
|
||||
// ...
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue