From 2011d1c969c0bb394231d944ac59d302783778c0 Mon Sep 17 00:00:00 2001 From: Facundo Redon Date: Mon, 26 Jan 2026 00:26:20 -0300 Subject: [PATCH] comits --- src/clima.ts | 18 +++++++++++++----- src/index.ts | 12 +++++------- src/logger.ts | 5 ++--- src/types.ts | 12 ++++++++++++ 4 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 src/types.ts diff --git a/src/clima.ts b/src/clima.ts index 9d3df00..0de82df 100755 --- a/src/clima.ts +++ b/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') { - 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) - return response +export async function ubicacion(lugar: string, limit = '1'): Promise { + 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); + if (!response.ok) { + return { + status: response.status, + message: response.statusText, + }; + } else { + return await response.json(); + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 586acda..df06ed6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,13 @@ import { ubicacion } from "./clima.js"; 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_API_KEY', typeof process.env.WEATHER_API_KEY) - console.error('Ciudad', typeof process.argv[2]) + console.error('WEATHER_URL', typeof process.env.WEATHER_URL); + console.error('WEATHER_API_KEY', typeof process.env.WEATHER_API_KEY); + console.error('Ciudad', typeof process.argv[2]); 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('url', buqueda.url) -console.log('Body', await buqueda.json()) +console.log('Busqueda', buqueda); diff --git a/src/logger.ts b/src/logger.ts index 57d3195..3b65bba 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -8,9 +8,8 @@ console.log(filename, fileExist); if (!fileExist) appendFileSync(filename, ['Date', 'Method', 'Domain', 'Path', 'Status'].join(',') + '\n', 'utf8'); export const log = (method: string, response: Response) => { - const { url, status } = response, - { host, pathname } = new URL(url); - appendFileSync(filename, [isoDateInTimeZone(), method, host, pathname, status].join(',') + '\n', 'utf8'); + const { host, pathname } = new URL(response.url); + appendFileSync(filename, [isoDateInTimeZone(), method, host, pathname, response.status].join(',') + '\n', 'utf8'); }; function isoDateInTimeZone(timeZone = 'America/Buenos_Aires', date = new Date()) { diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..8f7ce61 --- /dev/null +++ b/src/types.ts @@ -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; +}>; \ No newline at end of file