stylus/background/openusercss-api.js
DecentM aa7af11dd4
feature: create API helpers
This contains event listeners for oucThemeById and oucUserById
2018-02-15 05:51:30 +01:00

107 lines
2.4 KiB
JavaScript

'use strict';
// begin:nanographql - Tiny graphQL client library
// License: MIT
const getOpname = /(query|mutation) ?([\w\d-_]+)? ?\(.*?\)? \{/;
const gql = str => {
str = Array.isArray(str) ? str.join('') : str;
const name = getOpname.exec(str);
return function (variables) {
const data = {query: str};
if (variables) data.variables = JSON.stringify(variables);
if (name && name.length) {
const operationName = name[2];
if (operationName) data.operationName = name[2];
}
return JSON.stringify(data);
};
};
// end:nanographql
const api = 'https://api.openusercss.org';
const doQuery = ({id}, queryString) => new Promise((resolve, reject) => {
const query = gql(queryString);
fetch(api, {
'method': 'POST',
'headers': new Headers({
'Content-Type': 'application/json'
}),
'body': query({
id
})
})
.then(res => {
res.json()
.then(response => {
if (response.errors) {
reject(new Error(JSON.stringify(response.errors)));
}
resolve(response);
});
})
.catch(reject);
});
window.API_METHODS = Object.assign(window.API_METHODS || {}, {
/**
* This function can be used to retrieve a theme object from the
* GraphQL API, set above
*
* Example:
* chrome.runtime.sendMessage({
* 'method': 'oucThemeById',
* 'id': '5a2f819f7c57c751001b49df'
* }, console.log);
*
* @param {ID} $0.id MongoDB style ID
* @returns {Promise.<{data: object}>} The GraphQL result with the `theme` object
*/
'oucThemeById': params => doQuery(params, `
query($id: ID!) {
theme(id: $id) {
_id
title
description
createdAt
lastUpdate
version
screenshots
user {
_id
displayname
}
}
}
`),
/**
* This function can be used to retrieve a user object from the
* GraphQL API, set above
*
* Example:
* chrome.runtime.sendMessage({
* 'method': 'oucUserById',
* 'id': '5a2f0361ba666f0b00b9c827'
* }, console.log);
*
* @param {ID} $0.id MongoDB style ID
* @returns {Promise.<{data: object}>} The GraphQL result with the `user` object
*/
'oucUserById': params => doQuery(params, `
query($id: ID!) {
user(id: $id) {
_id
displayname
avatarUrl
smallAvatarUrl
bio
}
}
`),
});