Stone.jsDocs
Paradigm

Routing

Redirects & responses

A handler returns a value and the framework turns it into a response. Most of the time you return plain data and let the context shape it; when you need control, you set a status, build a response, or declare a redirect on the route itself.

#Returning data

Return an object or array and it becomes the body, serialised by the active context (JSON on an HTTP adapter, a rendered view in the browser). This is the default, and it keeps handlers platform-agnostic.

app/Tasks.tsts
@Get('/:id')
show (event: IncomingHttpEvent) {
  return this.tasks.find(event.get('id'))   // -> serialised by the context
}

#Controlling the response

When you need a specific status or headers, build a response with the HTTP layer's helpers. Throwing a domain error maps to the right status through the error handler, so the common cases stay declarative.

app/Tasks.tsts
import { jsonHttpResponse } from '@stone-js/http-core'

@Post('/')
create (event: IncomingHttpEvent) {
  const task = this.tasks.add(event.get('title'))
  return jsonHttpResponse(task, 201)         // explicit status
}

#Redirects

A route can redirect without a handler at all, using the redirect option, or a handler can return a redirect response. Use the route option for static moves (renamed paths), the response for conditional ones.

app/routes.tsts
// Static: declare it on the route, no handler needed.
@Get('/old-tasks', { redirect: '/tasks' })
legacy () {}

// Conditional: return a redirect from the handler.
@Post('/tasks')
create (event) {
  const task = this.tasks.add(event.get('title'))
  return reactRedirectResponse(`/tasks/${task.id}`)
}

#Redirect options

FormTypeDescription
redirect: '/path'stringRedirect to a path (default status 302).
redirect: { location, status }objectRedirect with an explicit status (301, 307, ...).
a redirect responsefrom the handlerBuild the redirect at runtime, based on the outcome.

Stone.js

Your app exists in every runtime. Until you run it.

An open-source project by Stone Foundation
Created by Mr. Stone (Evens Pierre)