gizmo-api
Version:
A minimal wrapper for interfacing with the Gizmo API.
67 lines • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.userHasBadge = exports.login = exports.getUserById = exports.getAuthenticatedUser = exports.searchForUser = exports.BADGES = void 0;
// Utils
const fetch_1 = require("./utils/fetch");
const helpers_1 = require("./utils/helpers");
// Constants
var _constants_1 = require("./constants.js");
Object.defineProperty(exports, "BADGES", { enumerable: true, get: function () { return _constants_1.BADGES; } });
async function searchForUser(query) {
const response = await (0, fetch_1.gizmoFetch)(`/users/search?q=${query}`);
if ("data" in response.data) {
return response.data.data;
}
else {
throw new Error(response.data.message);
}
}
exports.searchForUser = searchForUser;
async function getAuthenticatedUser(token) {
const response = await (0, fetch_1.gizmoFetch)("/users/@me", {
headers: {
"Authorization": `Bearer ${token}`
},
credentials: "same-origin"
});
if ("data" in response.data) {
return response.data.data;
}
else {
throw new Error(response.data.message);
}
}
exports.getAuthenticatedUser = getAuthenticatedUser;
async function getUserById(id) {
const response = await (0, fetch_1.gizmoFetch)(`/users/${id}`);
if ("data" in response.data) {
return response.data.data;
}
else {
throw new Error(response.data.message);
}
}
exports.getUserById = getUserById;
async function login(username, password) {
const response = await (0, fetch_1.gizmoFetch)("/users/login", {
headers: {
"Authorization": `Basic ${(0, helpers_1.stringToBase64)(`${username}:${password}`)}`
},
credentials: "same-origin"
});
if ("data" in response.data) {
return response.data.data;
}
else {
throw new Error(response.data.message);
}
}
exports.login = login;
function userHasBadge(user, badge) {
if (typeof badge !== "string") {
throw new Error("Invalid badge type.");
}
return user.badges.includes(badge);
}
exports.userHasBadge = userHasBadge;
//# sourceMappingURL=index.js.map