-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.luau
99 lines (83 loc) · 2.81 KB
/
init.luau
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
--!strict
local net = require'@lune/net';
type luneFirebaseImpl = {
__index: luneFirebaseImpl,
getFirebase: (name: string, scope: string?) -> (Firebase),
}
type FirebaseImpl = {
__index: FirebaseImpl,
get: (self: Firebase, key: string) -> (any),
set: (self: Firebase, key: string, value: any, method: "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT"?) -> (net.FetchResponse),
delete: (self: Firebase, key: string) -> (net.FetchResponse),
increment: (self: Firebase, key: string, delta: number?) -> (net.FetchResponse?),
update: (self: Firebase, key: string, callback: (any) -> (any), snapshot: any) -> (net.FetchResponse?),
}
export type Firebase = typeof(setmetatable({} :: {
path: string,
auth: string,
}, {} :: FirebaseImpl))
local config = {
DefaultScope = '';
AuthenticationToken = '';
}
local Firebase = {} :: FirebaseImpl
Firebase.__index = Firebase;
function Firebase.get(self, key)
key = if key:sub(1, 1) ~= '/' then `/{key}` else key;
local dir = `{self.path}{net.urlEncode(key)}{self.auth}`;
local response = net.request({
url = dir,
method = 'GET',
})
if not response.ok then
error(`responseError : {response.statusMessage} \n statusCode : {response.statusCode}`);
end
return net.jsonDecode(response.body);
end
function Firebase.set(self, key, value, method)
method = method or 'PUT';
key = if key:sub(1, 1) ~= '/' then `/{key}` else key;
local dir = `{self.path}{net.urlEncode(key)}{self.auth}`;
local response = net.request({
url = dir,
method = method,
headers = { ["Content-Type"] = "application/x-www-form-urlencoded" },
body = net.jsonEncode(value),
})
if not response.ok then
error(`responseError : {response.statusMessage} \n statusCode : {response.statusCode}`);
end
return response;
end
function Firebase.delete(self, key)
return self:set(key, '', 'DELETE');
end
function Firebase.increment(self, key, delta)
local data = self:get(key) or 0 :: number;
delta = if type(delta) == 'number' then delta else 1;
if type(data) == 'number' then
data += delta :: number
return self:set(key, data)
end
return
end
function Firebase.update(self, key, callback, snapshot)
local data = snapshot or self:get(key);
local updated = callback(data);
if updated then
return self:set(key, updated, 'PATCH');
end
return
end
local luneFirebase = {} :: luneFirebaseImpl;
luneFirebase.__index = luneFirebase;
function luneFirebase.getFirebase(name, scope)
scope = scope or config.DefaultScope;
local path = `{scope}{net.urlEncode(name)}`;
local auth = `.json?auth={config.AuthenticationToken}`;
return setmetatable({
path=path,
auth=auth,
}, Firebase)
end
return luneFirebase;