API routes
Any file in the src/routes directory with a name that matches *.api.js or *.api.ts will become an API route. Routing convention is the same as in pages:
| Module name | URL path |
|---|---|
src/routes/api/index.api.jsx | /api |
src/routes/api/users.api.jsx | /api/users |
src/routes/api/users/index.api.jsx | also /api/users |
Dynamic routes and catch-all routes are also supported (/api/user/[userId].api.js, /path/[...rest].api.js ctc.).
API route modules export method handlers that match the name of the HTTP method they handle, but in lowercase, like get, post, put, etc. Since delete is a reserved word in JavaScript, del is used instead.
Rakkas's HTTP handling is built on HatTip, a set of JavaScript libraries for building HTTP server applications that run on many platforms like Node.js, Cloudflare Workers, Vercel, Netlify, Deno, and more. API method handlers are passed a HatTip RequestContext object that represents the HTTP request. Its request property is a standard Request object. Handlers are expected to return a standard Response object (or a promise of one). Response and Request follow the Fetch API standard.
You can call the new Response constructor or use the helper functions from the @hattip/response package to build responses:
import { json } from "@hattip/response";
export function get() {
return json({ hello: "world" });
// Shorthand for:
// return new Response(JSON.stringify({ hello: "world" }), {
// headers: { "content-type": "application/json" },
// });
}
Fetching data from downstream servers
A server-side fetch implementation is available on the backend for fetching data from downstream servers.