$app/server
import {
import command
command,
import form
form,
function getRequestEvent(): RequestEvent<Partial<Record<string, string>>, string | null>
Returns the current RequestEvent
. Can be used inside server hooks, server load
functions, actions, and endpoints (and functions called by them).
In environments without AsyncLocalStorage
, this must be called synchronously (i.e. not after an await
).
getRequestEvent,
import prerender
prerender,
import query
query,
function read(asset: string): Response
Read the contents of an imported asset from the filesystem
read
} from '$app/server';
command
自 2.27 起可用
¥Available since 2.27
创建远程命令。从浏览器调用时,该函数将通过 fetch
调用在服务器上执行。
¥Creates a remote command. When called from the browser, the function will be invoked on the server via a fetch
call.
完整文档请参见 远程函数。
¥See Remote functions for full documentation.
function command<Output>(
fn: () => Output
): RemoteCommand<void, Output>;
function command<Input, Output>(
validate: 'unchecked',
fn: (arg: Input) => Output
): RemoteCommand<Input, Output>;
function command<Schema extends StandardSchemaV1, Output>(
validate: Schema,
fn: (arg: StandardSchemaV1.InferOutput<Schema>) => Output
): RemoteCommand<
StandardSchemaV1.InferOutput<Schema>,
Output
>;
form
自 2.27 起可用
¥Available since 2.27
创建一个可展开到 <form>
元素上的表单对象。
¥Creates a form object that can be spread onto a <form>
element.
完整文档请参见 远程函数。
¥See Remote functions for full documentation.
function form<T>(
fn: (data: FormData) => MaybePromise<T>
): RemoteForm<T>;
getRequestEvent
自可用以来 2.20.0
¥Available since 2.20.0
返回当前 RequestEvent
。可在服务器钩子、服务器 load
函数、操作和端点(以及它们调用的函数)中使用。
¥Returns the current RequestEvent
. Can be used inside server hooks, server load
functions, actions, and endpoints (and functions called by them).
在没有 AsyncLocalStorage
的环境中,必须同步调用此函数(即不在 await
之后)。
¥In environments without AsyncLocalStorage
, this must be called synchronously (i.e. not after an await
).
function getRequestEvent(): RequestEvent<
AppLayoutParams<'/'>,
any
>;
prerender
自 2.27 起可用
¥Available since 2.27
创建一个远程预渲染函数。从浏览器调用时,该函数将通过 fetch
调用在服务器上执行。
¥Creates a remote prerender function. When called from the browser, the function will be invoked on the server via a fetch
call.
完整文档请参见 远程函数。
¥See Remote functions for full documentation.
function prerender<Output>(
fn: () => MaybePromise<Output>,
options?:
| {
inputs?: RemotePrerenderInputsGenerator<void>;
dynamic?: boolean;
}
| undefined
): RemotePrerenderFunction<void, Output>;
function prerender<Input, Output>(
validate: 'unchecked',
fn: (arg: Input) => MaybePromise<Output>,
options?:
| {
inputs?: RemotePrerenderInputsGenerator<Input>;
dynamic?: boolean;
}
| undefined
): RemotePrerenderFunction<Input, Output>;
function prerender<Schema extends StandardSchemaV1, Output>(
schema: Schema,
fn: (
arg: StandardSchemaV1.InferOutput<Schema>
) => MaybePromise<Output>,
options?:
| {
inputs?: RemotePrerenderInputsGenerator<
StandardSchemaV1.InferOutput<Schema>
>;
dynamic?: boolean;
}
| undefined
): RemotePrerenderFunction<
StandardSchemaV1.InferOutput<Schema>,
Output
>;
query
自 2.27 起可用
¥Available since 2.27
创建远程查询。从浏览器调用时,该函数将通过 fetch
调用在服务器上执行。
¥Creates a remote query. When called from the browser, the function will be invoked on the server via a fetch
call.
完整文档请参见 远程函数。
¥See Remote functions for full documentation.
function query<Output>(
fn: () => MaybePromise<Output>
): RemoteQueryFunction<void, Output>;
function query<Input, Output>(
validate: 'unchecked',
fn: (arg: Input) => MaybePromise<Output>
): RemoteQueryFunction<Input, Output>;
function query<Schema extends StandardSchemaV1, Output>(
schema: Schema,
fn: (
arg: StandardSchemaV1.InferOutput<Schema>
) => MaybePromise<Output>
): RemoteQueryFunction<
StandardSchemaV1.InferOutput<Schema>,
Output
>;
read
自 2.4.0 起可用
¥Available since 2.4.0
从文件系统读取导入资源的内容。
¥Read the contents of an imported asset from the filesystem
import { function read(asset: string): Response
Read the contents of an imported asset from the filesystem
read } from '$app/server';
import const somefile: string
somefile from './somefile.txt';
const const asset: Response
asset = function read(asset: string): Response
Read the contents of an imported asset from the filesystem
read(const somefile: string
somefile);
const const text: string
text = await const asset: Response
asset.Body.text(): Promise<string>
text();
function read(asset: string): Response;