elm-pages 3.0.0-beta.30 → 3.0.0-beta.32

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 (41) hide show
  1. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmi +0 -0
  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 +4 -104
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +7125 -6415
  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 +4 -4
  12. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmi +0 -0
  13. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Internal-RoutePattern.elmo +0 -0
  14. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmi +0 -0
  15. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-NoContractViolations.elmo +0 -0
  16. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  17. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  18. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  19. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
  20. package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +4 -104
  21. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +16847 -16037
  22. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  23. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  24. package/generator/review/elm.json +5 -5
  25. package/generator/src/build.js +2 -1
  26. package/generator/src/cli.js +104 -97
  27. package/generator/src/compatibility-key.js +1 -1
  28. package/generator/src/compile-elm.js +18 -1
  29. package/generator/src/render.js +0 -2
  30. package/generator/src/resolve-elm-module.js +64 -0
  31. package/generator/src/rewrite-client-elm-json.js +1 -0
  32. package/generator/src/rewrite-elm-json-help.js +56 -0
  33. package/generator/src/rewrite-elm-json.js +13 -3
  34. package/generator/template/elm.json +1 -2
  35. package/package.json +17 -16
  36. package/src/Pages/Internal/Platform/Cli.elm +91 -0
  37. package/src/Pages/Internal/Platform.elm +1 -0
  38. package/src/Scaffold/Form.elm +190 -128
  39. package/src/Scaffold/Route.elm +280 -254
  40. package/src/Server/Session.elm +29 -9
  41. package/src/Server/SetCookie.elm +11 -3
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "elm-pages",
3
3
  "type": "module",
4
- "version": "3.0.0-beta.30",
4
+ "version": "3.0.0-beta.32",
5
5
  "homepage": "https://elm-pages.com",
6
6
  "moduleResolution": "node",
7
7
  "description": "Type-safe static sites, written in pure elm with your own custom elm-markup syntax.",
@@ -25,27 +25,27 @@
25
25
  "author": "Dillon Kearns",
26
26
  "license": "BSD-3-Clause",
27
27
  "dependencies": {
28
- "busboy": "^1.0.0",
28
+ "busboy": "^1.6.0",
29
29
  "chokidar": "^3.5.3",
30
30
  "commander": "^10.0.0",
31
31
  "connect": "^3.7.0",
32
- "cookie-signature": "^1.1.0",
32
+ "cookie-signature": "^1.2.1",
33
33
  "cross-spawn": "7.0.3",
34
34
  "devcert": "^1.2.2",
35
35
  "elm-doc-preview": "^5.0.5",
36
36
  "elm-hot": "^1.1.6",
37
- "esbuild": "0.17.10",
38
- "fs-extra": "^11.1.0",
37
+ "esbuild": "^0.17.12",
38
+ "fs-extra": "^11.1.1",
39
39
  "globby": "13.1.3",
40
40
  "gray-matter": "^4.0.3",
41
41
  "jsesc": "^3.0.2",
42
42
  "kleur": "^4.1.5",
43
43
  "make-fetch-happen": "^11.0.3",
44
- "memfs": "^3.4.7",
44
+ "memfs": "^3.4.13",
45
45
  "micromatch": "^4.0.5",
46
46
  "serve-static": "^1.15.0",
47
- "terser": "^5.16.3",
48
- "vite": "^4.1.1",
47
+ "vite": "^4.2.1",
48
+ "terser": "^5.16.8",
49
49
  "which": "^3.0.0"
50
50
  },
51
51
  "devDependencies": {
@@ -53,18 +53,19 @@
53
53
  "@types/fs-extra": "^11.0.1",
54
54
  "@types/make-fetch-happen": "^10.0.1",
55
55
  "@types/micromatch": "^4.0.2",
56
- "@types/node": "^18.11.9",
57
- "@types/serve-static": "^1.15.0",
58
- "cypress": "^12.5.1",
56
+ "@types/node": "^18.15.7",
57
+ "@types/serve-static": "^1.15.1",
58
+ "cypress": "^12.8.1",
59
59
  "elm-codegen": "^0.3.0",
60
60
  "elm-optimize-level-2": "^0.3.5",
61
- "elm-review": "^2.8.2",
61
+ "elm-review": "^2.9.2",
62
62
  "elm-test": "^0.19.1-revision11",
63
- "elm-tooling": "^1.12.0",
63
+ "elm-tooling": "^1.13.1",
64
64
  "elm-verify-examples": "^5.2.0",
65
- "elmi-to-json": "^1.2.0",
66
- "typescript": "^4.9.5",
67
- "vitest": "^0.28.5"
65
+ "elmi-to-json": "^1.4.3",
66
+ "typescript": "^5.0.2",
67
+ "vite": "^4.2.1",
68
+ "vitest": "^0.29.7"
68
69
  },
