Skip to content

Instantly share code, notes, and snippets.

@adrianmg
Created April 25, 2018 15:55
Show Gist options
  • Save adrianmg/39d28aff8a4023bbbd33e92f6b6f399f to your computer and use it in GitHub Desktop.
Save adrianmg/39d28aff8a4023bbbd33e92f6b6f399f to your computer and use it in GitHub Desktop.

Revisions

  1. adrianmg created this gist Apr 25, 2018.
    39 changes: 39 additions & 0 deletions figma-api-basic-request.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    var request = require("request");

    let lastUpdate;
    const time = 1000;
    const source = "xxx";
    const options = {
    url: `https://api.figma.com/v1/files/${source}`,
    headers: {
    "Content-Type": "application/json",
    "x-figma-token": "xxx"
    }
    };

    function callback(error, response, body) {
    try {
    body = JSON.parse(body);

    if (body.lastModified != lastUpdate) {
    lastUpdate = body.lastModified;

    console.log("Document updated: ", lastUpdate);

    return lastUpdate;
    }
    } catch (error) {
    console.log("Error: ", error);
    }

    setTimeout(requestUpdate, time);
    }

    function requestUpdate() {
    request.get(options, callback);
    }

    // Initiates
    exports.startPoll = function() {
    setTimeout(requestUpdate, time);
    };