refactor(categories): change controller route from 'api/categories' to 'categories'
fix(products): update category fetch logic to ensure data is an array refactor(products): simplify GET handler by integrating authentication directly
This commit is contained in:
@@ -2,7 +2,7 @@ import { Controller, Get } from '@nestjs/common';
|
|||||||
import { CategoriesService } from './categories.service';
|
import { CategoriesService } from './categories.service';
|
||||||
import { Public } from '../auth/decorators/public.decorator';
|
import { Public } from '../auth/decorators/public.decorator';
|
||||||
|
|
||||||
@Controller('api/categories')
|
@Controller('categories')
|
||||||
export class CategoriesController {
|
export class CategoriesController {
|
||||||
constructor(private readonly categoriesService: CategoriesService) {}
|
constructor(private readonly categoriesService: CategoriesService) {}
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,9 @@ export default function EditProductForm({ product }: Props) {
|
|||||||
if (isOpen && categoryTree.length === 0) {
|
if (isOpen && categoryTree.length === 0) {
|
||||||
fetch('/api/categories')
|
fetch('/api/categories')
|
||||||
.then((r) => r.json())
|
.then((r) => r.json())
|
||||||
.then((data: CategoryNode[]) => setCategoryTree(data))
|
.then((data: unknown) => {
|
||||||
|
if (Array.isArray(data)) setCategoryTree(data as CategoryNode[]);
|
||||||
|
})
|
||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
}, [isOpen]);
|
}, [isOpen]);
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { getAuthHeaders } from '../../../lib/auth-headers';
|
import { auth } from '../../../auth';
|
||||||
|
|
||||||
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
|
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export const GET = auth(async function GET(req) {
|
||||||
const authHeaders = await getAuthHeaders();
|
const token = (req.auth as any)?.accessToken as string | undefined;
|
||||||
const res = await fetch(`${API_BASE}/api/products`, {
|
if (!token) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
method: 'GET',
|
|
||||||
headers: { ...authHeaders },
|
const url = new URL(req.url);
|
||||||
|
const query = url.searchParams.toString();
|
||||||
|
const res = await fetch(`${API_BASE}/api/products${query ? `?${query}` : ''}`, {
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
return NextResponse.json(data, { status: res.status });
|
return NextResponse.json(data, { status: res.status });
|
||||||
}
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user