2021-01-01 14:27:58 +00:00
|
|
|
/* global addAPI */// common.js
|
2018-02-15 08:27:00 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
/* CURRENTLY UNUSED */
|
|
|
|
|
2018-02-15 08:27:00 +00:00
|
|
|
(() => {
|
|
|
|
// begin:nanographql - Tiny graphQL client library
|
|
|
|
// Author: yoshuawuyts (https://github.com/yoshuawuyts)
|
|
|
|
// License: MIT
|
|
|
|
// Modified by DecentM to fit project standards
|
|
|
|
|
|
|
|
const getOpname = /(query|mutation) ?([\w\d-_]+)? ?\(.*?\)? \{/;
|
|
|
|
const gql = str => {
|
|
|
|
str = Array.isArray(str) ? str.join('') : str;
|
|
|
|
const name = getOpname.exec(str);
|
|
|
|
|
|
|
|
return 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';
|
2021-01-01 14:27:58 +00:00
|
|
|
const doQuery = async ({id}, queryString) => {
|
2018-02-15 08:27:00 +00:00
|
|
|
const query = gql(queryString);
|
2021-01-01 14:27:58 +00:00
|
|
|
return (await fetch(api, {
|
2018-02-15 08:27:00 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: new Headers({
|
2020-11-18 11:17:15 +00:00
|
|
|
'Content-Type': 'application/json',
|
2018-02-15 08:27:00 +00:00
|
|
|
}),
|
|
|
|
body: query({
|
2020-11-18 11:17:15 +00:00
|
|
|
id,
|
|
|
|
}),
|
2021-01-01 14:27:58 +00:00
|
|
|
})).json();
|
2018-02-15 08:27:00 +00:00
|
|
|
};
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
addAPI(/** @namespace- API */ { // TODO: remove "-" when this is implemented
|
2018-02-15 08:27:00 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
});
|
|
|
|
})();
|