Skip to content

Commit

Permalink
⚡️ removed all dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
loloToster committed Apr 24, 2023
1 parent a16682e commit 8cc78a5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 56 deletions.
43 changes: 0 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
],
"author": "loloToster",
"license": "MIT",
"dependencies": {
"axios": "^0.21.4"
},
"devDependencies": {
"@types/jest": "^29.2.4",
"gen-esm-wrapper": "^1.1.3",
Expand Down
13 changes: 3 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosResponse } from "axios";
import { get } from "./request";

import currentParser from "./parsers/weather/current-parser";
import forecastParser from "./parsers/weather/forecast-parser";
Expand Down Expand Up @@ -427,7 +427,7 @@ export class OpenWeatherAPI {

/**
* Getter for daily weather
*
*
* @param limit - maximum length of returned array
* @param includeToday - boolean indicating whether to include today's weather in returned array
* @param options - options used only for this call
Expand Down Expand Up @@ -659,14 +659,7 @@ export class OpenWeatherAPI {
}

private async fetch(url: string) {
let res: AxiosResponse;

try {
res = await axios.get(url);
} catch (err: any) {
res = err.response;
}

const res = await get(url);
const data = res.data;

if (data.cod && parseInt(data.cod) !== 200) {
Expand Down
45 changes: 45 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export interface Resp {
status: number | undefined;
data: any;
}

const inBrowser =
typeof window !== "undefined" && typeof window.fetch !== "undefined";
const inNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null;

export async function get(url: string): Promise<Resp> {
if (inBrowser) {
const response = await window.fetch(url);

return {
status: response.status,
data: await response.json(),
};
} else if (inNode) {
const getter = url.startsWith("https")
? await import("https")
: await import("http");

return await new Promise((res, rej) => {
const request = getter.get(url, (response) => {
let data = "";
response.on("error", rej);
response.on("data", (chunk) => (data += chunk.toString()));
response.on("end", () =>
res({
status: response.statusCode,
data: JSON.parse(data),
})
);
});

request.on("error", rej);
request.end();
});
} else {
throw new Error("Unknown environment");
}
}

0 comments on commit 8cc78a5

Please sign in to comment.