elm-pages 3.0.0-beta.8 → 3.0.0

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 (164) hide show
  1. package/README.md +11 -2
  2. package/adapter/netlify.js +207 -0
  3. package/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +2730 -2938
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmi +0 -0
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
  6. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmo +0 -0
  7. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  9. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  10. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm.json +1 -1
  11. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1527 -422
  12. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +16840 -13653
  13. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  14. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +2 -2
  15. package/generator/dead-code-review/elm.json +9 -7
  16. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +59 -10
  17. package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +52 -36
  18. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmi +0 -0
  19. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmo +0 -0
  20. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmi +0 -0
  21. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmo +0 -0
  22. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  23. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  24. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  25. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
  26. package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1527 -422
  27. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +25118 -21832
  28. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  29. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +2 -2
  30. package/generator/review/elm.json +10 -10
  31. package/generator/src/RouteBuilder.elm +93 -128
  32. package/generator/src/SharedTemplate.elm +8 -7
  33. package/generator/src/SiteConfig.elm +3 -2
  34. package/generator/src/basepath-middleware.js +3 -3
  35. package/generator/src/build.js +147 -63
  36. package/generator/src/cli.js +292 -88
  37. package/generator/src/codegen.js +29 -27
  38. package/generator/src/compatibility-key.js +3 -0
  39. package/generator/src/compile-elm.js +43 -26
  40. package/generator/src/config.js +2 -4
  41. package/generator/src/copy-dir.js +2 -2
  42. package/generator/src/dev-server.js +159 -92
  43. package/generator/src/dir-helpers.js +9 -26
  44. package/generator/src/elm-codegen.js +5 -4
  45. package/generator/src/elm-file-constants.js +2 -3
  46. package/generator/src/error-formatter.js +12 -11
  47. package/generator/src/file-helpers.js +3 -4
  48. package/generator/src/generate-template-module-connector.js +23 -23
  49. package/generator/src/init.js +9 -8
  50. package/generator/src/pre-render-html.js +10 -13
  51. package/generator/src/render-test.js +109 -0
  52. package/generator/src/render-worker.js +25 -28
  53. package/generator/src/render.js +321 -142
  54. package/generator/src/request-cache.js +265 -162
  55. package/generator/src/resolve-elm-module.js +64 -0
  56. package/generator/src/rewrite-client-elm-json.js +6 -5
  57. package/generator/src/rewrite-elm-json-help.js +56 -0
  58. package/generator/src/rewrite-elm-json.js +17 -7
  59. package/generator/src/route-codegen-helpers.js +16 -31
  60. package/generator/src/seo-renderer.js +12 -7
  61. package/generator/src/vite-utils.js +1 -2
  62. package/generator/static-code/elm-pages.js +10 -0
  63. package/generator/static-code/hmr.js +79 -13
  64. package/generator/template/app/Api.elm +3 -2
  65. package/generator/template/app/Effect.elm +155 -0
  66. package/generator/template/app/ErrorPage.elm +49 -6
  67. package/generator/template/app/Route/Blog/Slug_.elm +86 -0
  68. package/generator/template/app/Route/Greet.elm +107 -0
  69. package/generator/template/app/Route/Hello.elm +119 -0
  70. package/generator/template/app/Route/Index.elm +26 -25
  71. package/generator/template/app/Shared.elm +38 -39
  72. package/generator/template/app/Site.elm +4 -7
  73. package/generator/template/app/View.elm +9 -8
  74. package/generator/template/codegen/elm.codegen.json +18 -0
  75. package/generator/template/custom-backend-task.ts +3 -0
  76. package/generator/template/elm-pages.config.mjs +13 -0
  77. package/generator/template/elm-tooling.json +0 -3
  78. package/generator/template/elm.json +25 -20
  79. package/generator/template/index.ts +1 -2
  80. package/generator/template/netlify.toml +4 -1
  81. package/generator/template/package.json +10 -4
  82. package/generator/template/script/.elm-pages/compiled-ports/custom-backend-task.mjs +7 -0
  83. package/generator/template/script/custom-backend-task.ts +3 -0
  84. package/generator/template/script/elm.json +61 -0
  85. package/generator/template/script/src/AddRoute.elm +312 -0
  86. package/generator/template/script/src/Stars.elm +42 -0
  87. package/package.json +30 -27
  88. package/src/ApiRoute.elm +249 -85
  89. package/src/BackendTask/Custom.elm +325 -0
  90. package/src/BackendTask/Env.elm +90 -0
  91. package/src/{DataSource → BackendTask}/File.elm +171 -56
  92. package/src/{DataSource → BackendTask}/Glob.elm +136 -125
  93. package/src/BackendTask/Http.elm +679 -0
  94. package/src/{DataSource → BackendTask}/Internal/Glob.elm +1 -1
  95. package/src/BackendTask/Internal/Request.elm +69 -0
  96. package/src/BackendTask/Random.elm +79 -0
  97. package/src/BackendTask/Time.elm +47 -0
  98. package/src/BackendTask.elm +531 -0
  99. package/src/FatalError.elm +90 -0
  100. package/src/FormData.elm +21 -18
  101. package/src/Head/Seo.elm +4 -4
  102. package/src/Head.elm +237 -7
  103. package/src/Internal/ApiRoute.elm +7 -5
  104. package/src/Internal/Request.elm +84 -4
  105. package/src/PageServerResponse.elm +6 -1
  106. package/src/Pages/ConcurrentSubmission.elm +127 -0
  107. package/src/Pages/Form.elm +340 -0
  108. package/src/Pages/FormData.elm +19 -0
  109. package/src/Pages/GeneratorProgramConfig.elm +15 -0
  110. package/src/Pages/Internal/FatalError.elm +5 -0
  111. package/src/Pages/Internal/Msg.elm +93 -0
  112. package/src/Pages/Internal/NotFoundReason.elm +4 -4
  113. package/src/Pages/Internal/Platform/Cli.elm +629 -767
  114. package/src/Pages/Internal/Platform/CompatibilityKey.elm +6 -0
  115. package/src/Pages/Internal/Platform/Effect.elm +1 -2
  116. package/src/Pages/Internal/Platform/GeneratorApplication.elm +379 -0
  117. package/src/Pages/Internal/Platform/StaticResponses.elm +65 -276
  118. package/src/Pages/Internal/Platform/ToJsPayload.elm +6 -9
  119. package/src/Pages/Internal/Platform.elm +330 -203
  120. package/src/Pages/Internal/ResponseSketch.elm +2 -2
  121. package/src/Pages/Internal/Script.elm +17 -0
  122. package/src/Pages/Internal/StaticHttpBody.elm +35 -1
  123. package/src/Pages/Manifest.elm +52 -11
  124. package/src/Pages/Navigation.elm +85 -0
  125. package/src/Pages/PageUrl.elm +26 -12
  126. package/src/Pages/ProgramConfig.elm +32 -22
  127. package/src/Pages/Script.elm +166 -0
  128. package/src/Pages/SiteConfig.elm +3 -2
  129. package/src/Pages/StaticHttp/Request.elm +2 -2
  130. package/src/Pages/StaticHttpRequest.elm +23 -99
  131. package/src/Pages/Url.elm +3 -3
  132. package/src/PagesMsg.elm +88 -0
  133. package/src/QueryParams.elm +21 -172
  134. package/src/RenderRequest.elm +7 -7
  135. package/src/RequestsAndPending.elm +37 -20
  136. package/src/Result/Extra.elm +26 -0
  137. package/src/Scaffold/Form.elm +569 -0
  138. package/src/Scaffold/Route.elm +1431 -0
  139. package/src/Server/Request.elm +476 -1001
  140. package/src/Server/Response.elm +130 -36
  141. package/src/Server/Session.elm +181 -111
  142. package/src/Server/SetCookie.elm +80 -32
  143. package/src/Stub.elm +53 -0
  144. package/src/Test/Html/Internal/ElmHtml/ToString.elm +8 -9
  145. package/src/{Path.elm → UrlPath.elm} +33 -36
  146. package/generator/template/public/images/icon-png.png +0 -0
  147. package/src/DataSource/Env.elm +0 -38
  148. package/src/DataSource/Http.elm +0 -446
  149. package/src/DataSource/Internal/Request.elm +0 -20
  150. package/src/DataSource/Port.elm +0 -90
  151. package/src/DataSource.elm +0 -538
  152. package/src/Form/Field.elm +0 -717
  153. package/src/Form/FieldStatus.elm +0 -36
  154. package/src/Form/FieldView.elm +0 -417
  155. package/src/Form/FormData.elm +0 -22
  156. package/src/Form/Validation.elm +0 -391
  157. package/src/Form/Value.elm +0 -118
  158. package/src/Form.elm +0 -1683
  159. package/src/FormDecoder.elm +0 -102
  160. package/src/Pages/FormState.elm +0 -256
  161. package/src/Pages/Generate.elm +0 -1151
  162. package/src/Pages/Internal/Form.elm +0 -17
  163. package/src/Pages/Msg.elm +0 -79
  164. package/src/Pages/Transition.elm +0 -70
