elm-pages 2.1.11 → 3.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/codegen/elm-pages-codegen.js +38507 -0
  2. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmi +0 -0
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmi +0 -0
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmo +0 -0
  6. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Reporter.elmi +0 -0
  7. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Reporter.elmo +0 -0
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Runner.elmi +0 -0
  9. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Runner.elmo +0 -0
  10. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  11. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  12. package/generator/{template/public/style.css → dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/lock} +0 -0
  13. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  14. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm.json +1 -0
  15. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +6795 -0
  16. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +25651 -0
  17. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +110 -0
  18. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +187 -0
  19. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/package.json +1 -0
  20. package/generator/dead-code-review/elm-stuff/tests-0.19.1/src/Reporter.elm +26 -0
  21. package/generator/dead-code-review/elm-stuff/tests-0.19.1/src/Runner.elm +62 -0
  22. package/generator/dead-code-review/elm.json +35 -0
  23. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +181 -0
  24. package/generator/dead-code-review/src/ReviewConfig.elm +9 -0
  25. package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +455 -0
  26. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmi +0 -0
  27. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmo +0 -0
  28. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmi +0 -0
  29. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmo +0 -0
  30. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolationsTest.elmi +0 -0
  31. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolationsTest.elmo +0 -0
  32. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Reporter.elmi +0 -0
  33. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Reporter.elmo +0 -0
  34. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Runner.elmi +0 -0
  35. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Runner.elmo +0 -0
  36. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  37. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  38. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/lock +0 -0
  39. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  40. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -0
  41. package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +6795 -0
  42. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +27617 -0
  43. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +110 -0
  44. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +187 -0
  45. package/generator/review/elm-stuff/tests-0.19.1/js/package.json +1 -0
  46. package/generator/review/elm-stuff/tests-0.19.1/src/Reporter.elm +26 -0
  47. package/generator/review/elm-stuff/tests-0.19.1/src/Runner.elm +62 -0
  48. package/generator/review/elm.json +13 -4
  49. package/{src → generator/review/src}/Pages/Review/NoContractViolations.elm +148 -148
  50. package/generator/review/tests/Pages/Review/NoContractViolationsTest.elm +331 -0
  51. package/generator/src/RouteBuilder.elm +420 -0
  52. package/generator/src/SharedTemplate.elm +4 -5
  53. package/generator/src/SiteConfig.elm +3 -9
  54. package/generator/src/build.js +308 -95
  55. package/generator/src/cli.js +103 -8
  56. package/generator/src/codegen.js +192 -35
  57. package/generator/src/compile-elm.js +183 -31
  58. package/generator/src/dev-server.js +353 -96
  59. package/generator/src/elm-application.json +3 -1
  60. package/generator/src/elm-codegen.js +34 -0
  61. package/generator/src/elm-file-constants.js +2 -0
  62. package/generator/src/error-formatter.js +20 -1
  63. package/generator/src/generate-template-module-connector.js +125 -927
  64. package/generator/src/hello.ts +5 -0
  65. package/generator/src/pre-render-html.js +58 -104
  66. package/generator/src/render-worker.js +27 -13
  67. package/generator/src/render.js +252 -197
  68. package/generator/src/request-cache-fs.js +18 -0
  69. package/generator/src/request-cache.js +128 -56
  70. package/generator/src/rewrite-client-elm-json.js +49 -0
  71. package/generator/src/route-codegen-helpers.js +62 -1
  72. package/generator/static-code/dev-style.css +22 -0
  73. package/generator/static-code/elm-pages.js +43 -39
  74. package/generator/static-code/hmr.js +98 -88
  75. package/generator/template/app/Api.elm +25 -0
  76. package/generator/template/app/ErrorPage.elm +38 -0
  77. package/generator/template/app/Route/Index.elm +87 -0
  78. package/generator/template/{src → app}/Shared.elm +34 -13
  79. package/generator/template/app/Site.elm +19 -0
  80. package/generator/template/{src → app}/View.elm +0 -0
  81. package/generator/template/elm-pages.config.mjs +5 -0
  82. package/generator/template/elm.json +1 -0
  83. package/generator/template/{public/index.js → index.ts} +7 -3
  84. package/generator/template/package.json +4 -4
  85. package/generator/template/public/favicon.ico +0 -0
  86. package/generator/template/public/images/icon-png.png +0 -0
  87. package/generator/template/src/.gitkeep +0 -0
  88. package/generator/template/style.css +4 -0
  89. package/package.json +33 -23
  90. package/src/ApiRoute.elm +176 -43
  91. package/src/BuildError.elm +10 -1
  92. package/src/CookieParser.elm +84 -0
  93. package/src/DataSource/Env.elm +38 -0
  94. package/src/DataSource/File.elm +27 -16
  95. package/src/DataSource/Glob.elm +126 -80
  96. package/src/DataSource/Http.elm +283 -304
  97. package/src/DataSource/Internal/Glob.elm +5 -21
  98. package/src/DataSource/Internal/Request.elm +25 -0
  99. package/src/DataSource/Port.elm +17 -14
  100. package/src/DataSource.elm +55 -318
  101. package/src/Form/Field.elm +717 -0
  102. package/src/Form/FieldStatus.elm +36 -0
  103. package/src/Form/FieldView.elm +417 -0
  104. package/src/Form/FormData.elm +22 -0
  105. package/src/Form/Validation.elm +391 -0
  106. package/src/Form/Value.elm +118 -0
  107. package/src/Form.elm +1683 -0
  108. package/src/FormData.elm +58 -0
  109. package/src/FormDecoder.elm +102 -0
  110. package/src/Head/Seo.elm +12 -4
  111. package/src/Head.elm +12 -2
  112. package/src/HtmlPrinter.elm +1 -1
  113. package/src/Internal/ApiRoute.elm +17 -4
  114. package/src/Internal/Request.elm +7 -0
  115. package/src/PageServerResponse.elm +68 -0
  116. package/src/Pages/ContentCache.elm +1 -229
  117. package/src/Pages/Fetcher.elm +58 -0
  118. package/src/Pages/FormState.elm +256 -0
  119. package/src/Pages/Generate.elm +800 -0
  120. package/src/Pages/Internal/Form.elm +17 -0
  121. package/src/Pages/Internal/NotFoundReason.elm +3 -55
  122. package/src/Pages/Internal/Platform/Cli.elm +777 -579
  123. package/src/Pages/Internal/Platform/Effect.elm +5 -5
  124. package/src/Pages/Internal/Platform/StaticResponses.elm +178 -394
  125. package/src/Pages/Internal/Platform/ToJsPayload.elm +24 -23
  126. package/src/Pages/Internal/Platform.elm +1244 -504
  127. package/src/Pages/Internal/ResponseSketch.elm +19 -0
  128. package/src/Pages/Internal/RoutePattern.elm +596 -45
  129. package/src/Pages/Manifest.elm +26 -0
  130. package/src/Pages/Msg.elm +79 -0
  131. package/src/Pages/ProgramConfig.elm +67 -14
  132. package/src/Pages/SiteConfig.elm +3 -6
  133. package/src/Pages/StaticHttp/Request.elm +4 -2
  134. package/src/Pages/StaticHttpRequest.elm +50 -215
  135. package/src/Pages/Transition.elm +70 -0
  136. package/src/Path.elm +1 -0
  137. package/src/Pattern.elm +98 -0
  138. package/src/RenderRequest.elm +2 -2
  139. package/src/RequestsAndPending.elm +111 -9
  140. package/src/Server/Request.elm +1253 -0
  141. package/src/Server/Response.elm +292 -0
  142. package/src/Server/Session.elm +316 -0
  143. package/src/Server/SetCookie.elm +169 -0
  144. package/src/TerminalText.elm +1 -1
  145. package/src/Test/Html/Internal/ElmHtml/Markdown.elm +0 -1
  146. package/src/Test/Html/Internal/ElmHtml/ToString.elm +1 -1
  147. package/generator/src/Page.elm +0 -359
  148. package/generator/src/codegen-template-module.js +0 -183
  149. package/generator/src/elm-pages-js-minified.js +0 -1
  150. package/generator/template/src/Api.elm +0 -14
  151. package/generator/template/src/Page/Index.elm +0 -69
  152. package/generator/template/src/Site.elm +0 -41
  153. package/src/DataSource/ServerRequest.elm +0 -60
  154. package/src/Internal/OptimizedDecoder.elm +0 -18
  155. package/src/KeepOrDiscard.elm +0 -6
  156. package/src/OptimizedDecoder/Pipeline.elm +0 -335
  157. package/src/OptimizedDecoder.elm +0 -818
  158. package/src/Pages/Internal/ApplicationType.elm +0 -6
  159. package/src/Pages/Secrets.elm +0 -83
  160. package/src/Secrets.elm +0 -111
  161. package/src/SecretsDict.elm +0 -45
