elm-pages 3.0.0-beta.3 → 3.0.0-beta.31
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/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +2864 -2589
- 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 +16458 -13724
- 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 +24542 -21748
- 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 +113 -107
- 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 +125 -88
- package/generator/src/cli.js +273 -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 +150 -133
- 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 -22
- 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 +320 -142
- package/generator/src/request-cache.js +252 -163
- package/generator/src/resolve-elm-module.js +63 -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/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 +29 -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 +128 -43
- package/src/{DataSource → BackendTask}/Glob.elm +136 -125
- package/src/BackendTask/Http.elm +673 -0
- package/src/{DataSource → BackendTask}/Internal/Glob.elm +1 -1
- package/src/BackendTask/Internal/Request.elm +28 -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 +89 -0
- package/src/Form/Field.elm +21 -9
- package/src/Form/FieldView.elm +94 -14
- package/src/Form.elm +275 -400
- 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/FormState.elm +6 -5
- package/src/Pages/GeneratorProgramConfig.elm +15 -0
- package/src/Pages/Internal/FatalError.elm +5 -0
- package/src/Pages/Internal/Form.elm +21 -1
- package/src/Pages/{Msg.elm → Internal/Msg.elm} +26 -16
- package/src/Pages/Internal/Platform/Cli.elm +598 -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 +373 -0
- package/src/Pages/Internal/Platform/StaticResponses.elm +73 -270
- package/src/Pages/Internal/Platform/ToJsPayload.elm +4 -7
- package/src/Pages/Internal/Platform.elm +216 -102
- 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 +14 -10
- 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 -98
- package/src/PagesMsg.elm +92 -0
- package/src/Path.elm +16 -19
- package/src/QueryParams.elm +21 -172
- package/src/RequestsAndPending.elm +8 -19
- package/src/Result/Extra.elm +26 -0
- package/src/Scaffold/Form.elm +560 -0
- package/src/Scaffold/Route.elm +1388 -0
- package/src/Server/Request.elm +43 -37
- package/src/Server/Session.elm +62 -42
- package/src/Server/SetCookie.elm +12 -4
- 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/Pages/Generate.elm +0 -800
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
module BackendTask.Http exposing
|
|
2
|
+
( get, getJson
|
|
3
|
+
, post
|
|
4
|
+
, Expect, expectString, expectJson, expectBytes, expectWhatever
|
|
5
|
+
, Error(..)
|
|
6
|
+
, request
|
|
7
|
+
, Body, emptyBody, stringBody, jsonBody, bytesBody
|
|
8
|
+
, getWithOptions
|
|
9
|
+
, CacheStrategy(..)
|
|
10
|
+
, withMetadata, Metadata
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
{-| `BackendTask.Http` requests are an alternative to doing Elm HTTP requests the traditional way using the `elm/http` package.
|
|
14
|
+
|
|
15
|
+
The key differences are:
|
|
16
|
+
|
|
17
|
+
- `BackendTask.Http.Request`s are performed once at build time (`Http.Request`s are performed at runtime, at whenever point you perform them)
|
|
18
|
+
- `BackendTask.Http.Request`s have a built-in `BackendTask.andThen` that allows you to perform follow-up requests without using tasks
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Scenarios where BackendTask.Http is a good fit
|
|
22
|
+
|
|
23
|
+
If you need data that is refreshed often you may want to do a traditional HTTP request with the `elm/http` package.
|
|
24
|
+
The kinds of situations that are served well by static HTTP are with data that updates moderately frequently or infrequently (or never).
|
|
25
|
+
A common pattern is to trigger a new build when data changes. Many JAMstack services
|
|
26
|
+
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
|
|
27
|
+
you may want to have your site rebuild everytime your calendar feed has an event added, or whenever a page or article is added
|
|
28
|
+
or updated on a CMS service like Contentful.
|
|
29
|
+
|
|
30
|
+
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
|
|
31
|
+
gains of using `BackendTask.Http.Request`s as well as the simplicity and robustness that comes with it. Read more about these benefits
|
|
32
|
+
in [this article introducing BackendTask.Http requests and some concepts around it](https://elm-pages.com/blog/static-http).
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Scenarios where BackendTask.Http is not a good fit
|
|
36
|
+
|
|
37
|
+
- Data that is specific to the logged-in user
|
|
38
|
+
- Data that needs to be the very latest and changes often (for example, sports scores)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
## Making a Request
|
|
42
|
+
|
|
43
|
+
@docs get, getJson
|
|
44
|
+
|
|
45
|
+
@docs post
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Decoding Request Body
|
|
49
|
+
|
|
50
|
+
@docs Expect, expectString, expectJson, expectBytes, expectWhatever
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## Error Handling
|
|
54
|
+
|
|
55
|
+
@docs Error
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## General Requests
|
|
59
|
+
|
|
60
|
+
@docs request
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
## Building a BackendTask.Http Request Body
|
|
64
|
+
|
|
65
|
+
The way you build a body is analogous to the `elm/http` package. Currently, only `emptyBody` and
|
|
66
|
+
`stringBody` are supported. If you have a use case that calls for a different body type, please open a Github issue
|
|
67
|
+
and describe your use case!
|
|
68
|
+
|
|
69
|
+
@docs Body, emptyBody, stringBody, jsonBody, bytesBody
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## Caching Options
|
|
73
|
+
|
|
74
|
+
`elm-pages` performs GET requests using a local HTTP cache by default. These requests are not performed using Elm's `elm/http`,
|
|
75
|
+
but rather are performed in NodeJS. Under the hood it uses [the NPM package `make-fetch-happen`](https://github.com/npm/make-fetch-happen).
|
|
76
|
+
Only GET requests made with `get`, `getJson`, or `getWithOptions` use local caching. Requests made with [`BackendTask.Http.request`](#request)
|
|
77
|
+
are not cached, even if the method is set to `GET`.
|
|
78
|
+
|
|
79
|
+
In dev mode, assets are cached more aggressively by default, whereas for a production build assets use a default to revalidate each cached response's freshness before using it (the `ForceRevalidate` [`CacheStrategy`](#CacheStrategy)).
|
|
80
|
+
|
|
81
|
+
The default caching behavior for GET requests is to use a local cache in `.elm-pages/http-cache`. This uses the same caching behavior
|
|
82
|
+
that browsers use to avoid re-downloading content when it hasn't changed. Servers can set HTTP response headers to explicitly control
|
|
83
|
+
this caching behavior.
|
|
84
|
+
|
|
85
|
+
- [`cache-control` HTTP response headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) let you set a length of time before considering an asset stale. This could mean that the server considers it acceptable for an asset to be somewhat outdated, or this could mean that the asset is guaranteed to be up-to-date until it is stale - those semantics are up to the server.
|
|
86
|
+
- `Last-Modified` and `ETag` HTTP response headers can be returned by the server allow [Conditional Requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Conditional_requests). Conditional Requests let us send back the `Last-Modified` timestamp or `etag` hash for assets that are in our local cache to the server to check if the asset is fresh, and skip re-downloading it if it is unchanged (or download a fresh one otherwise).
|
|
87
|
+
|
|
88
|
+
It's important to note that depending on how the server sets these HTTP response headers, we may have outdated data - either because the server explicitly allows assets to become outdated with their cache-control headers, OR because cache-control headers are not set. When these headers aren't explicitly set, [clients are allowed to cache assets for 10% of the amount of time since it was last modified](https://httpwg.org/specs/rfc7234.html#heuristic.freshness).
|
|
89
|
+
For production builds, the default caching will ignore both the implicit and explicit information about an asset's freshness and _always_ revalidate it before using a locally cached response.
|
|
90
|
+
|
|
91
|
+
@docs getWithOptions
|
|
92
|
+
|
|
93
|
+
@docs CacheStrategy
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
## Including HTTP Metadata
|
|
97
|
+
|
|
98
|
+
@docs withMetadata, Metadata
|
|
99
|
+
|
|
100
|
+
-}
|
|
101
|
+
|
|
102
|
+
import BackendTask exposing (BackendTask)
|
|
103
|
+
import Base64
|
|
104
|
+
import Bytes exposing (Bytes)
|
|
105
|
+
import Bytes.Decode
|
|
106
|
+
import Dict exposing (Dict)
|
|
107
|
+
import FatalError exposing (FatalError)
|
|
108
|
+
import Json.Decode
|
|
109
|
+
import Json.Encode as Encode
|
|
110
|
+
import Pages.Internal.StaticHttpBody as Body
|
|
111
|
+
import Pages.StaticHttp.Request as HashRequest
|
|
112
|
+
import Pages.StaticHttpRequest exposing (RawRequest(..))
|
|
113
|
+
import RequestsAndPending
|
|
114
|
+
import TerminalText
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
{-| Build an empty body for a BackendTask.Http request. See [elm/http's `Http.emptyBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#emptyBody).
|
|
118
|
+
-}
|
|
119
|
+
emptyBody : Body
|
|
120
|
+
emptyBody =
|
|
121
|
+
Body.EmptyBody
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
{-| Build a body from `Bytes` for a BackendTask.Http request. See [elm/http's `Http.bytesBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#bytesBody).
|
|
125
|
+
-}
|
|
126
|
+
bytesBody : String -> Bytes -> Body
|
|
127
|
+
bytesBody =
|
|
128
|
+
Body.BytesBody
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
{-| Builds a string body for a BackendTask.Http request. See [elm/http's `Http.stringBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#stringBody).
|
|
132
|
+
|
|
133
|
+
Note from the `elm/http` docs:
|
|
134
|
+
|
|
135
|
+
> The first argument is a [MIME type](https://en.wikipedia.org/wiki/Media_type) of the body. Some servers are strict about this!
|
|
136
|
+
|
|
137
|
+
-}
|
|
138
|
+
stringBody : String -> String -> Body
|
|
139
|
+
stringBody contentType content =
|
|
140
|
+
Body.StringBody contentType content
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
{-| Builds a JSON body for a BackendTask.Http request. See [elm/http's `Http.jsonBody`](https://package.elm-lang.org/packages/elm/http/latest/Http#jsonBody).
|
|
144
|
+
-}
|
|
145
|
+
jsonBody : Encode.Value -> Body
|
|
146
|
+
jsonBody content =
|
|
147
|
+
Body.JsonBody content
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
{-| A body for a BackendTask.Http request.
|
|
151
|
+
-}
|
|
152
|
+
type alias Body =
|
|
153
|
+
Body.Body
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
{-| A simplified helper around [`BackendTask.Http.get`](#get), which builds up a BackendTask.Http GET request with `expectJson`.
|
|
157
|
+
|
|
158
|
+
import BackendTask
|
|
159
|
+
import BackendTask.Http
|
|
160
|
+
import FatalError exposing (FatalError)
|
|
161
|
+
import Json.Decode as Decode exposing (Decoder)
|
|
162
|
+
|
|
163
|
+
getRequest : BackendTask (FatalError Error) Int
|
|
164
|
+
getRequest =
|
|
165
|
+
BackendTask.Http.getJson
|
|
166
|
+
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
167
|
+
(Decode.field "stargazers_count" Decode.int)
|
|
168
|
+
|
|
169
|
+
-}
|
|
170
|
+
getJson :
|
|
171
|
+
String
|
|
172
|
+
-> Json.Decode.Decoder a
|
|
173
|
+
-> BackendTask { fatal : FatalError, recoverable : Error } a
|
|
174
|
+
getJson url decoder =
|
|
175
|
+
getWithOptions
|
|
176
|
+
{ url = url
|
|
177
|
+
, expect = expectJson decoder
|
|
178
|
+
, headers = []
|
|
179
|
+
, timeoutInMs = Nothing
|
|
180
|
+
, retries = Nothing
|
|
181
|
+
, cacheStrategy = Nothing
|
|
182
|
+
, cachePath = Nothing
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
{-| A simplified helper around [`BackendTask.Http.getWithOptions`](#getWithOptions), which builds up a GET request with
|
|
187
|
+
the default retries, timeout, and HTTP caching options. If you need to configure those options or include HTTP request headers,
|
|
188
|
+
use the more flexible `getWithOptions`.
|
|
189
|
+
|
|
190
|
+
import BackendTask
|
|
191
|
+
import BackendTask.Http
|
|
192
|
+
import FatalError exposing (FatalError)
|
|
193
|
+
|
|
194
|
+
getRequest : BackendTask (FatalError Error) String
|
|
195
|
+
getRequest =
|
|
196
|
+
BackendTask.Http.get
|
|
197
|
+
"https://api.github.com/repos/dillonkearns/elm-pages"
|
|
198
|
+
BackendTask.Http.expectString
|
|
199
|
+
|
|
200
|
+
-}
|
|
201
|
+
get :
|
|
202
|
+
String
|
|
203
|
+
-> Expect a
|
|
204
|
+
-> BackendTask { fatal : FatalError, recoverable : Error } a
|
|
205
|
+
get url expect =
|
|
206
|
+
getWithOptions
|
|
207
|
+
{ url = url
|
|
208
|
+
, expect = expect
|
|
209
|
+
, headers = []
|
|
210
|
+
, timeoutInMs = Nothing
|
|
211
|
+
, retries = Nothing
|
|
212
|
+
, cacheStrategy = Nothing
|
|
213
|
+
, cachePath = Nothing
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
{-| Perform a GET request, with some additional options for the HTTP request, including options for caching behavior.
|
|
218
|
+
|
|
219
|
+
- `retries` - Default is 0. Will try performing request again if set to a number greater than 0.
|
|
220
|
+
- `timeoutInMs` - Default is no timeout.
|
|
221
|
+
- `cacheStrategy` - The [caching options are passed to the NPM package `make-fetch-happen`](https://github.com/npm/make-fetch-happen#opts-cache)
|
|
222
|
+
- `cachePath` - override the default directory for the local HTTP cache. This can be helpful if you want more granular control to clear some HTTP caches more or less frequently than others. Or you may want to preserve the local cache for some requests in your build server, but not store the cache for other requests.
|
|
223
|
+
|
|
224
|
+
-}
|
|
225
|
+
getWithOptions :
|
|
226
|
+
{ url : String
|
|
227
|
+
, expect : Expect a
|
|
228
|
+
, headers : List ( String, String )
|
|
229
|
+
, cacheStrategy : Maybe CacheStrategy
|
|
230
|
+
, retries : Maybe Int
|
|
231
|
+
, timeoutInMs : Maybe Int
|
|
232
|
+
, cachePath : Maybe String
|
|
233
|
+
}
|
|
234
|
+
-> BackendTask { fatal : FatalError, recoverable : Error } a
|
|
235
|
+
getWithOptions request__ =
|
|
236
|
+
let
|
|
237
|
+
request_ : HashRequest.Request
|
|
238
|
+
request_ =
|
|
239
|
+
{ url = request__.url
|
|
240
|
+
, headers = request__.headers
|
|
241
|
+
, body = emptyBody
|
|
242
|
+
, method = "GET"
|
|
243
|
+
, cacheOptions =
|
|
244
|
+
{ cacheStrategy = request__.cacheStrategy
|
|
245
|
+
, retries = request__.retries
|
|
246
|
+
, timeoutInMs = request__.timeoutInMs
|
|
247
|
+
, cachePath = request__.cachePath
|
|
248
|
+
}
|
|
249
|
+
|> encodeOptions
|
|
250
|
+
|> Just
|
|
251
|
+
}
|
|
252
|
+
in
|
|
253
|
+
requestRaw request_ request__.expect
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
{-| -}
|
|
257
|
+
post :
|
|
258
|
+
String
|
|
259
|
+
-> Body
|
|
260
|
+
-> Expect a
|
|
261
|
+
-> BackendTask { fatal : FatalError, recoverable : Error } a
|
|
262
|
+
post url body expect =
|
|
263
|
+
request
|
|
264
|
+
{ url = url
|
|
265
|
+
, method = "POST"
|
|
266
|
+
, headers = []
|
|
267
|
+
, body = body
|
|
268
|
+
, retries = Nothing
|
|
269
|
+
, timeoutInMs = Nothing
|
|
270
|
+
}
|
|
271
|
+
expect
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
{-| Analogous to the `Expect` type in the `elm/http` package. This represents how you will process the data that comes
|
|
275
|
+
back in your BackendTask.Http request.
|
|
276
|
+
|
|
277
|
+
You can derive `ExpectJson` from `ExpectString`. Or you could build your own helper to process the String
|
|
278
|
+
as XML, for example, or give an `elm-pages` build error if the response can't be parsed as XML.
|
|
279
|
+
|
|
280
|
+
-}
|
|
281
|
+
type Expect value
|
|
282
|
+
= ExpectJson (Json.Decode.Decoder value)
|
|
283
|
+
| ExpectString (String -> value)
|
|
284
|
+
| ExpectBytes (Bytes.Decode.Decoder value)
|
|
285
|
+
| ExpectWhatever value
|
|
286
|
+
| ExpectMetadata (Metadata -> Expect value)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
{-| Gives the HTTP response body as a raw String.
|
|
290
|
+
|
|
291
|
+
import BackendTask exposing (BackendTask)
|
|
292
|
+
import BackendTask.Http
|
|
293
|
+
|
|
294
|
+
request : BackendTask String
|
|
295
|
+
request =
|
|
296
|
+
BackendTask.Http.request
|
|
297
|
+
{ url = "https://example.com/file.txt"
|
|
298
|
+
, method = "GET"
|
|
299
|
+
, headers = []
|
|
300
|
+
, body = BackendTask.Http.emptyBody
|
|
301
|
+
}
|
|
302
|
+
BackendTask.Http.expectString
|
|
303
|
+
|
|
304
|
+
-}
|
|
305
|
+
expectString : Expect String
|
|
306
|
+
expectString =
|
|
307
|
+
ExpectString identity
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
{-| Handle the incoming response as JSON and don't optimize the asset and strip out unused values.
|
|
311
|
+
Be sure to use the `BackendTask.Http.request` function if you want an optimized request that
|
|
312
|
+
strips out unused JSON to optimize your asset size. This function makes sense to use for things like a GraphQL request
|
|
313
|
+
where the JSON payload is already trimmed down to the data you explicitly requested.
|
|
314
|
+
|
|
315
|
+
If the function you pass to `expectString` yields an `Err`, then you will get a build error that will
|
|
316
|
+
fail your `elm-pages` build and print out the String from the `Err`.
|
|
317
|
+
|
|
318
|
+
-}
|
|
319
|
+
expectJson : Json.Decode.Decoder value -> Expect value
|
|
320
|
+
expectJson =
|
|
321
|
+
ExpectJson
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
{-| -}
|
|
325
|
+
withMetadata : (Metadata -> value -> combined) -> Expect value -> Expect combined
|
|
326
|
+
withMetadata combineFn originalExpect =
|
|
327
|
+
-- known-unoptimized-recursion
|
|
328
|
+
case originalExpect of
|
|
329
|
+
ExpectJson jsonDecoder ->
|
|
330
|
+
ExpectMetadata (\metadata -> ExpectJson (jsonDecoder |> Json.Decode.map (combineFn metadata)))
|
|
331
|
+
|
|
332
|
+
ExpectString stringToValue ->
|
|
333
|
+
ExpectMetadata
|
|
334
|
+
(\metadata ->
|
|
335
|
+
ExpectString (\string -> string |> stringToValue |> combineFn metadata)
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
ExpectBytes bytesDecoder ->
|
|
339
|
+
ExpectMetadata (\metadata -> ExpectBytes (bytesDecoder |> Bytes.Decode.map (combineFn metadata)))
|
|
340
|
+
|
|
341
|
+
ExpectWhatever value ->
|
|
342
|
+
ExpectMetadata (\metadata -> ExpectWhatever (combineFn metadata value))
|
|
343
|
+
|
|
344
|
+
ExpectMetadata metadataToExpect ->
|
|
345
|
+
ExpectMetadata (\metadata -> withMetadata combineFn (metadataToExpect metadata))
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
{-| -}
|
|
349
|
+
expectBytes : Bytes.Decode.Decoder value -> Expect value
|
|
350
|
+
expectBytes =
|
|
351
|
+
ExpectBytes
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
{-| -}
|
|
355
|
+
expectWhatever : value -> Expect value
|
|
356
|
+
expectWhatever =
|
|
357
|
+
ExpectWhatever
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
expectToString : Expect a -> String
|
|
361
|
+
expectToString expect =
|
|
362
|
+
-- known-unoptimized-recursion
|
|
363
|
+
case expect of
|
|
364
|
+
ExpectJson _ ->
|
|
365
|
+
"ExpectJson"
|
|
366
|
+
|
|
367
|
+
ExpectString _ ->
|
|
368
|
+
"ExpectString"
|
|
369
|
+
|
|
370
|
+
ExpectBytes _ ->
|
|
371
|
+
"ExpectBytes"
|
|
372
|
+
|
|
373
|
+
ExpectWhatever _ ->
|
|
374
|
+
"ExpectWhatever"
|
|
375
|
+
|
|
376
|
+
ExpectMetadata toExpect ->
|
|
377
|
+
-- It's safe to call this with fake metadata to get the kind of Expect because the exposed
|
|
378
|
+
-- API, `withMetadata`, will never change the type of Expect it returns based on the metadata, it simply
|
|
379
|
+
-- wraps the Expect with the additional Metadata.
|
|
380
|
+
-- It's important not to expose the raw `ExpectMetadata` constructor however because that would break that guarantee.
|
|
381
|
+
toExpect
|
|
382
|
+
{ url = ""
|
|
383
|
+
, statusCode = 123
|
|
384
|
+
, statusText = ""
|
|
385
|
+
, headers = Dict.empty
|
|
386
|
+
}
|
|
387
|
+
|> expectToString
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
{-| -}
|
|
391
|
+
request :
|
|
392
|
+
{ url : String
|
|
393
|
+
, method : String
|
|
394
|
+
, headers : List ( String, String )
|
|
395
|
+
, body : Body
|
|
396
|
+
, retries : Maybe Int
|
|
397
|
+
, timeoutInMs : Maybe Int
|
|
398
|
+
}
|
|
399
|
+
-> Expect a
|
|
400
|
+
-> BackendTask { fatal : FatalError, recoverable : Error } a
|
|
401
|
+
request request__ expect =
|
|
402
|
+
let
|
|
403
|
+
request_ : HashRequest.Request
|
|
404
|
+
request_ =
|
|
405
|
+
{ url = request__.url
|
|
406
|
+
, headers = request__.headers
|
|
407
|
+
, method = request__.method
|
|
408
|
+
, body = request__.body
|
|
409
|
+
, cacheOptions =
|
|
410
|
+
{ cacheStrategy = Nothing -- cache strategy only applies to GET and HEAD, need to use getWithOptions to customize
|
|
411
|
+
, cachePath = Nothing
|
|
412
|
+
, retries = request__.retries
|
|
413
|
+
, timeoutInMs = request__.timeoutInMs
|
|
414
|
+
}
|
|
415
|
+
|> encodeOptions
|
|
416
|
+
|> Just
|
|
417
|
+
}
|
|
418
|
+
in
|
|
419
|
+
requestRaw request_ expect
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
{-| -}
|
|
423
|
+
type CacheStrategy
|
|
424
|
+
= IgnoreCache -- 'no-store'
|
|
425
|
+
| ForceRevalidate -- 'no-cache'
|
|
426
|
+
| ForceReload -- 'reload'
|
|
427
|
+
| ForceCache -- 'force-cache'
|
|
428
|
+
| ErrorUnlessCached -- 'only-if-cached'
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
encodeOptions :
|
|
432
|
+
{ cacheStrategy : Maybe CacheStrategy
|
|
433
|
+
, cachePath : Maybe String
|
|
434
|
+
, retries : Maybe Int
|
|
435
|
+
, timeoutInMs : Maybe Int
|
|
436
|
+
}
|
|
437
|
+
-> Encode.Value
|
|
438
|
+
encodeOptions options =
|
|
439
|
+
Encode.object
|
|
440
|
+
([ ( "cache"
|
|
441
|
+
, options.cacheStrategy
|
|
442
|
+
|> Maybe.map
|
|
443
|
+
(\cacheStrategy ->
|
|
444
|
+
case cacheStrategy of
|
|
445
|
+
IgnoreCache ->
|
|
446
|
+
"no-store"
|
|
447
|
+
|
|
448
|
+
ForceRevalidate ->
|
|
449
|
+
"no-cache"
|
|
450
|
+
|
|
451
|
+
ForceReload ->
|
|
452
|
+
"reload"
|
|
453
|
+
|
|
454
|
+
ForceCache ->
|
|
455
|
+
"force-cache"
|
|
456
|
+
|
|
457
|
+
ErrorUnlessCached ->
|
|
458
|
+
"only-if-cached"
|
|
459
|
+
)
|
|
460
|
+
|> Maybe.map Encode.string
|
|
461
|
+
)
|
|
462
|
+
, ( "retry", options.retries |> Maybe.map Encode.int )
|
|
463
|
+
, ( "timeout", options.timeoutInMs |> Maybe.map Encode.int )
|
|
464
|
+
, ( "cachePath", options.cachePath |> Maybe.map Encode.string )
|
|
465
|
+
]
|
|
466
|
+
|> List.filterMap
|
|
467
|
+
(\( a, b ) -> b |> Maybe.map (Tuple.pair a))
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
{-| Build a `BackendTask.Http` request (analogous to [Http.request](https://package.elm-lang.org/packages/elm/http/latest/Http#request)).
|
|
472
|
+
This function takes in all the details to build a `BackendTask.Http` request, but you can build your own simplified helper functions
|
|
473
|
+
with this as a low-level detail, or you can use functions like [BackendTask.Http.get](#get).
|
|
474
|
+
-}
|
|
475
|
+
requestRaw :
|
|
476
|
+
HashRequest.Request
|
|
477
|
+
-> Expect a
|
|
478
|
+
-> BackendTask { fatal : FatalError, recoverable : Error } a
|
|
479
|
+
requestRaw request__ expect =
|
|
480
|
+
let
|
|
481
|
+
request_ : HashRequest.Request
|
|
482
|
+
request_ =
|
|
483
|
+
{ url = request__.url
|
|
484
|
+
, headers =
|
|
485
|
+
( "elm-pages-internal", expectToString expect )
|
|
486
|
+
:: request__.headers
|
|
487
|
+
, method = request__.method
|
|
488
|
+
, body = request__.body
|
|
489
|
+
, cacheOptions = request__.cacheOptions
|
|
490
|
+
}
|
|
491
|
+
in
|
|
492
|
+
Request
|
|
493
|
+
[ request_ ]
|
|
494
|
+
(\maybeMockResolver rawResponseDict ->
|
|
495
|
+
(case maybeMockResolver of
|
|
496
|
+
Just mockResolver ->
|
|
497
|
+
mockResolver request_
|
|
498
|
+
|
|
499
|
+
Nothing ->
|
|
500
|
+
rawResponseDict |> RequestsAndPending.get (request_ |> HashRequest.hash)
|
|
501
|
+
)
|
|
502
|
+
|> (\maybeResponse ->
|
|
503
|
+
case maybeResponse of
|
|
504
|
+
Just rawResponse ->
|
|
505
|
+
Ok rawResponse
|
|
506
|
+
|
|
507
|
+
Nothing ->
|
|
508
|
+
--Err (Pages.StaticHttpRequest.UserCalledStaticHttpFail ("INTERNAL ERROR - expected request" ++ request_.url))
|
|
509
|
+
Err (BadBody Nothing ("INTERNAL ERROR - expected request" ++ request_.url))
|
|
510
|
+
)
|
|
511
|
+
|> Result.andThen
|
|
512
|
+
(\(RequestsAndPending.Response maybeResponse body) ->
|
|
513
|
+
let
|
|
514
|
+
maybeBadResponse : Maybe Error
|
|
515
|
+
maybeBadResponse =
|
|
516
|
+
case maybeResponse of
|
|
517
|
+
Just response ->
|
|
518
|
+
if not (response.statusCode >= 200 && response.statusCode < 300) then
|
|
519
|
+
case body of
|
|
520
|
+
RequestsAndPending.StringBody s ->
|
|
521
|
+
BadStatus
|
|
522
|
+
{ url = response.url
|
|
523
|
+
, statusCode = response.statusCode
|
|
524
|
+
, statusText = response.statusText
|
|
525
|
+
, headers = response.headers
|
|
526
|
+
}
|
|
527
|
+
s
|
|
528
|
+
|> Just
|
|
529
|
+
|
|
530
|
+
RequestsAndPending.BytesBody bytes ->
|
|
531
|
+
BadStatus
|
|
532
|
+
{ url = response.url
|
|
533
|
+
, statusCode = response.statusCode
|
|
534
|
+
, statusText = response.statusText
|
|
535
|
+
, headers = response.headers
|
|
536
|
+
}
|
|
537
|
+
(Base64.fromBytes bytes |> Maybe.withDefault "")
|
|
538
|
+
|> Just
|
|
539
|
+
|
|
540
|
+
RequestsAndPending.JsonBody value ->
|
|
541
|
+
BadStatus
|
|
542
|
+
{ url = response.url
|
|
543
|
+
, statusCode = response.statusCode
|
|
544
|
+
, statusText = response.statusText
|
|
545
|
+
, headers = response.headers
|
|
546
|
+
}
|
|
547
|
+
(Encode.encode 0 value)
|
|
548
|
+
|> Just
|
|
549
|
+
|
|
550
|
+
RequestsAndPending.WhateverBody ->
|
|
551
|
+
BadStatus
|
|
552
|
+
{ url = response.url
|
|
553
|
+
, statusCode = response.statusCode
|
|
554
|
+
, statusText = response.statusText
|
|
555
|
+
, headers = response.headers
|
|
556
|
+
}
|
|
557
|
+
""
|
|
558
|
+
|> Just
|
|
559
|
+
|
|
560
|
+
else
|
|
561
|
+
Nothing
|
|
562
|
+
|
|
563
|
+
Nothing ->
|
|
564
|
+
Nothing
|
|
565
|
+
in
|
|
566
|
+
case maybeBadResponse of
|
|
567
|
+
Just badResponse ->
|
|
568
|
+
Err badResponse
|
|
569
|
+
|
|
570
|
+
Nothing ->
|
|
571
|
+
toResultThing ( expect, body, maybeResponse )
|
|
572
|
+
)
|
|
573
|
+
|> BackendTask.fromResult
|
|
574
|
+
|> BackendTask.mapError
|
|
575
|
+
(\error ->
|
|
576
|
+
FatalError.recoverable (errorToString error) error
|
|
577
|
+
)
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
toResultThing :
|
|
582
|
+
( Expect value
|
|
583
|
+
, RequestsAndPending.ResponseBody
|
|
584
|
+
, Maybe RequestsAndPending.RawResponse
|
|
585
|
+
)
|
|
586
|
+
-> Result Error value
|
|
587
|
+
toResultThing ( expect, body, maybeResponse ) =
|
|
588
|
+
case ( expect, body, maybeResponse ) of
|
|
589
|
+
( ExpectMetadata toExpect, _, Just rawResponse ) ->
|
|
590
|
+
let
|
|
591
|
+
asMetadata : Metadata
|
|
592
|
+
asMetadata =
|
|
593
|
+
{ url = rawResponse.url
|
|
594
|
+
, statusCode = rawResponse.statusCode
|
|
595
|
+
, statusText = rawResponse.statusText
|
|
596
|
+
, headers = rawResponse.headers
|
|
597
|
+
}
|
|
598
|
+
in
|
|
599
|
+
toResultThing ( toExpect asMetadata, body, maybeResponse )
|
|
600
|
+
|
|
601
|
+
( ExpectJson decoder, RequestsAndPending.JsonBody json, _ ) ->
|
|
602
|
+
json
|
|
603
|
+
|> Json.Decode.decodeValue decoder
|
|
604
|
+
|> Result.mapError
|
|
605
|
+
(\error ->
|
|
606
|
+
error
|
|
607
|
+
|> Json.Decode.errorToString
|
|
608
|
+
|> BadBody (Just error)
|
|
609
|
+
)
|
|
610
|
+
|
|
611
|
+
( ExpectString mapStringFn, RequestsAndPending.StringBody string, _ ) ->
|
|
612
|
+
string
|
|
613
|
+
|> mapStringFn
|
|
614
|
+
|> Ok
|
|
615
|
+
|
|
616
|
+
( ExpectBytes bytesDecoder, RequestsAndPending.BytesBody rawBytes, _ ) ->
|
|
617
|
+
rawBytes
|
|
618
|
+
|> Bytes.Decode.decode bytesDecoder
|
|
619
|
+
|> Result.fromMaybe
|
|
620
|
+
(BadBody Nothing "Bytes decoding failed.")
|
|
621
|
+
|
|
622
|
+
( ExpectWhatever whateverValue, RequestsAndPending.WhateverBody, _ ) ->
|
|
623
|
+
Ok whateverValue
|
|
624
|
+
|
|
625
|
+
_ ->
|
|
626
|
+
Err (BadBody Nothing "Unexpected combination, internal error")
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
errorToString : Error -> { title : String, body : String }
|
|
630
|
+
errorToString error =
|
|
631
|
+
{ title = "HTTP Error"
|
|
632
|
+
, body =
|
|
633
|
+
(case error of
|
|
634
|
+
BadUrl string ->
|
|
635
|
+
[ TerminalText.text ("BadUrl " ++ string)
|
|
636
|
+
]
|
|
637
|
+
|
|
638
|
+
Timeout ->
|
|
639
|
+
[ TerminalText.text "Timeout"
|
|
640
|
+
]
|
|
641
|
+
|
|
642
|
+
NetworkError ->
|
|
643
|
+
[ TerminalText.text "NetworkError"
|
|
644
|
+
]
|
|
645
|
+
|
|
646
|
+
BadStatus _ string ->
|
|
647
|
+
[ TerminalText.text ("BadStatus: " ++ string)
|
|
648
|
+
]
|
|
649
|
+
|
|
650
|
+
BadBody _ string ->
|
|
651
|
+
[ TerminalText.text ("BadBody: " ++ string)
|
|
652
|
+
]
|
|
653
|
+
)
|
|
654
|
+
|> TerminalText.toString
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
{-| -}
|
|
659
|
+
type alias Metadata =
|
|
660
|
+
{ url : String
|
|
661
|
+
, statusCode : Int
|
|
662
|
+
, statusText : String
|
|
663
|
+
, headers : Dict String String
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
{-| -}
|
|
668
|
+
type Error
|
|
669
|
+
= BadUrl String
|
|
670
|
+
| Timeout
|
|
671
|
+
| NetworkError
|
|
672
|
+
| BadStatus Metadata String
|
|
673
|
+
| BadBody (Maybe Json.Decode.Error) String
|