route.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ResultDto } from '@/types/response/common';
  3. import { fetchJson } from '@/lib/utils/server';
  4. export async function GET(request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
  5. const { path } = await params;
  6. const endpoint = `/api/store/${path.join('/')}`;
  7. const url = new URL(request.url);
  8. const res: ResultDto = await fetchJson(`${endpoint}${url.search}`, { method: 'GET' });
  9. return NextResponse.json(res);
  10. }
  11. export async function POST(request: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
  12. const { path } = await params;
  13. const endpoint = `/api/store/${path.join('/')}`;
  14. const contentType = request.headers.get('content-type') || '';
  15. const raw = await request.arrayBuffer();
  16. if (raw.byteLength > 0) {
  17. const res: ResultDto = await fetchJson(endpoint, {
  18. method: 'POST',
  19. body: raw,
  20. headers: contentType ? { 'Content-Type': contentType } : undefined
  21. });
  22. return NextResponse.json(res);
  23. }
  24. const res: ResultDto = await fetchJson(endpoint, { method: 'POST' });
  25. return NextResponse.json(res);
  26. }
  27. export async function DELETE(_: NextRequest, { params }: { params: Promise<{ path: string[] }> }) {
  28. const { path } = await params;
  29. const endpoint = `/api/store/${path.join('/')}`;
  30. const res: ResultDto = await fetchJson(endpoint, { method: 'DELETE' });
  31. return NextResponse.json(res);
  32. }