elm-pages 3.0.0-beta.10 → 3.0.0-beta.12

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 (78) hide show
  1. package/README.md +1 -1
  2. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  6. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm.json +1 -1
  7. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1326 -121
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +15156 -13244
  9. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  10. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  11. package/generator/dead-code-review/elm.json +6 -5
  12. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +1 -0
  13. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  14. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  15. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  16. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
  17. package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1326 -121
  18. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +14574 -12631
  19. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  20. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  21. package/generator/review/elm.json +6 -6
  22. package/generator/src/build.js +6 -9
  23. package/generator/src/cli.js +120 -42
  24. package/generator/src/codegen.js +11 -10
  25. package/generator/src/compatibility-key.js +1 -1
  26. package/generator/src/elm-codegen.js +3 -0
  27. package/generator/src/render-worker.js +1 -1
  28. package/generator/src/render.js +222 -37
  29. package/generator/src/request-cache.js +1 -0
  30. package/generator/src/rewrite-elm-json.js +3 -3
  31. package/package.json +12 -12
  32. package/src/ApiRoute.elm +147 -9
  33. package/src/DataSource/Env.elm +27 -3
  34. package/src/DataSource.elm +11 -22
  35. package/src/Form.elm +32 -32
  36. package/src/Head.elm +112 -8
  37. package/src/MultiDict.elm +49 -0
  38. package/src/Pages/Generate.elm +4 -1
  39. package/src/Pages/GeneratorProgramConfig.elm +15 -0
  40. package/src/Pages/Internal/Platform/CompatibilityKey.elm +1 -1
  41. package/src/Pages/Internal/Platform/GeneratorApplication.elm +455 -0
  42. package/src/Pages/Manifest.elm +24 -0
  43. package/src/Pages/Script.elm +100 -0
  44. package/src/PairingHeap.elm +137 -0
  45. package/src/Parser/Extra/String.elm +33 -0
  46. package/src/Parser/Extra.elm +69 -0
  47. package/src/ProgramTest/ComplexQuery.elm +360 -0
  48. package/src/ProgramTest/EffectSimulation.elm +122 -0
  49. package/src/ProgramTest/Failure.elm +367 -0
  50. package/src/ProgramTest/HtmlHighlighter.elm +116 -0
  51. package/src/ProgramTest/HtmlParserHacks.elm +58 -0
  52. package/src/ProgramTest/HtmlRenderer.elm +73 -0
  53. package/src/ProgramTest/Program.elm +30 -0
  54. package/src/ProgramTest/StringLines.elm +26 -0
  55. package/src/ProgramTest/TestHtmlHacks.elm +132 -0
  56. package/src/ProgramTest/TestHtmlParser.elm +201 -0
  57. package/src/ProgramTest.elm +2339 -0
  58. package/src/Query/Extra.elm +55 -0
  59. package/src/Result/Extra.elm +21 -0
  60. package/src/Server/Request.elm +2 -2
  61. package/src/SimulatedEffect/Cmd.elm +69 -0
  62. package/src/SimulatedEffect/Http.elm +330 -0
  63. package/src/SimulatedEffect/Navigation.elm +69 -0
  64. package/src/SimulatedEffect/Ports.elm +62 -0
  65. package/src/SimulatedEffect/Process.elm +24 -0
  66. package/src/SimulatedEffect/Sub.elm +48 -0
  67. package/src/SimulatedEffect/Task.elm +252 -0
  68. package/src/SimulatedEffect/Time.elm +25 -0
  69. package/src/SimulatedEffect.elm +42 -0
  70. package/src/String/Extra.elm +6 -0
  71. package/src/Test/Http.elm +145 -0
  72. package/src/TestResult.elm +35 -0
  73. package/src/TestState.elm +305 -0
  74. package/src/Url/Extra.elm +100 -0
  75. package/src/Vendored/Diff.elm +321 -0
  76. package/src/Vendored/Failure.elm +217 -0
  77. package/src/Vendored/FormatMonochrome.elm +44 -0
  78. package/src/Vendored/Highlightable.elm +53 -0
