11module Cqrs.Command exposing
2- ( StandardRequestConfig , TaskRequestConfig , CommandResponse
3- , request, requestWithConfig , requestTask, requestTaskWithConfig
2+ ( StandardRequestConfiguration , TaskRequestConfiguration , RequestSettings , RequestWithConfigurationSettings , RequestTaskSettings , RequestTaskWithConfigurationSettings , CommandResponse
3+ , request, requestWithConfiguration , requestTask, requestTaskWithConfiguration
44 , succeed, fail, fromResult, fromRemoteData
55 , reason
66 , mapError, unwrap, unpack, partition, toResult, toMaybe
@@ -10,12 +10,12 @@ module Cqrs.Command exposing
1010
1111{- |
1212
13- @docs StandardRequestConfig, TaskRequestConfig , CommandResponse
13+ @docs StandardRequestConfiguration, TaskRequestConfiguration, RequestSettings, RequestWithConfigurationSettings, RequestTaskSettings, RequestTaskWithConfigurationSettings , CommandResponse
1414
1515
1616## HTTP
1717
18- @docs request, requestWithConfig , requestTask, requestTaskWithConfig
18+ @docs request, requestWithConfiguration , requestTask, requestTaskWithConfiguration
1919
2020
2121## Constructors
@@ -55,7 +55,7 @@ import Task exposing (Task)
5555
5656{- | Configuration for requests resulting in a `Cmd`
5757-}
58- type alias StandardRequestConfig =
58+ type alias StandardRequestConfiguration =
5959 { headers : List Header
6060 , timeout : Maybe Float
6161 , tracker : Maybe String
@@ -65,86 +65,134 @@ type alias StandardRequestConfig =
6565
6666{- | Configuration for requests resulting in a `Task`
6767-}
68- type alias TaskRequestConfig =
68+ type alias TaskRequestConfiguration =
6969 { headers : List Header
7070 , timeout : Maybe Float
7171 , risky : Bool
7272 }
7373
7474
75+ {- | Configuration settings for a `Cqrs.Command.request`
76+ -}
77+ type alias RequestSettings issue error msg =
78+ { body : Json . Encode . Value
79+ , defaultError : error
80+ , errorMapper : issue -> error
81+ , toError : Decoder issue
82+ , toMsg : CommandResponse error -> msg
83+ , url : String
84+ }
85+
86+
87+ {- | Configuration settings for a `Cqrs.Command.requestWithConfig`
88+ -}
89+ type alias RequestWithConfigurationSettings issue error msg =
90+ { body : Json . Encode . Value
91+ , config : StandardRequestConfiguration
92+ , defaultError : error
93+ , errorMapper : issue -> error
94+ , toError : Decoder issue
95+ , toMsg : CommandResponse error -> msg
96+ , url : String
97+ }
98+
99+
100+ {- | Configuration settings for a `Cqrs.Command.requestTask`
101+ -}
102+ type alias RequestTaskSettings issue error =
103+ { body : Json . Encode . Value
104+ , defaultError : error
105+ , errorMapper : issue -> error
106+ , toError : Decoder issue
107+ , url : String
108+ }
109+
110+
111+ {- | Configuration settings for a `Cqrs.Command.requestTaskWithConfig`
112+ -}
113+ type alias RequestTaskWithConfigurationSettings issue error =
114+ { body : Json . Encode . Value
115+ , config : TaskRequestConfiguration
116+ , defaultError : error
117+ , errorMapper : issue -> error
118+ , toError : Decoder issue
119+ , url : String
120+ }
121+
122+
75123{- | Represents the possible states of a command.
76124-}
77- type CommandResponse e
125+ type CommandResponse error
78126 = Succeeded
79- | Failed e
127+ | Failed error
80128
81129
82- {- | Constructs the `Succeeded` variant of a `Cqrs.Command.CommandResponse e `.
130+ {- | Constructs the `Succeeded` variant of a `Cqrs.Command.CommandResponse error `.
83131
84132This is useful for testing how your UI will respond to the happy path, without actually sending a command.
85133
86134-}
87- succeed : CommandResponse e
135+ succeed : CommandResponse error
88136succeed =
89137 Succeeded
90138
91139
92- {- | Constructs the `Failed` variant of a `Cqrs.Command.CommandResponse e `.
140+ {- | Constructs the `Failed` variant of a `Cqrs.Command.CommandResponse error `.
93141
94142This is useful for testing how your UI will respond to the sad path, without actually sending a command.
95143
96144-}
97- fail : e -> CommandResponse e
145+ fail : error -> CommandResponse error
98146fail =
99147 Failed
100148
101149
102- {- | Checks if a given `Cqrs.Command.CommandResponse e ` is a `Succeeded` variant.
150+ {- | Checks if a given `Cqrs.Command.CommandResponse error ` is a `Succeeded` variant.
103151
104152 succeeded Succeeded --> True
105153
106154 succeeded (Failed "reason") --> False
107155
108156-}
109- succeeded : CommandResponse e -> Bool
157+ succeeded : CommandResponse error -> Bool
110158succeeded =
111159 toResult >> Result . Extra . isOk
112160
113161
114- {- | Checks if a given `Cqrs.Command.CommandResponse e ` is a `Failed` variant.
162+ {- | Checks if a given `Cqrs.Command.CommandResponse error ` is a `Failed` variant.
115163
116164 failed Succeeded --> False
117165
118166 failed (Failed "reason") --> True
119167
120168-}
121- failed : CommandResponse e -> Bool
169+ failed : CommandResponse error -> Bool
122170failed =
123171 toResult >> Result . Extra . isErr
124172
125173
126- {- | If a given `Cqrs.Command.CommandResponse e ` was unsuccessful, the reason for the failure will be returned.
174+ {- | If a given `Cqrs.Command.CommandResponse error ` was unsuccessful, the reason for the failure will be returned.
127175
128176 reason Succeeded --> Nothing
129177
130178 reason (Failed "reason") --> Just "reason"
131179
132180-}
133- reason : CommandResponse e -> Maybe e
181+ reason : CommandResponse error -> Maybe error
134182reason =
135183 toResult >> Result . Extra . error
136184
137185
138- {- | Decodes a given payload into a `Cqrs.Command.CommandResponse e `.
186+ {- | Decodes a given payload into a `Cqrs.Command.CommandResponse error `.
139187-}
140- decoder : Decoder e -> Decoder (CommandResponse e )
188+ decoder : Decoder error -> Decoder (CommandResponse error )
141189decoder errorFn =
142190 let
143- error : Decoder ( CommandResponse e )
191+ error : Decoder ( CommandResponse error )
144192 error =
145193 Json . Decode . map Failed <| Json . Decode . at [ " error" ] errorFn
146194
147- success : Decoder ( CommandResponse e )
195+ success : Decoder ( CommandResponse error )
148196 success =
149197 Json . Decode . succeed Succeeded
150198 in
@@ -154,54 +202,54 @@ decoder errorFn =
154202 ]
155203
156204
157- {- | Sends a command to the given URL with the provided body and returns a parsed `Cqrs.Command.CommandResponse e ` in turn.
205+ {- | Sends a command to the given URL with the provided body and returns a parsed `Cqrs.Command.CommandResponse error ` in turn.
158206-}
159- request : String -> ( e -> f ) -> f -> Json . Encode . Value -> Json . Decode . Decoder e -> ( CommandResponse f -> msg ) -> Cmd msg
160- request url errorMapper defaultError body toError toMsg =
207+ request : RequestSettings issue error msg -> Cmd msg
208+ request { body , defaultError, errorMapper , toError, toMsg, url } =
161209 RemoteData . Http . post url ( fromRemoteData errorMapper defaultError >> toMsg) ( decoder toError) body
162210
163211
164212{- | Similar to `request` but a `StandardRequestConfig` can be passed in for cases where customisation of the request headers, etc are desired.
165213-}
166- requestWithConfig : StandardRequestConfig -> String -> ( e -> f ) -> f -> Json . Encode . Value -> Json . Decode . Decoder e -> ( CommandResponse f -> msg ) -> Cmd msg
167- requestWithConfig config url errorMapper defaultError body toError toMsg =
214+ requestWithConfiguration : RequestWithConfigurationSettings issue error msg -> Cmd msg
215+ requestWithConfiguration { body , config , defaultError, errorMapper , toError, toMsg, url } =
168216 RemoteData . Http . postWithConfig config url ( fromRemoteData errorMapper defaultError >> toMsg) ( decoder toError) body
169217
170218
171- {- | Sends a command to the given URL and returns a `Task` containing the parsed `Cqrs.Command.CommandResponse e `.
172- The value contained within the `Cqrs.Command.CommandResponse e `, will be the value parsed via the provided decoder.
219+ {- | Sends a command to the given URL and returns a `Task` containing the parsed `Cqrs.Command.CommandResponse error `.
220+ The value contained within the `Cqrs.Command.CommandResponse error `, will be the value parsed via the provided decoder.
173221-}
174- requestTask : String -> ( e -> f ) -> f -> Json . Encode . Value -> Json . Decode . Decoder e -> Task () (CommandResponse f )
175- requestTask url errorMapper defaultError body toError =
222+ requestTask : RequestTaskSettings issue error -> Task () (CommandResponse error )
223+ requestTask { body , defaultError, errorMapper , toError, url } =
176224 RemoteData . Http . postTask url ( decoder toError) body
177225 |> Task . map ( fromRemoteData errorMapper defaultError)
178226
179227
180228{- | Similar to `requestTask` but a `TaskRequestConfig` can be passed in for cases where customisation of the request headers, etc are desired.
181229-}
182- requestTaskWithConfig : TaskRequestConfig -> String -> ( e -> f ) -> f -> Json . Encode . Value -> Json . Decode . Decoder e -> Task () (CommandResponse f )
183- requestTaskWithConfig config url errorMapper defaultError body toError =
230+ requestTaskWithConfiguration : RequestTaskWithConfigurationSettings issue error -> Task () (CommandResponse error )
231+ requestTaskWithConfiguration { body , config , defaultError, errorMapper , toError, url } =
184232 RemoteData . Http . postTaskWithConfig config url ( decoder toError) body
185233 |> Task . map ( fromRemoteData errorMapper defaultError)
186234
187235
188- {- | Converts a given `RemoteData x (CommandResponse e )` into a `Cqrs.Command.CommandResponse e `.
236+ {- | Converts a given `RemoteData x (CommandResponse issue )` into a `Cqrs.Command.CommandResponse error `.
189237-}
190- fromRemoteData : (e -> f ) -> f -> RemoteData x (CommandResponse e ) -> CommandResponse f
238+ fromRemoteData : (issue -> error ) -> error -> RemoteData x (CommandResponse issue ) -> CommandResponse error
191239fromRemoteData errorMapper defaultError response =
192240 RemoteData . unwrap ( fail defaultError) ( mapError errorMapper) response
193241
194242
195- {- | Maps the `Failed` variant of a given `Cqrs.Command.CommandResponse e `.
243+ {- | Maps the `Failed` variant of a given `Cqrs.Command.CommandResponse error `.
196244-}
197- mapError : (e -> f ) -> CommandResponse e -> CommandResponse f
245+ mapError : (error -> nextError ) -> CommandResponse error -> CommandResponse nextError
198246mapError fn =
199247 toResult >> Result . mapError fn >> fromResult
200248
201249
202- {- | Converts a `Cqrs.Command.CommandResponse e ` to a `Result e a `
250+ {- | Converts a `Cqrs.Command.CommandResponse error ` to a `Result error data `
203251-}
204- toResult : CommandResponse e -> Result e ()
252+ toResult : CommandResponse error -> Result error ()
205253toResult response =
206254 case response of
207255 Succeeded ->
@@ -211,36 +259,36 @@ toResult response =
211259 Result . Err error
212260
213261
214- {- | Converts a `Result e a ` to a `Cqrs.Command.CommandResponse e `
262+ {- | Converts a `Result error data ` to a `Cqrs.Command.CommandResponse error `
215263-}
216- fromResult : Result e a -> CommandResponse e
264+ fromResult : Result error data -> CommandResponse error
217265fromResult result =
218266 Result . Extra . unpack fail ( always succeed) result
219267
220268
221- {- | Converts a `Cqrs.Command.CommandResponse e ` to a `Maybe a `
269+ {- | Converts a `Cqrs.Command.CommandResponse error ` to a `Maybe () `
222270-}
223- toMaybe : CommandResponse e -> Maybe ()
271+ toMaybe : CommandResponse error -> Maybe ()
224272toMaybe =
225273 toResult >> Result . toMaybe
226274
227275
228- {- | Partitions a series of `CommandResponse e ` instances into a tuple of successful and unsuccessful responses.
276+ {- | Partitions a series of `CommandResponse error ` instances into a tuple of successful and unsuccessful responses.
229277-}
230- partition : List (CommandResponse e ) -> ( List () , List e )
278+ partition : List (CommandResponse error ) -> ( List () , List error )
231279partition =
232280 List . map toResult >> Result . Extra . partition
233281
234282
235- {- | Convert a `CommandResponse e ` to an `a ` by applying either the first function if the `CommandResponse e ` is an `Failed` variant or the second function if the `CommandResponse e ` is a `Succeeded` variant.
283+ {- | Convert a `CommandResponse error ` to a `value ` by applying either the first function if the `CommandResponse error ` is an `Failed` variant or the second function if the `CommandResponse error ` is a `Succeeded` variant.
236284-}
237- unpack : (e -> b ) -> (() -> b ) -> CommandResponse e -> b
285+ unpack : (error -> value ) -> (() -> value ) -> CommandResponse error -> value
238286unpack errorFn dataFn =
239287 toResult >> Result . Extra . unpack errorFn dataFn
240288
241289
242- {- | Convert a `CommandResponse e ` to an `a ` by applying a function if the `CommandResponse e ` is a `Succeeded` variant or using the provided default value if it is an `Failed` variant.
290+ {- | Convert a `CommandResponse error ` to a `value ` by applying a function if the `CommandResponse error ` is a `Succeeded` variant or using the provided default value if it is an `Failed` variant.
243291-}
244- unwrap : b -> (() -> b ) -> CommandResponse e -> b
292+ unwrap : value -> (() -> value ) -> CommandResponse error -> value
245293unwrap default dataFn =
246294 toResult >> Result . Extra . unwrap default dataFn
0 commit comments