UNPKG

runelite

Version:
120 lines (119 loc) 6.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.timeseries = exports.prices = exports.mapping = exports.latest = void 0; const axios_1 = require("axios"); const UAstart = "npm runelite | -"; /** * Get the latest high and low prices for the items that we have data for, * and the Unix timestamp when that transaction took place * @param useragent - (required) a User-Agent that describes what you're using it for, * and if you're willing, some sort of contact info (like an email or Discord). * @param id - (optional) Item ID. If provided, will only display the latest price for this item. * @returns An associative array object. * @see https://oldschool.runescape.wiki/w/RuneScape:Real-time_Prices */ const latest = (options) => __awaiter(void 0, void 0, void 0, function* () { const { id, useragent } = options || {}; if (!useragent) throw new Error("useragent is required"); const url = id ? `https://prices.runescape.wiki/api/v1/osrs/latest?id=${id}` : `https://prices.runescape.wiki/api/v1/osrs/latest`; const response = (yield axios_1.default.get(url, { headers: { "User-Agent": `${UAstart} | ${useragent}` }, })).data.data; return response; }); exports.latest = latest; // Mapping does not need to be updated often, so we can cache it. const mappingCache = {}; /** * Gives a list of objects containing the name, * id, examine text, members status, lowalch, highalch, * GE buy limit, icon file name (on the wiki). * * The mapping is not updated often, so it is cached. * @param useragent - (required) a User-Agent that describes what you're using it for, * and if you're willing, some sort of contact info (like an email or Discord). * @returns An associative array object. * @see https://oldschool.runescape.wiki/w/RuneScape:Real-time_Prices */ const mapping = (options) => __awaiter(void 0, void 0, void 0, function* () { const { useragent } = options || {}; if (!useragent) throw new Error("useragent is required"); const cached = Object.keys(mappingCache).length > 0; const url = "https://prices.runescape.wiki/api/v1/osrs/mapping"; if (!cached) { const response = (yield axios_1.default.get(url, { headers: { "User-Agent": `${UAstart} | ${useragent}` }, })).data; response.forEach((item) => { mappingCache[item.id] = item; }); } return mappingCache; }); exports.mapping = mapping; /** * Gives 5 or 60 minute average of item high and low prices as well as the number traded * for the items that we have data on. Comes with a Unix timestamp indicating the block the data is from. * @param interval - (optional) Either "5m" or "60m". Defaults to "5m". * @param useragent - (required) a User-Agent that describes what you're using it for, * and if you're willing, some sort of contact info (like an email or Discord). * @param timestep - (optional) timestep to return prices for. * If provided, will display 5-minute averages for all items we have data on for this time. * The timestamp field represents the beginning of the 5-minute period being averaged * @returns An associative array object. * @see https://oldschool.runescape.wiki/w/RuneScape:Real-time_Prices */ const prices = (options) => __awaiter(void 0, void 0, void 0, function* () { const { timestamp, useragent } = options || {}; let { timestep } = options || {}; if (!useragent) throw new Error("useragent is required"); if (timestep) timestep = timestep.toLowerCase(); if (timestep !== "5m" && timestep !== "1h") console.error("interval must be '5m' or '1h'. Falling back to 5min, in future, this will be an error."); timestep = timestep !== null && timestep !== void 0 ? timestep : "5m"; const url = timestamp ? `https://prices.runescape.wiki/api/v1/osrs/${timestep}?timestamp=${timestamp}` : `https://prices.runescape.wiki/api/v1/osrs/${timestep}`; const response = (yield axios_1.default.get(url, { headers: { "User-Agent": `${UAstart} | ${useragent}` }, })).data; Object.keys(response.data).forEach((key) => { response.data[key].timestamp = response.timestamp; }); return response.data; }); exports.prices = prices; /** * Gives a list of the high and low prices of item with the given id at the given interval, up to 300 maximum. * @param useragent - (required) a User-Agent that describes what you're using it for, * and if you're willing, some sort of contact info (like an email or Discord). * @param id - (required) Item id to return a time-series for. * @param timestep - (required) Timestep of the time-series. Valid options are "5m", "1h" and "6h". * @returns A timeseries array. * @see https://oldschool.runescape.wiki/w/RuneScape:Real-time_Prices */ const timeseries = (options) => __awaiter(void 0, void 0, void 0, function* () { var _a; const { timestep, id, useragent } = options || {}; if (!useragent) throw new Error("useragent is required"); const url = `https://prices.runescape.wiki/api/v1/osrs/timeseries?timestep=${timestep}&id=${id}`; const response = (yield axios_1.default.get(url, { headers: { "User-Agent": `${UAstart} | ${useragent}` }, })).data; return (_a = response.data[id]) !== null && _a !== void 0 ? _a : response.data; }); exports.timeseries = timeseries;