|
| 1 | +xquery version "3.1"; |
| 2 | + |
| 3 | +module namespace rh = "http://localhost/manuForma/request-helper"; |
| 4 | + |
| 5 | +import module namespace request = "http://exist-db.org/xquery/request"; |
| 6 | + |
| 7 | +(:~ |
| 8 | + : Get a HTTP request parameter. |
| 9 | + : Unlike request:get-parameter this will ignore parameters that have an empty string value. |
| 10 | + : |
| 11 | + : @param $name the name of the HTTP request parameter |
| 12 | + : |
| 13 | + : @return the value of the parameter if it is valid, or the empty sequence. |
| 14 | + :) |
| 15 | +declare function rh:request-param($name as xs:string) { |
| 16 | + rh:request-param($name, ()) |
| 17 | +}; |
| 18 | + |
| 19 | +(:~ |
| 20 | + : Get a HTTP request parameter. |
| 21 | + : Unlike request:get-parameter this will ignore parameters that have an empty string value, and return the default. |
| 22 | + : |
| 23 | + : @param $name the name of the HTTP request parameter |
| 24 | + : @param $default the default value to return if there is no valid parameter |
| 25 | + : |
| 26 | + : @return the value of the parameter if it is valid, or the default. |
| 27 | + :) |
| 28 | +declare function rh:request-param($name as xs:string, $default) { |
| 29 | + let $values := request:get-parameter($name, ())[. ne ""] |
| 30 | + return |
| 31 | + if (exists($values)) then |
| 32 | + $values |
| 33 | + else |
| 34 | + $default |
| 35 | +}; |
| 36 | + |
| 37 | +(:~ |
| 38 | + : Get a HTTP request parameter as a boolean value. |
| 39 | + : Unlike request:get-parameter this will ignore parameters that have an empty string value. |
| 40 | + : |
| 41 | + : @param $name the name of the HTTP request parameter |
| 42 | + : |
| 43 | + : @return the boolean value of the parameter. |
| 44 | + :) |
| 45 | +declare function rh:request-param-bool($name as xs:string) as xs:boolean { |
| 46 | + lower-case(rh:request-param($name, "false")[1]) eq "true" |
| 47 | +}; |
| 48 | + |
| 49 | +(:~ |
| 50 | + : Get HTTP request parameters. |
| 51 | + : Unlike request:get-parameter this will ignore parameters that have an empty string value. |
| 52 | + : |
| 53 | + : @param $name-prefix the name prefix of the HTTP request parameters |
| 54 | + : |
| 55 | + : @return a map or the names and values of the parameters if valid, or the empty sequence. |
| 56 | + :) |
| 57 | +declare function rh:request-params($name-prefix as xs:string) as map(xs:string, xs:string*)* { |
| 58 | + rh:request-params($name-prefix, ()) |
| 59 | +}; |
| 60 | + |
| 61 | +(:~ |
| 62 | + : Get HTTP request parameters. |
| 63 | + : Unlike request:get-parameter this will ignore parameters that have an empty string value, and return the default. |
| 64 | + : |
| 65 | + : @param $name-prefix the name prefix of the HTTP request parameters |
| 66 | + : @param $default the default value to return if there is no valid parameter |
| 67 | + : |
| 68 | + : @return a map or the names and values of the parameters if valid, or the default. |
| 69 | + :) |
| 70 | +declare function rh:request-params($name-prefix as xs:string, $default-value) as map(xs:string, xs:string*)* { |
| 71 | + for $name in request:get-parameter-names()[starts-with(., $name-prefix)] |
| 72 | + let $value := rh:request-param($name, $default-value) |
| 73 | + return |
| 74 | + if (exists($value)) then |
| 75 | + map { "name": $name, "value": $value } |
| 76 | + else |
| 77 | + () |
| 78 | +}; |
0 commit comments