Header menu logo Falco.UnionRoutes

Extractor Module

Factory functions for creating extractors to use with EndpointConfig.

Route.endpoints needs to know how to populate each field in your route types:

  • Built-in types (Guid, string, int, bool) - automatic
  • Single-case DU wrappers (e.g., PostId of Guid) - automatic
  • Custom types - use Extractor.parser
  • PreCondition/OverridablePreCondition fields - use Extractor.precondition (covers both)

Example

 let config: EndpointConfig<AppError> = {
     Preconditions = [ yield! Extractor.precondition<UserId, _> requireAuth ]
     Parsers = [
         Extractor.parser<Slug> (fun s -> Ok (Slug s))                                     // no constraint
         Extractor.constrainedParser<Slug> [| RouteConstraint.Alpha |] (fun s -> Ok (Slug s))  // adds :alpha
         Extractor.typedParser<bool, Toggle> (fun b -> Ok (if b then On else Off))          // adds :bool
     ]
     MakeError = fun msg -> BadRequest msg
     CombineErrors = fun errors -> errors |> List.head
     ToErrorResponse = fun e -> Response.ofPlainText (string e)
 }
val config: 'a
union case Result.Ok: ResultValue: 'T -> Result<'T,'TError>
type bool = System.Boolean
Multiple items
module List from Microsoft.FSharp.Collections

--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T with get member IsEmpty: bool with get member Item: index: int -> 'T with get ...
val head: list: 'T list -> 'T
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String

Functions and values

Function or value Description

Extractor.constrainedParser constraints parser

Full Usage: Extractor.constrainedParser constraints parser

Parameters:
    constraints : RouteConstraint[] - ASP.NET Core route constraints to apply (e.g., Alpha).
    parser : Parser<'T> - Function that parses a string into the target type.

Returns: FieldParser A FieldParser for EndpointConfig.

Registers a string parser with explicit route constraints.

Use this when you want ASP.NET Core to validate the route parameter before your parser runs. For example, constrainedParser<Slug> [| RouteConstraint.Alpha |] slugParser adds an :alpha constraint so only alphabetic values reach the parser.

constraints : RouteConstraint[]

ASP.NET Core route constraints to apply (e.g., Alpha).

parser : Parser<'T>

Function that parses a string into the target type.

Returns: FieldParser

A FieldParser for EndpointConfig.

Extractor.parser parser

Full Usage: Extractor.parser parser

Parameters:
    parser : Parser<'T> - Function that parses a string into the type.

Returns: FieldParser A FieldParser for EndpointConfig.

Registers a parser for a custom type used in route or query parameters.

Only needed for custom types. Built-in types and single-case DU wrappers around built-in types are handled automatically.

This parser adds no route constraints. Use Extractor.constrainedParser to add constraints (e.g., :alpha), or Extractor.typedParser for typed input with implicit constraints.

parser : Parser<'T>

Function that parses a string into the type.

Returns: FieldParser

A FieldParser for EndpointConfig.

Extractor.precondition extractor

Full Usage: Extractor.precondition extractor

Parameters:
    extractor : Extractor<'T, 'E> - Async function that extracts the value from HTTP context.

Returns: PreconditionExtractor<'E> list A list of PreconditionExtractors covering both PreCondition and OverridablePreCondition.

Registers an async extractor for both PreCondition<'T> and OverridablePreCondition<'T> fields.

extractor : Extractor<'T, 'E>

Async function that extracts the value from HTTP context.

Returns: PreconditionExtractor<'E> list

A list of PreconditionExtractors covering both PreCondition and OverridablePreCondition.

Extractor.preconditionSync extractor

Full Usage: Extractor.preconditionSync extractor

Parameters:
    extractor : SyncExtractor<'T, 'E> - Synchronous function that extracts the value from HTTP context.

Returns: PreconditionExtractor<'E> list A list of PreconditionExtractors covering both PreCondition and OverridablePreCondition.

Registers a sync extractor for both PreCondition<'T> and OverridablePreCondition<'T> fields. Convenience wrapper that wraps a synchronous extractor in Task.FromResult.

extractor : SyncExtractor<'T, 'E>

Synchronous function that extracts the value from HTTP context.

Returns: PreconditionExtractor<'E> list

A list of PreconditionExtractors covering both PreCondition and OverridablePreCondition.

Extractor.typedParser parser

Full Usage: Extractor.typedParser parser

Parameters:
    parser : 'TInput -> Result<'TOutput, string> - Function that converts the pre-parsed typed value into the target type.

Returns: FieldParser A FieldParser for EndpointConfig.

Registers a typed parser where the input type determines the implicit route constraint.

The parser receives a pre-parsed typed value (not a raw string) because the ASP.NET Core route constraint guarantees the format. For example, a typedParser<bool, ToggleState> receives a bool value since the :bool constraint ensures only valid booleans reach the handler.

parser : 'TInput -> Result<'TOutput, string>

Function that converts the pre-parsed typed value into the target type.

Returns: FieldParser

A FieldParser for EndpointConfig.

Type something to start searching.