69
70
  "files": [
70
71
  "generator/src/",
@@ -21,6 +21,7 @@ import Json.Decode as Decode
21
21
  import Json.Encode
22
22
  import PageServerResponse exposing (PageServerResponse)
23
23
  import Pages.Flags
24
+ import Pages.Internal.FatalError
24
25
  import Pages.Internal.NotFoundReason as NotFoundReason exposing (NotFoundReason)
25
26
  import Pages.Internal.Platform.CompatibilityKey
26
27
  import Pages.Internal.Platform.Effect as Effect exposing (Effect)
@@ -677,6 +678,96 @@ initLegacy site ((RenderRequest.SinglePage includeHtml singleRequest _) as rende
677
678
  config.sharedData
678
679
  globalHeadTags
679
680
  )
681
+ |> BackendTask.onError
682
+ (\((Pages.Internal.FatalError.FatalError fatalError) as error) ->
683
+ let
684
+ isPreRendered : Bool
685
+ isPreRendered =
686
+ let
687
+ keys : Int
688
+ keys =
689
+ RenderRequest.maybeRequestPayload renderRequest |> Maybe.map (Decode.decodeValue (Decode.keyValuePairs Decode.value)) |> Maybe.withDefault (Ok []) |> Result.withDefault [] |> List.map Tuple.first |> List.length
690
+ in
691
+ -- TODO this is a bit hacky, would be nice to clean up the way of checking whether this is server-rendered or pre-rendered
692
+ keys <= 1
693
+ in
694
+ if isDevServer || isPreRendered then
695
+ -- we want to stop the build for pre-rendered routes, and give a dev server error popup in the dev server
696
+ BackendTask.fail error
697
+
698
+ else
699
+ --only render the production ErrorPage in production server-rendered Routes
700
+ config.sharedData
701
+ |> BackendTask.andThen
702
+ (\justSharedData ->
703
+ let
704
+ errorPage : errorPage
705
+ errorPage =
706
+ config.internalError fatalError.body
707
+
708
+ dataThing : pageData
709
+ dataThing =
710
+ errorPage
711
+ |> config.errorPageToData
712
+
713
+ statusCode : Int
714
+ statusCode =
715
+ config.errorStatusCode errorPage
716
+
717
+ byteEncodedPageData : Bytes
718
+ byteEncodedPageData =
719
+ ResponseSketch.HotUpdate
720
+ dataThing
721
+ justSharedData
722
+ -- TODO remove shared action data
723
+ Nothing
724
+ |> config.encodeResponse
725
+ |> Bytes.Encode.encode
726
+
727
+ pageModel : userModel
728
+ pageModel =
729
+ config.init
730
+ Pages.Flags.PreRenderFlags
731
+ justSharedData
732
+ dataThing
733
+ Nothing
734
+ (Just
735
+ { path =
736
+ { path = currentPage.path
737
+ , query = Nothing
738
+ , fragment = Nothing
739
+ }
740
+ , metadata = currentPage.route
741
+ , pageUrl = Nothing
742
+ }
743
+ )
744
+ |> Tuple.first
745
+
746
+ currentPage : { path : Path, route : route }
747
+ currentPage =
748
+ { path = serverRequestPayload.path, route = urlToRoute config currentUrl }
749
+
750
+ viewValue : { title : String, body : List (Html (PagesMsg userMsg)) }
751
+ viewValue =
752
+ (config.view Dict.empty Dict.empty Nothing currentPage Nothing justSharedData dataThing Nothing |> .view)
753
+ pageModel
754
+ in
755
+ { route = Path.toAbsolute currentPage.path
756
+ , contentJson = Dict.empty
757
+ , html = viewValue.body |> bodyToString
758
+ , errors = []
759
+ , head = [] -- TODO render head tags --config.view Dict.empty Dict.empty Nothing pathAndRoute Nothing justSharedData pageData Nothing |> .head
760
+ , title = viewValue.title
761
+ , staticHttpCache = Dict.empty
762
+ , is404 = False
763
+ , statusCode = statusCode
764
+ , headers = []
765
+ }
766
+ |> ToJsPayload.PageProgress
767
+ |> Effect.SendSinglePageNew byteEncodedPageData
768
+ |> BackendTask.succeed
769
+ )
770
+ )
680
771
 
681
772
  Just notFoundReason ->
682
773
  render404Page config
@@ -309,6 +309,7 @@ init config flags url key =
309
309
  type Msg userMsg pageData actionData sharedData errorPage
310
310
  = LinkClicked Browser.UrlRequest
311
311
  | UrlChanged Url
312
+ -- TODO rename to PagesMsg
312
313
  | UserMsg (PagesMsg userMsg)
313
314
  | SetField { formId : String, name : String, value : String }
314
315
  | UpdateCacheAndUrlNew Bool Url (Maybe userMsg) (Result Http.Error ( Url, ResponseSketch pageData actionData sharedData ))
@@ -1,6 +1,7 @@
1
1
  module Scaffold.Form exposing
2
2
  ( Kind(..), provide, restArgsParser
3
3
  , Context
4
+ , fieldEncoder, recordEncoder
4
5
  )
5
6
 
6
7
  {-|
@@ -9,12 +10,15 @@ module Scaffold.Form exposing
9
10
 
10
11
  @docs Context
11
12
 
13
+ @docs fieldEncoder, recordEncoder
14
+
12
15
  -}
13
16
 
14
17
  import Cli.Option
15
18
  import Elm
16
19
  import Elm.Annotation as Type
17
20
  import Elm.Declare
21
+ import Elm.Op
18
22
  import List.Extra
19
23
  import Result.Extra
20
24
 
@@ -40,17 +44,7 @@ type alias Context =
40
44
  }
