Scoop API

scoop:scoop.http.server:0.1.0

src/extractors.scoop

Selected production source · src/extractors.scoop

1package scoop.http.server
3/** A suspending, statically typed extraction step inside the existing request scope. */
4public typealias HttpExtractor<T> = suspend (HttpContext) -> T
6/** Extracts once, then invokes the handler only after extraction succeeds. */
7public fun <T> adapt(extract: HttpExtractor<T>, handle: suspend (HttpContext, T) -> HttpResult): HttpHandler =
8 suspend { ctx -> val value = extract(ctx); handle(ctx, value) }
10/** Builds a query extraction step using an explicit record codec. */
11public fun <T> queryExtractor(codec: ValueDecoder<T>, options: QueryDecodeOptions = QueryDecodeOptions()): HttpExtractor<T> =
12 suspend { ctx -> ctx.request.query.decode(codec, options) }
14/** Builds a query extraction step using the type's companion record codec. */
15public fun <T> queryExtractor(options: QueryDecodeOptions = QueryDecodeOptions()): HttpExtractor<T>
16 where T.Companion : ValueDecoder<T> = queryExtractor(T.Companion, options)
18/** Builds a JSON extraction step using an explicit codec and optional aggregation limit. */
19public fun <T> jsonExtractor(codec: ValueDecoder<T>, maxBytes: Long? = None): HttpExtractor<T> {
20 if (present(maxBytes) && maxBytes!! < 0L) throw IllegalArgumentException(Some("negative aggregate body limit"))
21 return suspend { ctx -> ctx.request.body.json(codec, maxBytes ?: ctx.request.body.bufferedBodyLimit) }
24/** Builds a JSON extraction step using the type's companion codec. */
25public fun <T> jsonExtractor(maxBytes: Long? = None): HttpExtractor<T>
26 where T.Companion : ValueDecoder<T> = jsonExtractor(T.Companion, maxBytes)