Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 14 additions & 34 deletions app/api/assistant/[assistant_id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
import { NextRequest, NextResponse } from "next/server";
import { apiClient } from "@/app/lib/apiClient";

/**
* GET /api/assistant/:assistant_id
Expand All @@ -7,46 +8,25 @@ import { NextRequest, NextResponse } from 'next/server';
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ assistant_id: string }> }
{ params }: { params: Promise<{ assistant_id: string }> },
) {
try {
// Get the API key from request headers
const apiKey = request.headers.get('X-API-KEY');

if (!apiKey) {
return NextResponse.json(
{ error: 'Missing X-API-KEY header' },
{ status: 401 }
);
}

const { assistant_id } = await params;

// Get backend URL from environment variable
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';

// Forward the request to the actual backend
const response = await fetch(`${backendUrl}/api/v1/assistant/${assistant_id}`, {
method: 'GET',
headers: {
'X-API-KEY': apiKey,
},
});

// Get the response data
const data = await response.json();

// Return the response with the same status code
if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}
const { status, data } = await apiClient(
request,
`/api/v1/assistant/${assistant_id}`,
);

return NextResponse.json(data, { status: 200 });
return NextResponse.json(data, { status });
} catch (error: unknown) {
console.error('Proxy error:', error);
console.error("Proxy error:", error);
return NextResponse.json(
{ error: 'Failed to forward request to backend', details: error instanceof Error ? error.message : String(error) },
{ status: 500 }
{
error: "Failed to forward request to backend",
details: error instanceof Error ? error.message : String(error),
},
{ status: 500 },
);
}
}
82 changes: 26 additions & 56 deletions app/api/collections/[collection_id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,56 @@
import { NextResponse } from 'next/server';

const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';
import { NextResponse } from "next/server";
import { apiClient } from "@/app/lib/apiClient";

// GET /api/collections/[collection_id] - Get a specific collection
export async function GET(
request: Request,
{ params }: { params: Promise<{ collection_id: string }> }
{ params }: { params: Promise<{ collection_id: string }> },
) {
const { collection_id } = await params;
const apiKey = request.headers.get('X-API-KEY');

if (!apiKey) {
return NextResponse.json(
{ error: 'Missing X-API-KEY header' },
{ status: 401 }
);
}

try {
const response = await fetch(
`${backendUrl}/api/v1/collections/${collection_id}?include_docs=true&include_url=true`,
{
headers: {
'X-API-KEY': apiKey,
},
}
const { status, data } = await apiClient(
request,
`/api/v1/collections/${collection_id}?include_docs=true&include_url=true`,
);

const text = await response.text();
const data = text ? JSON.parse(text) : {};

if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}

return NextResponse.json(data, { status: response.status });
return NextResponse.json(data, { status });
} catch (error: unknown) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : String(error), data: null },
{ status: 500 }
{
success: false,
error: error instanceof Error ? error.message : String(error),
data: null,
},
{ status: 500 },
);
}
}

// DELETE /api/collection/[collection_id] - Delete a collection
export async function DELETE(
request: Request,
{ params }: { params: Promise<{ collection_id: string }> }
{ params }: { params: Promise<{ collection_id: string }> },
) {
const { collection_id } = await params;
const apiKey = request.headers.get('X-API-KEY');

if (!apiKey) {
return NextResponse.json(
{ error: 'Missing X-API-KEY header' },
{ status: 401 }
);
}

try {
const response = await fetch(
`${backendUrl}/api/v1/collections/${collection_id}`,
const { status, data } = await apiClient(
request,
`/api/v1/collections/${collection_id}`,
{
method: 'DELETE',
headers: {
'X-API-KEY': apiKey,
},
}
method: "DELETE",
},
);

const text = await response.text();
const data = text ? JSON.parse(text) : { success: true };

if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}

return NextResponse.json(data, { status: response.status });
return NextResponse.json(data ?? { success: true }, { status });
} catch (error: unknown) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : String(error), data: null },
{ status: 500 }
{
success: false,
error: error instanceof Error ? error.message : String(error),
data: null,
},
{ status: 500 },
);
}
}
42 changes: 13 additions & 29 deletions app/api/collections/jobs/[job_id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
import { NextResponse } from 'next/server';

const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';
import { NextResponse } from "next/server";
import { apiClient } from "@/app/lib/apiClient";

// GET /api/collections/jobs/[job_id] - Get collection job status
export async function GET(
request: Request,
{ params }: { params: Promise<{ job_id: string }> }
{ params }: { params: Promise<{ job_id: string }> },
) {
const { job_id } = await params;
const apiKey = request.headers.get('X-API-KEY');

if (!apiKey) {
return NextResponse.json(
{ error: 'Missing X-API-KEY header' },
{ status: 401 }
);
}

try {
const response = await fetch(
`${backendUrl}/api/v1/collections/jobs/${job_id}`,
{
headers: {
'X-API-KEY': apiKey,
},
}
const { status, data } = await apiClient(
request,
`/api/v1/collections/jobs/${job_id}`,
);

const text = await response.text();
const data = text ? JSON.parse(text) : {};

if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}

return NextResponse.json(data, { status: response.status });
return NextResponse.json(data, { status });
} catch (error: unknown) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : String(error), data: null },
{ status: 500 }
{
success: false,
error: error instanceof Error ? error.message : String(error),
data: null,
},
{ status: 500 },
);
}
}
81 changes: 19 additions & 62 deletions app/api/collections/route.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,41 @@
import { NextRequest, NextResponse } from 'next/server';

import { NextRequest, NextResponse } from "next/server";
import { apiClient } from "@/app/lib/apiClient";

export async function GET(request: Request) {
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';
const apiKey = request.headers.get('X-API-KEY');

if (!apiKey) {
return NextResponse.json(
{ error: 'Missing X-API-KEY header' },
{ status: 401 }
);
}

try {
const response = await fetch(`${backendUrl}/api/v1/collections/`, {
headers: {
'X-API-KEY': apiKey,
},
});

// Handle empty responses (204 No Content, etc.)
const text = await response.text();
const data = text ? JSON.parse(text) : [];

if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}

return NextResponse.json(data, { status: response.status });
const { status, data } = await apiClient(request, "/api/v1/collections/");
return NextResponse.json(data, { status });
} catch (error: unknown) {
return NextResponse.json(
{ success: false, error: error instanceof Error ? error.message : String(error), data: null },
{ status: 500 }
{
success: false,
error: error instanceof Error ? error.message : String(error),
data: null,
},
{ status: 500 },
);
}
}

export async function POST(request: NextRequest) {
try {
// Get the API key from request headers
const apiKey = request.headers.get('X-API-KEY');

if (!apiKey) {
return NextResponse.json(
{ error: 'Missing X-API-KEY header' },
{ status: 401 }
);
}

// Get the JSON body from the request
const body = await request.json();

// Get backend URL from environment variable
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8000';

// Forward the request to the actual backend
const response = await fetch(`${backendUrl}/api/v1/collections/`, {
method: 'POST',
const { status, data } = await apiClient(request, "/api/v1/collections/", {
method: "POST",
body: JSON.stringify(body),
headers: {
'X-API-KEY': apiKey,
'Content-Type': 'application/json',
},
});

// Handle empty responses (204 No Content, etc.)
const text = await response.text();
const data = text ? JSON.parse(text) : { success: true };

// Return the response with the same status code
if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}

return NextResponse.json(data, { status: response.status });
return NextResponse.json(data, { status });
} catch (error: unknown) {
console.error('Proxy error:', error);
console.error("Proxy error:", error);
return NextResponse.json(
{ error: 'Failed to forward request to backend', details: error instanceof Error ? error.message : String(error) },
{ status: 500 }
{
error: "Failed to forward request to backend",
details: error instanceof Error ? error.message : String(error),
},
{ status: 500 },
);
}
}
Loading
Loading