41
45
 
42
46
 
43
- {-| -}
44
- formWithFields :
45
- Bool
46
- -> List ( String, Kind )
47
- ->
48
- ({ formState : Context
49
- , params : List { name : String, kind : Kind, param : Elm.Expression }
50
- }
51
- -> Elm.Expression
52
- )
53
- -> { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression }
47
+ formWithFields : Bool -> List ( String, Kind ) -> ({ formState : { errors : Elm.Expression, isTransitioning : Elm.Expression, submitAttempted : Elm.Expression, data : Elm.Expression, expression : Elm.Expression }, params : List { name : String, kind : Kind, param : Elm.Expression } } -> Elm.Expression) -> { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression, value : List String -> Elm.Expression }
54
48
  formWithFields elmCssView fields viewFn =
55
49
  Elm.Declare.function "form"
56
50
  []
@@ -59,84 +53,89 @@ formWithFields elmCssView fields viewFn =
59
53
  |> List.foldl
60
54
  (\( fieldName, kind ) chain ->
61
55
  chain
62
- |> formField fieldName
63
- (case kind of
64
- FieldText ->
65
- formFieldText
66
- |> formFieldRequired (Elm.string "Required")
67
-
68
- FieldInt ->
69
- formFieldInt { invalid = \_ -> Elm.string "" }
70
- |> formFieldRequired (Elm.string "Required")
71
-
72
- FieldTextarea ->
73
- formFieldText
74
- |> formFieldRequired (Elm.string "Required")
75
- |> formFieldTextarea
76
- { rows = Elm.nothing
77
- , cols = Elm.nothing
78
- }
56
+ |> Elm.Op.pipe
57
+ (formField fieldName
58
+ (case kind of
59
+ FieldText ->
60
+ formFieldText
61
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
62
+
63
+ FieldInt ->
64
+ formFieldInt { invalid = \_ -> Elm.string "" }
65
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
66
+
67
+ FieldTextarea ->
68
+ formFieldText
69
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
70
+ |> Elm.Op.pipe
71
+ (formFieldTextarea
72
+ { rows = Elm.nothing
73
+ , cols = Elm.nothing
74
+ }
75
+ )
79
76
 
80
- FieldFloat ->
81
- formFieldFloat { invalid = \_ -> Elm.string "" }
82
- |> formFieldRequired (Elm.string "Required")
77
+ FieldFloat ->
78
+ formFieldFloat { invalid = \_ -> Elm.string "" }
79
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
83
80
 
84
- FieldTime ->
85
- formFieldTime { invalid = \_ -> Elm.string "" }
86
- |> formFieldRequired (Elm.string "Required")
81
+ FieldTime ->
82
+ formFieldTime { invalid = \_ -> Elm.string "" }
83
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
87
84
 
88
- FieldDate ->
89
- formFieldDate { invalid = \_ -> Elm.string "" }
90
- |> formFieldRequired (Elm.string "Required")
85
+ FieldDate ->
86
+ formFieldDate { invalid = \_ -> Elm.string "" }
87
+ |> Elm.Op.pipe (formFieldRequired (Elm.string "Required"))
91
88
 
92
- FieldCheckbox ->
93
- formFieldCheckbox
89
+ FieldCheckbox ->
90
+ formFieldCheckbox
91
+ )
94
92
  )
95
93
  )
96
- (formInit
97
- (Elm.function (List.map fieldToParam fields)
98
- (\params ->
99
- Elm.record
100
- [ ( "combine"
101
- , params
102
- |> List.foldl
103
- (\fieldExpression chain ->
104
- chain
105
- |> validationAndMap fieldExpression
106
- )
107
- (validationSucceed (Elm.val "ParsedForm"))
108
- )
109
- , ( "view"
110
- , Elm.fn ( "formState", Nothing )
111
- (\formState ->
112
- let
113
- mappedParams : List { name : String, kind : Kind, param : Elm.Expression }
114
- mappedParams =
115
- params
116
- |> List.Extra.zip fields
117
- |> List.map
118
- (\( ( name, kind ), param ) ->
119
- { name = name
120
- , kind = kind
121
- , param = param
122
- }
123
- )
124
- in
125
- viewFn
126
- { formState =
127
- { errors = formState |> Elm.get "errors"
128
- , isTransitioning = formState |> Elm.get "isTransitioning"
129
- , submitAttempted = formState |> Elm.get "submitAttempted"
130
- , data = formState |> Elm.get "data"
131
- , expression = formState
132
- }
133
- , params = mappedParams
134
- }
94
+ (Elm.function (List.map fieldToParam fields)
95
+ (\params ->
96
+ Elm.record
97
+ [ ( "combine"
98
+ , params
99
+ |> List.foldl
100
+ (\fieldExpression chain ->
101
+ chain
102
+ |> Elm.Op.pipe (validationAndMap fieldExpression)
135
103
  )
136
- )
137
- ]
138
- )
104
+ (Elm.val "ParsedForm"
105
+ |> Elm.Op.pipe validationSucceed
106
+ )
107
+ )
108
+ , ( "view"
109
+ , Elm.fn ( "formState", Nothing )
110
+ (\formState ->
111
+ let
112
+ mappedParams : List { name : String, kind : Kind, param : Elm.Expression }
113
+ mappedParams =
114
+ params
115
+ |> List.Extra.zip fields
116
+ |> List.map
117
+ (\( ( name, kind ), param ) ->
118
+ { name = name
119
+ , kind = kind
120
+ , param = param
121
+ }
122
+ )
123
+ in
124
+ viewFn
125
+ { formState =
126
+ { errors = formState |> Elm.get "errors"
127
+ , isTransitioning = formState |> Elm.get "isTransitioning"
128
+ , submitAttempted = formState |> Elm.get "submitAttempted"
129
+ , data = formState |> Elm.get "data"
130
+ , expression = formState
131
+ }
132
+ , params = mappedParams
133
+ }
134
+ )
135
+ )
136
+ ]
139
137
  )
138
+ |> Elm.Op.pipe formInit
140
139
  )
141
140
  |> Elm.withType
142
141
  (Type.namedWith [ "Form" ]
@@ -226,15 +225,11 @@ provide { fields, view, elmCssView } =
226
225
 
227
226
  else
228
227
  let
229
- form : { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression }
228
+ form : { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression, value : List String -> Elm.Expression }
230
229
  form =
231
230
  formWithFields elmCssView fields view
232
231
 
233
- formHandlersDeclaration :
234
- { declaration : Elm.Declaration
235
- , call : List Elm.Expression -> Elm.Expression
236
- , callFrom : List String -> List Elm.Expression -> Elm.Expression
237
- }
232
+ formHandlersDeclaration : { declaration : Elm.Declaration, call : List Elm.Expression -> Elm.Expression, callFrom : List String -> List Elm.Expression -> Elm.Expression, value : List String -> Elm.Expression }
238
233
  formHandlersDeclaration =
239
234
  -- TODO customizable formHandlers name?
240
235
  Elm.Declare.function "formHandlers"
@@ -295,8 +290,8 @@ provide { fields, view, elmCssView } =
295
290
  }
296
291
 
297
292
 
298
- validationAndMap : Elm.Expression -> Elm.Expression -> Elm.Expression
299
- validationAndMap andMapArg andMapArg0 =
293
+ validationAndMap : Elm.Expression -> Elm.Expression
294
+ validationAndMap andMapArg =
300
295
  Elm.apply
301
296
  (Elm.value
302
297
  { importFrom = [ "Form", "Validation" ]
@@ -304,19 +299,16 @@ validationAndMap andMapArg andMapArg0 =
304
299
  , annotation = Nothing
305
300
  }
306
301
  )
307
- [ andMapArg, andMapArg0 ]
302
+ [ andMapArg ]
308
303
 
309
304
 
310
- validationSucceed : Elm.Expression -> Elm.Expression
311
- validationSucceed succeedArg =
312
- Elm.apply
313
- (Elm.value
314
- { importFrom = [ "Form", "Validation" ]
315
- , name = "succeed"
316
- , annotation = Nothing
317
- }
318
- )
319
- [ succeedArg ]
305
+ validationSucceed : Elm.Expression
306
+ validationSucceed =
307
+ Elm.value
308
+ { importFrom = [ "Form", "Validation" ]
309
+ , name = "succeed"
310
+ , annotation = Nothing
311
+ }
320
312
 
321
313
 
322
314
  formFieldText : Elm.Expression
@@ -328,8 +320,8 @@ formFieldText =
328
320
  }
329
321
 
330
322
 
331
- formFieldRequired : Elm.Expression -> Elm.Expression -> Elm.Expression
332
- formFieldRequired requiredArg requiredArg0 =
323
+ formFieldRequired : Elm.Expression -> Elm.Expression
324
+ formFieldRequired requiredArg =
333
325
  Elm.apply
334
326
  (Elm.value
335
327
  { importFrom = [ "Form", "Field" ]
@@ -337,7 +329,7 @@ formFieldRequired requiredArg requiredArg0 =
337
329
  , annotation = Nothing
338
330
  }
339
331
  )
340
- [ requiredArg, requiredArg0 ]
332
+ [ requiredArg ]
341
333
 
342
334
 
343
335
  formFieldInt : { invalid : Elm.Expression -> Elm.Expression } -> Elm.Expression
@@ -361,8 +353,7 @@ formFieldInt intArg =
361
353
  formFieldTextarea :
362
354
  { rows : Elm.Expression, cols : Elm.Expression }
363
355
  -> Elm.Expression
364
- -> Elm.Expression
365
- formFieldTextarea textareaArg textareaArg0 =
356
+ formFieldTextarea textareaArg =
366
357
  Elm.apply
367
358
  (Elm.value
368
359
  { importFrom = [ "Form", "Field" ]
@@ -374,7 +365,6 @@ formFieldTextarea textareaArg textareaArg0 =
374
365
  [ Tuple.pair "rows" textareaArg.rows
375
366
  , Tuple.pair "cols" textareaArg.cols
376
367
  ]
377
- , textareaArg0
378
368
  ]
379
369
 
380
370
 
@@ -438,8 +428,8 @@ formFieldFloat floatArg =
438
428
  ]
439
429
 
440
430
 
441
- formField : String -> Elm.Expression -> Elm.Expression -> Elm.Expression
442
- formField fieldArg fieldArg0 fieldArg1 =
431
+ formField : String -> Elm.Expression -> Elm.Expression
432
+ formField fieldArg fieldArg0 =
443
433
  Elm.apply
444
434
  (Elm.value
445
435
  { importFrom = [ "Form" ]
@@ -447,29 +437,28 @@ formField fieldArg fieldArg0 fieldArg1 =
447
437
  , annotation = Nothing
448
438
  }
449
439
  )
450
- [ Elm.string fieldArg, fieldArg0, fieldArg1 ]
440
+ [ Elm.string fieldArg, fieldArg0 ]
451
441
 
452
442
 
453
- formInit : Elm.Expression -> Elm.Expression
454
- formInit initArg =
455
- Elm.apply
456
- (Elm.value
457
- { importFrom = [ "Form" ]
458
- , name = "hiddenKind"
459
- , annotation = Nothing
460
- }
461
- )
462
- [ Elm.tuple (Elm.string "kind") (Elm.string "regular")
463
- , Elm.string "Expected kind."
464
- , Elm.apply
465
- (Elm.value
466
- { importFrom = [ "Form" ]
467
- , name = "init"
468
- , annotation = Nothing
469
- }
443
+ formInit : Elm.Expression
444
+ formInit =
445
+ Elm.value
446
+ { importFrom = [ "Form" ]
447
+ , name = "init"
448
+ , annotation = Nothing
449
+ }
450
+ |> Elm.Op.pipe
451
+ (Elm.apply
452
+ (Elm.value
453
+ { importFrom = [ "Form" ]
454
+ , name = "hiddenKind"
455
+ , annotation = Nothing
456
+ }
457
+ )
458
+ [ Elm.tuple (Elm.string "kind") (Elm.string "regular")
459
+ , Elm.string "Expected kind."
460
+ ]
470
461
  )
471
- [ initArg ]
472
- ]
473
462
 
474
463
 
475
464
  initCombined : Elm.Expression -> Elm.Expression -> Elm.Expression
@@ -482,3 +471,76 @@ initCombined initCombinedArg initCombinedArg0 =
482
471
  }
483
472
  )
484
473
  [ initCombinedArg, initCombinedArg0 ]
474
+
475
+
476
+ {-| Generate a JSON Encoder for the form fields. This can be helpful for sending the validated form data through a
477
+ BackendTask.Custom or to an external API from your scaffolded Route Module code.
478
+ -}
479
+ recordEncoder : Elm.Expression -> List ( String, Kind ) -> Elm.Expression
480
+ recordEncoder record fields =
481
+ fields
482
+ |> List.map
483
+ (\( field, kind ) ->
484
+ Elm.tuple
485
+ (Elm.string field)
486
+ (fieldEncoder record field kind)
487
+ )
488
+ |> Elm.list
489
+ |> List.singleton
490
+ |> Elm.apply
491
+ (Elm.value
492
+ { importFrom = [ "Json", "Encode" ]
493
+ , name = "object"
494
+ , annotation =
495
+ Just
496
+ (Type.function
497
+ [ Type.list
498
+ (Type.tuple
499
+ Type.string
500
+ (Type.namedWith [ "Json", "Encode" ] "Value" [])
501
+ )
502
+ ]
503
+ (Type.namedWith [ "Json", "Encode" ] "Value" [])
504
+ )
505
+ }
506
+ )
507
+
508
+
509
+ {-| -}
510
+ fieldEncoder : Elm.Expression -> String -> Kind -> Elm.Expression
511
+ fieldEncoder record name kind =
512
+ Elm.apply
513
+ (case kind of
514
+ FieldInt ->
515
+ encoder "int"
516
+
517
+ FieldText ->
518
+ encoder "string"
519
+
520
+ FieldTextarea ->
521
+ encoder "string"
522
+
523
+ FieldFloat ->
524
+ encoder "float"
525
+
526
+ FieldTime ->
527
+ -- TODO fix time encoder
528
+ encoder "int"
529
+
530
+ FieldDate ->
531
+ -- TODO fix date encoder
532
+ encoder "int"
533
+
534
+ FieldCheckbox ->
535
+ encoder "bool"
536
+ )
537
+ [ Elm.get name record ]
538
+
539
+
540
+ encoder : String -> Elm.Expression
541
+ encoder name =
542
+ Elm.value
543
+ { importFrom = [ "Json", "Encode" ]
544
+ , name = name
545
+ , annotation = Nothing
546
+ }