comits
This commit is contained in:
18
src/clima.ts
18
src/clima.ts
@@ -1,7 +1,15 @@
|
|||||||
import { log } from "./logger.js"
|
import { log } from "./logger.js";
|
||||||
|
import { ErrorResponse, GeoResponse } from "./types.js";
|
||||||
|
|
||||||
export async function ubicacion(lugar: string, limit = '1') {
|
export async function ubicacion(lugar: string, limit = '1'): Promise<ErrorResponse | GeoResponse> {
|
||||||
const response = await fetch(`${process.env.WEATHER_URL}/geo/1.0/direct?q=${lugar}&limit=${limit}&appid=${process.env.WEATHER_API_KEY}`)
|
const response = await fetch(`${process.env.WEATHER_URL}/geo/1.0/direct?q=${lugar}&limit=${limit}&appid=${process.env.WEATHER_API_KEY}`);
|
||||||
log('GET', response)
|
log('GET', response);
|
||||||
return response
|
if (!response.ok) {
|
||||||
|
return {
|
||||||
|
status: response.status,
|
||||||
|
message: response.statusText,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return await response.json();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
12
src/index.ts
12
src/index.ts
@@ -1,15 +1,13 @@
|
|||||||
import { ubicacion } from "./clima.js";
|
import { ubicacion } from "./clima.js";
|
||||||
|
|
||||||
if (typeof process.env.WEATHER_URL === 'undefined' || typeof process.env.WEATHER_API_KEY === 'undefined' || typeof process.argv[2] === 'undefined') {
|
if (typeof process.env.WEATHER_URL === 'undefined' || typeof process.env.WEATHER_API_KEY === 'undefined' || typeof process.argv[2] === 'undefined') {
|
||||||
console.error('WEATHER_URL', typeof process.env.WEATHER_URL)
|
console.error('WEATHER_URL', typeof process.env.WEATHER_URL);
|
||||||
console.error('WEATHER_API_KEY', typeof process.env.WEATHER_API_KEY)
|
console.error('WEATHER_API_KEY', typeof process.env.WEATHER_API_KEY);
|
||||||
console.error('Ciudad', typeof process.argv[2])
|
console.error('Ciudad', typeof process.argv[2]);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const buqueda = await ubicacion(process.argv[2])
|
const buqueda = await ubicacion(process.argv[2]);
|
||||||
|
|
||||||
console.log(buqueda.status, buqueda.statusText)
|
console.log('Busqueda', buqueda);
|
||||||
console.log('url', buqueda.url)
|
|
||||||
console.log('Body', await buqueda.json())
|
|
||||||
|
|||||||
@@ -8,9 +8,8 @@ console.log(filename, fileExist);
|
|||||||
if (!fileExist) appendFileSync(filename, ['Date', 'Method', 'Domain', 'Path', 'Status'].join(',') + '\n', 'utf8');
|
if (!fileExist) appendFileSync(filename, ['Date', 'Method', 'Domain', 'Path', 'Status'].join(',') + '\n', 'utf8');
|
||||||
|
|
||||||
export const log = (method: string, response: Response) => {
|
export const log = (method: string, response: Response) => {
|
||||||
const { url, status } = response,
|
const { host, pathname } = new URL(response.url);
|
||||||
{ host, pathname } = new URL(url);
|
appendFileSync(filename, [isoDateInTimeZone(), method, host, pathname, response.status].join(',') + '\n', 'utf8');
|
||||||
appendFileSync(filename, [isoDateInTimeZone(), method, host, pathname, status].join(',') + '\n', 'utf8');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function isoDateInTimeZone(timeZone = 'America/Buenos_Aires', date = new Date()) {
|
function isoDateInTimeZone(timeZone = 'America/Buenos_Aires', date = new Date()) {
|
||||||
|
|||||||
12
src/types.ts
Normal file
12
src/types.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
export type ErrorResponse = {
|
||||||
|
status: number;
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GeoResponse = Array<{
|
||||||
|
name: string;
|
||||||
|
lat: number;
|
||||||
|
lon: number;
|
||||||
|
country: string;
|
||||||
|
state: string;
|
||||||
|
}>;
|
||||||
Reference in New Issue
Block a user