vue-tweet-pure
Version:
A Vue 3 component for embedding tweets in your Vue.js applications.
55 lines (54 loc) • 2.38 kB
JavaScript
//#region src/api/fetch-tweet.ts
var e = "https://cdn.syndication.twimg.com", t = class extends Error {
status;
data;
constructor({ message: e, status: t, data: n }) {
super(e), this.name = "TwitterApiError", this.status = t, this.data = n;
}
}, n = /^[0-9]+$/;
function r(e) {
return (Number(e) / 0x38d7ea4c68000 * Math.PI).toString(6 ** 2).replace(/(0+|\.)/g, "");
}
async function i(i, a) {
if (i.length > 40 || !n.test(i)) throw Error(`Invalid tweet id: ${i}`);
let o = new URL(`${e}/tweet-result`);
o.searchParams.set("id", i), o.searchParams.set("lang", "en"), o.searchParams.set("features", [
"tfw_timeline_list:",
"tfw_follower_count_sunset:true",
"tfw_tweet_edit_backend:on",
"tfw_refsrc_session:on",
"tfw_fosnr_soft_interventions_enabled:on",
"tfw_show_birdwatch_pivots_enabled:on",
"tfw_show_business_verified_badge:on",
"tfw_duplicate_scribes_to_settings:on",
"tfw_use_profile_image_shape_enabled:on",
"tfw_show_blue_verified_badge:on",
"tfw_legacy_timeline_sunset:true",
"tfw_show_gov_verified_badge:on",
"tfw_show_business_affiliate_badge:on",
"tfw_tweet_edit_frontend:on"
].join(";")), o.searchParams.set("token", r(i));
let s = await fetch(o.toString(), a), c = s.headers.get("content-type")?.includes("application/json") ? await s.json() : void 0;
if (s.ok) return c?.__typename === "TweetTombstone" ? { tombstone: !0 } : { data: c };
if (s.status === 404) return { notFound: !0 };
throw new t({
message: typeof c.error == "string" ? c.error : `Failed to fetch tweet at "${o}" with "${s.status}".`,
status: s.status,
data: c
});
}
//#endregion
//#region src/api/get-tweet.ts
async function a(e, t) {
let { data: n, tombstone: r, notFound: a } = await i(e, t);
return a ? console.error(`The tweet ${e} does not exist or has been deleted by the account owner. Update your code to remove this tweet when possible.`) : r && console.error(`The tweet ${e} has been made private by the account owner. Update your code to remove this tweet when possible.`), n;
}
//#endregion
//#region src/api/get-oembed.ts
async function o(e) {
let t = await fetch(`https://publish.twitter.com/oembed?url=${e}`);
if (t.ok) return t.json();
if (t.status !== 404) throw Error(`Fetch for embedded tweet failed with code: ${t.status}`);
}
//#endregion
export { t as TwitterApiError, i as fetchTweet, o as getOEmbed, a as getTweet };