latinfo 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +40 -45
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -5,8 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const fs_1 = __importDefault(require("fs"));
8
+ const http_1 = __importDefault(require("http"));
8
9
  const path_1 = __importDefault(require("path"));
9
10
  const os_1 = __importDefault(require("os"));
11
+ const child_process_1 = require("child_process");
10
12
  const API_URL = process.env.LATINFO_API_URL || 'https://api.latinfo.dev';
11
13
  const GITHUB_CLIENT_ID = 'Ov23li5fcQaiCsVtaMKK';
12
14
  const CONFIG_DIR = path_1.default.join(os_1.default.homedir(), '.latinfo');
@@ -29,54 +31,47 @@ function deleteConfig() {
29
31
  }
30
32
  catch { }
31
33
  }
32
- // --- GitHub Device Flow ---
33
- async function login() {
34
- console.log('Iniciando login con GitHub...\n');
35
- // 1. Request device code
36
- const codeRes = await fetch('https://github.com/login/device/code', {
37
- method: 'POST',
38
- headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
39
- body: JSON.stringify({ client_id: GITHUB_CLIENT_ID, scope: 'read:user,user:email' }),
40
- });
41
- const codeData = await codeRes.json();
42
- console.log(` Abre: ${codeData.verification_uri}`);
43
- console.log(` Código: ${codeData.user_code}\n`);
44
- console.log('Esperando autorización...');
45
- // 2. Poll for access token
46
- const interval = (codeData.interval || 5) * 1000;
47
- const deadline = Date.now() + codeData.expires_in * 1000;
48
- let accessToken = null;
49
- while (Date.now() < deadline) {
50
- await new Promise(r => setTimeout(r, interval));
51
- const tokenRes = await fetch('https://github.com/login/oauth/access_token', {
52
- method: 'POST',
53
- headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
54
- body: JSON.stringify({
55
- client_id: GITHUB_CLIENT_ID,
56
- device_code: codeData.device_code,
57
- grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
58
- }),
34
+ // --- GitHub Authorization Code Flow ---
35
+ function openBrowser(url) {
36
+ const cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
37
+ (0, child_process_1.exec)(`${cmd} "${url}"`);
38
+ }
39
+ async function waitForCallback(port) {
40
+ return new Promise((resolve, reject) => {
41
+ const server = http_1.default.createServer((req, res) => {
42
+ const url = new URL(req.url || '', `http://localhost:${port}`);
43
+ const code = url.searchParams.get('code');
44
+ if (code) {
45
+ res.writeHead(200, { 'Content-Type': 'text/html' });
46
+ res.end('<h2>Listo. Puedes cerrar esta ventana.</h2>');
47
+ server.close();
48
+ resolve(code);
49
+ }
50
+ else {
51
+ res.writeHead(400);
52
+ res.end('Missing code');
53
+ server.close();
54
+ reject(new Error('No se recibió código de GitHub'));
55
+ }
59
56
  });
60
- const tokenData = await tokenRes.json();
61
- if (tokenData.access_token) {
62
- accessToken = tokenData.access_token;
63
- break;
64
- }
65
- if (tokenData.error === 'expired_token') {
66
- console.error('Código expirado. Intenta de nuevo.');
67
- process.exit(1);
68
- }
69
- // authorization_pending or slow_down → keep polling
70
- }
71
- if (!accessToken) {
72
- console.error('Timeout. Intenta de nuevo.');
73
- process.exit(1);
74
- }
75
- // 3. Exchange for Latinfo API key
57
+ server.listen(port, () => { });
58
+ setTimeout(() => { server.close(); reject(new Error('Timeout esperando autorización')); }, 120_000);
59
+ });
60
+ }
61
+ async function login() {
62
+ const port = 8400;
63
+ const redirectUri = `http://localhost:${port}/callback`;
64
+ const scope = 'read:user,user:email';
65
+ const authUrl = `https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}`;
66
+ console.log('Abriendo GitHub...');
67
+ openBrowser(authUrl);
68
+ // 1. Wait for GitHub to redirect back with code
69
+ const code = await waitForCallback(port);
70
+ // 2. Exchange code for access token (server-side, needs client_secret)
76
71
  const authRes = await fetch(`${API_URL}/auth/github`, {
77
72
  method: 'POST',
78
73
  headers: { 'Content-Type': 'application/json' },
79
- body: JSON.stringify({ access_token: accessToken }),
74
+ body: JSON.stringify({ code, redirect_uri: redirectUri }),
80
75
  });
81
76
  if (!authRes.ok) {
82
77
  console.error('Error obteniendo API key:', await authRes.text());
@@ -84,7 +79,7 @@ async function login() {
84
79
  }
85
80
  const authData = await authRes.json();
86
81
  saveConfig({ api_key: authData.api_key, github_username: authData.github_username });
87
- console.log(`\nLogueado como ${authData.github_username}`);
82
+ console.log(`Logueado como ${authData.github_username}`);
88
83
  }
89
84
  // --- Commands ---
90
85
  async function ruc(rucNumber) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "latinfo",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Public data API for Latin America - SDK & CLI",
5
5
  "main": "dist/sdk.js",
6
6
  "types": "dist/sdk.d.ts",