update package
This commit is contained in:
42
apps/insights.saladeaula.digital/app/routes/~.api.$/route.ts
Normal file
42
apps/insights.saladeaula.digital/app/routes/~.api.$/route.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { Route } from './+types'
|
||||
|
||||
import type { User } from '@repo/auth/auth'
|
||||
import { userContext } from '@repo/auth/context'
|
||||
import { authMiddleware } from '@repo/auth/middleware/auth'
|
||||
|
||||
export const middleware: Route.MiddlewareFunction[] = [authMiddleware]
|
||||
export const loader = proxy
|
||||
export const action = proxy
|
||||
|
||||
async function proxy({
|
||||
request,
|
||||
context
|
||||
}: Route.ActionArgs): Promise<Response> {
|
||||
const pathname = new URL(request.url).pathname.replace(/^\/~\/api\//, '')
|
||||
const user = context.get(userContext) as User
|
||||
const url = new URL(pathname, context.cloudflare.env.API_URL)
|
||||
const headers = new Headers(request.headers)
|
||||
|
||||
headers.set('Authorization', `Bearer ${user.accessToken}`)
|
||||
|
||||
const response = await fetch(url.toString(), {
|
||||
method: request.method,
|
||||
headers,
|
||||
...(['GET', 'HEAD'].includes(request.method)
|
||||
? {}
|
||||
: { body: await request.text() })
|
||||
})
|
||||
|
||||
console.log(headers)
|
||||
|
||||
const contentType = response.headers.get('content-type') || ''
|
||||
const body =
|
||||
contentType.includes('application/json') || contentType.startsWith('text/')
|
||||
? await response.text()
|
||||
: await response.arrayBuffer()
|
||||
|
||||
return new Response(body, {
|
||||
status: response.status,
|
||||
headers: response.headers
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user