Skip to content

Commit

Permalink
Merge pull request #97 from rtjord/pulkit-frontend
Browse files Browse the repository at this point in the history
Added changes for connecting end-points
  • Loading branch information
rtjord authored Dec 8, 2024
2 parents 3ac6806 + 66325ea commit a3f2350
Show file tree
Hide file tree
Showing 11 changed files with 4,225 additions and 3,475 deletions.
79 changes: 79 additions & 0 deletions frontend/app/api/package-proxy/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { NextResponse } from 'next/server';

// GET handler for downloading package and getting package info
export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const id = params.id;
// Check if this is a rate, cost, or download request
const { pathname } = new URL(request.url);

let endpoint = `/package/${id}`;
if (pathname.endsWith('/rate')) {
endpoint = `/package/${id}/rate`;
} else if (pathname.endsWith('/cost')) {
endpoint = `/package/${id}/cost`;
}

const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}${endpoint}`, {
headers: {
'Content-Type': 'application/json',
'X-Authorization': ''
}
});

console.log(`${endpoint} Status:`, response.status);

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || response.statusText);
}

const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Package proxy error:', error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to process request' },
{ status: 500 }
);
}
}

// POST handler for updating package
export async function POST(
request: Request,
{ params }: { params: { id: string } }
) {
try {
const id = params.id;
const body = await request.json();

const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/package/${id}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Authorization': ''
},
body: JSON.stringify(body)
});

console.log('Update Package Status:', response.status);

if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || response.statusText);
}

const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Package update error:', error);
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to update package' },
{ status: 500 }
);
}
}
31 changes: 31 additions & 0 deletions frontend/app/api/package-proxy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NextResponse } from 'next/server';

export async function POST(request: Request) {
try {
const packageData = await request.json();

const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/package`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Authorization': ''
},
body: JSON.stringify(packageData)
});

const data = await response.json();

if (!response.ok) {
console.error('API Error:', data);
return NextResponse.json(data, { status: response.status });
}

return NextResponse.json(data);
} catch (error) {
console.error('Error in package proxy:', error);
return NextResponse.json(
{ error: 'Failed to process package upload' },
{ status: 500 }
);
}
}
96 changes: 96 additions & 0 deletions frontend/app/api/search-proxy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { NextResponse } from 'next/server';

export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
const type = searchParams.get('type');
const query = searchParams.get('q');

if (!query) {
return NextResponse.json({ error: 'Query parameter is required' }, { status: 400 });
}

let response;

switch (type) {
case 'name':
response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/byName/${encodeURIComponent(query)}`, {
headers: {
'Content-Type': 'application/json',
'X-Authorization': ''
}
});
break;

case 'id':
response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/package/${encodeURIComponent(query)}`, {
headers: {
'Content-Type': 'application/json',
'X-Authorization': ''
}
});
break;

default:
return NextResponse.json({ error: 'Invalid search type' }, { status: 400 });
}

console.log(`${type.toUpperCase()} Search Status:`, response.status);
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Search proxy error:', error);
return NextResponse.json({ error: 'Failed to process search request' }, { status: 500 });
}
}

export async function POST(request: Request) {
try {
const { searchParams } = new URL(request.url);
const type = searchParams.get('type');
const body = await request.json();

if (!body) {
return NextResponse.json({ error: 'Request body is required' }, { status: 400 });
}

let response;

switch (type) {
case 'regex':
const regexQuery = body.query.includes('.*') ? body.query : `.*${body.query}.*`;
response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/package/byRegEx`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Authorization': ''
},
body: JSON.stringify({
RegEx: regexQuery
})
});
break;

case 'smart':
response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL}/recommend`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Authorization': ''
},
body: JSON.stringify({ Description: body.query })
});
break;

default:
return NextResponse.json({ error: 'Invalid search type' }, { status: 400 });
}

console.log(`${type.toUpperCase()} Search Status:`, response.status);
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
console.error('Search proxy error:', error);
return NextResponse.json({ error: 'Failed to process search request' }, { status: 500 });
}
}
Loading

0 comments on commit a3f2350

Please sign in to comment.