microStudio client

This is an asynchronous microStudio client I wrote in JavaScript for use with microScript. It can be used to do things like finding public projects and, given an access token, automate your microStudio account.

//javascript

class Client {
  socket;
  ready;
  callbacks = {};
  request_id = 0;
  
  constructor(url = "wss://microstudio.dev") {
    const socket = new WebSocket(url);
    this.socket = socket;
    socket.addEventListener("message", (function(event) {
      const data = JSON.parse(event.data);
      const request_id = data.request_id;
      delete data.request_id;
      if (this.callbacks[request_id]) {
        this.callbacks[request_id](data);
      }
    }).bind(this));
    this.ready = new Promise((resolve, reject) => {
      socket.addEventListener("open", resolve);
      socket.addEventListener("error", reject);
    })
  }
  
  async send(data) {
    await this.ready;
    data.request_id = this.request_id++;
    this.socket.send(JSON.stringify(data));
    return await new Promise(resolve => {
      this.callbacks[data.request_id] = resolve;
    });
  }
}

global.Client = Client;

If you're using it in a JavaScript project, simply remove the //javascript and replace global in the last line with window.

To learn more about how requests are structured, run this code in the browser console on microstudio.dev:

const send = Client.prototype.send;
Client.prototype.send = function(data) {
  console.log("Sending:", data);
  send.call(this, data);
}

The JavaScript above will log any requests sent to the console. For example, if you open the Explore section, microStudio will send a request similar to this:

{
  name: "get_public_projects",
  ranking: "hot",
  type: "all",
  tags: [],
  search: "",
  position: 0,
  offset: 0
}