-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGenerate.elm
More file actions
2793 lines (2508 loc) · 131 KB
/
Generate.elm
File metadata and controls
2793 lines (2508 loc) · 131 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module OpenApi.Generate exposing (ContentSchema, Message, Path, Mime, files)
{-|
@docs ContentSchema, Message, Path, Mime, files
-}
import CliMonad exposing (CliMonad)
import Common
import Dict
import Dict.Extra
import Elm
import Elm.Annotation
import Elm.Arg
import Elm.Case
import Elm.Op
import FastDict
import FastSet
import Gen.BackendTask
import Gen.BackendTask.Http
import Gen.Base64
import Gen.Basics
import Gen.Bytes
import Gen.Bytes.Decode
import Gen.Debug
import Gen.Dict
import Gen.Effect.Http
import Gen.Effect.Task
import Gen.FatalError
import Gen.Http
import Gen.Json.Decode
import Gen.Json.Encode
import Gen.List
import Gen.Maybe
import Gen.String
import Gen.Task
import Gen.Url.Builder
import Json.Schema.Definitions
import JsonSchema.Generate
import List.Extra
import NonEmpty
import OpenApi
import OpenApi.Common.Internal
import OpenApi.Components
import OpenApi.Config
import OpenApi.MediaType
import OpenApi.Operation
import OpenApi.Parameter
import OpenApi.Path
import OpenApi.Reference
import OpenApi.RequestBody
import OpenApi.Response
import OpenApi.Schema
import OpenApi.SecurityRequirement
import OpenApi.SecurityScheme
import OpenApi.Server
import Pretty
import Regex exposing (Regex)
import SchemaUtils
import String.Extra
{-| -}
type alias Mime =
String
{-| -}
type alias Message =
{ message : String
, path : Path
, details : Pretty.Doc ()
}
{-| -}
type alias Path =
List String
{-| -}
type ContentSchema
= EmptyContent
| JsonContent Common.Type
| StringContent Mime
| BytesContent Mime
| Base64Content Mime
| ReferenceContent (Common.RefTo Common.RequestBody)
type alias AuthorizationInfo =
{ headers : Elm.Expression -> List ( Elm.Expression, Elm.Expression )
, query : Elm.Expression -> List ( Elm.Expression, Elm.Expression )
, params : List ( Common.UnsafeName, Elm.Annotation.Annotation )
, scopes : List String
}
type alias PerPackage a =
{ core : a
, elmPages : a
, lamderaProgramTest : a
}
{-| -}
files :
OpenApi.Config.Generate
-> OpenApi.OpenApi
->
Result
Message
{ modules :
List
{ moduleName : List String
, declarations : FastDict.Dict String { group : String, declaration : Elm.Declaration }
}
, warnings : List Message
, requiredPackages : FastSet.Set String
}
files { namespace, generateTodos, effectTypes, server, formats, warnOnMissingEnums, keepGoing } apiSpec =
case extractEnums apiSpec of
Err e ->
Err e
Ok enums ->
serverInfo server
|> CliMonad.andThen
(\info ->
[ pathDeclarations effectTypes info
, schemasDeclarations
, responsesDeclarations
, requestBodiesDeclarations
, serverDeclarations info
|> CliMonad.succeed
|> CliMonad.withPath (Common.UnsafeName "servers")
]
|> CliMonad.combine
)
|> CliMonad.map List.concat
|> CliMonad.withPath (Common.UnsafeName (String.join "." namespace))
|> CliMonad.run
SchemaUtils.oneOfDeclarations
{ openApi = apiSpec
, generateTodos = generateTodos
, enums = enums
, namespace = namespace
, formats = formats
, warnOnMissingEnums = warnOnMissingEnums
, keepGoing = keepGoing
}
|> Result.map
(\{ declarations, warnings, requiredPackages } ->
{ modules =
declarations
|> Dict.Extra.groupBy (\{ moduleName } -> Common.moduleToNamespace namespace moduleName)
|> Dict.toList
|> List.map
(\( moduleName, group ) ->
{ moduleName = moduleName
, declarations =
group
|> List.map
(\declaration ->
( declaration.name
, { group = declaration.group
, declaration = declaration.declaration
}
)
)
|> FastDict.fromList
}
)
, warnings = warnings
, requiredPackages = requiredPackages
}
)
extractEnums :
OpenApi.OpenApi
->
Result
Message
(FastDict.Dict (List String) { name : Common.UnsafeName, documentation : Maybe String })
extractEnums openApi =
openApi
|> OpenApi.components
|> Maybe.map OpenApi.Components.schemas
|> Maybe.withDefault Dict.empty
|> Dict.foldl
(\name schema q ->
Result.andThen
(\acc ->
case OpenApi.Schema.get schema of
Json.Schema.Definitions.ObjectSchema subSchema ->
case SchemaUtils.subschemaToEnumMaybe subSchema of
Ok (Just { decodedEnums, hasNull }) ->
if hasNull then
-- If the enum can contain null then we require another named enum, without null, to name it
Ok acc
else
Ok
(FastDict.insert
(List.sort (NonEmpty.toList decodedEnums))
{ name = Common.UnsafeName name
, documentation = subSchema.description
}
acc
)
Ok Nothing ->
Ok acc
Err e ->
Err
{ message = e
, path = [ name, "Extracting enums" ]
, details = Pretty.empty
}
_ ->
Ok acc
)
q
)
(Ok FastDict.empty)
serverDeclarations : ServerInfo -> List CliMonad.Declaration
serverDeclarations server =
case server of
MultipleServers list ->
list
|> List.map
(\{ name, url, description } ->
let
safeName : String
safeName =
Common.toValueName name
in
{ moduleName = Common.Servers
, name = safeName
, declaration =
url
|> stripTrailingSlash
|> Elm.string
|> Elm.declaration safeName
|> (case description of
Nothing ->
identity
Just doc ->
Elm.withDocumentation doc
)
|> Elm.exposeConstructor
, group = "Servers"
}
)
SingleServer _ ->
[]
stripTrailingSlash : String -> String
stripTrailingSlash input =
if String.endsWith "/" input then
String.dropRight 1 input
else
input
pathDeclarations : List OpenApi.Config.EffectType -> ServerInfo -> CliMonad (List CliMonad.Declaration)
pathDeclarations effectTypes server =
CliMonad.getApiSpec
|> CliMonad.andThen
(\spec ->
spec
|> OpenApi.paths
|> Dict.toList
|> CliMonad.combineMap
(\( url, path ) ->
[ ( "GET", OpenApi.Path.get )
, ( "POST", OpenApi.Path.post )
, ( "PUT", OpenApi.Path.put )
, ( "PATCH", OpenApi.Path.patch )
, ( "DELETE", OpenApi.Path.delete )
, ( "HEAD", OpenApi.Path.head )
, ( "TRACE", OpenApi.Path.trace )
]
|> List.filterMap (\( method, getter ) -> Maybe.map (Tuple.pair method) (getter path))
|> CliMonad.combineMap
(\( method, operation ) ->
toRequestFunctions server effectTypes method url operation
|> CliMonad.errorToWarning
)
|> CliMonad.map (List.filterMap identity >> List.concat)
)
|> CliMonad.map List.concat
|> CliMonad.withPath (Common.UnsafeName "paths")
)
responsesDeclarations : CliMonad (List CliMonad.Declaration)
responsesDeclarations =
CliMonad.getApiSpec
|> CliMonad.andThen
(\spec ->
spec
|> OpenApi.components
|> Maybe.map OpenApi.Components.responses
|> Maybe.withDefault Dict.empty
|> Dict.foldl
(\name schema ->
CliMonad.map2 (::)
(responseToDeclarations (Common.UnsafeName name) schema)
)
(CliMonad.succeed [])
|> CliMonad.map List.concat
|> CliMonad.withPath (Common.UnsafeName "responses")
)
requestBodiesDeclarations : CliMonad (List CliMonad.Declaration)
requestBodiesDeclarations =
CliMonad.getApiSpec
|> CliMonad.andThen
(\spec ->
spec
|> OpenApi.components
|> Maybe.map OpenApi.Components.requestBodies
|> Maybe.withDefault Dict.empty
|> Dict.foldl
(\name schema ->
CliMonad.map2 (::)
(requestBodyToDeclarations (Common.UnsafeName name) schema)
)
(CliMonad.succeed [])
|> CliMonad.map List.concat
|> CliMonad.withPath (Common.UnsafeName "requestBodies")
)
schemasDeclarations : CliMonad (List CliMonad.Declaration)
schemasDeclarations =
CliMonad.getApiSpec
|> CliMonad.andThen
(\spec ->
spec
|> OpenApi.components
|> Maybe.map OpenApi.Components.schemas
|> Maybe.withDefault Dict.empty
|> Dict.foldl
(\name schema ->
CliMonad.map2
(\decls declAcc -> decls ++ declAcc)
(JsonSchema.Generate.schemaToDeclarations Common.Schema
(Common.UnsafeName name)
(OpenApi.Schema.get schema)
)
)
(CliMonad.succeed [])
|> CliMonad.withPath (Common.UnsafeName "schemas")
)
unitDeclarations : Common.Component -> Common.UnsafeName -> CliMonad (List CliMonad.Declaration)
unitDeclarations component name =
let
typeName : Common.TypeName
typeName =
Common.toTypeName name
in
CliMonad.combine
[ { moduleName = Common.Types component
, name = typeName
, declaration =
Elm.alias typeName Elm.Annotation.unit
|> Elm.expose
, group = "Aliases"
}
|> CliMonad.succeed
, CliMonad.map2
(\importFrom schemaDecoder ->
{ moduleName = Common.Json component
, name = "decode" ++ typeName
, declaration =
Elm.declaration ("decode" ++ typeName)
(schemaDecoder
|> Elm.withType (Gen.Json.Decode.annotation_.decoder (Elm.Annotation.named importFrom typeName))
)
|> Elm.exposeConstructor
, group = "Decoders"
}
)
(CliMonad.moduleToNamespace (Common.Types component))
(SchemaUtils.typeToDecoder Common.Unit)
, CliMonad.map2
(\importFrom encoder ->
{ moduleName = Common.Json component
, name = "encode" ++ typeName
, declaration =
Elm.declaration ("encode" ++ typeName)
(Elm.functionReduced "rec" encoder
|> Elm.withType (Elm.Annotation.function [ Elm.Annotation.named importFrom typeName ] Gen.Json.Encode.annotation_.value)
)
|> Elm.expose
, group = "Encoders"
}
)
(CliMonad.moduleToNamespace (Common.Types component))
(SchemaUtils.typeToEncoder Common.Unit)
]
responseToDeclarations : Common.UnsafeName -> OpenApi.Reference.ReferenceOr OpenApi.Response.Response -> CliMonad (List CliMonad.Declaration)
responseToDeclarations name reference =
case OpenApi.Reference.toConcrete reference of
Just response ->
let
content : Dict.Dict String OpenApi.MediaType.MediaType
content =
OpenApi.Response.content response
in
if Dict.isEmpty content then
-- If there is no input content then we go with the unit value, `()` as the response type
unitDeclarations Common.Response name
else
responseToSchema response
|> CliMonad.withPath name
|> CliMonad.andThen (JsonSchema.Generate.schemaToDeclarations Common.Response name)
Nothing ->
CliMonad.fail "Could not convert reference to concrete value"
|> CliMonad.withPath name
requestBodyToDeclarations : Common.UnsafeName -> OpenApi.Reference.ReferenceOr OpenApi.RequestBody.RequestBody -> CliMonad (List CliMonad.Declaration)
requestBodyToDeclarations name reference =
case OpenApi.Reference.toConcrete reference of
Just requestBody ->
let
content : Dict.Dict String OpenApi.MediaType.MediaType
content =
OpenApi.RequestBody.content requestBody
in
if Dict.isEmpty content then
-- If there is no content then we go with the unit value, `()` as the requestBody type
unitDeclarations Common.RequestBody name
else
requestBodyToSchema requestBody
|> CliMonad.withPath name
|> CliMonad.andThen (JsonSchema.Generate.schemaToDeclarations Common.RequestBody name)
Nothing ->
CliMonad.fail "Could not convert reference to concrete value"
|> CliMonad.withPath name
toRequestFunctions : ServerInfo -> List OpenApi.Config.EffectType -> String -> String -> OpenApi.Operation.Operation -> CliMonad (List CliMonad.Declaration)
toRequestFunctions server effectTypes method pathUrl operation =
let
functionName : String
functionName =
OpenApi.Operation.operationId operation
|> Maybe.withDefault pathUrl
|> makeNamespaceValid
|> removeInvalidChars
|> String.Extra.camelize
|> (\n ->
if String.isEmpty n then
"root"
else
n
)
isSinglePackage : Bool
isSinglePackage =
(effectTypes
|> List.map OpenApi.Config.effectTypeToPackage
|> List.Extra.unique
|> List.length
)
== 1
toMsg : Elm.Expression -> Elm.Expression
toMsg config =
Elm.get "toMsg" config
body :
ContentSchema
-> CliMonad (Elm.Expression -> PerPackage Elm.Expression)
body bodyContent =
case bodyContent of
EmptyContent ->
CliMonad.succeed
(\_ ->
{ core = Gen.Http.emptyBody
, elmPages = Gen.BackendTask.Http.emptyBody
, lamderaProgramTest = Gen.Effect.Http.emptyBody
}
)
JsonContent type_ ->
SchemaUtils.typeToEncoder type_
|> CliMonad.map
(\encoder config ->
let
encoded : Elm.Expression
encoded =
encoder <| Elm.get "body" config
in
{ core = Gen.Http.jsonBody encoded
, elmPages = Gen.BackendTask.Http.jsonBody encoded
, lamderaProgramTest = Gen.Effect.Http.jsonBody encoded
}
)
StringContent mime ->
CliMonad.succeed <|
\config ->
let
toBody : (Elm.Expression -> Elm.Expression -> Elm.Expression) -> Elm.Expression
toBody f =
f (Elm.string mime) (Elm.get "body" config)
in
{ core = toBody Gen.Http.call_.stringBody
, elmPages = toBody Gen.BackendTask.Http.call_.stringBody
, lamderaProgramTest = toBody Gen.Effect.Http.call_.stringBody
}
BytesContent mime ->
CliMonad.succeed <|
\config ->
let
toBody : (String -> Elm.Expression -> Elm.Expression) -> Elm.Expression
toBody f =
f mime (Elm.get "body" config)
in
{ core = toBody Gen.Http.bytesBody
, elmPages = toBody Gen.BackendTask.Http.bytesBody
, lamderaProgramTest = toBody Gen.Effect.Http.bytesBody
}
Base64Content mime ->
CliMonad.succeed <|
\config ->
let
toBody : (Elm.Expression -> Elm.Expression -> Elm.Expression) -> Elm.Expression
toBody f =
f (Elm.string mime)
(Elm.get "body" config
|> Gen.Base64.fromBytes
|> Gen.Maybe.withDefault (Elm.string "")
)
in
{ core = toBody Gen.Http.call_.stringBody
, elmPages = toBody Gen.BackendTask.Http.call_.stringBody
, lamderaProgramTest = toBody Gen.Effect.Http.call_.stringBody
}
ReferenceContent _ ->
CliMonad.map
(\e _ ->
{ core = e
, elmPages = e
, lamderaProgramTest = e
}
)
(CliMonad.todo "toRequestFunctions: branch 'ReferenceContent _' not implemented")
bodyParams : ContentSchema -> CliMonad (List ( Common.UnsafeName, Elm.Annotation.Annotation ))
bodyParams contentSchema =
let
annotation : CliMonad (Maybe Elm.Annotation.Annotation)
annotation =
case contentSchema of
EmptyContent ->
CliMonad.succeed Nothing
JsonContent type_ ->
SchemaUtils.typeToAnnotationWithNullable type_
|> CliMonad.map Just
StringContent _ ->
CliMonad.succeed (Just Elm.Annotation.string)
BytesContent _ ->
CliMonad.succeed (Just Gen.Bytes.annotation_.bytes)
|> CliMonad.withRequiredPackage "elm/bytes"
Base64Content _ ->
CliMonad.succeed (Just Gen.Bytes.annotation_.bytes)
|> CliMonad.withRequiredPackage "elm/bytes"
|> CliMonad.withRequiredPackage Common.base64PackageName
ReferenceContent _ ->
CliMonad.fail "toRequestFunctions: branch 'ReferenceContent _' not implemented"
in
annotation
|> CliMonad.map
(\maybeAnnotation ->
case maybeAnnotation of
Nothing ->
[]
Just ann ->
[ ( Common.UnsafeName "body", ann ) ]
)
headersFromList : (Elm.Expression -> Elm.Expression -> Elm.Expression) -> AuthorizationInfo -> Elm.Expression -> List (Elm.Expression -> ( Elm.Expression, Elm.Expression, Bool )) -> Elm.Expression
headersFromList f auth config headerFunctions =
let
headerParams : List ( Elm.Expression, Elm.Expression, Bool )
headerParams =
List.map (\toHeader -> toHeader config) headerFunctions
hasMaybes : Bool
hasMaybes =
List.any (\( _, _, isMaybe ) -> isMaybe) headerParams
authHeaders : List Elm.Expression
authHeaders =
List.map
(\( k, v ) ->
if hasMaybes then
Elm.just
(f k v)
else
f k v
)
(auth.headers config)
paramHeaders : List Elm.Expression
paramHeaders =
List.map
(\( k, v, isMaybe ) ->
if isMaybe then
Gen.Maybe.map (f k) v
else if hasMaybes then
Elm.just (f k v)
else
f k v
)
headerParams
in
case authHeaders ++ paramHeaders of
[] ->
Elm.list []
allHeaders ->
allHeaders
|> Elm.list
|> (if hasMaybes then
Gen.List.call_.filterMap Gen.Basics.values_.identity
else
identity
)
documentation : AuthorizationInfo -> String
documentation { scopes } =
let
summaryDoc : Maybe String
summaryDoc =
OpenApi.Operation.summary operation
descriptionDoc : Maybe String
descriptionDoc =
OpenApi.Operation.description operation
scopesDoc : Maybe String
scopesDoc =
if List.isEmpty scopes then
Nothing
else
("This operations requires the following scopes:"
:: List.map
(\scope ->
" - `" ++ scope ++ "`"
)
scopes
)
|> String.join "\n"
|> Just
in
[ summaryDoc
, descriptionDoc
, scopesDoc
]
|> List.filterMap identity
|> String.join "\n\n"
step : OperationUtils -> CliMonad (List CliMonad.Declaration)
step { successType, bodyTypeAnnotation, errorTypeDeclaration, errorTypeAnnotation, expect, resolver } =
let
declarationGroup :
(PerPackage (CliMonad (Elm.Expression -> Elm.Expression)) -> CliMonad (Elm.Expression -> Elm.Expression))
-> AuthorizationInfo
-> ((Elm.Expression -> Elm.Expression) -> a)
-> List ( OpenApi.Config.EffectType, a -> ( String, Elm.Expression ) )
-> CliMonad (List CliMonad.Declaration)
declarationGroup package auth sharedData list =
if List.any (\( effectType, _ ) -> List.member effectType effectTypes) list then
package expect
|> CliMonad.map
(\specificExpect ->
let
shared : a
shared =
sharedData specificExpect
in
List.filterMap
(\( effectType, toDeclaration ) ->
if List.member effectType effectTypes then
let
( name, expr ) =
toDeclaration shared
in
{ moduleName =
if isSinglePackage then
Common.Api Nothing
else
Common.Api (Just (OpenApi.Config.effectTypeToPackage effectType))
, name = name
, declaration =
expr
|> Elm.declaration name
|> Elm.withDocumentation (documentation auth)
|> Elm.expose
, group =
operationToGroup operation
}
|> Just
else
Nothing
)
list
)
else
CliMonad.succeed []
elmHttpCommands :
AuthorizationInfo
-> List (Elm.Expression -> ( Elm.Expression, Elm.Expression, Bool ))
-> Elm.Annotation.Annotation
-> (Elm.Expression -> PerPackage Elm.Expression)
-> (Elm.Expression -> Elm.Expression)
-> ({ requireToMsg : Bool } -> PerPackage Elm.Annotation.Annotation)
-> CliMonad (List CliMonad.Declaration)
elmHttpCommands auth toHeaderParams _ toBody replaced paramType =
declarationGroup .core
auth
(\specificExpect ->
{ cmdArg =
\config ->
Elm.record
[ ( "url", replaced config )
, ( "method", Elm.string method )
, ( "headers"
, headersFromList Gen.Http.call_.header auth config toHeaderParams
)
, ( "expect", specificExpect <| toMsg config )
, ( "body", (toBody config).core )
, ( "timeout", Gen.Maybe.make_.nothing )
, ( "tracker", Gen.Maybe.make_.nothing )
]
, cmdAnnotation =
Elm.Annotation.function
[ (paramType { requireToMsg = True }).core ]
(Elm.Annotation.cmd (Elm.Annotation.var "msg"))
, recordAnnotation =
Elm.Annotation.function
[ (paramType { requireToMsg = True }).core ]
(Elm.Annotation.record
[ ( "method", Elm.Annotation.string )
, ( "headers", Elm.Annotation.list Gen.Http.annotation_.header )
, ( "url", Elm.Annotation.string )
, ( "body", Gen.Http.annotation_.body )
, ( "expect", Gen.Http.annotation_.expect (Elm.Annotation.var "msg") )
, ( "timeout", Elm.Annotation.maybe Elm.Annotation.float )
, ( "tracker", Elm.Annotation.maybe Elm.Annotation.string )
]
)
}
)
[ ( OpenApi.Config.ElmHttpCmd
, \{ cmdArg, cmdAnnotation } ->
( functionName
, Elm.fn
(Elm.Arg.var "config")
(\config -> Gen.Http.call_.request (cmdArg config))
|> Elm.withType cmdAnnotation
)
)
, ( OpenApi.Config.ElmHttpCmdRisky
, \{ cmdArg, cmdAnnotation } ->
( functionName ++ "Risky"
, Elm.fn
(Elm.Arg.var "config")
(\config -> Gen.Http.call_.riskyRequest (cmdArg config))
|> Elm.withType cmdAnnotation
)
)
, ( OpenApi.Config.ElmHttpCmdRecord
, \{ cmdArg, recordAnnotation } ->
( functionName ++ "Record"
, Elm.fn
(Elm.Arg.var "config")
cmdArg
|> Elm.withType recordAnnotation
)
)
]
elmHttpTasks :
AuthorizationInfo
-> List (Elm.Expression -> ( Elm.Expression, Elm.Expression, Bool ))
-> Elm.Annotation.Annotation
-> (Elm.Expression -> PerPackage Elm.Expression)
-> (Elm.Expression -> Elm.Expression)
-> ({ requireToMsg : Bool } -> PerPackage Elm.Annotation.Annotation)
-> CliMonad (List CliMonad.Declaration)
elmHttpTasks auth toHeaderParams successAnnotation toBody replaced paramType =
declarationGroup .core
auth
(\_ ->
{ taskArg =
\config ->
Elm.record
[ ( "url", replaced config )
, ( "method", Elm.string method )
, ( "headers"
, headersFromList Gen.Http.call_.header auth config toHeaderParams
)
, ( "resolver", resolver.core )
, ( "body", (toBody config).core )
, ( "timeout", Gen.Maybe.make_.nothing )
]
, taskAnnotation =
Elm.Annotation.function
[ (paramType { requireToMsg = False }).core ]
(Gen.Task.annotation_.task
(OpenApi.Common.Internal.annotation_.error errorTypeAnnotation bodyTypeAnnotation)
successAnnotation
)
, recordAnnotation =
Elm.Annotation.function
[ (paramType { requireToMsg = False }).core ]
(Elm.Annotation.record
[ ( "method", Elm.Annotation.string )
, ( "headers", Gen.Http.annotation_.header )
, ( "url", Elm.Annotation.string )
, ( "body", Gen.Http.annotation_.body )
, ( "resolver"
, Gen.Http.annotation_.resolver
(OpenApi.Common.Internal.annotation_.error errorTypeAnnotation bodyTypeAnnotation)
successAnnotation
)
, ( "timeout", Elm.Annotation.maybe Elm.Annotation.float )
]
)
}
)
[ ( OpenApi.Config.ElmHttpTask
, \{ taskArg, taskAnnotation } ->
( functionName ++ "Task"
, Elm.fn
(Elm.Arg.var "config")
(\config -> Gen.Http.call_.task (taskArg config))
|> Elm.withType taskAnnotation
)
)
, ( OpenApi.Config.ElmHttpTaskRisky
, \{ taskArg, taskAnnotation } ->
( functionName ++ "TaskRisky"
, Elm.fn
(Elm.Arg.var "config")
(\config -> Gen.Http.call_.riskyTask (taskArg config))
|> Elm.withType taskAnnotation
)
)
, ( OpenApi.Config.ElmHttpTaskRecord
, \{ taskArg, recordAnnotation } ->
( functionName ++ "TaskRecord"
, Elm.fn
(Elm.Arg.var "config")
taskArg
|> Elm.withType recordAnnotation
)
)
]
dillonkearnsElmPagesBackendTask :
AuthorizationInfo
-> List (Elm.Expression -> ( Elm.Expression, Elm.Expression, Bool ))
-> Elm.Annotation.Annotation
-> (Elm.Expression -> PerPackage Elm.Expression)
-> (Elm.Expression -> Elm.Expression)
-> ({ requireToMsg : Bool } -> PerPackage Elm.Annotation.Annotation)
-> CliMonad (List CliMonad.Declaration)
dillonkearnsElmPagesBackendTask auth toHeaderParams successAnnotation toBody replaced paramType =
declarationGroup .elmPages
auth
(\specificExpect ->
{ taskArg =
\config ->
Elm.record
[ ( "url", replaced config )
, ( "method", Elm.string method )
, ( "headers"
, headersFromList Elm.tuple auth config toHeaderParams
)
, ( "body", (toBody config).elmPages )
, ( "retries", Gen.Maybe.make_.nothing )
, ( "timeoutInMs", Gen.Maybe.make_.nothing )
]
, taskAnnotation =
Elm.Annotation.function
[ (paramType { requireToMsg = False }).elmPages ]
(Gen.BackendTask.annotation_.backendTask
(Elm.Annotation.record
[ ( "fatal", Gen.FatalError.annotation_.fatalError )
, ( "recoverable", Gen.BackendTask.Http.annotation_.error )
]
)
successAnnotation
)
, recordAnnotation =
Elm.Annotation.function
[ (paramType { requireToMsg = False }).elmPages ]
(Elm.Annotation.tuple
(Elm.Annotation.record
[ ( "url", Elm.Annotation.string )
, ( "method", Elm.Annotation.string )
, ( "headers", Elm.Annotation.list (Elm.Annotation.tuple Elm.Annotation.string Elm.Annotation.string) )
, ( "body", Gen.BackendTask.Http.annotation_.body )
, ( "retries", Elm.Annotation.maybe Elm.Annotation.int )
, ( "timeoutInMs", Elm.Annotation.maybe Elm.Annotation.int )
]
)
(Gen.BackendTask.Http.annotation_.expect (Elm.Annotation.var "a"))
)
, specificExpect = specificExpect
}
)
[ ( OpenApi.Config.DillonkearnsElmPagesTask
, \{ taskArg, taskAnnotation, specificExpect } ->
( functionName
, Elm.fn
(Elm.Arg.var "config")
(\config -> Gen.BackendTask.Http.call_.request (taskArg config) (specificExpect <| toMsg config))
|> Elm.withType taskAnnotation
)
)
, ( OpenApi.Config.DillonkearnsElmPagesTaskRecord
, \{ taskArg, recordAnnotation, specificExpect } ->
( functionName
, Elm.fn
(Elm.Arg.var "config")
(\config -> Elm.tuple (taskArg config) (specificExpect <| toMsg config))
|> Elm.withType recordAnnotation
)
)
]
lamderaProgramTestCommands :
AuthorizationInfo
-> List (Elm.Expression -> ( Elm.Expression, Elm.Expression, Bool ))
-> Elm.Annotation.Annotation
-> (Elm.Expression -> PerPackage Elm.Expression)
-> (Elm.Expression -> Elm.Expression)
-> ({ requireToMsg : Bool } -> PerPackage Elm.Annotation.Annotation)
-> CliMonad (List CliMonad.Declaration)
lamderaProgramTestCommands auth toHeaderParams _ toBody replaced paramType =
declarationGroup .lamderaProgramTest
auth
(\specificExpect ->
{ cmdArg =
\config ->
Elm.record
[ ( "url", replaced config )
, ( "method", Elm.string method )
, ( "headers"
, headersFromList Gen.Effect.Http.call_.header auth config toHeaderParams
)
, ( "expect", specificExpect <| toMsg config )