@virtuals-protocol/game-twitter-node
Version:
Strongly typed, full-featured, light, versatile yet powerful Virtual Twitter API v2 client for Node.js. Forked from twitter-api-v2.
81 lines (80 loc) • 3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwitterApiReadOnly = exports.TwitterApiReadWrite = exports.TwitterApi = void 0;
const client_v1_1 = __importDefault(require("../v1/client.v1"));
const client_v2_1 = __importDefault(require("../v2/client.v2"));
const readwrite_1 = __importDefault(require("./readwrite"));
const client_ads_1 = __importDefault(require("../ads/client.ads"));
// "Real" exported client for usage of TwitterApi.
/**
* Twitter v1.1 and v2 API client.
*/
class TwitterApi extends readwrite_1.default {
/* Direct access to subclients */
get v1() {
throw new Error("v1 is not supported");
if (this._v1)
return this._v1;
return (this._v1 = new client_v1_1.default(this));
}
get v2() {
if (this._v2)
return this._v2;
return (this._v2 = new client_v2_1.default(this));
}
/**
* Get a client with read/write rights.
*/
get readWrite() {
return this;
}
/**
* Get Twitter Ads API client
*/
get ads() {
if (this._ads)
return this._ads;
return (this._ads = new client_ads_1.default(this));
}
/* Static helpers */
static getErrors(error) {
var _a;
if (typeof error !== "object")
return [];
if (!("data" in error))
return [];
return (_a = error.data.errors) !== null && _a !== void 0 ? _a : [];
}
/** Extract another image size than obtained in a `profile_image_url` or `profile_image_url_https` field of a user object. */
static getProfileImageInSize(profileImageUrl, size) {
const lastPart = profileImageUrl.split("/").pop();
const sizes = ["normal", "bigger", "mini"];
let originalUrl = profileImageUrl;
for (const availableSize of sizes) {
if (lastPart.includes(`_${availableSize}`)) {
originalUrl = profileImageUrl.replace(`_${availableSize}`, "");
break;
}
}
if (size === "original") {
return originalUrl;
}
const extPos = originalUrl.lastIndexOf(".");
if (extPos !== -1) {
const ext = originalUrl.slice(extPos + 1);
return originalUrl.slice(0, extPos) + "_" + size + "." + ext;
}
else {
return originalUrl + "_" + size;
}
}
}
exports.TwitterApi = TwitterApi;
var readwrite_2 = require("./readwrite");
Object.defineProperty(exports, "TwitterApiReadWrite", { enumerable: true, get: function () { return __importDefault(readwrite_2).default; } });
var readonly_1 = require("./readonly");
Object.defineProperty(exports, "TwitterApiReadOnly", { enumerable: true, get: function () { return __importDefault(readonly_1).default; } });
exports.default = TwitterApi;