-
Notifications
You must be signed in to change notification settings - Fork 16
/
firebase.coffee
119 lines (86 loc) · 4.94 KB
/
firebase.coffee
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
# Documentation of this Module: https://github.com/marckrenn/framer-Firebase
# ------ : ------- Firebase REST API: https://firebase.google.com/docs/reference/rest/database/
# Firebase REST API Class ----------------------------
class exports.Firebase extends Framer.BaseClass
@.define "status",
get: -> @_status # readOnly
constructor: (@options={}) ->
@projectID = @options.projectID ?= null
@secret = @options.secret ?= null
@debug = @options.debug ?= false
@_status ?= "disconnected"
@secretEndPoint = if @secret then "?auth=#{@secret}" else "?" # hotfix
super
console.log "Firebase: Connecting to Firebase Project '#{@projectID}' ... \n URL: 'https://#{@projectID}.firebaseio.com'" if @debug
@.onChange "connection"
request = (project, secret, path, callback, method, data, parameters, debug) ->
url = "https://#{project}.firebaseio.com#{path}.json#{secret}"
if parameters?
if parameters.shallow then url += "&shallow=true"
if parameters.format is "export" then url += "&format=export"
switch parameters.print
when "pretty" then url += "&print=pretty"
when "silent" then url += "&print=silent"
if typeof parameters.download is "string"
url += "&download=#{parameters.download}"
window.open(url,"_self")
url += "&orderBy=" + '"' + parameters.orderBy + '"' if typeof parameters.orderBy is "string"
url += "&limitToFirst=#{parameters.limitToFirst}" if typeof parameters.limitToFirst is "number"
url += "&limitToLast=#{parameters.limitToLast}" if typeof parameters.limitToLast is "number"
url += "&startAt=#{parameters.startAt}" if typeof parameters.startAt is "number"
url += "&endAt=#{parameters.endAt}" if typeof parameters.endAt is "number"
url += "&equalTo=#{parameters.equalTo}" if typeof parameters.equalTo is "number"
console.log "Firebase: New '#{method}'-request with data: '#{JSON.stringify(data)}' \n URL: '#{url}'" if debug
options =
method: method
headers:
'content-type': 'application/json; charset=utf-8'
if data?
options.body = JSON.stringify(data)
r = fetch(url, options)
.then (res) ->
if !res.ok then throw Error(res.statusText)
json = res.json()
json.then callback
return json
.catch (error) => console.warn(error)
return r
# Third argument can also accept options, rather than callback
parseArgs = (l, args..., cb) ->
if typeof args[l-1] is "object"
args[l] = args[l-1]
args[l-1] = null
return cb.apply(null, args)
# Available methods
get: (args...) -> parseArgs 2, args..., (path, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "GET", null, parameters, @debug)
put: (args...) -> parseArgs 3, args..., (path, data, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "PUT", data, parameters, @debug)
post: (args...) -> parseArgs 3, args..., (path, data, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "POST", data, parameters, @debug)
patch: (args...) -> parseArgs 3, args..., (path, data, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "PATCH", data, parameters, @debug)
delete: (args...) -> parseArgs 2, args..., (path, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "DELETE", null, parameters, @debug)
onChange: (path, callback) ->
if path is "connection"
url = "https://#{@projectID}.firebaseio.com/.json#{@secretEndPoint}"
currentStatus = "disconnected"
source = new EventSource(url)
source.addEventListener "open", =>
if currentStatus is "disconnected"
@._status = "connected"
callback("connected") if callback?
console.log "Firebase: Connection to Firebase Project '#{@projectID}' established" if @debug
currentStatus = "connected"
source.addEventListener "error", =>
if currentStatus is "connected"
@._status = "disconnected"
callback("disconnected") if callback?
console.warn "Firebase: Connection to Firebase Project '#{@projectID}' closed" if @debug
currentStatus = "disconnected"
return
url = "https://#{@projectID}.firebaseio.com#{path}.json#{@secretEndPoint}"
source = new EventSource(url)
console.log "Firebase: Listening to changes made to '#{path}' \n URL: '#{url}'" if @debug
source.addEventListener "put", (ev) =>
callback(JSON.parse(ev.data).data, "put", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"),1)) if callback?
console.log "Firebase: Received changes made to '#{path}' via 'PUT': #{JSON.parse(ev.data).data} \n URL: '#{url}'" if @debug
source.addEventListener "patch", (ev) =>
callback(JSON.parse(ev.data).data, "patch", JSON.parse(ev.data).path, _.tail(JSON.parse(ev.data).path.split("/"),1)) if callback?
console.log "Firebase: Received changes made to '#{path}' via 'PATCH': #{JSON.parse(ev.data).data} \n URL: '#{url}'" if @debug