@@ -6,33 +6,17 @@ module DataSource.Internal.Glob exposing
6
6
  )
7
7
 
8
8
  import List.Extra
9
- import Regex exposing (Regex)
10
9
 
11
10
 
12
11
  {-| -}
13
12
  type Glob a
14
- = Glob String String (String -> List String -> ( a, List String ))
13
+ = Glob String (String -> List String -> ( a, List String ))
15
14
 
16
15
 
17
- run : String -> Glob a -> { match : a, pattern : String }
18
- run rawInput (Glob pattern regex applyCapture) =
19
- let
20
- fullRegex : String
21
- fullRegex =
22
- "^" ++ regex ++ "$"
23
-
24
- regexCaptures : List String
25
- regexCaptures =
26
- Regex.find parsedRegex rawInput
27
- |> List.concatMap .submatches
28
- |> List.map (Maybe.withDefault "")
29
-
30
- parsedRegex : Regex
31
- parsedRegex =
32
- Regex.fromString fullRegex |> Maybe.withDefault Regex.never
33
- in
16
+ run : String -> List String -> Glob a -> { match : a, pattern : String }
17
+ run rawInput captures (Glob pattern applyCapture) =
34
18
  { match =
35
- regexCaptures
19
+ captures
36
20
  |> List.reverse
37
21
  |> applyCapture rawInput
38
22
  |> Tuple.first
@@ -42,7 +26,7 @@ run rawInput (Glob pattern regex applyCapture) =
42
26
 
43
27
  {-| -}
44
28
  toPattern : Glob a -> String
45
- toPattern (Glob pattern _ _) =
29
+ toPattern (Glob pattern _) =
46
30
  pattern
47
31
 
48
32
 
@@ -0,0 +1,25 @@
1
+ module DataSource.Internal.Request exposing (request)
2
+
3
+ {-| Build a `DataSource.Http` request (analogous to [Http.request](https://package.elm-lang.org/packages/elm/http/latest/Http#request)).
4
+ This function takes in all the details to build a `DataSource.Http` request, but you can build your own simplified helper functions
5
+ with this as a low-level detail, or you can use functions like [DataSource.Http.get](#get).
6
+ -}
7
+
8
+ import DataSource exposing (DataSource)
9
+ import DataSource.Http exposing (Body, Expect)
10
+
11
+
12
+ request :
13
+ { name : String
14
+ , body : Body
15
+ , expect : Expect a
16
+ }
17
+ -> DataSource a
18
+ request { name, body, expect } =
19
+ DataSource.Http.request
20
+ { url = "elm-pages-internal://" ++ name
21
+ , method = "GET"
22
+ , headers = []
23
+ , body = body
24
+ }
25
+ expect
@@ -8,9 +8,9 @@ module DataSource.Port exposing (get)
8
8
 
9
9
  import DataSource
10
10
  import DataSource.Http
11
- import Json.Encode
12
- import OptimizedDecoder exposing (Decoder)
13
- import Secrets
11
+ import DataSource.Internal.Request
12
+ import Json.Decode exposing (Decoder)
13
+ import Json.Encode as Encode
14
14
 
15
15
 
16
16
  {-| In a vanilla Elm application, ports let you either send or receive JSON data between your Elm application and the JavaScript context in the user's browser at runtime.
@@ -64,7 +64,7 @@ module.exports =
64
64
 
65
65
  `port-data-source.js`
66
66
 
67
- Any time you throw an exception from a DataaSource.Port definition, it will result in a build error in your `elm-pages build` or dev server. In the example above, if the environment variable
67
+ Any time you throw an exception from a DataSource.Port definition, it will result in a build error in your `elm-pages build` or dev server. In the example above, if the environment variable
68
68
  is not found it will result in a build failure. Notice that the NPM package `kleur` is being used in this example to add color to the output for that build error. You can use any tool you
69
69
  prefer to add ANSI color codes within the error string in an exception and it will show up with color output in the build output and dev server.
70
70
 
@@ -74,14 +74,17 @@ prefer to add ANSI color codes within the error string in an exception and it wi
74
74
  As with any JavaScript or NodeJS code, avoid doing blocking IO operations. For example, avoid using `fs.readFileSync`, because blocking IO can slow down your elm-pages builds and dev server.
75
75
 
76
76
  -}
77
- get : String -> Json.Encode.Value -> Decoder b -> DataSource.DataSource b
77
+ get : String -> Encode.Value -> Decoder b -> DataSource.DataSource b
78
78
  get portName input decoder =
79
- DataSource.Http.request
80
- (Secrets.succeed
81
- { url = "port://" ++ portName
82
- , method = "GET"
83
- , headers = []
84
- , body = DataSource.Http.jsonBody input
85
- }
86
- )
87
- decoder
79
+ DataSource.Internal.Request.request
80
+ { name = "port"
81
+ , body =
82
+ Encode.object
83
+ [ ( "input", input )
84
+ , ( "portName", Encode.string portName )
85
+ ]
86
+ |> DataSource.Http.jsonBody
87
+ , expect =
88
+ decoder
89
+ |> DataSource.Http.expectJson
90
+ }
@@ -5,7 +5,6 @@ module DataSource exposing
5
5
  , andThen, resolve, combine
6
6
  , andMap
7
7
  , map2, map3, map4, map5, map6, map7, map8, map9
8
- , distill, validate, distillCodec, distillSerializeCodec
9
8
  )
10
9
 
11
10
  {-| In an `elm-pages` app, each page can define a value `data` which is a `DataSource` that will be resolved **before** `init` is called. That means it is also available
@@ -56,7 +55,7 @@ If we define a `DataSource` that hits that endpoint:
56
55
 
57
56
  data =
58
57
  DataSource.Http.get
59
- (Secrets.succeed "https://my-api.example.com/increment-counter")
58
+ "https://my-api.example.com/increment-counter"
60
59
  Decode.int
61
60
 
62
61
  No matter how many places we use that `DataSource`, its response will be "locked in" (let's say the response was `3`, then every page would have the same value of `3` for that request).
@@ -82,37 +81,12 @@ So it's best to use that mental model to avoid confusion.
82
81
 
83
82
  @docs map2, map3, map4, map5, map6, map7, map8, map9
84
83
 
85
-
86
- ## Optimizing Page Data
87
-
88
- Distilling data lets you reduce the amount of data loaded on the client. You can also use it to perform computations at
89
- build-time or server-request-time, store the result of the computation and then simply load that result on the client
90
- without needing redo the computation again on the client.
91
-
92
- @docs distill, validate, distillCodec, distillSerializeCodec
93
-
94
-
95
- ### Ensuring Unique Distill Keys
96
-
97
- If you use the same string key for two different distilled values that have differing encoded JSON, then you
98
- will get a build error (and an error in the dev server for that page). That means you can safely distill values
99
- and let the build command tell you about these issues if they arise.
100
-
101
84
  -}
102
85
 
103
- import Codec
104
- import Dict exposing (Dict)
105
- import Dict.Extra
106
- import Json.Decode as Decode
107
- import Json.Encode as Encode
108
- import KeepOrDiscard exposing (KeepOrDiscard)
109
- import Pages.Internal.ApplicationType as ApplicationType exposing (ApplicationType)
110
86
  import Pages.Internal.StaticHttpBody as Body
111
- import Pages.Secrets
112
87
  import Pages.StaticHttp.Request as HashRequest
113
- import Pages.StaticHttpRequest exposing (RawRequest(..), WhatToDo)
88
+ import Pages.StaticHttpRequest exposing (RawRequest(..))
114
89
  import RequestsAndPending exposing (RequestsAndPending)
115
- import Serialize
116
90
 
117
91
 
118
92
  {-| A DataSource represents data that will be gathered at build time. Multiple `DataSource`s can be combined together using the `mapN` functions,
@@ -148,219 +122,22 @@ A common use for this is to map your data into your elm-pages view:
148
122
  -}
149
123
  map : (a -> b) -> DataSource a -> DataSource b
150
124
  map fn requestInfo =
151
- -- elm-review: known-unoptimized-recursion
152
125
  case requestInfo of
153
126
  RequestError error ->
154
127
  RequestError error
155
128
 
156
- Request partiallyStripped ( urls, lookupFn ) ->
157
- Request partiallyStripped
158
- ( urls
159
- , \keepOrDiscard appType rawResponses ->
160
- map fn (lookupFn keepOrDiscard appType rawResponses)
161
- )
162
-
163
- ApiRoute stripped value ->
164
- ApiRoute stripped (fn value)
165
-
166
-
167
- dontSaveData : DataSource a -> DataSource a
168
- dontSaveData requestInfo =
169
- case requestInfo of
170
- RequestError _ ->
171
- requestInfo
172
-
173
- Request partiallyStripped ( urls, lookupFn ) ->
174
- Request partiallyStripped
175
- ( urls
176
- , \_ appType rawResponses ->
177
- lookupFn KeepOrDiscard.Discard appType rawResponses
178
- )
179
-
180
- ApiRoute _ _ ->
181
- requestInfo
182
-
183
-
184
- {-| This is the low-level `distill` function. In most cases, you'll want to use `distill` with a `Codec` from either
185
- [`miniBill/elm-codec`](https://package.elm-lang.org/packages/miniBill/elm-codec/latest/) or
186
- [`MartinSStewart/elm-serialize`](https://package.elm-lang.org/packages/MartinSStewart/elm-serialize/latest/)
187
- -}
188
- distill :
189
- String
190
- -> (raw -> Encode.Value)
191
- -> (Decode.Value -> Result String distilled)
192
- -> DataSource raw
193
- -> DataSource distilled
194
- distill uniqueKey encode decode dataSource =
195
- -- elm-review: known-unoptimized-recursion
196
- case dataSource of
197
- RequestError error ->
198
- RequestError error
199
-
200
- Request partiallyStripped ( urls, lookupFn ) ->
201
- Request partiallyStripped
202
- ( urls
203
- , \_ appType rawResponses ->
204
- case appType of
205
- ApplicationType.Browser ->
206
- rawResponses
207
- |> RequestsAndPending.get uniqueKey
208
- |> (\maybeResponse ->
209
- case maybeResponse of
210
- Just rawResponse ->
211
- rawResponse
212
- |> Decode.decodeString Decode.value
213
- |> Result.mapError Decode.errorToString
214
- |> Result.andThen decode
215
- |> Result.mapError Pages.StaticHttpRequest.DecoderError
216
- |> Result.map (Tuple.pair Dict.empty)
217
-
218
- Nothing ->
219
- Err (Pages.StaticHttpRequest.MissingHttpResponse ("distill://" ++ uniqueKey) [])
220
- )
221
- |> toResult
222
-
223
- ApplicationType.Cli ->
224
- lookupFn KeepOrDiscard.Discard appType rawResponses
225
- |> distill uniqueKey encode decode
226
- )
227
-
228
- ApiRoute strippedResponses value ->
129
+ Request urls lookupFn ->
229
130
  Request
230
- (strippedResponses
231
- |> Dict.insert
232
- -- TODO should this include a prefix? Probably.
233
- uniqueKey
234
- (Pages.StaticHttpRequest.DistilledResponse (encode value))
235
- )
236
- ( []
237
- , \_ _ _ ->
238
- value
239
- |> encode
240
- |> decode
241
- |> fromResult
242
- )
243
-
131
+ urls
132
+ (mapLookupFn fn lookupFn)
244
133
 
245
- {-| [`distill`](#distill) with a `Serialize.Codec` from [`MartinSStewart/elm-serialize`](https://package.elm-lang.org/packages/MartinSStewart/elm-serialize/latest).
246
-
247
- import DataSource
248
- import DataSource.Http
249
- import Secrets
250
- import Serialize
134
+ ApiRoute value ->
135
+ ApiRoute (fn value)
251
136
 
252
- millionRandomSum : DataSource Int
253
- millionRandomSum =
254
- DataSource.Http.get
255
- (Secrets.succeed "https://example.com/api/one-million-random-numbers.json")
256
- (Decode.list Decode.int)
257
- |> DataSource.map List.sum
258
- -- all of this expensive computation and data will happen before it hits the client!
259
- -- the user's browser simply loads up a single Int and runs an Int decoder to get it
260
- |> DataSource.distillSerializeCodec "million-random-sum" Serialize.int
261
137
 
262
- If we didn't distill the data here, then all million Ints would have to be loaded in order to load the page.
263
- The reason the data for these `DataSource`s needs to be loaded is that `elm-pages` hydrates into an Elm app. If it
264
- output only HTML then we could build the HTML and throw away the data. But we need to ensure that the hydrated Elm app
265
- has all the data that a page depends on, even if it the HTML for the page is also pre-rendered.
266
-
267
- Using a `Codec` makes it safer to distill data because you know it is reversible.
268
-
269
- -}
270
- distillSerializeCodec :
271
- String
272
- -> Serialize.Codec error value
273
- -> DataSource value
274
- -> DataSource value
275
- distillSerializeCodec uniqueKey serializeCodec =
276
- distill uniqueKey
277
- (Serialize.encodeToJson serializeCodec)
278
- (Serialize.decodeFromJson serializeCodec
279
- >> Result.mapError
280
- (\error ->
281
- case error of
282
- Serialize.DataCorrupted ->
283
- "DataCorrupted"
284
-
285
- Serialize.CustomError _ ->
286
- "CustomError"
287
-
288
- Serialize.SerializerOutOfDate ->
289
- "SerializerOutOfDate"
290
- )
291
- )
292
-
293
-
294
- {-| [`distill`](#distill) with a `Codec` from [`miniBill/elm-codec`](https://package.elm-lang.org/packages/miniBill/elm-codec/latest/).
295
-
296
- import Codec
297
- import DataSource
298
- import DataSource.Http
299
- import Secrets
300
-
301
- millionRandomSum : DataSource Int
302
- millionRandomSum =
303
- DataSource.Http.get
304
- (Secrets.succeed "https://example.com/api/one-million-random-numbers.json")
305
- (Decode.list Decode.int)
306
- |> DataSource.map List.sum
307
- -- all of this expensive computation and data will happen before it hits the client!
308
- -- the user's browser simply loads up a single Int and runs an Int decoder to get it
309
- |> DataSource.distillCodec "million-random-sum" Codec.int
310
-
311
- If we didn't distill the data here, then all million Ints would have to be loaded in order to load the page.
312
- The reason the data for these `DataSource`s needs to be loaded is that `elm-pages` hydrates into an Elm app. If it
313
- output only HTML then we could build the HTML and throw away the data. But we need to ensure that the hydrated Elm app
314
- has all the data that a page depends on, even if it the HTML for the page is also pre-rendered.
315
-
316
- Using a `Codec` makes it safer to distill data because you know it is reversible.
317
-
318
- -}
319
- distillCodec :
320
- String
321
- -> Codec.Codec value
322
- -> DataSource value
323
- -> DataSource value
324
- distillCodec uniqueKey codec =
325
- distill uniqueKey
326
- (Codec.encodeToValue codec)
327
- (Codec.decodeValue codec >> Result.mapError Decode.errorToString)
328
-
329
-
330
- toResult : Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, b ) -> RawRequest b
331
- toResult result =
332
- case result of
333
- Err error ->
334
- RequestError error
335
-
336
- Ok ( stripped, okValue ) ->
337
- ApiRoute stripped okValue
338
-
339
-
340
- {-| -}
341
- validate :
342
- (unvalidated -> validated)
343
- -> (unvalidated -> DataSource (Result String ()))
344
- -> DataSource unvalidated
345
- -> DataSource validated
346
- validate markValidated validateDataSource unvalidatedDataSource =
347
- unvalidatedDataSource
348
- |> andThen
349
- (\unvalidated ->
350
- unvalidated
351
- |> validateDataSource
352
- |> andThen
353
- (\result ->
354
- case result of
355
- Ok () ->
356
- succeed <| markValidated unvalidated
357
-
358
- Err error ->
359
- fail error
360
- )
361
- |> dontSaveData
362
- )
363
- |> dontSaveData
138
+ mapLookupFn : (a -> b) -> (d -> c -> DataSource a) -> d -> c -> DataSource b
139
+ mapLookupFn fn lookupFn maybeMock requests =
140
+ map fn (lookupFn maybeMock requests)
364
141
 
365
142
 
366
143
  {-| Helper to remove an inner layer of Request wrapping.
@@ -434,7 +211,6 @@ combine =
434
211
  -}
435
212
  map2 : (a -> b -> c) -> DataSource a -> DataSource b -> DataSource c
436
213
  map2 fn request1 request2 =
437
- -- elm-review: known-unoptimized-recursion
438
214
  case ( request1, request2 ) of
439
215
  ( RequestError error, _ ) ->
440
216
  RequestError error
@@ -442,93 +218,58 @@ map2 fn request1 request2 =
442
218
  ( _, RequestError error ) ->
443
219
  RequestError error
444
220
 
445
- ( Request newDict1 ( urls1, lookupFn1 ), Request newDict2 ( urls2, lookupFn2 ) ) ->
446
- Request (combineReducedDicts newDict1 newDict2)
447
- ( urls1 ++ urls2
448
- , \keepOrDiscard appType rawResponses ->
449
- map2 fn
450
- (lookupFn1 keepOrDiscard appType rawResponses)
451
- (lookupFn2 keepOrDiscard appType rawResponses)
452
- )
453
-
454
- ( Request dict1 ( urls1, lookupFn1 ), ApiRoute stripped2 value2 ) ->
455
- Request dict1
456
- ( urls1
457
- , \keepOrDiscard appType rawResponses ->
458
- map2 fn
459
- (lookupFn1 keepOrDiscard appType rawResponses)
460
- (ApiRoute stripped2 value2)
461
- )
462
-
463
- ( ApiRoute stripped2 value2, Request dict1 ( urls1, lookupFn1 ) ) ->
464
- Request dict1
465
- ( urls1
466
- , \keepOrDiscard appType rawResponses ->
467
- map2 fn
468
- (ApiRoute stripped2 value2)
469
- (lookupFn1 keepOrDiscard appType rawResponses)
470
- )
471
-
472
- ( ApiRoute stripped1 value1, ApiRoute stripped2 value2 ) ->
473
- ApiRoute
474
- (combineReducedDicts stripped1 stripped2)
475
- (fn value1 value2)
476
-
477
-
478
- {-| Takes two dicts representing responses, some of which have been reduced, and picks the shorter of the two.
479
- This is assuming that there are no duplicate URLs, so it can safely choose between either a raw or a reduced response.
480
- It would not work correctly if it chose between two responses that were reduced with different `Json.Decode.Exploration.Decoder`s.
481
- -}
482
- combineReducedDicts : Dict String WhatToDo -> Dict String WhatToDo -> Dict String WhatToDo
483
- combineReducedDicts dict1 dict2 =
484
- if Dict.size dict1 > Dict.size dict2 then
485
- uniqueInsertAll dict2 dict1
221
+ ( Request urls1 lookupFn1, Request urls2 lookupFn2 ) ->
222
+ Request
223
+ (urls1 ++ urls2)
224
+ (mapReq fn lookupFn1 lookupFn2)
486
225
 
487
- else
488
- uniqueInsertAll dict1 dict2
226
+ ( Request urls1 lookupFn1, ApiRoute value2 ) ->
227
+ Request
228
+ urls1
229
+ (mapReq fn lookupFn1 (\_ _ -> ApiRoute value2))
489
230
 
231
+ ( ApiRoute value2, Request urls1 lookupFn1 ) ->
232
+ Request
233
+ urls1
234
+ (mapReq fn (\_ _ -> ApiRoute value2) lookupFn1)
490
235
 
491
- uniqueInsertAll : Dict String WhatToDo -> Dict String WhatToDo -> Dict String WhatToDo
492
- uniqueInsertAll dictToDedupeMerge startingDict =
493
- Dict.foldl
494
- (\key value acc -> Dict.Extra.insertDedupe (Pages.StaticHttpRequest.merge key) key value acc)
495
- startingDict
496
- dictToDedupeMerge
236
+ ( ApiRoute value1, ApiRoute value2 ) ->
237
+ ApiRoute (fn value1 value2)
497
238
 
498
239
 
499
- lookup : KeepOrDiscard -> ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, value )
500
- lookup =
501
- lookupHelp Dict.empty
240
+ mapReq : (a -> b -> c) -> (e -> d -> DataSource a) -> (e -> d -> DataSource b) -> e -> d -> DataSource c
241
+ mapReq fn lookupFn1 lookupFn2 maybeMock rawResponses =
242
+ map2 fn
243
+ (lookupFn1 maybeMock rawResponses)
244
+ (lookupFn2 maybeMock rawResponses)
502
245
 
503
246
 
504
- lookupHelp : Dict String WhatToDo -> KeepOrDiscard -> ApplicationType -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error ( Dict String WhatToDo, value )
505
- lookupHelp strippedSoFar keepOrDiscard appType requestInfo rawResponses =
247
+ lookup : Maybe Pages.StaticHttpRequest.MockResolver -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error value
248
+ lookup maybeMockResolver requestInfo rawResponses =
506
249
  case requestInfo of
507
250
  RequestError error ->
508
251
  Err error
509
252
 
510
- Request strippedResponses ( urls, lookupFn ) ->
511
- lookupHelp (combineReducedDicts strippedResponses strippedSoFar)
512
- keepOrDiscard
513
- appType
514
- (addUrls urls (lookupFn keepOrDiscard appType rawResponses))
253
+ Request urls lookupFn ->
254
+ lookup maybeMockResolver
255
+ (addUrls urls (lookupFn maybeMockResolver rawResponses))
515
256
  rawResponses
516
257
 
517
- ApiRoute stripped value ->
518
- Ok ( combineReducedDicts stripped strippedSoFar, value )
258
+ ApiRoute value ->
259
+ Ok value
519
260
 
520
261
 
521
- addUrls : List (Pages.Secrets.Value HashRequest.Request) -> DataSource value -> DataSource value
262
+ addUrls : List HashRequest.Request -> DataSource value -> DataSource value
522
263
  addUrls urlsToAdd requestInfo =
523
264
  case requestInfo of
524
265
  RequestError error ->
525
266
  RequestError error
526
267
 
527
- Request stripped ( initialUrls, function ) ->
528
- Request stripped ( initialUrls ++ urlsToAdd, function )
268
+ Request initialUrls function ->
269
+ Request (initialUrls ++ urlsToAdd) function
529
270
 
530
- ApiRoute stripped value ->
531
- ApiRoute stripped value
271
+ ApiRoute value ->
272
+ ApiRoute value
532
273
 
533
274
 
534
275
  {-| The full details to perform a StaticHttp request.
@@ -538,20 +279,21 @@ type alias RequestDetails =
538
279
  , method : String
539
280
  , headers : List ( String, String )
540
281
  , body : Body.Body
282
+ , useCache : Bool
541
283
  }
542
284
 
543
285
 
544
- lookupUrls : DataSource value -> List (Pages.Secrets.Value RequestDetails)
286
+ lookupUrls : DataSource value -> List RequestDetails
545
287
  lookupUrls requestInfo =
546
288
  case requestInfo of
547
289
  RequestError _ ->
548
290
  -- TODO should this have URLs passed through?
549
291
  []
550
292
 
551
- Request _ ( urls, _ ) ->
293
+ Request urls _ ->
552
294
  urls
553
295
 
554
- ApiRoute _ _ ->
296
+ ApiRoute _ ->
555
297
  []
556
298
 
557
299
 
@@ -574,32 +316,27 @@ from the previous response to build up the URL, headers, etc. that you send to t
574
316
  -}
575
317
  andThen : (a -> DataSource b) -> DataSource a -> DataSource b
576
318
  andThen fn requestInfo =
577
- -- TODO should this be non-empty Dict? Or should it be passed down some other way?
578
- Request Dict.empty
579
- ( lookupUrls requestInfo
580
- , \keepOrDiscard appType rawResponses ->
581
- lookup
582
- keepOrDiscard
583
- appType
319
+ Request
320
+ (lookupUrls requestInfo)
321
+ (\maybeMockResolver rawResponses ->
322
+ lookup maybeMockResolver
584
323
  requestInfo
585
324
  rawResponses
586
325
  |> (\result ->
587
326
  case result of
588
327
  Err error ->
589
- -- TODO should I pass through strippedResponses here?
590
- --( strippedResponses, fn value )
591
328
  RequestError error
592
329
 
593
- Ok ( strippedResponses, value ) ->
330
+ Ok value ->
594
331
  case fn value of
595
- Request dict ( values, function ) ->
596
- Request (combineReducedDicts strippedResponses dict) ( values, function )
332
+ Request values function ->
333
+ Request values function
597
334
 
598
335
  RequestError error ->
599
336
  RequestError error
600
337
 
601
- ApiRoute dict finalValue ->
602
- ApiRoute (combineReducedDicts strippedResponses dict) finalValue
338
+ ApiRoute finalValue ->
339
+ ApiRoute finalValue
603
340
  )
604
341
  )
605
342
 
@@ -637,7 +374,7 @@ andMap =
637
374
  -}
638
375
  succeed : a -> DataSource a
639
376
  succeed value =
640
- ApiRoute Dict.empty value
377
+ ApiRoute value
641
378
 
642
379
 
643
380
  {-| Stop the StaticHttp chain with the given error message. If you reach a `fail` in your request,