latinfo 0.1.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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,187 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const os_1 = __importDefault(require("os"));
10
+ const API_URL = process.env.LATINFO_API_URL || 'https://api.latinfo.dev';
11
+ const GITHUB_CLIENT_ID = 'Ov23li5fcQaiCsVtaMKK';
12
+ const CONFIG_DIR = path_1.default.join(os_1.default.homedir(), '.latinfo');
13
+ const CONFIG_FILE = path_1.default.join(CONFIG_DIR, 'config.json');
14
+ function loadConfig() {
15
+ try {
16
+ return JSON.parse(fs_1.default.readFileSync(CONFIG_FILE, 'utf-8'));
17
+ }
18
+ catch {
19
+ return null;
20
+ }
21
+ }
22
+ function saveConfig(config) {
23
+ fs_1.default.mkdirSync(CONFIG_DIR, { recursive: true });
24
+ fs_1.default.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
25
+ }
26
+ function deleteConfig() {
27
+ try {
28
+ fs_1.default.unlinkSync(CONFIG_FILE);
29
+ }
30
+ catch { }
31
+ }
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
+ }),
59
+ });
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
76
+ const authRes = await fetch(`${API_URL}/auth/github`, {
77
+ method: 'POST',
78
+ headers: { 'Content-Type': 'application/json' },
79
+ body: JSON.stringify({ access_token: accessToken }),
80
+ });
81
+ if (!authRes.ok) {
82
+ console.error('Error obteniendo API key:', await authRes.text());
83
+ process.exit(1);
84
+ }
85
+ const authData = await authRes.json();
86
+ saveConfig({ api_key: authData.api_key, github_username: authData.github_username });
87
+ console.log(`\nLogueado como ${authData.github_username}`);
88
+ }
89
+ // --- Commands ---
90
+ async function ruc(rucNumber) {
91
+ const config = loadConfig();
92
+ if (!config) {
93
+ console.error('No logueado. Ejecuta: latinfo login');
94
+ process.exit(1);
95
+ }
96
+ if (!/^\d{11}$/.test(rucNumber)) {
97
+ console.error('RUC inválido. Debe tener 11 dígitos.');
98
+ process.exit(1);
99
+ }
100
+ const res = await fetch(`${API_URL}/ruc/${rucNumber}`, {
101
+ headers: { Authorization: `Bearer ${config.api_key}` },
102
+ });
103
+ if (!res.ok) {
104
+ const err = await res.json();
105
+ console.error(err.message || err.error);
106
+ process.exit(1);
107
+ }
108
+ const data = await res.json();
109
+ console.log(`
110
+ RUC: ${data.ruc}
111
+ Razón Social: ${data.razonSocial}
112
+ Estado: ${data.estado}
113
+ Condición: ${data.condicion}
114
+ Ubigeo: ${data.ubigeo}
115
+ Dirección: ${[data.tipoVia, data.nombreVia, data.numero].filter(v => v && v !== '-').join(' ')}
116
+ Zona: ${[data.codigoZona, data.tipoZona].filter(v => v && v !== '-').join(' ')}
117
+ `.trim());
118
+ }
119
+ async function search(query) {
120
+ const config = loadConfig();
121
+ if (!config) {
122
+ console.error('No logueado. Ejecuta: latinfo login');
123
+ process.exit(1);
124
+ }
125
+ const res = await fetch(`${API_URL}/search?q=${encodeURIComponent(query)}`, {
126
+ headers: { Authorization: `Bearer ${config.api_key}` },
127
+ });
128
+ if (!res.ok) {
129
+ const err = await res.json();
130
+ console.error(err.message || err.error);
131
+ process.exit(1);
132
+ }
133
+ const data = await res.json();
134
+ if (data.count === 0) {
135
+ console.log('Sin resultados.');
136
+ return;
137
+ }
138
+ for (const r of data.results) {
139
+ console.log(` ${r.ruc} ${r.razonSocial} [${r.estado}]`);
140
+ }
141
+ console.log(`\n${data.count} resultado(s)`);
142
+ }
143
+ function whoami() {
144
+ const config = loadConfig();
145
+ if (!config) {
146
+ console.error('No logueado. Ejecuta: latinfo login');
147
+ process.exit(1);
148
+ }
149
+ console.log(config.github_username);
150
+ }
151
+ function logout() {
152
+ deleteConfig();
153
+ console.log('Sesión cerrada.');
154
+ }
155
+ function help() {
156
+ console.log(`
157
+ latinfo - Public data API for Latin America
158
+
159
+ Comandos:
160
+ login Autenticarse con GitHub
161
+ logout Cerrar sesión
162
+ whoami Ver usuario actual
163
+ ruc <ruc> Consultar un RUC (11 dígitos)
164
+ search <texto> Buscar por razón social
165
+ help Mostrar esta ayuda
166
+ `.trim());
167
+ }
168
+ // --- Main ---
169
+ const [command, ...args] = process.argv.slice(2);
170
+ switch (command) {
171
+ case 'login':
172
+ login().catch(e => { console.error(e); process.exit(1); });
173
+ break;
174
+ case 'logout':
175
+ logout();
176
+ break;
177
+ case 'whoami':
178
+ whoami();
179
+ break;
180
+ case 'ruc':
181
+ ruc(args[0]).catch(e => { console.error(e); process.exit(1); });
182
+ break;
183
+ case 'search':
184
+ search(args.join(' ')).catch(e => { console.error(e); process.exit(1); });
185
+ break;
186
+ default: help();
187
+ }
package/dist/sdk.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ interface RucResult {
2
+ ruc: string;
3
+ razonSocial: string;
4
+ estado: string;
5
+ condicion: string;
6
+ ubigeo: string;
7
+ tipoVia: string;
8
+ nombreVia: string;
9
+ codigoZona: string;
10
+ tipoZona: string;
11
+ numero: string;
12
+ interior: string;
13
+ lote: string;
14
+ departamento: string;
15
+ manzana: string;
16
+ kilometro: string;
17
+ }
18
+ interface DniResult extends RucResult {
19
+ dni: string;
20
+ }
21
+ interface SearchResult {
22
+ ruc: string;
23
+ razonSocial: string;
24
+ estado: string;
25
+ condicion: string;
26
+ }
27
+ interface SearchResponse {
28
+ count: number;
29
+ results: SearchResult[];
30
+ }
31
+ export declare class Latinfo {
32
+ private apiKey;
33
+ private baseUrl;
34
+ constructor(apiKey: string, options?: {
35
+ baseUrl?: string;
36
+ });
37
+ private request;
38
+ ruc(ruc: string): Promise<RucResult>;
39
+ dni(dni: string): Promise<DniResult>;
40
+ search(query: string): Promise<SearchResponse>;
41
+ }
42
+ export {};
package/dist/sdk.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Latinfo = void 0;
4
+ const DEFAULT_API_URL = 'https://api.latinfo.dev';
5
+ class Latinfo {
6
+ apiKey;
7
+ baseUrl;
8
+ constructor(apiKey, options) {
9
+ this.apiKey = apiKey;
10
+ this.baseUrl = options?.baseUrl || DEFAULT_API_URL;
11
+ }
12
+ async request(path) {
13
+ const res = await fetch(`${this.baseUrl}${path}`, {
14
+ headers: { Authorization: `Bearer ${this.apiKey}` },
15
+ });
16
+ if (!res.ok) {
17
+ const body = await res.json();
18
+ throw new Error(body.message || body.error);
19
+ }
20
+ return res.json();
21
+ }
22
+ async ruc(ruc) {
23
+ return this.request(`/ruc/${ruc}`);
24
+ }
25
+ async dni(dni) {
26
+ return this.request(`/dni/${dni}`);
27
+ }
28
+ async search(query) {
29
+ return this.request(`/search?q=${encodeURIComponent(query)}`);
30
+ }
31
+ }
32
+ exports.Latinfo = Latinfo;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "latinfo",
3
+ "version": "0.1.0",
4
+ "description": "Public data API for Latin America - SDK & CLI",
5
+ "main": "dist/sdk.js",
6
+ "types": "dist/sdk.d.ts",
7
+ "bin": {
8
+ "latinfo": "dist/index.js"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/sdk.d.ts",
13
+ "default": "./dist/sdk.js"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "build": "tsc",
18
+ "dev": "tsx src/index.ts"
19
+ },
20
+ "files": ["dist"],
21
+ "devDependencies": {
22
+ "tsx": "^4.19.0",
23
+ "typescript": "^5.6.0",
24
+ "@types/node": "^22.0.0"
25
+ }
26
+ }