@@ -1,20 +0,0 @@
1
- module DataSource.Internal.Request exposing (request)
2
-
3
- import DataSource exposing (DataSource)
4
- import DataSource.Http exposing (Body, Expect)
5
-
6
-
7
- request :
8
- { name : String
9
- , body : Body
10
- , expect : Expect a
11
- }
12
- -> DataSource a
13
- request { name, body, expect } =
14
- DataSource.Http.request
15
- { url = "elm-pages-internal://" ++ name
16
- , method = "GET"
17
- , headers = []
18
- , body = body
19
- }
20
- expect
@@ -1,90 +0,0 @@
1
- module DataSource.Port exposing (get)
2
-
3
- {-|
4
-
5
- @docs get
6
-
7
- -}
8
-
9
- import DataSource
10
- import DataSource.Http
11
- import DataSource.Internal.Request
12
- import Json.Decode exposing (Decoder)
13
- import Json.Encode as Encode
14
-
15
-
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.
17
-
18
- With `DataSource.Port`, you send and receive JSON to JavaScript running in NodeJS during build-time. This means that you can call shell scripts, or run NPM packages that are installed, or anything else you could do with NodeJS.
19
-
20
- A `DataSource.Port` will call an async JavaScript function with the given name. The function receives the input JSON value, and the Decoder is used to decode the return value of the async function.
21
-
22
- Here is the Elm code and corresponding JavaScript definition for getting an environment variable (or a build error if it isn't found).
23
-
24
- import DataSource exposing (DataSource)
25
- import DataSource.Port
26
- import Json.Encode
27
- import OptimizedDecoder as Decode
28
-
29
- data : DataSource String
30
- data =
31
- DataSource.Port.get "environmentVariable"
32
- (Json.Encode.string "EDITOR")
33
- Decode.string
34
-
35
- -- will resolve to "VIM" if you run `EDITOR=vim elm-pages dev`
36
-
37
- ```javascript
38
- const kleur = require("kleur");
39
-
40
-
41
- module.exports =
42
- /**
43
- * @param { unknown } fromElm
44
- * @returns { Promise<unknown> }
45
- */
46
- {
47
- environmentVariable: async function (name) {
48
- const result = process.env[name];
49
- if (result) {
50
- return result;
51
- } else {
52
- throw `No environment variable called ${kleur
53
- .yellow()
54
- .underline(name)}\n\nAvailable:\n\n${Object.keys(process.env).join(
55
- "\n"
56
- )}`;
57
- }
58
- },
59
- }
60
- ```
61
-
62
-
63
- ## Error Handling
64
-
65
- `port-data-source.js`
66
-
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
- 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
- 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
-
71
-
72
- ## Performance
73
-
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
-
76
- -}
77
- get : String -> Encode.Value -> Decoder b -> DataSource.DataSource b
78
- get portName input 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
- }
@@ -1,538 +0,0 @@
1
- module DataSource exposing
2
- ( DataSource
3
- , map, succeed, fail
4
- , fromResult
5
- , andThen, resolve, combine
6
- , andMap
7
- , map2, map3, map4, map5, map6, map7, map8, map9
8
- )
9
-
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
11
- when the page's HTML is pre-rendered during the build step. You can also access the resolved data in `head` to use it for the page's SEO meta tags.
12
-
13
- A `DataSource` lets you pull in data from:
14
-
15
- - Local files ([`DataSource.File`](DataSource-File))
16
- - HTTP requests ([`DataSource.Http`](DataSource-Http))
17
- - Globs, i.e. listing out local files based on a pattern like `content/*.txt` ([`DataSource.Glob`](DataSource-Glob))
18
- - Ports, i.e. getting JSON data from running custom NodeJS, similar to a port in a vanilla Elm app except run at build-time in NodeJS, rather than at run-time in the browser ([`DataSource.Port`](DataSource-Port))
19
- - Hardcoded data (`DataSource.succeed "Hello!"`)
20
- - Or any combination of the above, using `DataSource.map2`, `DataSource.andThen`, or other combining/continuing helpers from this module
21
-
22
-
23
- ## Where Does DataSource Data Come From?
24
-
25
- Data from a `DataSource` is resolved when you load a page in the `elm-pages` dev server, or when you run `elm-pages build`.
26
-
27
- Because `elm-pages` hydrates into a full Elm single-page app, it does need the data in order to initialize the Elm app.
28
- So why not just get the data the old-fashioned way, with `elm/http`, for example?
29
-
30
- A few reasons:
31
-
32
- 1. DataSource's allow you to pull in data that you wouldn't normally be able to access from an Elm app, like local files, or listings of files in a folder. Not only that, but the dev server knows to automatically hot reload the data when the files it depends on change, so you can edit the files you used in your DataSource and see the page hot reload as you save!
33
- 2. Because `elm-pages` has a build step, you know that your `DataSource.Http` requests succeeded, your decoders succeeded, your custom DataSource validations succeeded, and everything went smoothly. If something went wrong, you get a build failure and can deal with the issues before the site goes live. That means your users won't see those errors, and as a developer you don't need to handle those error cases in your code! Think of it as "parse, don't validate", but for your entire build.
34
- 3. You don't have to worry about an API being down, or hitting it repeatedly. You can build in data and it will end up as JSON files served up with all the other assets of your site. If your CDN (static site host) is down, then the rest of your site is probably down anyway. If your site host is up, then so is all of your `DataSource` data. Also, it will be served up extremely quickly without needing to wait for any database queries to be performed, `andThen` requests to be resolved, etc., because all of that work and waiting was done at build-time!
35
- 4. You can pre-render pages, including the SEO meta tags, with all that rich, well-typed Elm data available! That's something you can't accomplish with a vanilla Elm app, and it's one of the main use cases for elm-pages.
36
-
37
-
38
- ## Mental Model
39
-
40
- You can think of a DataSource as a declarative (not imperative) definition of data. It represents where to get the data from, and how to transform it (map, combine with other DataSources, etc.).
41
-
42
- Even though an HTTP request is non-deterministic, you should think of it that way as much as possible with a DataSource because elm-pages will only perform a given DataSource.Http request once, and
43
- it will share the result between any other DataSource.Http requests that have the exact same URL, Method, Body, and Headers.
44
-
45
- So calling a function to increment a counter on a server through an HTTP request would not be a good fit for a `DataSource`. Let's imagine we have an HTTP endpoint that gives these stateful results when called repeatedly:
46
-
47
- <https://my-api.example.com/increment-counter>
48
- -> Returns 1
49
- <https://my-api.example.com/increment-counter>
50
- -> Returns 2
51
- <https://my-api.example.com/increment-counter>
52
- -> Returns 3
53
-
54
- If we define a `DataSource` that hits that endpoint:
55
-
56
- data =
57
- DataSource.Http.get
58
- "https://my-api.example.com/increment-counter"
59
- Decode.int
60
-
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).
62
-
63
- So even though HTTP requests, JavaScript code, etc. can be non-deterministic, a `DataSource` always represents a single snapshot of a resource, and those values will be re-used as if they were a deterministic, declarative resource.
64
- So it's best to use that mental model to avoid confusion.
65
-
66
-
67
- ## Basics
68
-
69
- @docs DataSource
70
-
71
- @docs map, succeed, fail
72
-
73
- @docs fromResult
74
-
75
-
76
- ## Chaining Requests
77
-
78
- @docs andThen, resolve, combine
79
-
80
- @docs andMap
81
-
82
- @docs map2, map3, map4, map5, map6, map7, map8, map9
83
-
84
- -}
85
-
86
- import Pages.Internal.StaticHttpBody as Body
87
- import Pages.StaticHttp.Request as HashRequest
88
- import Pages.StaticHttpRequest exposing (RawRequest(..))
89
- import RequestsAndPending exposing (RequestsAndPending)
90
-
91
-
92
- {-| A DataSource represents data that will be gathered at build time. Multiple `DataSource`s can be combined together using the `mapN` functions,
93
- very similar to how you can manipulate values with Json Decoders in Elm.
94
- -}
95
- type alias DataSource value =
96
- RawRequest value
97
-
98
-
99
- {-| Transform a request into an arbitrary value. The same underlying HTTP requests will be performed during the build
100
- step, but mapping allows you to change the resulting values by applying functions to the results.
101
-
102
- A common use for this is to map your data into your elm-pages view:
103
-
104
- import DataSource
105
- import Json.Decode as Decode exposing (Decoder)
106
-
107
- view =
108
- DataSource.Http.get
109
- (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
110
- (Decode.field "stargazers_count" Decode.int)
111
- |> DataSource.map
112
- (\stars ->
113
- { view =
114
- \model viewForPage ->
115
- { title = "Current stars: " ++ String.fromInt stars
116
- , body = Html.text <| "⭐️ " ++ String.fromInt stars
117
- , head = []
118
- }
119
- }
120
- )
121
-
122
- -}
123
- map : (a -> b) -> DataSource a -> DataSource b
124
- map fn requestInfo =
125
- case requestInfo of
126
- ApiRoute value ->
127
- ApiRoute (fn value)
128
-
129
- Request urls lookupFn ->
130
- Request
131
- urls
132
- (mapLookupFn fn lookupFn)
133
-
134
- RequestError error ->
135
- RequestError error
136
-
137
-
138
- mapLookupFn : (a -> b) -> (d -> c -> DataSource a) -> d -> c -> DataSource b
139
- mapLookupFn fn lookupFn maybeMock requests =
140
- map fn (lookupFn maybeMock requests)
141
-
142
-
143
- {-| Helper to remove an inner layer of Request wrapping.
144
- -}
145
- resolve : DataSource (List (DataSource value)) -> DataSource (List value)
146
- resolve =
147
- andThen combine
148
-
149
-
150
- {-| Turn a list of `StaticHttp.Request`s into a single one.
151
-
152
- import DataSource
153
- import Json.Decode as Decode exposing (Decoder)
154
-
155
- type alias Pokemon =
156
- { name : String
157
- , sprite : String
158
- }
159
-
160
- pokemonDetailRequest : StaticHttp.Request (List Pokemon)
161
- pokemonDetailRequest =
162
- StaticHttp.get
163
- (Secrets.succeed "https://pokeapi.co/api/v2/pokemon/?limit=3")
164
- (Decode.field "results"
165
- (Decode.list
166
- (Decode.map2 Tuple.pair
167
- (Decode.field "name" Decode.string)
168
- (Decode.field "url" Decode.string)
169
- |> Decode.map
170
- (\( name, url ) ->
171
- StaticHttp.get (Secrets.succeed url)
172
- (Decode.at
173
- [ "sprites", "front_default" ]
174
- Decode.string
175
- |> Decode.map (Pokemon name)
176
- )
177
- )
178
- )
179
- )
180
- )
181
- |> StaticHttp.andThen StaticHttp.combine
182
-
183
- -}
184
- combine : List (DataSource value) -> DataSource (List value)
185
- combine items =
186
- List.foldl (map2 (::)) (succeed []) items |> map List.reverse
187
-
188
-
189
- {-| Like map, but it takes in two `Request`s.
190
-
191
- view siteMetadata page =
192
- StaticHttp.map2
193
- (\elmPagesStars elmMarkdownStars ->
194
- { view =
195
- \model viewForPage ->
196
- { title = "Repo Stargazers"
197
- , body = starsView elmPagesStars elmMarkdownStars
198
- }
199
- , head = head elmPagesStars elmMarkdownStars
200
- }
201
- )
202
- (get
203
- (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
204
- (Decode.field "stargazers_count" Decode.int)
205
- )
206
- (get
207
- (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-markdown")
208
- (Decode.field "stargazers_count" Decode.int)
209
- )
210
-
211
- -}
212
- map2 : (a -> b -> c) -> DataSource a -> DataSource b -> DataSource c
213
- map2 fn request1 request2 =
214
- case ( request1, request2 ) of
215
- ( ApiRoute value1, ApiRoute value2 ) ->
216
- ApiRoute (fn value1 value2)
217
-
218
- ( Request urls1 lookupFn1, Request urls2 lookupFn2 ) ->
219
- Request
220
- (urls1 ++ urls2)
221
- (mapReq fn lookupFn1 lookupFn2)
222
-
223
- ( Request urls1 lookupFn1, ApiRoute value2 ) ->
224
- Request
225
- urls1
226
- (mapReq fn lookupFn1 (\_ _ -> ApiRoute value2))
227
-
228
- ( ApiRoute value2, Request urls1 lookupFn1 ) ->
229
- Request
230
- urls1
231
- (mapReq fn (\_ _ -> ApiRoute value2) lookupFn1)
232
-
233
- ( RequestError error, _ ) ->
234
- RequestError error
235
-
236
- ( _, RequestError error ) ->
237
- RequestError error
238
-
239
-
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)
245
-
246
-
247
- lookup : Maybe Pages.StaticHttpRequest.MockResolver -> DataSource value -> RequestsAndPending -> Result Pages.StaticHttpRequest.Error value
248
- lookup maybeMockResolver requestInfo rawResponses =
249
- case requestInfo of
250
- Request urls lookupFn ->
251
- lookup maybeMockResolver
252
- (addUrls urls (lookupFn maybeMockResolver rawResponses))
253
- rawResponses
254
-
255
- ApiRoute value ->
256
- Ok value
257
-
258
- RequestError error ->
259
- Err error
260
-
261
-
262
- addUrls : List HashRequest.Request -> DataSource value -> DataSource value
263
- addUrls urlsToAdd requestInfo =
264
- case requestInfo of
265
- ApiRoute value ->
266
- ApiRoute value
267
-
268
- Request initialUrls function ->
269
- Request (initialUrls ++ urlsToAdd) function
270
-
271
- RequestError error ->
272
- RequestError error
273
-
274
-
275
- {-| The full details to perform a StaticHttp request.
276
- -}
277
- type alias RequestDetails =
278
- { url : String
279
- , method : String
280
- , headers : List ( String, String )
281
- , body : Body.Body
282
- , useCache : Bool
283
- }
284
-
285
-
286
- lookupUrls : DataSource value -> List RequestDetails
287
- lookupUrls requestInfo =
288
- case requestInfo of
289
- ApiRoute _ ->
290
- []
291
-
292
- Request urls _ ->
293
- urls
294
-
295
- RequestError _ ->
296
- -- TODO should this have URLs passed through?
297
- []
298
-
299
-
300
- {-| Build off of the response from a previous `DataSource` request to build a follow-up request. You can use the data
301
- from the previous response to build up the URL, headers, etc. that you send to the subsequent request.
302
-
303
- import DataSource
304
- import Json.Decode as Decode exposing (Decoder)
305
-
306
- licenseData : DataSource String
307
- licenseData =
308
- DataSource.Http.get
309
- (Secrets.succeed "https://api.github.com/repos/dillonkearns/elm-pages")
310
- (Decode.at [ "license", "url" ] Decode.string)
311
- |> DataSource.andThen
312
- (\licenseUrl ->
313
- DataSource.Http.get (Secrets.succeed licenseUrl) (Decode.field "description" Decode.string)
314
- )
315
-
316
- -}
317
- andThen : (a -> DataSource b) -> DataSource a -> DataSource b
318
- andThen fn requestInfo =
319
- Request
320
- (lookupUrls requestInfo)
321
- (\maybeMockResolver rawResponses ->
322
- lookup maybeMockResolver
323
- requestInfo
324
- rawResponses
325
- |> (\result ->
326
- case result of
327
- Ok value ->
328
- case fn value of
329
- ApiRoute finalValue ->
330
- ApiRoute finalValue
331
-
332
- Request values function ->
333
- Request values function
334
-
335
- RequestError error ->
336
- RequestError error
337
-
338
- Err error ->
339
- RequestError error
340
- )
341
- )
342
-
343
-
344
- {-| A helper for combining `DataSource`s in pipelines.
345
- -}
346
- andMap : DataSource a -> DataSource (a -> b) -> DataSource b
347
- andMap =
348
- map2 (|>)
349
-
350
-
351
- {-| This is useful for prototyping with some hardcoded data, or for having a view that doesn't have any StaticHttp data.
352
-
353
- import DataSource
354
-
355
- view :
356
- List ( PagePath, Metadata )
357
- ->
358
- { path : PagePath
359
- , frontmatter : Metadata
360
- }
361
- ->
362
- StaticHttp.Request
363
- { view : Model -> View -> { title : String, body : Html Msg }
364
- , head : List (Head.Tag Pages.PathKey)
365
- }
366
- view siteMetadata page =
367
- StaticHttp.succeed
368
- { view =
369
- \model viewForPage ->
370
- mainView model viewForPage
371
- , head = head page.frontmatter
372
- }
373
-
374
- -}
375
- succeed : a -> DataSource a
376
- succeed value =
377
- ApiRoute value
378
-
379
-
380
- {-| Stop the StaticHttp chain with the given error message. If you reach a `fail` in your request,
381
- you will get a build error. Or in the dev server, you will see the error message in an overlay in your browser (and in
382
- the terminal).
383
- -}
384
- fail : String -> DataSource a
385
- fail errorMessage =
386
- RequestError (Pages.StaticHttpRequest.UserCalledStaticHttpFail errorMessage)
387
-
388
-
389
- {-| Turn an Err into a DataSource failure.
390
- -}
391
- fromResult : Result String value -> DataSource value
392
- fromResult result =
393
- case result of
394
- Ok okValue ->
395
- succeed okValue
396
-
397
- Err error ->
398
- fail error
399
-
400
-
401
- {-| -}
402
- map3 :
403
- (value1 -> value2 -> value3 -> valueCombined)
404
- -> DataSource value1
405
- -> DataSource value2
406
- -> DataSource value3
407
- -> DataSource valueCombined
408
- map3 combineFn request1 request2 request3 =
409
- succeed combineFn
410
- |> map2 (|>) request1
411
- |> map2 (|>) request2
412
- |> map2 (|>) request3
413
-
414
-
415
- {-| -}
416
- map4 :
417
- (value1 -> value2 -> value3 -> value4 -> valueCombined)
418
- -> DataSource value1
419
- -> DataSource value2
420
- -> DataSource value3
421
- -> DataSource value4
422
- -> DataSource valueCombined
423
- map4 combineFn request1 request2 request3 request4 =
424
- succeed combineFn
425
- |> map2 (|>) request1
426
- |> map2 (|>) request2
427
- |> map2 (|>) request3
428
- |> map2 (|>) request4
429
-
430
-
431
- {-| -}
432
- map5 :
433
- (value1 -> value2 -> value3 -> value4 -> value5 -> valueCombined)
434
- -> DataSource value1
435
- -> DataSource value2
436
- -> DataSource value3
437
- -> DataSource value4
438
- -> DataSource value5
439
- -> DataSource valueCombined
440
- map5 combineFn request1 request2 request3 request4 request5 =
441
- succeed combineFn
442
- |> map2 (|>) request1
443
- |> map2 (|>) request2
444
- |> map2 (|>) request3
445
- |> map2 (|>) request4
446
- |> map2 (|>) request5
447
-
448
-
449
- {-| -}
450
- map6 :
451
- (value1 -> value2 -> value3 -> value4 -> value5 -> value6 -> valueCombined)
452
- -> DataSource value1
453
- -> DataSource value2
454
- -> DataSource value3
455
- -> DataSource value4
456
- -> DataSource value5
457
- -> DataSource value6
458
- -> DataSource valueCombined
459
- map6 combineFn request1 request2 request3 request4 request5 request6 =
460
- succeed combineFn
461
- |> map2 (|>) request1
462
- |> map2 (|>) request2
463
- |> map2 (|>) request3
464
- |> map2 (|>) request4
465
- |> map2 (|>) request5
466
- |> map2 (|>) request6
467
-
468
-
469
- {-| -}
470
- map7 :
471
- (value1 -> value2 -> value3 -> value4 -> value5 -> value6 -> value7 -> valueCombined)
472
- -> DataSource value1
473
- -> DataSource value2
474
- -> DataSource value3
475
- -> DataSource value4
476
- -> DataSource value5
477
- -> DataSource value6
478
- -> DataSource value7
479
- -> DataSource valueCombined
480
- map7 combineFn request1 request2 request3 request4 request5 request6 request7 =
481
- succeed combineFn
482
- |> map2 (|>) request1
483
- |> map2 (|>) request2
484
- |> map2 (|>) request3
485
- |> map2 (|>) request4
486
- |> map2 (|>) request5
487
- |> map2 (|>) request6
488
- |> map2 (|>) request7
489
-
490
-
491
- {-| -}
492
- map8 :
493
- (value1 -> value2 -> value3 -> value4 -> value5 -> value6 -> value7 -> value8 -> valueCombined)
494
- -> DataSource value1
495
- -> DataSource value2
496
- -> DataSource value3
497
- -> DataSource value4
498
- -> DataSource value5
499
- -> DataSource value6
500
- -> DataSource value7
501
- -> DataSource value8
502
- -> DataSource valueCombined
503
- map8 combineFn request1 request2 request3 request4 request5 request6 request7 request8 =
504
- succeed combineFn
505
- |> map2 (|>) request1
506
- |> map2 (|>) request2
507
- |> map2 (|>) request3
508
- |> map2 (|>) request4
509
- |> map2 (|>) request5
510
- |> map2 (|>) request6
511
- |> map2 (|>) request7
512
- |> map2 (|>) request8
513
-
514
-
515
- {-| -}
516
- map9 :
517
- (value1 -> value2 -> value3 -> value4 -> value5 -> value6 -> value7 -> value8 -> value9 -> valueCombined)
518
- -> DataSource value1
519
- -> DataSource value2
520
- -> DataSource value3
521
- -> DataSource value4
522
- -> DataSource value5
523
- -> DataSource value6
524
- -> DataSource value7
525
- -> DataSource value8
526
- -> DataSource value9
527
- -> DataSource valueCombined
528
- map9 combineFn request1 request2 request3 request4 request5 request6 request7 request8 request9 =
529
- succeed combineFn
530
- |> map2 (|>) request1
531
- |> map2 (|>) request2
532
- |> map2 (|>) request3
533
- |> map2 (|>) request4
534
- |> map2 (|>) request5
535
- |> map2 (|>) request6
536
- |> map2 (|>) request7
537
- |> map2 (|>) request8
538
- |> map2 (|>) request9