forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase_test.js
74 lines (62 loc) · 2.43 KB
/
firebase_test.js
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
// ---- Sample firebase middleware -----
var fbHandler = new TykJS.TykEventHandlers.NewEventHandler({});
fbHandler.NewHandler(function(event, context) {
// You can log to Tyk console output by calloing the built-in log() function:
log("Running firebase JSVM Handler");
/* The Event object:
{
"EventType": "Event Typ Code",
"EventMetaData": {
"Message": "MEvent descirption",
"Path": "/{{api_id}}/{{path}}",
"Origin": "1.1.1.1:PORT",
"Key": "{{Auth Key}}"
},
"TimeStamp": "2015-01-15 17:21:15.111157073 +0000 UTC"
}
*/
newRequest = {
"Method": "POST",
"Body": JSON.stringify(event),
"Headers": {},
"Domain": "",
"Resource": "/middleware/fb.json",
"FormData": {}
};
if (newRequest.Domain === "") {
log("Please specify a Firebase endpoint in the request...");
return
}
log("--- CREATING FIREBASE RECORD ---")
// Use the built-in TykMakeHttpRequest method to make RESTFULL API Calls
response = TykMakeHttpRequest(JSON.stringify(newRequest));
/* Repsonses are JSON-encoded, so they need to be parsed before using, it looks like this:
type TykJSHttpResponse struct {
Code int
Body string
Headers map[string][]string
}
*/
usableResponse = JSON.parse(response);
log("Response code: " + usableResponse.Code);
log("Response body: " + usableResponse.Body);
log("--- FIREBASE RECORD CREATED ---")
fbResponse = JSON.parse(usableResponse.Body);
getDetails = {
"Method": "GET",
"Body": "",
"Headers": {},
"Domain": "https://glaring-torch-9311.firebaseio.com",
"Resource": "/middleware/fb/" + fbResponse.name + ".json",
"FormData": {}
};
log("--- GETTING RECORD FOR VERIFICATION ---")
log("URL: /middleware/fb/" + fbResponse.name + ".json")
responseDetails = JSON.parse(TykMakeHttpRequest(JSON.stringify(getDetails)));
objDetails = JSON.parse(responseDetails.Body)
log("Key: " + objDetails.EventMetaData.Key);
log("Message: " + objDetails.EventMetaData.Message);
log("--- DONE ---")
});
// Ensure init with a post-declaration log message
log("Firebase JS event handler initialised");