@@ -24,6 +24,7 @@ function fullPath(portsHash, request, hasFsAccess) {
24
24
  if (hasFsAccess) {
25
25
  return path.join(
26
26
  process.cwd(),
27
+ // TODO use parameter or something other than global for this `global.isRunningGenerator` condition
27
28
  ".elm-pages",
28
29
  "http-response-cache",
29
30
  requestToString(requestWithPortHash)
@@ -1,14 +1,14 @@
1
1
  const fs = require("fs");
2
2
 
3
- module.exports = async function () {
3
+ module.exports = async function (sourceElmJsonPath, targetElmJsonPath) {
4
4
  var elmJson = JSON.parse(
5
- (await fs.promises.readFile("./elm.json")).toString()
5
+ (await fs.promises.readFile(sourceElmJsonPath)).toString()
6
6
  );
7
7
 
8
8
  // write new elm.json
9
9
 
10
10
  await writeFileIfChanged(
11
- "./elm-stuff/elm-pages/elm.json",
11
+ targetElmJsonPath,
12
12
  JSON.stringify(rewriteElmJson(elmJson))
13
13
  );
14
14
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elm-pages",
3
- "version": "3.0.0-beta.10",
3
+ "version": "3.0.0-beta.12",
4
4
  "homepage": "https://elm-pages.com",
5
5
  "moduleResolution": "node",
6
6
  "description": "Type-safe static sites, written in pure elm with your own custom elm-markup syntax.",
@@ -26,14 +26,14 @@
26
26
  "dependencies": {
27
27
  "busboy": "^1.0.0",
28
28
  "chokidar": "^3.5.3",
29
- "commander": "^9.4.0",
29
+ "commander": "9.4.1",
30
30
  "connect": "^3.7.0",
31
31
  "cookie-signature": "^1.1.0",
32
32
  "cross-spawn": "7.0.3",
33
33
  "devcert": "^1.2.2",
34
34
  "elm-doc-preview": "^5.0.5",
35
35
  "elm-hot": "^1.1.6",
36
- "esbuild": "^0.15.5",
36
+ "esbuild": "^0.15.14",
37
37
  "fs-extra": "^10.1.0",
38
38
  "globby": "11.0.4",
39
39
  "gray-matter": "^4.0.3",
@@ -44,26 +44,26 @@
44
44
  "node-fetch": "^2.6.7",
45
45
  "object-hash": "^2.2.0",
46
46
  "serve-static": "^1.15.0",
47
- "terser": "^5.14.2",
48
- "vite": "^3.1.8",
47
+ "terser": "^5.15.1",
48
+ "vite": "^3.2.4",
49
49
  "which": "^2.0.2"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/cross-spawn": "^6.0.2",
53
53
  "@types/fs-extra": "^9.0.13",
54
54
  "@types/micromatch": "^4.0.2",
55
- "@types/node": "12.20.12",
55
+ "@types/node": "^18.11.9",
56
56
  "@types/serve-static": "^1.15.0",
57
- "cypress": "^10.6.0",
57
+ "cypress": "^11.1.0",
58
58
  "elm-codegen": "^0.2.0",
59
- "elm-optimize-level-2": "^0.1.5",
60
- "elm-review": "^2.7.4",
61
- "elm-test": "^0.19.1-revision9",
62
- "elm-tooling": "^1.8.0",
59
+ "elm-optimize-level-2": "^0.3.5",
60
+ "elm-review": "^2.8.2",
61
+ "elm-test": "^0.19.1-revision10",
62
+ "elm-tooling": "^1.10.0",
63
63
  "elm-verify-examples": "^5.2.0",
64
64
  "elmi-to-json": "^1.2.0",
65
65
  "mocha": "^10.0.0",
66
- "typescript": "^4.7.4"
66
+ "typescript": "^4.9.3"
67
67
  },
68
68
  "files": [
69
69
  "generator/src/",
package/src/ApiRoute.elm CHANGED
@@ -1,8 +1,9 @@
1
1
  module ApiRoute exposing
2
- ( ApiRoute, ApiRouteBuilder, Response
2
+ ( single, preRender
3
+ , serverRender
4
+ , preRenderWithFallback
5
+ , ApiRoute, ApiRouteBuilder, Response
3
6
  , capture, literal, slash, succeed
4
- , single, preRender
5
- , preRenderWithFallback, serverRender
6
7
  , withGlobalHeadTags
7
8
  , toJson, getBuildTimeRoutes, getGlobalHeadTagsDataSource
8
9
  )
@@ -11,19 +12,153 @@ module ApiRoute exposing
11
12
  to a DataSource so you can pull in HTTP data, etc. Because ApiRoutes don't hydrate into Elm apps (like pages in elm-pages do), you can pull in as much data as you want in
12
13
  the DataSource for your ApiRoutes, and it won't effect the payload size. Instead, the size of an ApiRoute is just the content you output for that route.
13
14
 
14
- @docs ApiRoute, ApiRouteBuilder, Response
15
-
16
- @docs capture, literal, slash, succeed
15
+ Similar to your elm-pages Route Modules, ApiRoute's can be either server-rendered or pre-rendered. Let's compare the differences between pre-rendered and server-rendered ApiRoutes, and the different
16
+ use cases they support.
17
17
 
18
18
 
19
19
  ## Pre-Rendering
20
20
 
21
+ A pre-rendered ApiRoute is just a generated file. For example:
22
+
23
+ - [An RSS feed](https://github.com/dillonkearns/elm-pages/blob/131f7b750cdefb2ba7a34a06be06dfbfafc79a86/examples/docs/app/Api.elm#L77-L84) ([Output file](https://elm-pages.com/blog/feed.xml))
24
+ - [A calendar feed in the ical format](https://github.com/dillonkearns/incrementalelm.com/blob/d4934d899d06232dc66dcf9f4b5eccc74bbc60d3/src/Api.elm#L51-L60) ([Output file](https://incrementalelm.com/live.ics))
25
+ - A redirect file for a hosting provider like Netlify
26
+
27
+ You could even generate a JavaScript file, an Elm file, or any file with a String body! It's really just a way to generate files, which are typically used to serve files to a user or Browser, but you execute them, copy them, etc. The only limit is your imagination!
28
+ The beauty is that you have a way to 1) pull in type-safe data using DataSource's, and 2) write those files, and all in pure Elm!
29
+
21
30
  @docs single, preRender
22
31
 
23
32
 
24
33
  ## Server Rendering
25
34
 
26
- @docs preRenderWithFallback, serverRender
35
+ You could use server-rendered ApiRoutes to do a lot of similar things, the main difference being that it will be served up through a URL and generated on-demand when that URL is requested.
36
+ So for example, for an RSS feed or ical calendar feed like in the pre-rendered examples, you could build the same routes, but you would be pulling in the list of posts or calendar events on-demand rather
37
+ than upfront at build-time. That means you can hit your database and serve up always-up-to-date data.
38
+
39
+ Not only that, but your server-rendered ApiRoutes have access to the incoming HTTP request payload just like your server-rendered Route Modules do. Just as with server-rendered Route Modules,
40
+ a server-rendered ApiRoute accesses the incoming HTTP request through a [Server.Request.Parser](Server-Request). Consider the use cases that this opens up:
41
+
42
+ - Serve up protected assets. For example, gated content, like a paid subscriber feed for a podcast that checks authentication information in a query parameter to authenticate that a user has an active paid subscription before serving up the Pro RSS feed.
43
+ - Serve up user-specific content, either through a cookie or other means of authentication
44
+ - Look at the [accepted content-type in the request headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) and use that to choose a response format, like XML or JSON ([full example](https://github.com/dillonkearns/elm-pages/blob/131f7b750cdefb2ba7a34a06be06dfbfafc79a86/examples/end-to-end/app/Api.elm#L76-L107)).
45
+ - Look at the [accepted language in the request headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language) and use that to choose a language for the response data.
46
+
47
+ @docs serverRender
48
+
49
+ You can also do a hybrid approach using `preRenderWithFallback`. This allows you to pre-render a set of routes at build-time, but build additional routes that weren't rendered at build-time on the fly on the server.
50
+ Conceptually, this is just a delayed version of a pre-rendered route. Because of that, you _do not_ have access to the incoming HTTP request (no `Server.Request.Parser` like in server-rendered ApiRoute's).
51
+ The strategy used to build these routes will differ depending on your hosting provider and the elm-pages adapter you have setup, but generally ApiRoute's that use `preRenderWithFallback` will be cached on the server
52
+ so within a certain time interval (or in the case of [Netlify's DPR](https://www.netlify.com/blog/2021/04/14/distributed-persistent-rendering-a-new-jamstack-approach-for-faster-builds/), until a new build is done)
53
+ that asset will be served up if that URL was already served up by the server.
54
+
55
+ @docs preRenderWithFallback
56
+
57
+
58
+ ## Defining ApiRoute's
59
+
60
+ You define your ApiRoute's in `app/Api.elm`. Here's a simple example:
61
+
62
+ module Api exposing (routes)
63
+
64
+ import ApiRoute
65
+ import DataSource exposing (DataSource)
66
+ import Server.Request
67
+
68
+ routes :
69
+ DataSource (List Route)
70
+ -> (Maybe { indent : Int, newLines : Bool } -> Html Never -> String)
71
+ -> List (ApiRoute.ApiRoute ApiRoute.Response)
72
+ routes getStaticRoutes htmlToString =
73
+ [ preRenderedExample
74
+ , requestPrinterExample
75
+ ]
76
+
77
+ {-| Generates the following files when you
78
+ run `elm-pages build`:
79
+
80
+ - `dist/users/1.json`
81
+ - `dist/users/2.json`
82
+ - `dist/users/3.json`
83
+
84
+ When you host it, these static assets will
85
+ be served at `/users/1.json`, etc.
86
+
87
+ -}
88
+ preRenderedExample : ApiRoute.ApiRoute ApiRoute.Response
89
+ preRenderedExample =
90
+ ApiRoute.succeed
91
+ (\userId ->
92
+ DataSource.succeed
93
+ (Json.Encode.object
94
+ [ ( "id", Json.Encode.string userId )
95
+ , ( "name", "Data for user " ++ userId |> Json.Encode.string )
96
+ ]
97
+ |> Json.Encode.encode 2
98
+ )
99
+ )
100
+ |> ApiRoute.literal "users"
101
+ |> ApiRoute.slash
102
+ |> ApiRoute.capture
103
+ |> ApiRoute.literal ".json"
104
+ |> ApiRoute.preRender
105
+ (\route ->
106
+ DataSource.succeed
107
+ [ route "1"
108
+ , route "2"
109
+ , route "3"
110
+ ]
111
+ )
112
+
113
+ {-| This returns a JSON response that prints information about the incoming
114
+ HTTP request. In practice you'd want to do something useful with that data,
115
+ and use more of the high-level helpers from the Server.Request API.
116
+ -}
117
+ requestPrinterExample : ApiRoute ApiRoute.Response
118
+ requestPrinterExample =
119
+ ApiRoute.succeed
120
+ (Server.Request.map4
121
+ (\rawBody method cookies queryParams ->
122
+ Encode.object
123
+ [ ( "rawBody"
124
+ , rawBody
125
+ |> Maybe.map Encode.string
126
+ |> Maybe.withDefault Encode.null
127
+ )
128
+ , ( "method"
129
+ , method
130
+ |> Server.Request.methodToString
131
+ |> Encode.string
132
+ )
133
+ , ( "cookies"
134
+ , cookies
135
+ |> Encode.dict
136
+ identity
137
+ Encode.string
138
+ )
139
+ , ( "queryParams"
140
+ , queryParams
141
+ |> Encode.dict
142
+ identity
143
+ (Encode.list Encode.string)
144
+ )
145
+ ]
146
+ |> Response.json
147
+ |> DataSource.succeed
148
+ )
149
+ Server.Request.rawBody
150
+ Server.Request.method
151
+ Server.Request.allCookies
152
+ Server.Request.queryParams
153
+ )
154
+ |> ApiRoute.literal "api"
155
+ |> ApiRoute.slash
156
+ |> ApiRoute.literal "request-test"
157
+ |> ApiRoute.serverRender
158
+
159
+ @docs ApiRoute, ApiRouteBuilder, Response
160
+
161
+ @docs capture, literal, slash, succeed
27
162
 
28
163
 
29
164
  ## Including Head Tags
@@ -54,7 +189,9 @@ type alias ApiRoute response =
54
189
  Internal.ApiRoute.ApiRoute response
55
190
 
56
191
 
57
- {-| -}
192
+ {-| Same as [`preRender`](#preRender), but for an ApiRoute that has no dynamic segments. This is just a bit simpler because
193
+ since there are no dynamic segments, you don't need to provide a DataSource with the list of dynamic segments to pre-render because there is only a single possible route.
194
+ -}
58
195
  single : ApiRouteBuilder (DataSource String) (List String) -> ApiRoute Response
59
196
  single handler =
60
197
  handler
@@ -235,7 +372,8 @@ toJson ((ApiRoute { kind }) as apiRoute) =
235
372
  ]
236
373
 
237
374
 
238
- {-| -}
375
+ {-| A literal String segment of a route.
376
+ -}
239
377
  literal : String -> ApiRouteBuilder a constructor -> ApiRouteBuilder a constructor
240
378
  literal segment (ApiRouteBuilder patterns pattern handler toString constructor) =
241
379
  ApiRouteBuilder
@@ -1,6 +1,28 @@
1
1
  module DataSource.Env exposing (get, expect)
2
2
 
3
- {-|
3
+ {-| Because DataSource's in `elm-pages` never run in the browser (see [the DataSource docs](DataSource)), you can access environment variables securely. As long as the environment variable isn't sent
4
+ down into the final `Data` value, it won't end up in the client!
5
+
6
+ import DataSource exposing (DataSource)
7
+ import DataSource.Env
8
+
9
+ type alias EnvVariables =
10
+ { sendGridKey : String
11
+ , siteUrl : String
12
+ }
13
+
14
+ sendEmail : Email -> DataSource ()
15
+ sendEmail email =
16
+ DataSource.map2 EnvVariables
17
+ (DataSource.Env.expect "SEND_GRID_KEY")
18
+ (DataSource.Env.get "BASE_URL"
19
+ |> DataSource.map (Maybe.withDefault "http://localhost:1234")
20
+ )
21
+ |> DataSource.andThen (sendEmailDataSource email)
22
+
23
+ sendEmailDataSource : Email -> EnvVariables -> DataSource ()
24
+ sendEmailDataSource email envVariables =
25
+ Debug.todo "Not defined here"
4
26
 
5
27
  @docs get, expect
6
28
 
@@ -13,7 +35,8 @@ import Json.Decode as Decode
13
35
  import Json.Encode as Encode
14
36
 
15
37
 
16
- {-| -}
38
+ {-| Get an environment variable, or Nothing if there is no environment variable matching that name.
39
+ -}
17
40
  get : String -> DataSource (Maybe String)
18
41
  get envVariableName =
19
42
  DataSource.Internal.Request.request
@@ -25,7 +48,8 @@ get envVariableName =
25
48
  }
26
49
 
27
50
 
28
- {-| -}
51
+ {-| Get an environment variable, or a DataSource failure if there is no environment variable matching that name.
52
+ -}
29
53
  expect : String -> DataSource String
30
54
  expect envVariableName =
31
55
  envVariableName
@@ -38,38 +38,27 @@ So why not just get the data the old-fashioned way, with `elm/http`, for example
38
38
  A few reasons:
39
39
 
40
40
  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!
41
- 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.
42
- 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!
43
- 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.
41
+ 2. You can pre-render HTML for your 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.
42
+ 3. 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. In the case of server-rendered routes, a DataSource failure will render a 500 page, so more care needs to be taken to make sure all common errors are handled properly, but the tradeoff is that you can use DataSource's to pull in highly dynamic data and even render user-specific pages.
43
+ 4. For static routes, 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 optimized binary-encoded data 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!
44
44
 
45
45
 
46
46
  ## Mental Model
47
47
 
48
48
  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.).
49
49
 
50
- 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
51
- it will share the result between any other DataSource.Http requests that have the exact same URL, Method, Body, and Headers.
52
50
 
53
- 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:
51
+ ## How do I actually use a DataSource?
54
52
 
55
- <https://my-api.example.com/increment-counter>
56
- -> Returns 1
57
- <https://my-api.example.com/increment-counter>
58
- -> Returns 2
59
- <https://my-api.example.com/increment-counter>
60
- -> Returns 3
53
+ This is very similar to Cmd's in Elm. You don't perform a Cmd just by running that code, as you might in a language like JavaScript. Instead, a Cmd _will not do anything_ unless you pass it to The Elm Architecture to have it perform it for you.
54
+ You pass a Cmd to The Elm Architecture by returning it in `init` or `update`. So actually a `Cmd` is just data describing a side-effect that the Elm runtime can perform, and how to build a `Msg` once it's done.
61
55
 
62
- If we define a `DataSource` that hits that endpoint:
56
+ `DataSource`'s are very similar. A `DataSource` doesn't do anything just by "running" it. Just like a `Cmd`, it's only data that describes a side-effect to perform. Specifically, it describes a side-effect that the _elm-pages runtime_ can perform.
57
+ There are a few places where we can pass a `DataSource` to the `elm-pages` runtime so it can perform it. Most commonly, you give a field called `data` in your Route Module's definition. Instead of giving a `Msg` when the side-effects are complete,
58
+ the page will render once all of the side-effects have run and all the data is resolved. `elm-pages` makes the resolved data available your Route Module's `init`, `view`, `update`, and `head` functions, similar to how a regular Elm app passes `Msg`'s in
59
+ to `update`.
63
60
 
64
- data =
65
- DataSource.Http.get
66
- "https://my-api.example.com/increment-counter"
67
- Decode.int
68
-
69
- 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).
70
-
71
- 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.
72
- So it's best to use that mental model to avoid confusion.
61
+ Any place in your `elm-pages` app where the framework lets you pass in a value of type `DataSource` is a place where you can give `elm-pages` a DataSource to perform (for example, `Site.head` where you define global head tags for your site).
73
62
 
74
63
 
75
64
  ## Basics
package/src/Form.elm CHANGED
@@ -272,7 +272,7 @@ import Dict exposing (Dict)
272
272
  import Form.Field as Field exposing (Field(..))
273
273
  import Form.FieldStatus as FieldStatus exposing (FieldStatus)
274
274
  import Form.FieldView
275
- import Form.Validation as Validation exposing (Combined, Validation)
275
+ import Form.Validation as Validation exposing (Combined)
276
276
  import Html exposing (Html)
277
277
  import Html.Attributes as Attr
278
278
  import Html.Lazy
@@ -332,7 +332,7 @@ dynamic :
332
332
  ->
333
333
  Form
334
334
  error
335
- { combine : Validation error parsed named constraints1
335
+ { combine : Validation.Validation error parsed named constraints1
336
336
  , view : subView
337
337
  }
338
338
  data
@@ -341,7 +341,7 @@ dynamic :
341
341
  Form
342
342
  error
343
343
  --((decider -> Validation error parsed named) -> combined)
344
- ({ combine : decider -> Validation error parsed named constraints1
344
+ ({ combine : decider -> Validation.Validation error parsed named constraints1
345
345
  , view : decider -> subView
346
346
  }
347
347
  -> combineAndView
@@ -674,14 +674,14 @@ hiddenField name (Field fieldParser _) (Form definitions parseFn toInitialValues
674
674
  toServerForm :
675
675
  Form
676
676
  error
677
- { combine : Validation error combined kind constraints
677
+ { combine : Validation.Validation error combined kind constraints
678
678
  , view : viewFn
679
679
  }
680
680
  data
681
681
  ->
682
682
  Form
683
683
  error
684
- { combine : Validation error (DataSource (Validation error combined kind constraints)) kind constraints
684
+ { combine : Validation.Validation error (DataSource (Validation.Validation error combined kind constraints)) kind constraints
685
685
  , view : viewFn
686
686
  }
687
687
  data
@@ -694,7 +694,7 @@ toServerForm (Form a b c) =
694
694
  { result : Dict String (List error)
695
695
  , isMatchCandidate : Bool
696
696
  , combineAndView :
697
- { combine : Validation error (DataSource (Validation error combined kind constraints)) kind constraints
697
+ { combine : Validation.Validation error (DataSource (Validation.Validation error combined kind constraints)) kind constraints
698
698
  , view : viewFn
699
699
  }
700
700
  }
@@ -844,7 +844,7 @@ parse :
844
844
  String
845
845
  -> AppContext app actionData
846
846
  -> data
847
- -> Form error { info | combine : Validation error parsed named constraints } data
847
+ -> Form error { info | combine : Validation.Validation error parsed named constraints } data
848
848
  -> ( Maybe parsed, Dict String (List error) )
849
849
  parse formId app data (Form _ parser _) =
850
850
  -- TODO Get transition context from `app` so you can check if the current form is being submitted
@@ -883,7 +883,7 @@ insertIfNonempty key values dict =
883
883
  {-| -}
884
884
  runServerSide :
885
885
  List ( String, String )
886
- -> Form error (Validation error parsed kind constraints) data
886
+ -> Form error (Validation.Validation error parsed kind constraints) data
887
887
  -> ( Bool, ( Maybe parsed, Dict String (List error) ) )
888
888
  runServerSide rawFormData (Form _ parser _) =
889
889
  let
@@ -989,7 +989,7 @@ renderHtml :
989
989
  ->
990
990
  FinalForm
991
991
  error
992
- (Validation error parsed named constraints)
992
+ (Validation.Validation error parsed named constraints)
993
993
  data
994
994
  (Context error data
995
995
  -> List (Html (Pages.Msg.Msg msg))
@@ -1025,14 +1025,14 @@ toDynamicFetcher :
1025
1025
  ->
1026
1026
  Form
1027
1027
  error
1028
- { combine : Validation error parsed field constraints
1028
+ { combine : Validation.Validation error parsed field constraints
1029
1029
  , view : Context error data -> view
1030
1030
  }
1031
1031
  data
1032
1032
  ->
1033
1033
  FinalForm
1034
1034
  error
1035
- (Validation error parsed field constraints)
1035
+ (Validation.Validation error parsed field constraints)
1036
1036
  data
1037
1037
  (Context error data -> view)
1038
1038
  userMsg
@@ -1098,14 +1098,14 @@ toDynamicTransition :
1098
1098
  ->
1099
1099
  Form
1100
1100
  error
1101
- { combine : Validation error parsed field constraints
1101
+ { combine : Validation.Validation error parsed field constraints
1102
1102
  , view : Context error data -> view
1103
1103
  }
1104
1104
  data
1105
1105
  ->
1106
1106
  FinalForm
1107
1107
  error
1108
- (Validation error parsed field constraints)
1108
+ (Validation.Validation error parsed field constraints)
1109
1109
  data
1110
1110
  (Context error data -> view)
1111
1111
  userMsg
@@ -1126,7 +1126,7 @@ toDynamicTransition name (Form a b c) =
1126
1126
  { result : Dict String (List error)
1127
1127
  , isMatchCandidate : Bool
1128
1128
  , combineAndView :
1129
- { combine : Validation error parsed field constraints
1129
+ { combine : Validation.Validation error parsed field constraints
1130
1130
  , view : Context error data -> view
1131
1131
  }
1132
1132
  }
@@ -1136,7 +1136,7 @@ toDynamicTransition name (Form a b c) =
1136
1136
  -> FormState
1137
1137
  ->
1138
1138
  { result :
1139
- ( Validation error parsed field constraints
1139
+ ( Validation.Validation error parsed field constraints
1140
1140
  , Dict String (List error)
1141
1141
  )
1142
1142
  , isMatchCandidate : Bool
@@ -1150,7 +1150,7 @@ toDynamicTransition name (Form a b c) =
1150
1150
  { result : Dict String (List error)
1151
1151
  , isMatchCandidate : Bool
1152
1152
  , combineAndView :
1153
- { combine : Validation error parsed field constraints
1153
+ { combine : Validation.Validation error parsed field constraints
1154
1154
  , view : Context error data -> view
1155
1155
  }
1156
1156
  }
@@ -1190,7 +1190,7 @@ renderStyledHtml :
1190
1190
  ->
1191
1191
  FinalForm
1192
1192
  error
1193
- (Validation error parsed named constraints)
1193
+ (Validation.Validation error parsed named constraints)
1194
1194
  data
1195
1195
  (Context error data
1196
1196
  -> List (Html.Styled.Html (Pages.Msg.Msg msg))
@@ -1219,7 +1219,7 @@ renderHelper :
1219
1219
  -> RenderOptions msg
1220
1220
  -> AppContext app actionData
1221
1221
  -> data
1222
- -> FormInternal error (Validation error parsed named constraints) data (Context error data -> List (Html (Pages.Msg.Msg msg)))
1222
+ -> FormInternal error (Validation.Validation error parsed named constraints) data (Context error data -> List (Html (Pages.Msg.Msg msg)))
1223
1223
  -> Html (Pages.Msg.Msg msg)
1224
1224
  renderHelper attrs maybe options formState data form =
1225
1225
  -- TODO Get transition context from `app` so you can check if the current form is being submitted
@@ -1261,7 +1261,7 @@ renderStyledHelper :
1261
1261
  -> RenderOptions msg
1262
1262
  -> AppContext app actionData
1263
1263
  -> data
1264
- -> FormInternal error (Validation error parsed named constraints) data (Context error data -> List (Html.Styled.Html (Pages.Msg.Msg msg)))
1264
+ -> FormInternal error (Validation.Validation error parsed named constraints) data (Context error data -> List (Html.Styled.Html (Pages.Msg.Msg msg)))
1265
1265
  -> Html.Styled.Html (Pages.Msg.Msg msg)
1266
1266
  renderStyledHelper attrs maybe options formState data form =
1267
1267
  -- TODO Get transition context from `app` so you can check if the current form is being submitted
@@ -1304,7 +1304,7 @@ helperValues :
1304
1304
  -> AppContext app actionData
1305
1305
  -> data
1306
1306
  ---> Form error parsed data view
1307
- -> FormInternal error (Validation error parsed named constraints) data (Context error data -> List view)
1307
+ -> FormInternal error (Validation.Validation error parsed named constraints) data (Context error data -> List view)
1308
1308
  -> { formId : String, hiddenInputs : List view, children : List view, isValid : Bool }
1309
1309
  helperValues toHiddenInput maybe options formState data (FormInternal fieldDefinitions parser toInitialValues) =
1310
1310
  let
@@ -1343,18 +1343,18 @@ helperValues toHiddenInput maybe options formState data (FormInternal fieldDefin
1343
1343
  |> Dict.union part2
1344
1344
 
1345
1345
  parsed :
1346
- { result : ( Validation error parsed named constraints, Dict String (List error) )
1346
+ { result : ( Validation.Validation error parsed named constraints, Dict String (List error) )
1347
1347
  , isMatchCandidate : Bool
1348
1348
  , view : Context error data -> List view
1349
1349
  }
1350
1350
  parsed =
1351
1351
  parser (Just data) thisFormState
1352
1352
 
1353
- withoutServerErrors : Validation error parsed named constraints
1353
+ withoutServerErrors : Validation.Validation error parsed named constraints
1354
1354
  withoutServerErrors =
1355
1355
  parsed |> mergeResults
1356
1356
 
1357
- withServerErrors : Validation error parsed named constraints
1357
+ withServerErrors : Validation.Validation error parsed named constraints
1358
1358
  withServerErrors =
1359
1359
  mergeResults
1360
1360
  { parsed
@@ -1498,7 +1498,7 @@ initCombined :
1498
1498
  Form
1499
1499
  error
1500
1500
  { combineAndView
1501
- | combine : Validation error parsed kind constraints
1501
+ | combine : Validation.Validation error parsed kind constraints
1502
1502
  }
1503
1503
  input
1504
1504
  -> ServerForms error combined
@@ -1511,7 +1511,7 @@ initCombined mapFn (Form _ parseFn _) =
1511
1511
  foo :
1512
1512
  { result : Dict String (List error)
1513
1513
  , isMatchCandidate : Bool
1514
- , combineAndView : { combineAndView | combine : Validation error parsed kind constraints }
1514
+ , combineAndView : { combineAndView | combine : Validation.Validation error parsed kind constraints }
1515
1515
  }
1516
1516
  foo =
1517
1517
  parseFn Nothing formState
@@ -1532,7 +1532,7 @@ combine :
1532
1532
  Form
1533
1533
  error
1534
1534
  { combineAndView
1535
- | combine : Validation error parsed kind constraints
1535
+ | combine : Validation.Validation error parsed kind constraints
1536
1536
  }
1537
1537
  input
1538
1538
  -> ServerForms error combined
@@ -1546,7 +1546,7 @@ combine mapFn (Form _ parseFn _) (ServerForms serverForms) =
1546
1546
  foo :
1547
1547
  { result : Dict String (List error)
1548
1548
  , isMatchCandidate : Bool
1549
- , combineAndView : { combineAndView | combine : Validation error parsed kind constraints }
1549
+ , combineAndView : { combineAndView | combine : Validation.Validation error parsed kind constraints }
1550
1550
  }
1551
1551
  foo =
1552
1552
  parseFn Nothing formState
@@ -1568,10 +1568,10 @@ initCombinedServer :
1568
1568
  Form
1569
1569
  error
1570
1570
  { combineAndView
1571
- | combine : Combined error (DataSource (Validation error parsed kind constraints))
1571
+ | combine : Combined error (DataSource (Validation.Validation error parsed kind constraints))
1572
1572
  }
1573
1573
  input
1574
- -> ServerForms error (DataSource (Validation error combined kind constraints))
1574
+ -> ServerForms error (DataSource (Validation.Validation error combined kind constraints))
1575
1575
  initCombinedServer mapFn serverForms =
1576
1576
  initCombined (DataSource.map (Validation.map mapFn)) serverForms
1577
1577
 
@@ -1584,11 +1584,11 @@ combineServer :
1584
1584
  error
1585
1585
  { combineAndView
1586
1586
  | combine :
1587
- Combined error (DataSource (Validation error parsed kind constraints))
1587
+ Combined error (DataSource (Validation.Validation error parsed kind constraints))
1588
1588
  }
1589
1589
  input
1590
- -> ServerForms error (DataSource (Validation error combined kind constraints))
1591
- -> ServerForms error (DataSource (Validation error combined kind constraints))
1590
+ -> ServerForms error (DataSource (Validation.Validation error combined kind constraints))
1591
+ -> ServerForms error (DataSource (Validation.Validation error combined kind constraints))
1592
1592
  combineServer mapFn a b =
1593
1593
  combine (DataSource.map (Validation.map mapFn)) a b
1594
1594