steamwebapi-trade-bot
Version:
The official TypeScript library for the Steamwebapi Trade Bot
185 lines (181 loc) • 5.32 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
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 __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/utils.ts
import axios from "axios";
// src/error.ts
import { AxiosError } from "axios";
var ApiError = class extends AxiosError {
constructor(error) {
super();
this.name = "APIError";
this.message = error.message;
this.status = error.status;
}
};
// src/utils.ts
var client = axios.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;
}
});
}
};
export {
Trade as default
};
//# sourceMappingURL=index.js.map