/
Rest.purs
165 lines (104 loc) · 5.15 KB
/
Rest.purs
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
module Erl.Cowboy.Handlers.Rest
where
import Attribute (Attribute(..), Behaviour)
import Effect.Uncurried (EffectFn2, EffectFn3)
import Erl.Atom (Atom)
import Erl.Cowboy.Handlers.Common as C
import Erl.Cowboy.Req (Req)
import Erl.Data.List (List)
import Erl.Data.Tuple (Tuple2)
import Erl.ModuleName (NativeModuleName)
foreign import data InitResult :: Type -> Type
foreign import initResult :: forall a. a -> Req -> InitResult a
type InitHandler c s = EffectFn2 Req c (InitResult s)
type TerminateHandler s = EffectFn3 C.TerminateReason Req s C.TerminateResult
-- | RestResult r s is the result of a rest callback with result r and state s
foreign import data RestResult :: Type -> Type -> Type
foreign import restResult :: forall r s. r -> s -> Req -> RestResult r s
foreign import stop :: forall r s. s -> Req -> RestResult r s
foreign import switchHandler :: forall r s. NativeModuleName -> s -> Req -> RestResult r s
type RestHandler r s = EffectFn2 Req s (RestResult r s)
-- | Handler for allowed_methods callback
type AllowedMethodsHandler s = RestHandler (List String) s
-- | Handler for allow_missing_post callback
type AllowedMissingPostHandler s = RestHandler Boolean s
-- | Handler for charsets_provided callback
type CharsetsProvidedHandler s = RestHandler (List String) s
data ContentType = ContentType String String ContentTypeParams | SimpleContentType String
data ContentTypeParams = AnyParams | ContentTypeParams (List (Tuple2 String String))
foreign import data ContentTypesAcceptedResult :: Type
-- foreign import data AcceptCallbackResult :: Type -> Type
-- type AcceptCallback s = EffectFn2 Req s (AcceptCallbackResult s)
newtype AcceptCallback = AcceptCallback Atom
foreign import contentTypesAcceptedResult :: List (Tuple2 ContentType AcceptCallback) -> ContentTypesAcceptedResult
-- | Handler for content_types_accepted callback
type ContentTypesAcceptedHandler s = RestHandler ContentTypesAcceptedResult s
foreign import data ContentTypesProvidedResult :: Type
-- foreign import data ProvideCallbackResult :: Type -> Type
-- type ProvideCallback s = EffectFn2 Req s (ProvideCallbackResult s)
newtype ProvideCallback = ProvideCallback Atom
foreign import contentTypesProvidedResult :: List (Tuple2 ContentType ProvideCallback) -> ContentTypesProvidedResult
-- | Handler for content_types_provided callback
type ContentTypesProvidedHandler s = RestHandler ContentTypesProvidedResult s
-- | Handler for delete_completed callback
type DeleteCompletedHandler s = RestHandler Boolean s
-- | Handler for delete_resource callback
type DeleteResourceHandler s = RestHandler Boolean s
-- TODO: Representation of calendar:datetime()
-- | Handler for expires callback
-- type ExpiresCallback = RestHandler
-- | Handler for forbidden callback
type ForbiddenHandler s = RestHandler Boolean s
-- | Strong or weak etag
data ETag = Strong String | Weak String
-- | Handler for generate_etag callback
type GenerateEtagHandler s = RestHandler ETag s
foreign import data IsAuthorizedResponse :: Type
foreign import authorized :: IsAuthorizedResponse
foreign import unauthorized :: String -> IsAuthorizedResponse
-- | Handler for is_authorized callback
type IsAuthorizedHandler s = RestHandler IsAuthorizedResponse s
-- | Handler for _ callback
type IsConflictHandler s = RestHandler Boolean s
-- | Handler for _ callback
type KnownMethodsHandler s = RestHandler (List String) s
-- | Handler for _ callback
type LanguagesProvidedHandler s = RestHandler (List String) s
-- TODO date
-- | Handler for _ callback
-- type LastModifiedHandler s = RestHandler ?
-- | Handler for _ callback
type MalformedRequestHandler s = RestHandler Boolean s
foreign import data MovedResult :: Type
foreign import notMoved :: MovedResult
foreign import moved :: String -> MovedResult
-- | Handler for moved_permanently callback
type MovedPermanentlyHandler s = RestHandler MovedResult s
-- | Handler for moved_temporarily callback
type MovedTemporarilyHandler s = RestHandler MovedResult s
-- | Handler for multiple_choices callback
type MultipleChoicesHandler s = RestHandler Boolean s
foreign import data OptionsResponse :: Type
foreign import optionsResponse :: OptionsResponse
-- | Handler for options callback
type OptionsHandler s = RestHandler OptionsResponse s
-- | Handler for previously_existed callback
type PreviouslyExistedHandler s = RestHandler Boolean s
-- | Handler for resource_exists callback
type ResourceExistsHandler s = RestHandler Boolean s
-- | Handler for service_available callback
type ServiceAvailableHandler s = RestHandler Boolean s
-- | Handler for uri_too_long callback
type UriTooLongHandler s = RestHandler Boolean s
-- | Handler for valid_content_headers callback
type ValidContentHeadersHandler s = RestHandler Boolean s
-- | Handler for valid_entity_length callback
type ValidEntityLengthHandler s = RestHandler Boolean s
-- | Handler for variances callback
type VariancesHandler s = RestHandler (List String) s
type CowboyRestBehaviour = Behaviour "cowboy_rest"
-- | A cowboy_rest behaviour. Note that while may callbacks are defined only init is mandatory
cowboyRestBehaviour :: forall a s.
{ init :: InitHandler a s
} -> CowboyRestBehaviour
cowboyRestBehaviour _ = Attribute