KScript documentação
PT

Idioma

Traduzido automaticamente do inglês, que é a versão original.

Campos e métodos de http_response

Respostas geradas por controladores e middleware: @response(), @view(), @redirect().

Um campo é um dado simples sobre o valor, lido com um ponto e sem colchetes: $value.name. Um método recebe argumentos ou executa alguma ação, e é chamado com ->: $value->name(arguments), ou com o valor passado por pipe: $value |> name(arguments).

Campos

CampoTipoDescrição
.bodystringO corpo como texto.
.headersmapOs cabeçalhos como um map.
.is_okboolVerdadeiro quando o status é 2xx.
.statusnumberO código de status HTTP, como 200 ou 404.
.typestringO Content-Type.

De KScript.Http

Estes precisam de <import package="KScript.Http" />import KScript.Http. Veja KScript.Http.

#http_response->header(name) → string KScript.Http

Um cabeçalho (ou null). Para Set-Cookie definido mais de uma vez, o primeiro; .headers tem todos.
Exemplo
<let name="response" value="@redirect('/login')" />
<echo>{ $response.status } { $response->header('Location') }</echo>
$response = @redirect('/login')
echo "{ $response.status } { $response->header('Location') }"

#http_response->with_cache(directives) → http_response KScript.Http

Define o Cache-Control: ->with_cache('public, max-age=300'), ou um número de segundos: ->with_cache(300).
Exemplo
<fn name="index" params="request">
  <return value="@view('posts/index')->with_cache(300)" />
</fn>
fn index(request) {
  return @view('posts/index')->with_cache(300)
}

#http_response->with_cookie(name, value, expires?: duration, path?, domain?, http_only?: bool, secure?: bool, same_site?: lax|strict|none) → http_response KScript.Http

Define um cookie e retorna a resposta. O cookie é HttpOnly e SameSite=Lax, a menos que se indique o contrário, Secure em https, e dura durante a sessão do navegador, a menos que expires: seja informado (como 30d).
ParâmetroRecebe
name obrigatórioqualquer valor
value obrigatórioqualquer valor
expires opcionalduração
path opcionalqualquer valor
domain opcionalqualquer valor
http_only opcionaluma flag: http_only ou not http_only
secure opcionaluma flag: secure ou not secure
same_site opcionaluma destas palavras: lax|strict|none

Os argumentos podem ser nomeados: ->with_cookie(…, value: …, expires: …).

Exemplo
<fn name="remember_theme" params="request">
  <return value="@redirect('/')->with_cookie('theme', 'dark', expires: 30d)" />
</fn>
fn remember_theme(request) {
  return @redirect('/')->with_cookie('theme', 'dark', expires: 30d)
}

#http_response->with_etag(value?, weak?: bool) → http_response KScript.Http

Define o ETag: um hash do valor, ou do corpo sem ele. Adicione o middleware etag (ou etag="true") para responder a If-None-Match com 304.
ParâmetroRecebe
value opcionalqualquer valor
weak opcionaluma flag: weak ou not weak

Os argumentos podem ser nomeados: ->with_etag(…, weak: …).

Exemplo
<fn name="show" params="request">
  <return value="@response({ id: 1, title: 'Hello' })->with_etag()" />
</fn>
fn show(request) {
  return @response({ id: 1, title: 'Hello' })->with_etag()
}

#http_response->with_header(name, value) → http_response KScript.Http

Define um cabeçalho e retorna a resposta: $next($request)->with_header('X-Frame-Options', 'DENY'). Set-Cookie adiciona um cookie a cada vez, em vez de substituir o anterior.

Os argumentos podem ser nomeados: ->with_header(…, value: …).

Exemplo
<fn name="secure_headers" params="request, next">
  <return value="$next($request)->with_header('X-Frame-Options', 'DENY')" />
</fn>
fn secure_headers(request, next) {
  return $next($request)->with_header('X-Frame-Options', 'DENY')
}

#http_response->with_status(code) → http_response KScript.Http

Altera o código de status e retorna a resposta.
Exemplo
<fn name="store" params="request">
  <return value="@response({ saved: true })->with_status(201)" />
</fn>
fn store(request) {
  return @response({ saved: true })->with_status(201)
}

#http_response->with_type(type) → http_response KScript.Http

Altera o Content-Type e retorna a resposta.
Exemplo
<fn name="export" params="request">
  <return value="@response('name,email')->with_type('text/csv')" />
</fn>
fn export(request) {
  return @response('name,email')->with_type('text/csv')
}

#http_response->without_cookie(name, path?, domain?) → http_response KScript.Http

Instrui o navegador a excluir um cookie e retorna a resposta. Informe o path e o domain com que ele foi definido.

Os argumentos podem ser nomeados: ->without_cookie(…, path: …, domain: …).

Exemplo
<fn name="forget_theme" params="request">
  <return value="@redirect('/')->without_cookie('theme')" />
</fn>
fn forget_theme(request) {
  return @redirect('/')->without_cookie('theme')
}

Métodos de qualquer tipo