Skip to content

Commit

Permalink
Merge pull request #17 from sophie-app/fix/odpt_middleware_env
Browse files Browse the repository at this point in the history
Fix/odpt middleware env
  • Loading branch information
mst-mkt authored Nov 5, 2024
2 parents 0eea126 + 9a09d21 commit 76739cd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apps/backend/.dev.vars.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
FRONTEND_BASE_URL=http://localhost:3000

OPENAI_API_KEY=

ODPT_ACCESS_TOKEN=
ODPT_CHALLENGE_ACCESS_TOKEN=
2 changes: 2 additions & 0 deletions apps/backend/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Client } from 'openapi-fetch'
import type { paths } from './lib/odptApiPath'
import { aiMiddleware } from './middlewares/ai'
import { corsMiddleware } from './middlewares/cors'
import { odptClientMiddleware } from './middlewares/odptClient'

export type BindingsType = {
FRONTEND_BASE_URL: string | undefined
Expand All @@ -31,6 +32,7 @@ const honoFactory = createFactory<HonoConfigType>({
initApp: (app) => {
app.use(corsMiddleware(), trimTrailingSlash(), prettyJSON(), poweredBy())
app.use(aiMiddleware)
app.use(odptClientMiddleware)
},
})

Expand Down
9 changes: 8 additions & 1 deletion apps/backend/src/middlewares/odptClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { createMiddleware } from 'hono/factory'
import createClient, { type Middleware } from 'openapi-fetch'
import { minLength, object, parse, pipe, string } from 'valibot'
import { ODPT_API_BASEURL, ODPT_CHALLENGE_API_BASEURL } from '../constants/odpt'
import type { paths } from '../lib/odptApiPath'

const odptEnvSchema = object({
ODPT_ACCESS_TOKEN: pipe(string(), minLength(1)),
ODPT_CHALLENGE_ACCESS_TOKEN: pipe(string(), minLength(1)),
})

const openapiClientAuthMiddleware = (apiKey: string) => {
return {
onRequest: ({ request }) => {
Expand All @@ -14,7 +20,8 @@ const openapiClientAuthMiddleware = (apiKey: string) => {
}

export const odptClientMiddleware = createMiddleware(async (c, next) => {
const { ODPT_ACCESS_TOKEN, ODPT_CHALLENGE_ACCESS_TOKEN } = c.env
const { ODPT_ACCESS_TOKEN, ODPT_CHALLENGE_ACCESS_TOKEN } = parse(odptEnvSchema, c.env)

const odptClient = createClient<paths>({
baseUrl: ODPT_API_BASEURL,
})
Expand Down

0 comments on commit 76739cd

Please sign in to comment.