@zikeji/hypixel
Version:
With IntelliSense support & test coverage, this is an unopinionated async/await API wrapper for Hypixel's Public API. It is developed in TypeScript complete with documentation, typed interfaces for all API responses, built-in rate-limit handling, flexible
61 lines • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getResultObject = getResultObject;
/** @hidden */
function getResultObject(response, keys) {
const clonedResponse = JSON.parse(JSON.stringify(response));
if (!(keys !== null && keys !== void 0 ? keys : []).every((key) => key in clonedResponse)) {
throw new TypeError(`One or more key in "${(keys !== null && keys !== void 0 ? keys : []).join('"," ')}" was not in the response.`);
}
const obj = {};
const { ratelimit, cached, cloudflareCache } = clonedResponse;
const meta = {};
if (cached) {
meta.cached = true;
delete clonedResponse.cached;
}
if (cloudflareCache) {
meta.cloudflareCache = cloudflareCache;
delete clonedResponse.cloudflareCache;
}
if (ratelimit) {
if (!cached &&
(!meta.cloudflareCache || meta.cloudflareCache.status !== "HIT")) {
meta.ratelimit = ratelimit;
}
delete clonedResponse.ratelimit;
}
let assignedMeta = false;
(keys !== null && keys !== void 0 ? keys : []).forEach((key) => {
const value = clonedResponse[key];
if (typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean" ||
typeof value === "undefined") {
delete clonedResponse[key];
assignedMeta = true;
meta[key] = value;
}
});
if ((keys !== null && keys !== void 0 ? keys : []).length === 0 || assignedMeta) {
// we want the remainder merged into the object.
Object.assign(obj, clonedResponse);
Object.defineProperty(obj, "meta", {
enumerable: false,
value: meta,
});
return obj;
}
// we want all the keys merged with the root and the remainder assigned to meta.
(keys !== null && keys !== void 0 ? keys : []).forEach((key) => {
Object.assign(obj, clonedResponse[key]);
delete clonedResponse[key];
});
Object.assign(meta, clonedResponse);
Object.defineProperty(obj, "meta", {
enumerable: false,
value: meta,
});
return obj;
}
//# sourceMappingURL=ResultObject.js.map