elm-pages 3.0.0-beta.4 → 3.0.0-beta.40
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.
- package/README.md +10 -1
- package/adapter/netlify.js +207 -0
- package/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +2678 -2725
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmi +0 -0
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmo +0 -0
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm.json +1 -1
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1447 -342
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +17004 -13817
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
- package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
- package/generator/dead-code-review/elm.json +9 -7
- package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +59 -10
- package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +52 -36
- package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmi +0 -0
- package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmo +0 -0
- package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmi +0 -0
- package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmo +0 -0
- package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
- package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
- package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
- package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
- package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1447 -342
- package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +25025 -21739
- package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
- package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
- package/generator/review/elm.json +10 -10
- package/generator/src/RouteBuilder.elm +115 -109
- package/generator/src/SharedTemplate.elm +3 -2
- package/generator/src/SiteConfig.elm +3 -2
- package/generator/src/basepath-middleware.js +3 -3
- package/generator/src/build.js +209 -92
- package/generator/src/cli.js +292 -88
- package/generator/src/codegen.js +29 -27
- package/generator/src/compatibility-key.js +3 -0
- package/generator/src/compile-elm.js +43 -26
- package/generator/src/config.js +39 -0
- package/generator/src/copy-dir.js +2 -2
- package/generator/src/dev-server.js +176 -138
- package/generator/src/dir-helpers.js +9 -26
- package/generator/src/elm-codegen.js +5 -4
- package/generator/src/elm-file-constants.js +2 -3
- package/generator/src/error-formatter.js +12 -11
- package/generator/src/file-helpers.js +3 -4
- package/generator/src/generate-template-module-connector.js +23 -23
- package/generator/src/init.js +9 -8
- package/generator/src/pre-render-html.js +39 -28
- package/generator/src/render-test.js +109 -0
- package/generator/src/render-worker.js +25 -28
- package/generator/src/render.js +321 -142
- package/generator/src/request-cache.js +265 -162
- package/generator/src/resolve-elm-module.js +64 -0
- package/generator/src/rewrite-client-elm-json.js +6 -5
- package/generator/src/rewrite-elm-json-help.js +56 -0
- package/generator/src/rewrite-elm-json.js +17 -7
- package/generator/src/route-codegen-helpers.js +16 -31
- package/generator/src/seo-renderer.js +12 -7
- package/generator/src/vite-utils.js +77 -0
- package/generator/static-code/elm-pages.js +10 -0
- package/generator/static-code/hmr.js +79 -13
- package/generator/template/app/Api.elm +6 -5
- package/generator/template/app/Effect.elm +123 -0
- package/generator/template/app/ErrorPage.elm +37 -6
- package/generator/template/app/Route/Index.elm +17 -10
- package/generator/template/app/Shared.elm +24 -47
- package/generator/template/app/Site.elm +19 -6
- package/generator/template/app/View.elm +1 -8
- package/generator/template/elm-tooling.json +0 -3
- package/generator/template/elm.json +32 -24
- package/generator/template/package.json +10 -4
- package/package.json +30 -27
- package/src/ApiRoute.elm +199 -61
- package/src/BackendTask/Custom.elm +325 -0
- package/src/BackendTask/Env.elm +90 -0
- package/src/{DataSource → BackendTask}/File.elm +171 -56
- package/src/{DataSource → BackendTask}/Glob.elm +136 -125
- package/src/BackendTask/Http.elm +679 -0
- package/src/{DataSource → BackendTask}/Internal/Glob.elm +1 -1
- package/src/BackendTask/Internal/Request.elm +69 -0
- package/src/BackendTask/Random.elm +79 -0
- package/src/BackendTask/Time.elm +47 -0
- package/src/BackendTask.elm +537 -0
- package/src/FatalError.elm +90 -0
- package/src/Head.elm +237 -7
- package/src/HtmlPrinter.elm +7 -3
- package/src/Internal/ApiRoute.elm +7 -5
- package/src/PageServerResponse.elm +6 -1
- package/src/Pages/Form.elm +229 -0
- package/src/Pages/GeneratorProgramConfig.elm +15 -0
- package/src/Pages/Internal/FatalError.elm +5 -0
- package/src/Pages/Internal/Msg.elm +93 -0
- package/src/Pages/Internal/Platform/Cli.elm +612 -763
- package/src/Pages/Internal/Platform/CompatibilityKey.elm +6 -0
- package/src/Pages/Internal/Platform/Effect.elm +1 -2
- package/src/Pages/Internal/Platform/GeneratorApplication.elm +379 -0
- package/src/Pages/Internal/Platform/StaticResponses.elm +65 -276
- package/src/Pages/Internal/Platform/ToJsPayload.elm +6 -9
- package/src/Pages/Internal/Platform.elm +327 -194
- package/src/Pages/Internal/Script.elm +17 -0
- package/src/Pages/Internal/StaticHttpBody.elm +35 -1
- package/src/Pages/Manifest.elm +29 -4
- package/src/Pages/PageUrl.elm +23 -9
- package/src/Pages/ProgramConfig.elm +26 -15
- package/src/Pages/Script.elm +109 -0
- package/src/Pages/SiteConfig.elm +3 -2
- package/src/Pages/StaticHttp/Request.elm +2 -2
- package/src/Pages/StaticHttpRequest.elm +23 -99
- package/src/Pages/Transition.elm +12 -3
- package/src/PagesMsg.elm +82 -0
- package/src/Path.elm +16 -19
- package/src/QueryParams.elm +21 -172
- package/src/RequestsAndPending.elm +37 -20
- package/src/Result/Extra.elm +26 -0
- package/src/Scaffold/Form.elm +546 -0
- package/src/Scaffold/Route.elm +1402 -0
- package/src/Server/Request.elm +73 -72
- package/src/Server/Session.elm +62 -42
- package/src/Server/SetCookie.elm +12 -4
- package/src/Stub.elm +53 -0
- package/src/Test/Html/Internal/ElmHtml/ToString.elm +8 -9
- package/src/DataSource/Env.elm +0 -38
- package/src/DataSource/Http.elm +0 -446
- package/src/DataSource/Internal/Request.elm +0 -20
- package/src/DataSource/Port.elm +0 -90
- package/src/DataSource.elm +0 -538
- package/src/Form/Field.elm +0 -717
- package/src/Form/FieldStatus.elm +0 -36
- package/src/Form/FieldView.elm +0 -417
- package/src/Form/FormData.elm +0 -22
- package/src/Form/Validation.elm +0 -391
- package/src/Form/Value.elm +0 -118
- package/src/Form.elm +0 -1683
- package/src/FormDecoder.elm +0 -102
- package/src/Pages/FormState.elm +0 -256
- package/src/Pages/Generate.elm +0 -800
- package/src/Pages/Internal/Form.elm +0 -17
- package/src/Pages/Msg.elm +0 -79
package/src/DataSource/Env.elm
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
module DataSource.Env exposing (get, expect)
|
|
2
|
-
|
|
3
|
-
{-|
|
|
4
|
-
|
|
5
|
-
@docs get, expect
|
|
6
|
-
|
|
7
|
-
-}
|
|
8
|
-
|
|
9
|
-
import DataSource exposing (DataSource)
|
|
10
|
-
import DataSource.Http
|
|
11
|
-
import DataSource.Internal.Request
|
|
12
|
-
import Json.Decode as Decode
|
|
13
|
-
import Json.Encode as Encode
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
{-| -}
|
|
17
|
-
get : String -> DataSource (Maybe String)
|
|
18
|
-
get envVariableName =
|
|
19
|
-
DataSource.Internal.Request.request
|
|
20
|
-
{ name = "env"
|
|
21
|
-
, body = DataSource.Http.jsonBody (Encode.string envVariableName)
|
|
22
|
-
, expect =
|
|
23
|
-
DataSource.Http.expectJson
|
|
24
|
-
(Decode.nullable Decode.string)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
{-| -}
|
|
29
|
-
expect : String -> DataSource String
|
|
30
|
-
expect envVariableName =
|
|
31
|
-
envVariableName
|
|
32
|
-
|> get
|
|
33
|
-
|> DataSource.andThen
|
|
34
|
-
(\maybeValue ->
|
|
35
|
-
maybeValue
|
|
36
|
-
|> Result.fromMaybe ("DataSource.Env.expect was expecting a variable `" ++ envVariableName ++ "` but couldn't find a variable with that name.")
|
|
37
|
-
|> DataSource.fromResult
|
|
38
|
-
)
|
package/src/DataSource/Http.elm
DELETED
|
@@ -1,446 +0,0 @@
|
|
|
1
|
-
module DataSource.Http exposing
|
|
2
|
-
( RequestDetails
|
|
3
|
-
, get, request
|
|
4
|
-
, Expect, expectString, expectJson, expectBytes, expectWhatever
|
|
5
|
-
, Response(..), Metadata, Error(..)
|
|
6
|
-
, expectStringResponse, expectBytesResponse
|
|
7
|
-
, Body, emptyBody, stringBody, jsonBody
|
|
8
|
-
, uncachedRequest
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
{-| `DataSource.Http` requests are an alternative to doing Elm HTTP requests the traditional way using the `elm/http` package.
|
|
12
|
-
|
|
13
|
-
The key differences are:
|
|
14
|
-
|
|
15
|
-
- `DataSource.Http.Request`s are performed once at build time (`Http.Request`s are performed at runtime, at whenever point you perform them)
|
|
16
|
-
- `DataSource.Http.Request`s have a built-in `DataSource.andThen` that allows you to perform follow-up requests without using tasks
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
## Scenarios where DataSource.Http is a good fit
|
|
20
|
-
|
|
21
|
-
If you need data that is refreshed often you may want to do a traditional HTTP request with the `elm/http` package.
|
|
22
|
-
The kinds of situations that are served well by static HTTP are with data that updates moderately frequently or infrequently (or never).
|
|
23
|
-
A common pattern is to trigger a new build when data changes. Many JAMstack services
|
|
24
|
-
allow you to send a WebHook to your host (for example, Netlify is a good static file host that supports triggering builds with webhooks). So
|
|
25
|
-
you may want to have your site rebuild everytime your calendar feed has an event added, or whenever a page or article is added
|
|
26
|
-
or updated on a CMS service like Contentful.
|
|
27
|
-
|
|
28
|
-
In scenarios like this, you can serve data that is just as up-to-date as it would be using `elm/http`, but you get the performance
|
|
29
|
-
gains of using `DataSource.Http.Request`s as well as the simplicity and robustness that comes with it. Read more about these benefits
|
|
30
|
-
in [this article introducing DataSource.Http requests and some concepts around it](https://elm-pages.com/blog/static-http).
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
## Scenarios where DataSource.Http is not a good fit
|
|
34
|
-
|
|
35
|
-
- Data that is specific to the logged-in user
|
|
36
|
-
- Data that needs to be the very latest and changes often (for example, sports scores)
|
|
37
|
-
|
|
38
|
-
@docs RequestDetails
|
|
39
|
-
@docs get, request
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
## Decoding Request Body
|
|
43
|
-
|
|
44
|
-
@docs Expect, expectString, expectJson, expectBytes, expectWhatever
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
## Expecting Responses
|
|
48
|
-
|
|
49
|
-
@docs Response, Metadata, Error
|
|
50
|
-
|
|
51
|
-
@docs expectStringResponse, expectBytesResponse
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
## Building a DataSource.Http Request Body
|
|
55
|
-
|
|
56
|
-
The way you build a body is analogous to the `elm/http` package. Currently, only `emptyBody` and
|
|
57
|
-
`stringBody` are supported. If you have a use case that calls for a different body type, please open a Github issue
|
|
58
|
-
and describe your use case!
|
|
59
|
-
|
|
60
|
-
@docs Body, emptyBody, stringBody, jsonBody
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
## Uncached Requests
|
|
64
|
-
|
|
65
|
-
@docs uncachedRequest
|
|
66
|
-
|
|
67
|
-
-}
|
|
68
|
-
|
|
69
|
-
import Bytes exposing (Bytes)
|
|
70
|
-
import Bytes.Decode
|
|
71
|
-
import DataSource exposing (DataSource)
|
|
72
|
-
import Dict exposing (Dict)
|
|
73
|
-
import Json.Decode
|
|
74
|
-
import Json.Encode as Encode
|
|
75
|
-
import Pages.Internal.StaticHttpBody as Body
|
|
76
|
-
import Pages.StaticHttp.Request as HashRequest
|
|
77
|
-
import Pages.StaticHttpRequest exposing (RawRequest(..))
|
|
78
|
-
import RequestsAndPending
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
{-| Build an empty body for a DataSource.Http request. See [elm/http's `Http.emptyBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#emptyBody).
|
|
82
|
-
-}
|
|
83
|
-
emptyBody : Body
|
|
84
|
-
emptyBody =
|
|
85
|
-
Body.EmptyBody
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
{-| Builds a string body for a DataSource.Http request. See [elm/http's `Http.stringBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#stringBody).
|
|
89
|
-
|
|
90
|
-
Note from the `elm/http` docs:
|
|
91
|
-
|
|
92
|
-
> The first argument is a [MIME type](https://en.wikipedia.org/wiki/Media_type) of the body. Some servers are strict about this!
|
|
93
|
-
|
|
94
|
-
-}
|
|
95
|
-
stringBody : String -> String -> Body
|
|
96
|
-
stringBody contentType content =
|
|
97
|
-
Body.StringBody contentType content
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
{-| Builds a JSON body for a DataSource.Http request. See [elm/http's `Http.jsonBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#jsonBody).
|
|
101
|
-
-}
|
|
102
|
-
jsonBody : Encode.Value -> Body
|
|
103
|
-
jsonBody content =
|
|
104
|
-
Body.JsonBody content
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
{-| A body for a DataSource.Http request.
|
|
108
|
-
-}
|
|
109
|
-
type alias Body =
|
|
110
|
-
Body.Body
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
{-| A simplified helper around [`DataSource.Http.request`](#request), which builds up a DataSource.Http GET request.
|
|
114
|
-
|
|
115
|
-
import DataSource
|
|
116
|
-
import DataSource.Http
|
|
117
|
-
import Json.Decode as Decode exposing (Decoder)
|
|
118
|
-
|
|
119
|
-
getRequest : DataSource Int
|
|
120
|
-
getRequest =
|
|
121
|
-
DataSource.Http.get
|
|
122
|
-
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
123
|
-
(Decode.field "stargazers_count" Decode.int)
|
|
124
|
-
|
|
125
|
-
-}
|
|
126
|
-
get :
|
|
127
|
-
String
|
|
128
|
-
-> Json.Decode.Decoder a
|
|
129
|
-
-> DataSource a
|
|
130
|
-
get url decoder =
|
|
131
|
-
request
|
|
132
|
-
((\okUrl ->
|
|
133
|
-
-- wrap in new variant
|
|
134
|
-
{ url = okUrl
|
|
135
|
-
, method = "GET"
|
|
136
|
-
, headers = []
|
|
137
|
-
, body = emptyBody
|
|
138
|
-
}
|
|
139
|
-
)
|
|
140
|
-
url
|
|
141
|
-
)
|
|
142
|
-
(expectJson decoder)
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
{-| The full details to perform a DataSource.Http request.
|
|
146
|
-
-}
|
|
147
|
-
type alias RequestDetails =
|
|
148
|
-
{ url : String
|
|
149
|
-
, method : String
|
|
150
|
-
, headers : List ( String, String )
|
|
151
|
-
, body : Body
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
{-| Analogous to the `Expect` type in the `elm/http` package. This represents how you will process the data that comes
|
|
156
|
-
back in your DataSource.Http request.
|
|
157
|
-
|
|
158
|
-
You can derive `ExpectJson` from `ExpectString`. Or you could build your own helper to process the String
|
|
159
|
-
as XML, for example, or give an `elm-pages` build error if the response can't be parsed as XML.
|
|
160
|
-
|
|
161
|
-
-}
|
|
162
|
-
type Expect value
|
|
163
|
-
= ExpectJson (Json.Decode.Decoder value)
|
|
164
|
-
| ExpectString (String -> value)
|
|
165
|
-
| ExpectResponse (Response String -> value)
|
|
166
|
-
| ExpectBytesResponse (Response Bytes -> value)
|
|
167
|
-
| ExpectBytes (Bytes.Decode.Decoder value)
|
|
168
|
-
| ExpectWhatever value
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
{-| Gives the HTTP response body as a raw String.
|
|
172
|
-
|
|
173
|
-
import DataSource exposing (DataSource)
|
|
174
|
-
import DataSource.Http
|
|
175
|
-
|
|
176
|
-
request : DataSource String
|
|
177
|
-
request =
|
|
178
|
-
DataSource.Http.request
|
|
179
|
-
{ url = "https://example.com/file.txt"
|
|
180
|
-
, method = "GET"
|
|
181
|
-
, headers = []
|
|
182
|
-
, body = DataSource.Http.emptyBody
|
|
183
|
-
}
|
|
184
|
-
DataSource.Http.expectString
|
|
185
|
-
|
|
186
|
-
-}
|
|
187
|
-
expectString : Expect String
|
|
188
|
-
expectString =
|
|
189
|
-
ExpectString identity
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
{-| Handle the incoming response as JSON and don't optimize the asset and strip out unused values.
|
|
193
|
-
Be sure to use the `DataSource.Http.request` function if you want an optimized request that
|
|
194
|
-
strips out unused JSON to optimize your asset size. This function makes sense to use for things like a GraphQL request
|
|
195
|
-
where the JSON payload is already trimmed down to the data you explicitly requested.
|
|
196
|
-
|
|
197
|
-
If the function you pass to `expectString` yields an `Err`, then you will get a build error that will
|
|
198
|
-
fail your `elm-pages` build and print out the String from the `Err`.
|
|
199
|
-
|
|
200
|
-
-}
|
|
201
|
-
expectJson : Json.Decode.Decoder value -> Expect value
|
|
202
|
-
expectJson =
|
|
203
|
-
ExpectJson
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
{-| -}
|
|
207
|
-
expectBytes : Bytes.Decode.Decoder value -> Expect value
|
|
208
|
-
expectBytes =
|
|
209
|
-
ExpectBytes
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
{-| -}
|
|
213
|
-
expectWhatever : value -> Expect value
|
|
214
|
-
expectWhatever =
|
|
215
|
-
ExpectWhatever
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
{-| -}
|
|
219
|
-
expectStringResponse : Expect (Response String)
|
|
220
|
-
expectStringResponse =
|
|
221
|
-
ExpectResponse identity
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
{-| -}
|
|
225
|
-
expectBytesResponse : Expect (Response Bytes)
|
|
226
|
-
expectBytesResponse =
|
|
227
|
-
ExpectBytesResponse identity
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
expectToString : Expect a -> String
|
|
231
|
-
expectToString expect =
|
|
232
|
-
case expect of
|
|
233
|
-
ExpectJson _ ->
|
|
234
|
-
"ExpectJson"
|
|
235
|
-
|
|
236
|
-
ExpectString _ ->
|
|
237
|
-
"ExpectString"
|
|
238
|
-
|
|
239
|
-
ExpectResponse _ ->
|
|
240
|
-
"ExpectResponse"
|
|
241
|
-
|
|
242
|
-
ExpectBytes _ ->
|
|
243
|
-
"ExpectBytes"
|
|
244
|
-
|
|
245
|
-
ExpectWhatever _ ->
|
|
246
|
-
"ExpectWhatever"
|
|
247
|
-
|
|
248
|
-
ExpectBytesResponse _ ->
|
|
249
|
-
"ExpectBytesResponse"
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
{-| -}
|
|
253
|
-
request :
|
|
254
|
-
RequestDetails
|
|
255
|
-
-> Expect a
|
|
256
|
-
-> DataSource a
|
|
257
|
-
request request__ expect =
|
|
258
|
-
let
|
|
259
|
-
request_ : HashRequest.Request
|
|
260
|
-
request_ =
|
|
261
|
-
{ url = request__.url
|
|
262
|
-
, headers = request__.headers
|
|
263
|
-
, method = request__.method
|
|
264
|
-
, body = request__.body
|
|
265
|
-
, useCache = True
|
|
266
|
-
}
|
|
267
|
-
in
|
|
268
|
-
requestRaw request_ expect
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
{-| -}
|
|
272
|
-
uncachedRequest :
|
|
273
|
-
RequestDetails
|
|
274
|
-
-> Expect a
|
|
275
|
-
-> DataSource a
|
|
276
|
-
uncachedRequest request__ expect =
|
|
277
|
-
let
|
|
278
|
-
request_ : HashRequest.Request
|
|
279
|
-
request_ =
|
|
280
|
-
{ url = request__.url
|
|
281
|
-
, headers = request__.headers
|
|
282
|
-
, method = request__.method
|
|
283
|
-
, body = request__.body
|
|
284
|
-
, useCache = False
|
|
285
|
-
}
|
|
286
|
-
in
|
|
287
|
-
requestRaw request_ expect
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
{-| Build a `DataSource.Http` request (analogous to [Http.request](https://package.elm-lang.org/packages/elm/http/latest/Http#request)).
|
|
291
|
-
This function takes in all the details to build a `DataSource.Http` request, but you can build your own simplified helper functions
|
|
292
|
-
with this as a low-level detail, or you can use functions like [DataSource.Http.get](#get).
|
|
293
|
-
-}
|
|
294
|
-
requestRaw :
|
|
295
|
-
HashRequest.Request
|
|
296
|
-
-> Expect a
|
|
297
|
-
-> DataSource a
|
|
298
|
-
requestRaw request__ expect =
|
|
299
|
-
let
|
|
300
|
-
request_ : HashRequest.Request
|
|
301
|
-
request_ =
|
|
302
|
-
{ url = request__.url
|
|
303
|
-
, headers =
|
|
304
|
-
( "elm-pages-internal", expectToString expect )
|
|
305
|
-
:: request__.headers
|
|
306
|
-
, method = request__.method
|
|
307
|
-
, body = request__.body
|
|
308
|
-
, useCache = False
|
|
309
|
-
}
|
|
310
|
-
in
|
|
311
|
-
Request
|
|
312
|
-
[ request_ ]
|
|
313
|
-
(\maybeMockResolver rawResponseDict ->
|
|
314
|
-
(case maybeMockResolver of
|
|
315
|
-
Just mockResolver ->
|
|
316
|
-
mockResolver request_
|
|
317
|
-
|
|
318
|
-
Nothing ->
|
|
319
|
-
rawResponseDict |> RequestsAndPending.get (request_ |> HashRequest.hash)
|
|
320
|
-
)
|
|
321
|
-
|> (\maybeResponse ->
|
|
322
|
-
case maybeResponse of
|
|
323
|
-
Just rawResponse ->
|
|
324
|
-
Ok rawResponse
|
|
325
|
-
|
|
326
|
-
Nothing ->
|
|
327
|
-
Err (Pages.StaticHttpRequest.MissingHttpResponse request_.url [ request_ ])
|
|
328
|
-
)
|
|
329
|
-
|> Result.andThen
|
|
330
|
-
(\(RequestsAndPending.Response maybeResponse body) ->
|
|
331
|
-
case ( expect, body, maybeResponse ) of
|
|
332
|
-
( ExpectJson decoder, RequestsAndPending.JsonBody json, _ ) ->
|
|
333
|
-
json
|
|
334
|
-
|> Json.Decode.decodeValue decoder
|
|
335
|
-
|> Result.mapError
|
|
336
|
-
(\error ->
|
|
337
|
-
error
|
|
338
|
-
|> Json.Decode.errorToString
|
|
339
|
-
|> Pages.StaticHttpRequest.DecoderError
|
|
340
|
-
)
|
|
341
|
-
|
|
342
|
-
( ExpectString mapStringFn, RequestsAndPending.StringBody string, _ ) ->
|
|
343
|
-
string
|
|
344
|
-
|> mapStringFn
|
|
345
|
-
|> Ok
|
|
346
|
-
|
|
347
|
-
( ExpectResponse mapResponse, RequestsAndPending.StringBody asStringBody, Just rawResponse ) ->
|
|
348
|
-
let
|
|
349
|
-
asMetadata : Metadata
|
|
350
|
-
asMetadata =
|
|
351
|
-
{ url = rawResponse.url
|
|
352
|
-
, statusCode = rawResponse.statusCode
|
|
353
|
-
, statusText = rawResponse.statusText
|
|
354
|
-
, headers = rawResponse.headers
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
rawResponseToResponse : Response String
|
|
358
|
-
rawResponseToResponse =
|
|
359
|
-
if 200 <= rawResponse.statusCode && rawResponse.statusCode < 300 then
|
|
360
|
-
GoodStatus_ asMetadata asStringBody
|
|
361
|
-
|
|
362
|
-
else
|
|
363
|
-
BadStatus_ asMetadata asStringBody
|
|
364
|
-
in
|
|
365
|
-
rawResponseToResponse
|
|
366
|
-
|> mapResponse
|
|
367
|
-
|> Ok
|
|
368
|
-
|
|
369
|
-
( ExpectBytesResponse mapResponse, RequestsAndPending.BytesBody rawBytesBody, Just rawResponse ) ->
|
|
370
|
-
let
|
|
371
|
-
asMetadata : Metadata
|
|
372
|
-
asMetadata =
|
|
373
|
-
{ url = rawResponse.url
|
|
374
|
-
, statusCode = rawResponse.statusCode
|
|
375
|
-
, statusText = rawResponse.statusText
|
|
376
|
-
, headers = rawResponse.headers
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
rawResponseToResponse : Response Bytes
|
|
380
|
-
rawResponseToResponse =
|
|
381
|
-
if 200 <= rawResponse.statusCode && rawResponse.statusCode < 300 then
|
|
382
|
-
GoodStatus_ asMetadata rawBytesBody
|
|
383
|
-
|
|
384
|
-
else
|
|
385
|
-
BadStatus_ asMetadata rawBytesBody
|
|
386
|
-
in
|
|
387
|
-
rawResponseToResponse
|
|
388
|
-
|> mapResponse
|
|
389
|
-
|> Ok
|
|
390
|
-
|
|
391
|
-
( ExpectBytes bytesDecoder, RequestsAndPending.BytesBody rawBytes, _ ) ->
|
|
392
|
-
rawBytes
|
|
393
|
-
|> Bytes.Decode.decode bytesDecoder
|
|
394
|
-
|> Result.fromMaybe
|
|
395
|
-
(Pages.StaticHttpRequest.DecoderError
|
|
396
|
-
"Bytes decoding failed."
|
|
397
|
-
)
|
|
398
|
-
|
|
399
|
-
( ExpectWhatever whateverValue, RequestsAndPending.WhateverBody, _ ) ->
|
|
400
|
-
Ok whateverValue
|
|
401
|
-
|
|
402
|
-
_ ->
|
|
403
|
-
Err
|
|
404
|
-
(Pages.StaticHttpRequest.DecoderError
|
|
405
|
-
"Internal error - unexpected body, expect, and raw response combination."
|
|
406
|
-
)
|
|
407
|
-
)
|
|
408
|
-
|> toResult
|
|
409
|
-
)
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
{-| -}
|
|
413
|
-
type alias Metadata =
|
|
414
|
-
{ url : String
|
|
415
|
-
, statusCode : Int
|
|
416
|
-
, statusText : String
|
|
417
|
-
, headers : Dict String String
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
{-| -}
|
|
422
|
-
type Response body
|
|
423
|
-
= BadUrl_ String
|
|
424
|
-
| Timeout_
|
|
425
|
-
| NetworkError_
|
|
426
|
-
| BadStatus_ Metadata body
|
|
427
|
-
| GoodStatus_ Metadata body
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
{-| -}
|
|
431
|
-
type Error
|
|
432
|
-
= BadUrl String
|
|
433
|
-
| Timeout
|
|
434
|
-
| NetworkError
|
|
435
|
-
| BadStatus Metadata String
|
|
436
|
-
| BadBody String
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
toResult : Result Pages.StaticHttpRequest.Error b -> RawRequest b
|
|
440
|
-
toResult result =
|
|
441
|
-
case result of
|
|
442
|
-
Err error ->
|
|
443
|
-
RequestError error
|
|
444
|
-
|
|
445
|
-
Ok okValue ->
|
|
446
|
-
ApiRoute okValue
|
|
@@ -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
|
package/src/DataSource/Port.elm
DELETED
|
@@ -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
|
-
}
|