-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMain.purs
171 lines (148 loc) · 4.52 KB
/
Main.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
166
167
168
169
170
171
module Payload.Examples.Movies.Main where
import Prelude
import Data.Either (Either, note)
import Data.Map as Map
import Effect.Aff (Aff)
import Node.HTTP as HTTP
import Payload.Server.Cookies (requestCookies)
import Payload.Spec (type (:), Spec(Spec), DELETE, GET, Guards, POST, Routes, Nil)
-- Example API based on The Movie Database API at
-- https://developers.themoviedb.org
moviesApiSpec :: Spec {
guards :: {
apiKey :: ApiKey,
sessionId :: SessionId
},
routes :: {
v1 :: Routes "/v1" {
guards :: Guards ("apiKey" : Nil),
auth :: Routes "/authentication" {
token :: Routes "/token" {
new :: GET "/new" {
response :: RequestTokenResponse
}
},
session :: Routes "/session" {
create :: POST "/new" {
body :: { requestToken :: String },
response :: SessionIdResponse
},
delete :: DELETE "/" {
body :: { sessionId :: String },
response :: StatusResponse
}
}
},
movies :: Routes "/movies" {
latest :: GET "/latest" {
response :: Movie
},
popular :: GET "/popular" {
response :: { results :: Array Movie }
},
byId :: Routes "/<movieId>" {
params :: { movieId :: Int },
get :: GET "/" {
response :: Movie
},
rating :: Routes "/rating" {
guards :: Guards ("sessionId" : Nil),
create :: POST "/rating" {
body :: RatingValue,
response :: StatusCodeResponse
},
delete :: DELETE "/rating" {
response :: StatusCodeResponse
}
}
}
}
}
}
}
moviesApiSpec = Spec
type Movie =
{ id :: Int
, title :: String }
type Date = String
type RequestTokenResponse =
{ success :: Boolean
, expiresAt :: Date
, requestToken :: String }
type SessionIdResponse =
{ success :: Boolean
, sessionId :: Date
, requestToken :: String }
type StatusResponse =
{ success :: Boolean }
type StatusCodeResponse =
{ statusCode :: Int
, statusMessage :: String }
type RatingValue =
{ value :: Number }
type ApiKey = String
type SessionId = String
data Path (s :: Symbol) = Path
moviesApi = {
handlers: {
v1: {
auth: {
token: {
new: newToken
},
session: {
create: createSession,
delete: deleteSession
}
},
movies: {
latest: latestMovie,
popular: popularMovies,
byId: {
get: getMovie,
rating: {
create: createRating,
delete: deleteRating
}
}
}
}
},
guards: {
apiKey: getApiKey,
sessionId: getSessionId
}
}
newToken :: forall r. { | r} -> Aff RequestTokenResponse
newToken _ = pure { success: true, expiresAt: "date", requestToken: "328dsdweoi" }
createSession :: forall r. { | r} -> Aff SessionIdResponse
createSession _ = pure { success: true, sessionId: "date", requestToken: "23988w9" }
deleteSession :: { body :: { sessionId :: String }
, guards :: { apiKey :: String }
} -> Aff StatusResponse
deleteSession _ = pure { success: true }
latestMovie :: forall r. { | r} -> Aff Movie
latestMovie _ = pure $ { id: 723, title: "The Godfather" }
popularMovies :: forall r. { | r} -> Aff { results :: Array Movie }
popularMovies _ = pure { results: [
{ id: 723, title: "The Godfather" },
{ id: 722, title: "Citizen Kane" }] }
getMovie :: forall r. { params :: { movieId :: Int } | r} -> Aff Movie
getMovie { params: { movieId } } = pure { id: movieId, title: "Fetched movie" }
createRating :: { params :: { movieId :: Int }
, guards :: { apiKey :: ApiKey, sessionId :: SessionId}
, body :: RatingValue
} -> Aff StatusCodeResponse
createRating _ = pure { statusCode: 1, statusMessage: "Created" }
deleteRating :: { params :: { movieId :: Int }
, guards :: { apiKey :: ApiKey, sessionId :: SessionId }
} -> Aff StatusCodeResponse
deleteRating _ = pure { statusCode: 1, statusMessage: "Deleted" }
getApiKey :: HTTP.Request -> Aff (Either String ApiKey)
getApiKey req = do
let cookies = requestCookies req
pure $ note "No cookie" $ Map.lookup "apiKey" cookies
getSessionId :: HTTP.Request -> Aff (Either String SessionId)
getSessionId req = do
let cookies = requestCookies req
pure $ note "No cookie" $ Map.lookup "sessionId" cookies