faceit-node-api
Version:
Lightweight node.js module for easier use of the Faceit Data API.
33 lines (32 loc) • 1.23 kB
JavaScript
export default class Faceit {
constructor(path) {
this.apiUrl = `https://open.faceit.com/data/v4/${path}`;
}
static setApiKey(apiKey) {
Faceit._apiKey = apiKey;
}
async processRequest(uri, queryparams) {
const params = queryparams ? transformQueryParams(queryparams) : {};
const paramsString = new URLSearchParams(params).toString();
const queryParams = paramsString.length ? `?${paramsString}` : '';
return fetch(`${this.apiUrl}${uri || ''}${queryParams}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${Faceit._apiKey}`,
'Content-Type': 'application/json',
},
})
.then((response) => response.json())
.then((data) => {
var _a;
if (!((_a = data.errors) === null || _a === void 0 ? void 0 : _a.length)) {
return data;
}
const { http_status, message } = data.errors[0];
throw new Error(`${http_status || ''} ${message}`);
});
}
}
function transformQueryParams(params) {
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, String(value)]));
}