Skip to content

Commit

Permalink
Refactor middleware to use createMiddleware function
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuaki640 committed Feb 25, 2024
1 parent e789c07 commit b75a88b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
9 changes: 3 additions & 6 deletions src/middleware/basic-auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { MiddlewareHandler } from "hono";
import { basicAuth } from "hono/basic-auth";
import { createMiddleware } from "hono/factory";

import { AppEnv } from "../types";

export const BasicAuthMiddleware: MiddlewareHandler<AppEnv> = async (
c,
next,
) => {
export const BasicAuthMiddleware = createMiddleware<AppEnv>(async (c, next) => {
const auth = basicAuth({
username: c.env.USERNAME,
password: c.env.PASSWORD,
});
return auth(c, next);
};
});
7 changes: 3 additions & 4 deletions src/middleware/openai.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { MiddlewareHandler } from "hono";
import { createMiddleware } from "hono/factory";
import OpenAI from "openai";

import { AppEnv } from "../types";

export const OpenaiMiddleware: MiddlewareHandler<AppEnv> = async (c, next) => {
export const OpenaiMiddleware = createMiddleware<AppEnv>(async (c, next) => {
const client = new OpenAI({
apiKey: c.env.OPENAI_API_KEY,
});
c.set("openai", client);
await next();
};
});

0 comments on commit b75a88b

Please sign in to comment.