Skip to content

Commit

Permalink
Merge pull request #21 from lukewestby/0.19
Browse files Browse the repository at this point in the history
0.19
  • Loading branch information
lukewestby authored Aug 21, 2018
2 parents 19c71ca + 7f925fd commit 4b19a72
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 238 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
elm-stuff
elm-package.json
40 changes: 0 additions & 40 deletions .travis.yml

This file was deleted.

21 changes: 3 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
# elm-http-builder

[![Build Status](https://travis-ci.org/lukewestby/elm-http-builder.svg?branch=master)](https://travis-ci.org/lukewestby/elm-http-builder)

Chainable functions for building HTTP requests.
Chainable functions for building HTTP requests

**Need help? Join the #http-builder channel in the [Elm Slack](https://elmlang.herokuapp.com)!**


> Thanks to @fredcy, @rileylark, and @etaque for the original discussion of the
API, and to @knewter for pairing and discussion on the 0.18 upgrade.

## Example

```elm
import Time
import Http
import HttpBuilder exposing (..)
import Json.Decode as Decode
Expand Down Expand Up @@ -45,14 +36,8 @@ addItem item =
|> withQueryParams [ ("hello", "world") ]
|> withHeader "X-My-Header" "Some Header Value"
|> withJsonBody (itemEncoder item)
|> withTimeout (10 * Time.second)
|> withExpect (Http.expectJson itemsDecoder)
|> withTimeout 10000
|> withExpectJson itemsDecoder
|> withCredentials
|> send handleRequestComplete
```

## Contributing

I'm happy to receive any feedback and ideas for about additional features. Any
input and pull requests are very welcome and encouraged. If you'd like to help
or have ideas, get in touch with me at @luke in the elmlang Slack!
17 changes: 0 additions & 17 deletions elm-package.json

This file was deleted.

19 changes: 19 additions & 0 deletions elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "package",
"name": "lukewestby/elm-http-builder",
"summary": "Pipeable functions for building HTTP requests",
"license": "MIT",
"version": "6.0.0",
"exposed-modules": [
"HttpBuilder"
],
"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/time": "1.0.0 <= v < 2.0.0",
"elm/url": "1.0.0 <= v < 2.0.0"
},
"test-dependencies": {}
}
64 changes: 52 additions & 12 deletions src/HttpBuilder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module HttpBuilder
, withHeaders
, withJsonBody
, withMultipartStringBody
, withQueryParam
, withQueryParams
, withStringBody
, withTimeout
Expand All @@ -43,7 +44,7 @@ configuration than what is provided by `elm-http` out of the box.
@docs withHeader, withHeaders, withBody, withStringBody, withJsonBody
@docs withMultipartStringBody, withUrlEncodedBody, withTimeout, withCredentials
@docs withQueryParams, withExpect, withCacheBuster, withExpectJson
@docs withQueryParam, withQueryParams, withExpect, withCacheBuster, withExpectJson
@docs withExpectString, withBearerToken
Expand All @@ -64,10 +65,10 @@ import Http
import Json.Decode exposing (Decoder)
import Json.Encode as Encode
import Maybe exposing (Maybe(..))
import Result exposing (Result(Err, Ok))
import String
import Task exposing (Task)
import Time exposing (Time)
import Time
import Url


{-| A type for chaining request configuration
Expand All @@ -78,7 +79,7 @@ type alias RequestBuilder a =
, url : String
, body : Http.Body
, expect : Http.Expect a
, timeout : Maybe Time
, timeout : Maybe Float
, withCredentials : Bool
, queryParams : List ( String, String )
, cacheBuster : Maybe String
Expand Down Expand Up @@ -199,7 +200,7 @@ withHeader key value builder =
withHeaders : List ( String, String ) -> RequestBuilder a -> RequestBuilder a
withHeaders headerPairs builder =
{ builder
| headers = List.map (uncurry Http.header) headerPairs ++ builder.headers
| headers = List.map (\( key, value ) -> Http.header key value) headerPairs ++ builder.headers
}


Expand Down Expand Up @@ -263,7 +264,7 @@ your type signatures.
-}
withMultipartStringBody : List ( String, String ) -> RequestBuilder a -> RequestBuilder a
withMultipartStringBody partPairs =
withBody <| Http.multipartBody <| List.map (uncurry Http.stringPart) partPairs
withBody <| Http.multipartBody <| List.map (\( key, value ) -> Http.stringPart key value) partPairs


{-| Convenience function for adding url encoded bodies
Expand All @@ -283,7 +284,7 @@ withUrlEncodedBody =
|> withTimeout (10 * Time.second)
-}
withTimeout : Time -> RequestBuilder a -> RequestBuilder a
withTimeout : Float -> RequestBuilder a -> RequestBuilder a
withTimeout timeout builder =
{ builder | timeout = Just timeout }

Expand All @@ -308,7 +309,16 @@ withCredentials builder =
-}
withExpect : Http.Expect b -> RequestBuilder a -> RequestBuilder b
withExpect expect builder =
{ builder | expect = expect }
{ method = builder.method
, headers = builder.headers
, url = builder.url
, body = builder.body
, timeout = builder.timeout
, withCredentials = builder.withCredentials
, queryParams = builder.queryParams
, cacheBuster = builder.cacheBuster
, expect = expect
}


{-| Choose a Json `Expect` for the request
Expand All @@ -319,7 +329,16 @@ withExpect expect builder =
-}
withExpectJson : Decoder b -> RequestBuilder a -> RequestBuilder b
withExpectJson decoder builder =
{ builder | expect = Http.expectJson decoder }
{ method = builder.method
, headers = builder.headers
, url = builder.url
, body = builder.body
, timeout = builder.timeout
, withCredentials = builder.withCredentials
, queryParams = builder.queryParams
, cacheBuster = builder.cacheBuster
, expect = Http.expectJson decoder
}


{-| Choose a String `Expect` for the request
Expand All @@ -330,7 +349,28 @@ withExpectJson decoder builder =
-}
withExpectString : RequestBuilder a -> RequestBuilder String
withExpectString builder =
{ builder | expect = Http.expectString }
{ method = builder.method
, headers = builder.headers
, url = builder.url
, body = builder.body
, timeout = builder.timeout
, withCredentials = builder.withCredentials
, queryParams = builder.queryParams
, cacheBuster = builder.cacheBuster
, expect = Http.expectString
}


{-| Add a query param to the url for the request
get "https://example.com/api/items/1"
|> withQueryParam "hello" "world"
-- sends a request to https://example.com/api/items/1?hello=world
-}
withQueryParam : String -> String -> RequestBuilder a -> RequestBuilder a
withQueryParam key value builder =
withQueryParams [ ( key, value ) ] builder


{-| Add some query params to the url for the request
Expand Down Expand Up @@ -433,7 +473,7 @@ toTaskWithCacheBuster paramName builder =
let
request timestamp =
builder
|> withQueryParams [ ( paramName, toString timestamp ) ]
|> withQueryParams [ ( paramName, String.fromInt (Time.posixToMillis timestamp) ) ]
|> toTaskPlain
in
Time.now |> Task.andThen request
Expand All @@ -460,7 +500,7 @@ queryPair ( key, value ) =

queryEscape : String -> String
queryEscape =
Http.encodeUri >> replace "%20" "+"
Url.percentEncode >> replace "%20" "+"


replace : String -> String -> String -> String
Expand Down
2 changes: 0 additions & 2 deletions tests/.gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions tests/Native/Polyfills.js

This file was deleted.

Loading

0 comments on commit 4b19a72

Please sign in to comment.