|
| 1 | +import { CodeBlock } from "../../components/CodeBlock.tsx"; |
| 2 | +import { CodeWindow } from "../../components/CodeWindow.tsx"; |
| 3 | +import { PageSection } from "../../components/PageSection.tsx"; |
| 4 | +import { SideBySide } from "../../components/SideBySide.tsx"; |
| 5 | +import { SectionHeading } from "../../components/homepage/SectionHeading.tsx"; |
| 6 | +import { FancyLink } from "../../components/FancyLink.tsx"; |
| 7 | + |
| 8 | +const apiCode = `import { createDefine } from "fresh"; |
| 9 | +const define = createDefine(); |
| 10 | +
|
| 11 | +export const handlers = define.handlers({ |
| 12 | + GET(ctx) { |
| 13 | + const user = db.getUser(ctx.params.id); |
| 14 | + return Response.json(user); |
| 15 | + }, |
| 16 | + POST(ctx) { |
| 17 | + const body = await ctx.req.json(); |
| 18 | + const user = db.updateUser(ctx.params.id, body); |
| 19 | + return Response.json(user); |
| 20 | + }, |
| 21 | + DELETE(ctx) { |
| 22 | + db.deleteUser(ctx.params.id); |
| 23 | + return new Response(null, { status: 204 }); |
| 24 | + }, |
| 25 | +});`; |
| 26 | + |
| 27 | +export function APIRoutesSection() { |
| 28 | + return ( |
| 29 | + <PageSection> |
| 30 | + <SideBySide mdColSplit="3/2" lgColSplit="3/2" reverseOnDesktop class="!items-start"> |
| 31 | + <div class="flex flex-col gap-4 md:sticky md:top-4"> |
| 32 | + <svg |
| 33 | + aria-hidden="true" |
| 34 | + xmlns="http://www.w3.org/2000/svg" |
| 35 | + class="icon icon-tabler icon-tabler-api text-fresh" |
| 36 | + width="4rem" |
| 37 | + height="4rem" |
| 38 | + viewBox="0 0 24 24" |
| 39 | + stroke-width="1.5" |
| 40 | + stroke="currentColor" |
| 41 | + fill="none" |
| 42 | + stroke-linecap="round" |
| 43 | + stroke-linejoin="round" |
| 44 | + > |
| 45 | + <title>API routes icon</title> |
| 46 | + <path stroke="none" d="M0 0h24v24H0z" fill="none" /> |
| 47 | + <path d="M4 13h5" /> |
| 48 | + <path d="M12 16v-8h3a2 2 0 0 1 2 2v1a2 2 0 0 1 -2 2h-3" /> |
| 49 | + <path d="M20 8v8" /> |
| 50 | + <path d="M9 16v-5.5a2.5 2.5 0 0 0 -5 0v5.5" /> |
| 51 | + </svg> |
| 52 | + <SectionHeading>Handlers for every method</SectionHeading> |
| 53 | + <p> |
| 54 | + Define{" "} |
| 55 | + <code class="bg-gray-100 px-1.5 py-0.5 rounded text-sm font-mono"> |
| 56 | + GET |
| 57 | + </code>,{" "} |
| 58 | + <code class="bg-gray-100 px-1.5 py-0.5 rounded text-sm font-mono"> |
| 59 | + POST |
| 60 | + </code>,{" "} |
| 61 | + <code class="bg-gray-100 px-1.5 py-0.5 rounded text-sm font-mono"> |
| 62 | + DELETE |
| 63 | + </code>{" "} |
| 64 | + — any HTTP method as a named handler on your route. Fresh maps |
| 65 | + requests to the right function automatically, with full type safety. |
| 66 | + </p> |
| 67 | + <FancyLink href="/docs/concepts/routing" class="mt-2"> |
| 68 | + Learn about handlers |
| 69 | + </FancyLink> |
| 70 | + </div> |
| 71 | + <div class="flex flex-col gap-4"> |
| 72 | + <CodeWindow name="routes/users/[id].tsx"> |
| 73 | + <CodeBlock code={apiCode} lang="js" /> |
| 74 | + </CodeWindow> |
| 75 | + </div> |
| 76 | + </SideBySide> |
| 77 | + </PageSection> |
| 78 | + ); |
| 79 | +} |
0 commit comments