KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

http_response のフィールドとメソッド

コントローラーやミドルウェアが作成するレスポンスです: @response()、@view()、@redirect()。

フィールドは、値についての単純な事実で、ドットを使い角括弧なしで読み取ります: $value.name。メソッドは引数を取るか処理を行い、-> で呼び出します: $value->name(arguments)。または、値をパイプで渡して呼び出します: $value |> name(arguments)。

フィールド

フィールド型説明
.bodystring本文をテキストとして返します。
.headersmapヘッダーをマップとして返します。
.is_okboolステータスが 2xx の場合に true になります。
.statusnumber200 や 404 などの HTTP ステータスコードです。
.typestringContent-Type です。

KScript.Http から

これらには <import package="KScript.Http" />import KScript.Http が必要です。KScript.Http を参照してください。

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

1つのヘッダーの値です(なければ null)。Set-Cookie が複数回設定されている場合は最初の値です。すべての値は .headers で取得できます。
例
<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

Cache-Control を設定します: ->with_cache('public, max-age=300')、または秒数で指定します: ->with_cache(300)。
例
<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

Cookie を設定し、レスポンスを返します。別の指定がない限り、Cookie は HttpOnly と SameSite=Lax になります。https では Secure になります。expires: を指定しない限り、ブラウザーのセッションの間は有効です(30d など)。
パラメーター受け取るもの
name 必須任意の値
value 必須任意の値
expires 任意duration
path 省略可能任意の値
domain 省略可能任意の値
http_only 省略可能フラグ: http_only または not http_only
secure 省略可能フラグ: secure または not secure
same_site 省略可能次のいずれかの語: lax|strict|none

引数には名前を付けられます: ->with_cookie(…, value: …, expires: …)。

例
<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

ETagを設定します。値、またはボディ(ETagがない場合)のハッシュです。etagミドルウェア(またはetag="true")を追加すると、If-None-Matchに304で応答します。
パラメーター受け取るもの
value optional任意の値
weak 任意フラグ:weak または not weak

引数には名前を付けられます: ->with_etag(…, weak: …)。

例
<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

ヘッダーを設定し、レスポンスを返します: $next($request)->with_header('X-Frame-Options', 'DENY')。Set-Cookieは、前回のクッキーを置き換えるのではなく、呼び出すたびにクッキーを追加します。

引数には名前を付けられます: ->with_header(…, value: …)。

例
<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

ステータスコードを変更してレスポンスを返します。
例
<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

Content-Typeを変更してレスポンスを返します。
例
<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

ブラウザーにCookieの削除を指示し、レスポンスを返します。設定時と同じpathとdomainを指定してください。

引数には名前を付けられます: ->without_cookie(…, path: …, domain: …)。

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

any のメソッド