mirror of
https://github.com/correl/elm-paginated.git
synced 2024-11-23 11:09:51 +00:00
Upgrade library to Elm 0.19
This commit is contained in:
parent
4a8cc6266b
commit
6ac7101d96
4 changed files with 52 additions and 53 deletions
|
@ -1,18 +0,0 @@
|
|||
{
|
||||
"version": "3.0.1",
|
||||
"summary": "A library for fetching data from paginated JSON REST APIs.",
|
||||
"repository": "https://github.com/correl/elm-paginated.git",
|
||||
"license": "MIT",
|
||||
"source-directories": [
|
||||
"src"
|
||||
],
|
||||
"exposed-modules": [
|
||||
"Paginated"
|
||||
],
|
||||
"dependencies": {
|
||||
"elm-community/maybe-extra": "4.0.0 <= v < 5.0.0",
|
||||
"elm-lang/core": "5.1.1 <= v < 6.0.0",
|
||||
"elm-lang/http": "1.0.0 <= v < 2.0.0"
|
||||
},
|
||||
"elm-version": "0.18.0 <= v < 0.19.0"
|
||||
}
|
20
elm.json
Normal file
20
elm.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"type": "package",
|
||||
"name": "correl/elm-paginated",
|
||||
"summary": "A library for fetching data from paginated JSON REST APIs.",
|
||||
"license": "MIT",
|
||||
"version": "3.0.1",
|
||||
"exposed-modules": [
|
||||
"Paginated"
|
||||
],
|
||||
"elm-version": "0.19.0 <= v < 0.20.0",
|
||||
"dependencies": {
|
||||
"elm/core": "1.0.0 <= v < 2.0.0",
|
||||
"elm/http": "1.0.0 <= v < 2.0.0",
|
||||
"elm/json": "1.0.0 <= v < 2.0.0",
|
||||
"elm/regex": "1.0.0 <= v < 2.0.0",
|
||||
"elm/time": "1.0.0 <= v < 2.0.0",
|
||||
"elm-community/maybe-extra": "5.0.0 <= v < 6.0.0"
|
||||
},
|
||||
"test-dependencies": {}
|
||||
}
|
|
@ -1,13 +1,9 @@
|
|||
module Paginated
|
||||
exposing
|
||||
( Request
|
||||
, RequestOptions
|
||||
, request
|
||||
, get
|
||||
, post
|
||||
, send
|
||||
, toTask
|
||||
)
|
||||
module Paginated exposing
|
||||
( Request, get, post
|
||||
, RequestOptions, request
|
||||
, send
|
||||
, toTask
|
||||
)
|
||||
|
||||
{-| A library for Facilitates fetching data from a paginated JSON API.
|
||||
|
||||
|
@ -39,7 +35,6 @@ import Http
|
|||
import Json.Decode exposing (Decoder)
|
||||
import Paginated.Util
|
||||
import Task exposing (Task)
|
||||
import Time
|
||||
|
||||
|
||||
{-| Describes an API request.
|
||||
|
@ -50,7 +45,7 @@ type alias RequestOptions a =
|
|||
, url : String
|
||||
, body : Http.Body
|
||||
, decoder : Decoder a
|
||||
, timeout : Maybe Time.Time
|
||||
, timeout : Maybe Float
|
||||
, withCredentials : Bool
|
||||
}
|
||||
|
||||
|
@ -78,7 +73,7 @@ headers and other options. For example:
|
|||
|
||||
Paginated.request
|
||||
{ method = "GET"
|
||||
, headers = [Http.header "Private-Token" "XXXXXXXXXXXXXXXX"]
|
||||
, headers = [ Http.header "Private-Token" "XXXXXXXXXXXXXXXX" ]
|
||||
, url = url
|
||||
, body = Http.emptyBody
|
||||
, decoder = decoder
|
||||
|
@ -128,8 +123,8 @@ send :
|
|||
(Result Http.Error (List a) -> msg)
|
||||
-> Request a
|
||||
-> Cmd msg
|
||||
send resultToMessage request =
|
||||
toTask request
|
||||
send resultToMessage request_ =
|
||||
toTask request_
|
||||
|> Task.attempt resultToMessage
|
||||
|
||||
|
||||
|
@ -154,8 +149,8 @@ recurse =
|
|||
Task.andThen
|
||||
(\response ->
|
||||
case response of
|
||||
Partial request _ ->
|
||||
httpRequest request
|
||||
Partial request_ _ ->
|
||||
httpRequest request_
|
||||
|> Http.toTask
|
||||
|> Task.map (update response)
|
||||
|> recurse
|
||||
|
@ -176,8 +171,8 @@ update old new =
|
|||
( Partial _ oldItems, Complete newItems ) ->
|
||||
Complete (oldItems ++ newItems)
|
||||
|
||||
( Partial _ oldItems, Partial request newItems ) ->
|
||||
Partial request (oldItems ++ newItems)
|
||||
( Partial _ oldItems, Partial request_ newItems ) ->
|
||||
Partial request_ (oldItems ++ newItems)
|
||||
|
||||
|
||||
{-| Convert a `Request` to a `Http.Request` that can then be sent via
|
||||
|
@ -212,6 +207,7 @@ fromResponse options response =
|
|||
Json.Decode.decodeString
|
||||
(Json.Decode.list options.decoder)
|
||||
response.body
|
||||
|> Result.mapError Json.Decode.errorToString
|
||||
|
||||
nextPage : Maybe String
|
||||
nextPage =
|
||||
|
@ -219,20 +215,20 @@ fromResponse options response =
|
|||
|> Maybe.map Paginated.Util.links
|
||||
|> Maybe.andThen (Dict.get "next")
|
||||
in
|
||||
case nextPage of
|
||||
Nothing ->
|
||||
Result.map Complete items
|
||||
case nextPage of
|
||||
Nothing ->
|
||||
Result.map Complete items
|
||||
|
||||
Just url ->
|
||||
Result.map
|
||||
(Partial (request { options | url = url }))
|
||||
items
|
||||
Just url ->
|
||||
Result.map
|
||||
(Partial (request { options | url = url }))
|
||||
items
|
||||
|
||||
|
||||
{-| Look up a header (case-insensitive)
|
||||
-}
|
||||
header : String -> Dict String String -> Maybe String
|
||||
header header headers =
|
||||
header name headers =
|
||||
let
|
||||
normalized =
|
||||
Dict.toList headers
|
||||
|
@ -240,6 +236,6 @@ header header headers =
|
|||
|> Dict.fromList
|
||||
|
||||
key =
|
||||
String.map Char.toLower header
|
||||
String.map Char.toLower name
|
||||
in
|
||||
Dict.get key normalized
|
||||
Dict.get key normalized
|
||||
|
|
|
@ -25,13 +25,14 @@ links s =
|
|||
_ ->
|
||||
Nothing
|
||||
in
|
||||
Regex.find Regex.All linkPattern s
|
||||
|> List.map .submatches
|
||||
|> List.map toTuples
|
||||
|> Maybe.Extra.values
|
||||
|> Dict.fromList
|
||||
Regex.find linkPattern s
|
||||
|> List.map .submatches
|
||||
|> List.map toTuples
|
||||
|> Maybe.Extra.values
|
||||
|> Dict.fromList
|
||||
|
||||
|
||||
linkPattern : Regex.Regex
|
||||
linkPattern =
|
||||
Regex.regex "<(.*?)>; rel=\"(.*?)\""
|
||||
Regex.fromString "<(.*?)>; rel=\"(.*?)\""
|
||||
|> Maybe.withDefault Regex.never
|
||||
|
|
Loading…
Reference in a new issue