steamwebapi-trade-bot
Version:
The official TypeScript library for the Steamwebapi Trade Bot
215 lines (210 loc) • 6.79 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => Trade
});
module.exports = __toCommonJS(src_exports);
// src/utils.ts
var import_axios2 = __toESM(require("axios"), 1);
// src/error.ts
var import_axios = require("axios");
var ApiError = class extends import_axios.AxiosError {
constructor(error) {
super();
this.name = "APIError";
this.message = error.message;
this.status = error.status;
}
};
// src/utils.ts
var client = import_axios2.default.create({
baseURL: "https://www.steamwebapi.com/steam/api/trade",
params: {}
});
client.interceptors.response.use(
null,
(error) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
const message = (_d = (_c = (_b = (_a = error.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error) == null ? void 0 : _c[0]) != null ? _d : error.message;
const status = (_i = (_h = (_f = (_e = error.response) == null ? void 0 : _e.data) == null ? void 0 : _f.status) != null ? _h : (_g = error.response) == null ? void 0 : _g.status) != null ? _i : 500;
throw new ApiError({ message, status });
},
{ synchronous: true }
);
var mergeParams = (params, defaultParams, mergeKeys) => {
const obj = {};
mergeKeys == null ? void 0 : mergeKeys.forEach((key) => {
var _a;
obj[key] = (_a = params == null ? void 0 : params[key]) != null ? _a : defaultParams == null ? void 0 : defaultParams[key];
});
return obj;
};
// src/index.ts
var ERROR = {
message: "Unknown Error",
status: 500
};
var Trade = class {
constructor(apiKey, config) {
this.apiKey = apiKey;
client.defaults.params.key = apiKey;
this.defaults = __spreadValues({}, config);
}
setApiKey(apiKey) {
this.apiKey = apiKey;
return this;
}
getApiKey() {
return this.apiKey;
}
createOffer(createParams) {
return __async(this, null, function* () {
const params = mergeParams(createParams, this.defaults, [
"steamloginsecure",
"partneritemassetids",
"myitemsassetids",
"tradelink",
"partnersteamid",
"message",
"game"
]);
try {
const createRes = yield client.post("/create", params);
if (createRes.status !== 200)
throw new ApiError(ERROR);
return createRes;
} catch (error) {
return error;
}
});
}
getOffers(statusParams) {
return __async(this, null, function* () {
const params = mergeParams(statusParams, this.defaults, [
"steamcommunityapikey",
"partnersteamid",
"assetid"
]);
try {
const offersRes = yield client.post("/status", params);
if (offersRes.status !== 200)
throw new ApiError(ERROR);
return offersRes;
} catch (error) {
return error;
}
});
}
cancelOffer(cancelParams) {
return __async(this, null, function* () {
const params = mergeParams(cancelParams, this.defaults, [
"steamloginsecure",
"tradeofferid"
]);
try {
const cancelRes = yield client.put("/cancel", params);
if (cancelRes.status !== 200)
throw new ApiError(ERROR);
return { success: true };
} catch (error) {
return error;
}
});
}
declineOffer(declineParams) {
return __async(this, null, function* () {
const params = mergeParams(declineParams, this.defaults, [
"steamloginsecure",
"tradeofferid"
]);
try {
const declineRes = yield client.put("/decline", params);
if (declineRes.status !== 200)
throw new ApiError(ERROR);
return { success: true };
} catch (error) {
return error;
}
});
}
acceptOffer(acceptParams) {
return __async(this, null, function* () {
const params = mergeParams(acceptParams, this.defaults, [
"steamloginsecure",
"partnersteamid",
"tradeofferid"
]);
try {
const acceptRes = yield client.put("/accept", params);
if (acceptRes.status !== 200)
throw new ApiError(ERROR);
return { success: true };
} catch (error) {
return error;
}
});
}
};
//# sourceMappingURL=index.cjs.map