refactor: ♻️ Always use explicit types in every function

This commit is contained in:
Jesse Wierzbinski 2024-11-02 00:43:33 +01:00
parent 54cea29ce9
commit c1dcdc78ae
No known key found for this signature in database
62 changed files with 359 additions and 226 deletions

View file

@ -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";

View file

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

View file

@ -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>() {
// ...
}