Skip to main content

API Route with Trybox

import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { run } from "trybox";

export async function GET(_req: NextRequest) {
  const r = await run(
    () => fetch("https://example.com").then((x) => x.text()),
    {
      retries: 2,
      timeout: 1000,
    }
  );
  if (!r.ok)
    return NextResponse.json(
      { error: r.error },
      { status: r.error.status || 500 }
    );
  return NextResponse.json({ data: r.data });
}

Server Component with Cancellation

import { run } from "trybox";

export default async function Page() {
  const r = await run(() => fetch("https://example.com").then((x) => x.json()));
  if (!r.ok) return <div>Error</div>;
  return <pre>{JSON.stringify(r.data, null, 2)}</pre>;
}