Route API
Route input
The route input
is the object received by any route handler.
ts
import { Response, route, Status } from "svarta";
// prettier-ignore
export default route
.handle(async (routeInput) => {
console.log(routeInput);
return new Response(Status.Ok, "Hello world!");
});
import { Response, route, Status } from "svarta";
// prettier-ignore
export default route
.handle(async (routeInput) => {
console.log(routeInput);
return new Response(Status.Ok, "Hello world!");
});
ctx
User-provided context. This can be set by using middlewares.
ts
import { Response, route, Status } from "svarta";
const myRoute = route.middleware(async () => {
return {
message: "Hello world!",
};
});
export default myRoute.handle(async ({ ctx }) => {
return new Response(Status.Ok, ctx.message);
});
import { Response, route, Status } from "svarta";
const myRoute = route.middleware(async () => {
return {
message: "Hello world!",
};
});
export default myRoute.handle(async ({ ctx }) => {
return new Response(Status.Ok, ctx.message);
});
path
Full path without the query parameters.
Example: /user?id=3
=> /user
ts
import { Response, route, Status } from "svarta";
export default route.handle(async ({ path }) => {
console.log(path); // => "/"
return new Response(Status.Ok, "Hello world!");
});
import { Response, route, Status } from "svarta";
export default route.handle(async ({ path }) => {
console.log(path); // => "/"
return new Response(Status.Ok, "Hello world!");
});
url
Full path with query parameters.
Example: /user?id=3
=> /user?id=3
ts
import { Response, route, Status } from "svarta";
export default route.handle(async ({ url }) => {
console.log(url);
return new Response(Status.Ok, "Hello world!");
});
import { Response, route, Status } from "svarta";
export default route.handle(async ({ url }) => {
console.log(url);
return new Response(Status.Ok, "Hello world!");
});
method
HTTP method (GET
, POST
, etc).
ts
import { Response, route, Status } from "svarta";
export default route.handle(async ({ method }) => {
console.log(method); // => "GET"
return new Response(Status.Ok, "Hello world!");
});
import { Response, route, Status } from "svarta";
export default route.handle(async ({ method }) => {
console.log(method); // => "GET"
return new Response(Status.Ok, "Hello world!");
});
headers
Can be used to get and set HTTP headers.
ts
import { Redirect, Response, route, Status } from "svarta";
import YAML from "yaml";
export default route.handle(async ({ headers }) => {
if (!headers.get("authorization")) {
return new Redirect("/", false);
}
headers.set("content-type", "application/yaml");
const body = YAML.stringify({
message: "welcome to the secret page",
});
return new Response(Status.Ok, body);
});
import { Redirect, Response, route, Status } from "svarta";
import YAML from "yaml";
export default route.handle(async ({ headers }) => {
if (!headers.get("authorization")) {
return new Redirect("/", false);
}
headers.set("content-type", "application/yaml");
const body = YAML.stringify({
message: "welcome to the secret page",
});
return new Response(Status.Ok, body);
});
input
Input body. Use route.input
to define an input, which will be automatically validated before your route handler runs. See more here.
output
Like input
, but validates the server response body. See more here.
params
Route parameters. See more here.
ts
import { Response, route, Status } from "svarta";
export const params = ["user"];
// prettier-ignore
export default route
.params(params)
.handle(async ({ params }) => {
return new Response(Status.Ok, `Hello ${params.user}`);
});
import { Response, route, Status } from "svarta";
export const params = ["user"];
// prettier-ignore
export default route
.params(params)
.handle(async ({ params }) => {
return new Response(Status.Ok, `Hello ${params.user}`);
});
query
Route query parameters.
ts
import { Response, route, Status } from "svarta";
export default route.handle(async ({ query }) => {
const result = await db.find({
query: query.q ?? "",
page: query.page ?? 0,
pageSize: 100,
});
return new Response(Status.Ok, result);
});
import { Response, route, Status } from "svarta";
export default route.handle(async ({ query }) => {
const result = await db.find({
query: query.q ?? "",
page: query.page ?? 0,
pageSize: 100,
});
return new Response(Status.Ok, result);
});
isDev
Development flag, which is true
if the app is run in development mode.
ts
import { Response, route, Status } from "svarta";
export default route.handle(async ({ isDev }) => {
if (!isDev) {
logRequest();
}
return new Response(Status.Ok, {
message: "hello",
});
});
import { Response, route, Status } from "svarta";
export default route.handle(async ({ isDev }) => {
if (!isDev) {
logRequest();
}
return new Response(Status.Ok, {
message: "hello",
});
});
cookies
Can be used to get and set HTTP cookies.
ts
import { Redirect, Response, route, Status } from "svarta";
export default route.handle(async ({ cookies }) => {
const session = cookies.get("session");
if (!session) {
return new Redirect("/", false);
}
const user = await getUser(session);
if (!user) {
return new Redirect("/", false);
}
cookies.set("session", generateSession(), {
httpOnly: true,
// additional cookie parameters here
// the options parameter is optional
});
return new Response(Status.Ok, {
message: "You are signed in",
});
});
import { Redirect, Response, route, Status } from "svarta";
export default route.handle(async ({ cookies }) => {
const session = cookies.get("session");
if (!session) {
return new Redirect("/", false);
}
const user = await getUser(session);
if (!user) {
return new Redirect("/", false);
}
cookies.set("session", generateSession(), {
httpOnly: true,
// additional cookie parameters here
// the options parameter is optional
});
return new Response(Status.Ok, {
message: "You are signed in",
});
});