github.com/dbernstein1/tyk@v2.9.0-beta9-dl-apic+incompatible/event_handlers/sample/firebase_test.js (about)

     1  // ---- Sample firebase middleware -----
     2  var fbHandler = new TykJS.TykEventHandlers.NewEventHandler({});
     3  
     4  fbHandler.NewHandler(function(event, context) {
     5      // You can log to Tyk console output by calloing the built-in log() function:
     6      log("Running firebase JSVM Handler");
     7      
     8      /*      The Event object:
     9              {
    10                  "Type": "Event Typ Code",
    11                  "Meta": {
    12                      "Message": "MEvent descriptions",
    13                      "Path": "/{{api_id}}/{{path}}",
    14                      "Origin": "1.1.1.1:PORT",
    15                      "Key": "{{Auth Key}}"
    16                  },
    17                  "TimeStamp": "2015-01-15 17:21:15.111157073 +0000 UTC"
    18              }
    19      */
    20      
    21      newRequest = {
    22          "Method": "POST",
    23          "Body": JSON.stringify(event),
    24          "Headers": {},
    25          "Domain": "",
    26          "Resource": "/middleware/fb.json",
    27          "FormData": {}
    28      };
    29      
    30      if (newRequest.Domain === "") {
    31          log("Please specify a Firebase endpoint in the request...");
    32          return
    33      }
    34      
    35      log("--- CREATING FIREBASE RECORD ---")
    36      // Use the built-in TykMakeHttpRequest method to make RESTFULL API Calls
    37      response = TykMakeHttpRequest(JSON.stringify(newRequest)); 
    38      
    39      /*      Repsonses are JSON-encoded, so they need to be parsed before using, it looks like this:
    40      
    41              type TykJSHttpResponse struct {
    42                  Code int
    43                  Body string
    44                  Headers map[string][]string
    45              }
    46      
    47      */
    48      
    49      usableResponse = JSON.parse(response);
    50      log("Response code: " + usableResponse.Code);
    51      log("Response body: " + usableResponse.Body);
    52      log("--- FIREBASE RECORD CREATED ---")
    53      
    54      fbResponse = JSON.parse(usableResponse.Body);
    55      getDetails = {
    56          "Method": "GET",
    57          "Body": "",
    58          "Headers": {},
    59          "Domain": "https://glaring-torch-9311.firebaseio.com",
    60          "Resource": "/middleware/fb/" + fbResponse.name + ".json",
    61          "FormData": {}
    62      };
    63      
    64      log("--- GETTING RECORD FOR VERIFICATION ---")
    65      log("URL: /middleware/fb/" + fbResponse.name + ".json")
    66      responseDetails = JSON.parse(TykMakeHttpRequest(JSON.stringify(getDetails))); 
    67      objDetails = JSON.parse(responseDetails.Body)
    68      log("Key: " + objDetails.Meta.Key);
    69      log("Message: " + objDetails.Meta.Message);
    70      log("--- DONE ---")
    71  });
    72  
    73  // Ensure init with a post-declaration log message
    74  log("Firebase JS event handler initialised");