kazatca / gql playground headers autofill

// ==UserScript==
// @name         gql playground headers autofill
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  setup auth headers
// @author       You
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
// @include      /\/graphql$/
// @license MIT
// @copyright 2018, kazatca (https://openuserjs.org//users/kazatca)
// ==/UserScript==

var inline_src = (<><![CDATA[

    const username = 'username';
    const password = 'password';

    const timeout = 270000;

    const getForm = values =>
      Object.keys(values).map(key => key + '=' + encodeURIComponent(values[key])).join('&');

    const post = (url, body) =>
      fetch(url, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json;charset=UTF-8'
        },
        body: JSON.stringify(body)
      })
      .then(resp => resp.json());

    const match = window.location.href.match(/utcom-gateway-(.*)\.k8s\./);
    let host = match ? `session-service-${match[1]}.k8s.unitedtraders.work` : `session-service.unitedtraders.work`

    const getToken = (username, password) =>
      post(`https://${host}/rest/grpc/com.unitedtraders.luna.sessionservice.api.sso.SsoService.authorizeByFirstFactor`, {
        realm: 'aurora',
        clientId: 'utcom',
        loginOrEmail: username,
        password: password,
        product: 'UTCOM',
        locale: 'ru'
      })
      .then(response => response.tokens.accessToken);

    const setAuthorization = accessToken =>
      s.dispatch({type: 'EDIT_HEADERS', payload: {headers: JSON.stringify({Authorization: 'Bearer ' + accessToken})}});

    const updateHeader = tokens =>
      (tokens ? updateToken(tokens.refresh_token): getToken(username, password))
      .then(accessToken => {
        if(!accessToken){
          return;
        }

        setAuthorization(accessToken);
      })

    const until = (cond, cb) =>
      cond() ? cb() : setTimeout(() => until(cond, cb), 500);

    console.log('headers autofill started');
    until(() => !!s, updateHeader);

]]></>).toString();
var c = Babel.transform(inline_src, { presets: [ "es2015", "es2016" ] });
eval(c.code);