UNPKG

@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.

99 lines (98 loc) 3.78 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const http = __importStar(require("http")); const open_1 = __importDefault(require("open")); const BASE_URL = "https://twitter.game.virtuals.io/accounts"; const getLoginUrl = async (apiKey) => { const response = await fetch(`${BASE_URL}/auth`, { headers: { "x-api-key": apiKey, }, }); if (!response.ok) { const error = await response.json(); throw new Error(error.message); } const data = await response.json(); return data.url; }; const verify = async (code, state) => { const response = await fetch(`${BASE_URL}/verify?code=${code}&state=${state}`); if (!response.ok) { const error = await response.json(); throw new Error(error.message); } const data = await response.json(); return data.token; }; const program = new commander_1.Command(); program .name("game-twitter-plugin") .description("CLI to authenticate and interact with GAME's Twitter API") .version("0.1.0"); program .command("auth") .description("Authenticate with Twitter API") .option("-k, --key <char>", "project's API key") .action((options) => { const apiKey = options.key; if (!apiKey) { console.error("API key is required!"); return; } const server = http.createServer(async (req, res) => { var _a; if (req.method === "GET" && ((_a = req.url) === null || _a === void 0 ? void 0 : _a.startsWith("/callback"))) { const query = new URLSearchParams(req.url.split("?")[1]); const code = query.get("code"); const state = query.get("state"); const token = await verify(code, state); res.writeHead(200, { "Content-Type": "text/plain" }); res.end("Authentication successful! You may close this window and return to the terminal."); console.log("Authenticated! Here's your access token:"); console.log(token); console.log("\n"); server.close(); } else { res.writeHead(404, { "Content-Type": "text/plain" }); res.end("Not Found"); } }); server.listen(8714, async () => { const url = await getLoginUrl(apiKey); console.log("\nWaiting for authentication...\n"); console.log("Visit the following URL to authenticate:"); console.log(url, "\n"); await (0, open_1.default)(url); }); }); program.parse(process.argv);