latinfo 0.2.0 → 0.3.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.
- package/dist/index.js +56 -22
- package/dist/sdk.d.ts +13 -3
- package/dist/sdk.js +27 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -43,7 +43,7 @@ async function waitForCallback(port) {
|
|
|
43
43
|
const code = url.searchParams.get('code');
|
|
44
44
|
if (code) {
|
|
45
45
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
46
|
-
res.end('<h2>
|
|
46
|
+
res.end('<h2>Done. You can close this window.</h2>');
|
|
47
47
|
server.close();
|
|
48
48
|
resolve(code);
|
|
49
49
|
}
|
|
@@ -51,11 +51,11 @@ async function waitForCallback(port) {
|
|
|
51
51
|
res.writeHead(400);
|
|
52
52
|
res.end('Missing code');
|
|
53
53
|
server.close();
|
|
54
|
-
reject(new Error('No
|
|
54
|
+
reject(new Error('No code received from GitHub'));
|
|
55
55
|
}
|
|
56
56
|
});
|
|
57
57
|
server.listen(port, () => { });
|
|
58
|
-
setTimeout(() => { server.close(); reject(new Error('Timeout
|
|
58
|
+
setTimeout(() => { server.close(); reject(new Error('Timeout waiting for authorization')); }, 120_000);
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
async function login() {
|
|
@@ -63,7 +63,7 @@ async function login() {
|
|
|
63
63
|
const redirectUri = `http://localhost:${port}/callback`;
|
|
64
64
|
const scope = 'read:user,user:email';
|
|
65
65
|
const authUrl = `https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}`;
|
|
66
|
-
console.log('
|
|
66
|
+
console.log('Opening GitHub...');
|
|
67
67
|
openBrowser(authUrl);
|
|
68
68
|
// 1. Wait for GitHub to redirect back with code
|
|
69
69
|
const code = await waitForCallback(port);
|
|
@@ -74,25 +74,25 @@ async function login() {
|
|
|
74
74
|
body: JSON.stringify({ code, redirect_uri: redirectUri }),
|
|
75
75
|
});
|
|
76
76
|
if (!authRes.ok) {
|
|
77
|
-
console.error('Error
|
|
77
|
+
console.error('Error getting API key:', await authRes.text());
|
|
78
78
|
process.exit(1);
|
|
79
79
|
}
|
|
80
80
|
const authData = await authRes.json();
|
|
81
81
|
saveConfig({ api_key: authData.api_key, github_username: authData.github_username });
|
|
82
|
-
console.log(`
|
|
82
|
+
console.log(`Logged in as ${authData.github_username}`);
|
|
83
83
|
}
|
|
84
84
|
// --- Commands ---
|
|
85
85
|
async function ruc(rucNumber) {
|
|
86
86
|
const config = loadConfig();
|
|
87
87
|
if (!config) {
|
|
88
|
-
console.error('
|
|
88
|
+
console.error('Not logged in. Run: latinfo login');
|
|
89
89
|
process.exit(1);
|
|
90
90
|
}
|
|
91
91
|
if (!/^\d{11}$/.test(rucNumber)) {
|
|
92
|
-
console.error('RUC
|
|
92
|
+
console.error('Invalid RUC. Must be 11 digits.');
|
|
93
93
|
process.exit(1);
|
|
94
94
|
}
|
|
95
|
-
const res = await fetch(`${API_URL}/ruc/${rucNumber}`, {
|
|
95
|
+
const res = await fetch(`${API_URL}/pe/ruc/${rucNumber}`, {
|
|
96
96
|
headers: { Authorization: `Bearer ${config.api_key}` },
|
|
97
97
|
});
|
|
98
98
|
if (!res.ok) {
|
|
@@ -111,13 +111,43 @@ async function ruc(rucNumber) {
|
|
|
111
111
|
Zona: ${[data.codigoZona, data.tipoZona].filter(v => v && v !== '-').join(' ')}
|
|
112
112
|
`.trim());
|
|
113
113
|
}
|
|
114
|
+
async function dni(dniNumber) {
|
|
115
|
+
const config = loadConfig();
|
|
116
|
+
if (!config) {
|
|
117
|
+
console.error('Not logged in. Run: latinfo login');
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
if (!/^\d{8}$/.test(dniNumber)) {
|
|
121
|
+
console.error('Invalid DNI. Must be 8 digits.');
|
|
122
|
+
process.exit(1);
|
|
123
|
+
}
|
|
124
|
+
const res = await fetch(`${API_URL}/pe/dni/${dniNumber}`, {
|
|
125
|
+
headers: { Authorization: `Bearer ${config.api_key}` },
|
|
126
|
+
});
|
|
127
|
+
if (!res.ok) {
|
|
128
|
+
const err = await res.json();
|
|
129
|
+
console.error(err.message || err.error);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
const data = await res.json();
|
|
133
|
+
console.log(`
|
|
134
|
+
DNI: ${data.dni}
|
|
135
|
+
RUC: ${data.ruc}
|
|
136
|
+
Razón Social: ${data.razonSocial}
|
|
137
|
+
Estado: ${data.estado}
|
|
138
|
+
Condición: ${data.condicion}
|
|
139
|
+
Ubigeo: ${data.ubigeo}
|
|
140
|
+
Dirección: ${[data.tipoVia, data.nombreVia, data.numero].filter(v => v && v !== '-').join(' ')}
|
|
141
|
+
Zona: ${[data.codigoZona, data.tipoZona].filter(v => v && v !== '-').join(' ')}
|
|
142
|
+
`.trim());
|
|
143
|
+
}
|
|
114
144
|
async function search(query) {
|
|
115
145
|
const config = loadConfig();
|
|
116
146
|
if (!config) {
|
|
117
|
-
console.error('
|
|
147
|
+
console.error('Not logged in. Run: latinfo login');
|
|
118
148
|
process.exit(1);
|
|
119
149
|
}
|
|
120
|
-
const res = await fetch(`${API_URL}/search?q=${encodeURIComponent(query)}`, {
|
|
150
|
+
const res = await fetch(`${API_URL}/pe/search?q=${encodeURIComponent(query)}`, {
|
|
121
151
|
headers: { Authorization: `Bearer ${config.api_key}` },
|
|
122
152
|
});
|
|
123
153
|
if (!res.ok) {
|
|
@@ -127,37 +157,38 @@ async function search(query) {
|
|
|
127
157
|
}
|
|
128
158
|
const data = await res.json();
|
|
129
159
|
if (data.count === 0) {
|
|
130
|
-
console.log('
|
|
160
|
+
console.log('No results found.');
|
|
131
161
|
return;
|
|
132
162
|
}
|
|
133
163
|
for (const r of data.results) {
|
|
134
164
|
console.log(` ${r.ruc} ${r.razonSocial} [${r.estado}]`);
|
|
135
165
|
}
|
|
136
|
-
console.log(`\n${data.count}
|
|
166
|
+
console.log(`\n${data.count} result(s)`);
|
|
137
167
|
}
|
|
138
168
|
function whoami() {
|
|
139
169
|
const config = loadConfig();
|
|
140
170
|
if (!config) {
|
|
141
|
-
console.error('
|
|
171
|
+
console.error('Not logged in. Run: latinfo login');
|
|
142
172
|
process.exit(1);
|
|
143
173
|
}
|
|
144
174
|
console.log(config.github_username);
|
|
145
175
|
}
|
|
146
176
|
function logout() {
|
|
147
177
|
deleteConfig();
|
|
148
|
-
console.log('
|
|
178
|
+
console.log('Logged out.');
|
|
149
179
|
}
|
|
150
180
|
function help() {
|
|
151
181
|
console.log(`
|
|
152
182
|
latinfo - Public data API for Latin America
|
|
153
183
|
|
|
154
|
-
|
|
155
|
-
login
|
|
156
|
-
logout
|
|
157
|
-
whoami
|
|
158
|
-
ruc <ruc>
|
|
159
|
-
|
|
160
|
-
|
|
184
|
+
Commands:
|
|
185
|
+
login Authenticate with GitHub
|
|
186
|
+
logout Sign out
|
|
187
|
+
whoami Show current user
|
|
188
|
+
ruc <ruc> Lookup by RUC (11 digits)
|
|
189
|
+
dni <dni> Lookup by DNI (8 digits)
|
|
190
|
+
search <query> Search by business name
|
|
191
|
+
help Show this help
|
|
161
192
|
`.trim());
|
|
162
193
|
}
|
|
163
194
|
// --- Main ---
|
|
@@ -175,6 +206,9 @@ switch (command) {
|
|
|
175
206
|
case 'ruc':
|
|
176
207
|
ruc(args[0]).catch(e => { console.error(e); process.exit(1); });
|
|
177
208
|
break;
|
|
209
|
+
case 'dni':
|
|
210
|
+
dni(args[0]).catch(e => { console.error(e); process.exit(1); });
|
|
211
|
+
break;
|
|
178
212
|
case 'search':
|
|
179
213
|
search(args.join(' ')).catch(e => { console.error(e); process.exit(1); });
|
|
180
214
|
break;
|
package/dist/sdk.d.ts
CHANGED
|
@@ -28,15 +28,25 @@ interface SearchResponse {
|
|
|
28
28
|
count: number;
|
|
29
29
|
results: SearchResult[];
|
|
30
30
|
}
|
|
31
|
+
declare class Country {
|
|
32
|
+
private request;
|
|
33
|
+
private prefix;
|
|
34
|
+
constructor(request: <T>(path: string) => Promise<T>, prefix: string);
|
|
35
|
+
protected countryRequest<T>(path: string): Promise<T>;
|
|
36
|
+
}
|
|
37
|
+
declare class Peru extends Country {
|
|
38
|
+
constructor(request: <T>(path: string) => Promise<T>);
|
|
39
|
+
ruc(ruc: string): Promise<RucResult>;
|
|
40
|
+
dni(dni: string): Promise<DniResult>;
|
|
41
|
+
search(query: string): Promise<SearchResponse>;
|
|
42
|
+
}
|
|
31
43
|
export declare class Latinfo {
|
|
32
44
|
private apiKey;
|
|
33
45
|
private baseUrl;
|
|
46
|
+
pe: Peru;
|
|
34
47
|
constructor(apiKey: string, options?: {
|
|
35
48
|
baseUrl?: string;
|
|
36
49
|
});
|
|
37
50
|
private request;
|
|
38
|
-
ruc(ruc: string): Promise<RucResult>;
|
|
39
|
-
dni(dni: string): Promise<DniResult>;
|
|
40
|
-
search(query: string): Promise<SearchResponse>;
|
|
41
51
|
}
|
|
42
52
|
export {};
|
package/dist/sdk.js
CHANGED
|
@@ -2,12 +2,39 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Latinfo = void 0;
|
|
4
4
|
const DEFAULT_API_URL = 'https://api.latinfo.dev';
|
|
5
|
+
class Country {
|
|
6
|
+
request;
|
|
7
|
+
prefix;
|
|
8
|
+
constructor(request, prefix) {
|
|
9
|
+
this.request = request;
|
|
10
|
+
this.prefix = prefix;
|
|
11
|
+
}
|
|
12
|
+
countryRequest(path) {
|
|
13
|
+
return this.request(`/${this.prefix}${path}`);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class Peru extends Country {
|
|
17
|
+
constructor(request) {
|
|
18
|
+
super(request, 'pe');
|
|
19
|
+
}
|
|
20
|
+
async ruc(ruc) {
|
|
21
|
+
return this.countryRequest(`/ruc/${ruc}`);
|
|
22
|
+
}
|
|
23
|
+
async dni(dni) {
|
|
24
|
+
return this.countryRequest(`/dni/${dni}`);
|
|
25
|
+
}
|
|
26
|
+
async search(query) {
|
|
27
|
+
return this.countryRequest(`/search?q=${encodeURIComponent(query)}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
5
30
|
class Latinfo {
|
|
6
31
|
apiKey;
|
|
7
32
|
baseUrl;
|
|
33
|
+
pe;
|
|
8
34
|
constructor(apiKey, options) {
|
|
9
35
|
this.apiKey = apiKey;
|
|
10
36
|
this.baseUrl = options?.baseUrl || DEFAULT_API_URL;
|
|
37
|
+
this.pe = new Peru(this.request.bind(this));
|
|
11
38
|
}
|
|
12
39
|
async request(path) {
|
|
13
40
|
const res = await fetch(`${this.baseUrl}${path}`, {
|
|
@@ -19,14 +46,5 @@ class Latinfo {
|
|
|
19
46
|
}
|
|
20
47
|
return res.json();
|
|
21
48
|
}
|
|
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
49
|
}
|
|
32
50
|
exports.Latinfo = Latinfo;
|