-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ls
More file actions
303 lines (233 loc) · 10.3 KB
/
index.ls
File metadata and controls
303 lines (233 loc) · 10.3 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
require! \base62
require! \querystring
{keys, last, map, Str} = require \prelude-ls
{is-equal-to-object} = require \prelude-extension
require! \./presentation-context
{compile-transformation}:pipe-transformation = require \pipe-transformation
{compile-and-execute-sync, from-error-value-tuple}:transpilation = require \transpilation
{UnAuthorizedException, UnAuthenticatedException} = Exceptions = require \./exceptions
# poly fill for promises and fetch API
(require \es6-promise).polyfill!
require \isomorphic-fetch
# bind-p :: p a -> (a -> p b) -> p b
bind-p = (p, f) --> p.then f
# return-p :: a -> p a
return-p = Promise.resolve
# :: Url -> ProjectId -> API
module.exports = (end-point, project-id) -->
# fetch-with-options :: String -> object -> p result
fetch-with-options = (url, options) ->
default-options =
credentials: \same-origin
headers: \Content-Type : \application/json
{ok}:res <- bind-p (fetch "#{end-point}/apis/#{url}", {} <<< default-options <<< options)
if !ok then
res.text!.then (text) ->
match res.status
| 401 => throw new UnAuthenticatedException text
| 403 => throw new UnAuthorizedException text
| _ => throw new Error text
throw text
else
res.json!
# get-json :: String -> p result
get-json = (url) ->
fetch-with-options url, method: \GET
# post-json :: String -> object -> p result
post-json = (url, json) -->
fetch-with-options do
url
method: \POST
body: JSON.stringify json
# delete-fetch :: String -> p a
delete-fetch = (url) ->
fetch-with-options url, method: \DELETE
# put-json :: String -> object -> p a
put-json = (url, json) ->
fetch-with-options do
url
method: \PUT
body: JSON.stringify json
# ----------- projects -----------
# get-projects :: String -> p [Project]
get-projects = (user-id) ->
get-json "users/#{user-id}/projects"
# get-my-projects :: () -> p [Project]
get-my-projects = ->
get-json "projects"
# ----------- data sources -----------
get-connections = (query-type, query-object = {}) ->
get-json "projects/#{project-id}/queryTypes/#{query-type}/connections#{querystring.stringify query-object}"
# ----------- documents -----------
# :: Document -> String -> Int -> p Document
save-document = (document) ->
post-json "projects/#{project-id}/documents", document
# get-documents :: () -> p [Document]
get-documents = ->
get-json "projects/#{project-id}/documents"
# :: DatasourceCue -> String -> p Document
load-default-document = (data-source-cue, transpilation-language) -->
post-json "projects/#{project-id}/defaultDocument", {data-source-cue, transpilation-language}
# :: String -> Int -> p Document
load-document-version = (document-id, version) -->
get-json "projects/#{project-id}/documents/#{document-id}/versions/#{version}"
# :: String -> p Document
load-latest-document = (document-id) ->
get-json "projects/#{project-id}/documents/#{document-id}"
# delete-document-version :: String -> Int -> p a
delete-document-version = (document-id, version) -->
delete-fetch "projects/#{project-id}/documents/#{document-id}/versions/#{version}"
# delete-document-and-history :: String -> p a
delete-document-and-hisotry = (document-id) ->
delete-fetch "projects/#{project-id}/documents/#{document-id}"
# :: [String] -> p [DOMElement]
require-deps = (client-external-libs) ->
# add urls to head
return (Promise.resolve client-external-libs) if client-external-libs.length == 0
# load all the urls in parallel
Promise.all do
client-external-libs |> map (url) ->
# TODO: use a different technique to differentiate file types
new Promise (res) ->
element = switch (last url.split \.)
| \js =>
script = document.create-element \script
..src = url
| \css =>
link = document.create-element \link
..type = \text/css
..rel = \stylesheet
..href = url
element.onload = ~> res url
document.head.append-child element
# ----------- projects -----------
add-project = (project) ->
post-json "projects", project
update-project = (project) ->
put-json "projects/#{project-id}", project
get-project = ->
get-json "projects/#{project-id}"
# ----------- execution -----------
execute = do ->
previous-call = null
(
task-id # :: String
display # :: Display
# document-id & version are used for security purposes
# to prevent guest user from executing changes to existing documents
document-id # :: String
version # :: Int
data-source-cue # :: {queryType :: String, connectionKind :: String, connectionName :: String, ...}
query # :: String
transpilation-language # :: String
compiled-parameters # :: object
cache # :: Boolean
) ->
args = {data-source-cue, query, transpilation-language, compiled-parameters}
if cache and previous-call and (args `is-equal-to-object` previous-call.args)
{result, execution-end-time} = previous-call.result-with-metadata
return-p do
from-cache: true
execution-duration: 0
execution-end-time: execution-end-time
result: result
else
result-with-metadata <- bind-p do
post-json do
"projects/#{project-id}/documents/#{document-id}/versions/#{version}/execute"
{
task-id
display
data-source-cue
query
transpilation-language
compiled-parameters
cache
}
previous-call := {args, result-with-metadata}
result-with-metadata
# ----------- storyboard -----------
# generate-uid :: () -> String
generate-uid = ->
base62.encode Date.now!
# :: Document -> p {execute, transformation-function, presentation-function}
compile-document1 = do ->
cache = {}
({
document-id
version
data-source-cue
query
client-external-libs
transpilation
transformation
presentation
}:document?) ->
cache-key = "#{document-id}-#{version}"
if cache[cache-key]
Promise.resolve cache[cache-key]
else
# load the dependencies
<- require-deps (client-external-libs ? []) .then _
# transformation-function :: result -> Parameters -> result
transformation-function <- compile-transformation transformation, transpilation.transformation .then _
# presentation-function :: DOMElement -> result -> Parameters -> DOM()
presentation-function <- compile-presentation presentation, transpilation.presentation .then _
cache[cache-key] =
document: document
# execute :: Boolean -> Parameters -> p result
execute: (cache, compiled-parameters) -->
op-info = {document}
# TODO: call execute with projectId
{result}? <- (execute do
data-source-cue
query
transpilation.query
compiled-parameters
cache
generate-uid!
op-info) .then _
result
# transform :: result -> Parameters -> p result
transformation-function: transformation-function
# presentation-function :: Parameters -> DOMElement -> result -> Void
presentation-function: presentation-function
# :: (DocumentId :: String) -> p {execute, transformation-function, presentation-function}
compile-document = do ->
(document-id, version) ->
document <- bind-p (load-document-version document-id, version)
compile-document1 document
# :: String -> p {execute, transformation-function, presentation-function}
compile-latest-document = do ->
cache = {}
(document-id) ->
document <- bind-p load-latest-document document-id
compile-document1 cache[cache-key] = document
# ----------- presentation -----------
# compile-presentation-sync :: String -> String -> [Error, (Parameters -> DOMElement -> result -> Void)]
compile-presentation-sync = (presentation, language) ->
compile-and-execute-sync presentation, language, presentation-context!
# compile-presentation :: String -> String -> p (Parameters -> DOMEelement -> result -> Void)
compile-presentation = from-error-value-tuple compile-presentation-sync
{} <<< transpilation <<< pipe-transformation <<< {
add-project
update-project
get-project
get-my-projects
get-connections
save-document
get-documents
load-default-document
load-document-version
load-latest-document
delete-document-version
delete-document-and-hisotry
require-deps
execute
generate-uid
compile-document
compile-latest-document
compile-presentation-sync
compile-presentation
Exceptions
}