Dev cleaning (#106)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { defineEventHandler, getCookie, getRequestHeaders, getRequestURL, readBody } from 'h3'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { method } = event.node.req
|
||||
const headers = getRequestHeaders(event)
|
||||
const url = getRequestURL(event)
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
const apiOrigin = config.public.API_ORIGIN
|
||||
const pathname = url.pathname.replace(/^\/api/, '')
|
||||
|
||||
const targetUrl = apiOrigin + pathname + (url.search || '')
|
||||
|
||||
const verificationId = headers['verification-id'] as string | undefined
|
||||
let bearer = ''
|
||||
if (verificationId) {
|
||||
bearer = getCookie(event, `Verification-${verificationId}`) || ''
|
||||
if (!bearer) bearer = getCookie(event, 'authentication') || ''
|
||||
} else {
|
||||
bearer = getCookie(event, 'authentication') || ''
|
||||
}
|
||||
|
||||
const forwardHeaders = new Headers()
|
||||
if (headers['content-type']) forwardHeaders.set('Content-Type', headers['content-type'])
|
||||
forwardHeaders.set('Authorization', `Bearer ${bearer}`)
|
||||
|
||||
let body: any
|
||||
if (['POST', 'PATCH'].includes(method!)) {
|
||||
if (headers['content-type']?.includes('multipart/form-data')) {
|
||||
body = await readBody(event)
|
||||
} else {
|
||||
body = await readBody(event)
|
||||
forwardHeaders.set('Content-Type', 'application/json')
|
||||
body = JSON.stringify(body)
|
||||
}
|
||||
}
|
||||
|
||||
const res = await fetch(targetUrl, {
|
||||
method,
|
||||
headers: forwardHeaders,
|
||||
body,
|
||||
})
|
||||
|
||||
return res
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
import { getRequestURL, readBody, setCookie } from 'h3'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
const url = getRequestURL(event)
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
const apiOrigin = config.public.API_ORIGIN
|
||||
|
||||
const cleanOrigin = apiOrigin.replace(/\/+$/, '')
|
||||
const cleanPath = url.pathname.replace(/^\/api\//, '').replace(/^\/+/, '')
|
||||
const externalUrl = `${cleanOrigin}/${cleanPath}${url.search}`
|
||||
|
||||
const resp = await fetch(externalUrl, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(body),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
console.log(resp)
|
||||
if (resp.status === 200) {
|
||||
const data = await resp.json()
|
||||
|
||||
if (data?.data?.accessToken) {
|
||||
setCookie(event, 'authentication', data.data.accessToken, {
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
sameSite: 'strict',
|
||||
maxAge: 60 * 60 * 24,
|
||||
})
|
||||
|
||||
delete data.data.accessToken
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
return new Response(await resp.text(), {
|
||||
status: resp.status,
|
||||
headers: {
|
||||
'Content-Type': resp.headers.get('content-type') || 'text/plain',
|
||||
},
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user