160 lines
4.3 KiB
TypeScript
160 lines
4.3 KiB
TypeScript
|
|
import { cookies, headers } from 'next/headers'
|
|
|
|
const api_url = 'http://127.0.0.1:8000/';
|
|
|
|
const rewrite_url = async (context:any, request:any)=>{
|
|
|
|
const { slug } = await context.params
|
|
const { searchParams } = await new URL(request.url)
|
|
|
|
const path = await slug.join("/")
|
|
|
|
const url_params = await searchParams.toString()
|
|
|
|
const full_url = url_params ? api_url+path+"?"+url_params : api_url+path+"/"
|
|
|
|
return full_url
|
|
}
|
|
|
|
export async function GET(request: Request, context: { params: any }) {
|
|
|
|
const new_api_url = await rewrite_url(context, request)
|
|
|
|
const cookiesList = cookies()
|
|
const refreshToken = cookiesList.get('refreshToken')
|
|
const accsessToken = cookiesList.get('accsessToken')
|
|
|
|
|
|
if (!accsessToken || !refreshToken ){ return Response.json('nextjs token not found'); }
|
|
|
|
const res = await fetch( new_api_url, {
|
|
cache: 'no-store',
|
|
method: request.method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': "Bearer "+accsessToken.value.toString()
|
|
},
|
|
})
|
|
|
|
const data = await res.json()
|
|
|
|
return new Response(JSON.stringify(data),{
|
|
status: res.status,
|
|
})
|
|
|
|
}
|
|
|
|
export async function POST(request: Request, context: { params: any }) {
|
|
|
|
const cookiesList = await cookies()
|
|
const refreshToken = await cookiesList.get('refreshToken')
|
|
const accsessToken = await cookiesList.get('accsessToken')
|
|
|
|
if (!accsessToken || !refreshToken ){ return Response.json('token not found'); }
|
|
|
|
const new_api_url = await rewrite_url(context, request)
|
|
|
|
|
|
const payload = await request.json()
|
|
|
|
const res = await fetch( new_api_url, {
|
|
method: "POST",
|
|
cache: "no-store",
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': "Bearer "+accsessToken.value.toString()
|
|
},
|
|
body: JSON.stringify(payload)
|
|
})
|
|
|
|
const data = await res.json()
|
|
|
|
return new Response(JSON.stringify(data),{
|
|
status: res.status,
|
|
headers: res.headers
|
|
})
|
|
}
|
|
|
|
export async function PUT(request: Request, context: { params: any }) {
|
|
|
|
const new_api_url = await rewrite_url(context, request)
|
|
|
|
const cookiesList = await cookies()
|
|
const refreshToken = await cookiesList.get('refreshToken')
|
|
const accsessToken = await cookiesList.get('accsessToken')
|
|
|
|
if (!accsessToken || !refreshToken ){ return Response.json('token not found'); }
|
|
|
|
const payload = await request.json()
|
|
|
|
const res = await fetch( new_api_url, {
|
|
method: request.method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': accsessToken.toString()
|
|
},
|
|
body: JSON.stringify(payload)
|
|
})
|
|
|
|
return new Response(res.body,{
|
|
status: res.status,
|
|
headers: res.headers
|
|
})
|
|
}
|
|
|
|
export async function DELETE(request: Request, context: { params: any }) {
|
|
|
|
|
|
console.log("DELETE METHODDDD!!!!!!!!!!")
|
|
|
|
const new_api_url = await rewrite_url(context, request)
|
|
|
|
const cookiesList = await cookies()
|
|
const refreshToken = await cookiesList.get('refreshToken')
|
|
const accsessToken = await cookiesList.get('accsessToken')
|
|
|
|
if (!accsessToken || !refreshToken ){ return Response.json('token not found'); }
|
|
|
|
const res = await fetch( new_api_url, {
|
|
cache: 'no-store',
|
|
method: request.method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': "Bearer "+accsessToken.value.toString()
|
|
}
|
|
})
|
|
|
|
return new Response(res.body,{
|
|
status: res.status,
|
|
headers: res.headers
|
|
})
|
|
|
|
}
|
|
|
|
export async function PATCH(request: Request, context: { params: any }) {
|
|
|
|
const cookiesList = await cookies()
|
|
const refreshToken = await cookiesList.get('refreshToken')
|
|
const accsessToken = await cookiesList.get('accsessToken')
|
|
|
|
if (!accsessToken || !refreshToken ){ return Response.json('token not found'); }
|
|
|
|
const new_api_url = await rewrite_url(context, request)
|
|
|
|
const payload = await request.json()
|
|
|
|
const res = await fetch( new_api_url, {
|
|
method: request.method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': accsessToken.toString()
|
|
},
|
|
body: JSON.stringify(payload)
|
|
})
|
|
|
|
return new Response(res.body,{
|
|
status: res.status,
|
|
headers: res.headers
|
|
})
|
|
} |