init project portal web
This commit is contained in:
55
app/api/rate-plan/create/route.ts
Normal file
55
app/api/rate-plan/create/route.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import axios from 'axios';
|
||||
import { cookies } from 'next/headers'
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export const POST = async (request: NextRequest) => {
|
||||
// INTERPOLATING API URL OF BACKEND
|
||||
const {
|
||||
offerVerId,
|
||||
offerType,
|
||||
reId,
|
||||
ratePlanName,
|
||||
ratePlanCode,
|
||||
remarks,
|
||||
ratePlanType,
|
||||
} = await request.json();
|
||||
|
||||
const url: string = `${process.env.NEXT_PUBLIC_API_URL}/priceplan/createpriceplan`
|
||||
const data = JSON.parse(cookies().get('credential')?.value ?? "")
|
||||
const token = `Bearer ${data.token}`
|
||||
|
||||
try {
|
||||
// MAKE AN API REQUEST
|
||||
/*
|
||||
* - WE USE AXIOS INSTEAD OF FETCH
|
||||
* - FETCH ALWAYS RETURNS "TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is already closed"
|
||||
* WHEN WE RUN "response.json()"
|
||||
*/
|
||||
const response = await axios(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': token
|
||||
},
|
||||
data: JSON.stringify({
|
||||
offerVerId,
|
||||
offerType,
|
||||
reId,
|
||||
ratePlanName,
|
||||
ratePlanCode,
|
||||
remarks,
|
||||
ratePlanType,
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
return NextResponse.json(response.data, { status: response.status });
|
||||
} catch (error: any) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
return NextResponse.json(error.response?.data || { message: 'Unknown error' }, { status: error.response?.status || 500 });
|
||||
}
|
||||
return NextResponse.json({ message: 'An unexpected error occurred' }, { status: 500 });
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user