scoop:scoop.http.server:0.1.0
src/extractors.scoop
Selected production source · src/extractors.scoop
1
package scoop.http.server3
/** A suspending, statically typed extraction step inside the existing request scope. */4
public typealias HttpExtractor<T> = suspend (HttpContext) -> T6
/** Extracts once, then invokes the handler only after extraction succeeds. */7
public 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. */11
public 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. */15
public 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. */19
public 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) }22
}24
/** Builds a JSON extraction step using the type's companion codec. */25
public fun <T> jsonExtractor(maxBytes: Long? = None): HttpExtractor<T>26
where T.Companion : ValueDecoder<T> = jsonExtractor(T.Companion, maxBytes)