Skip to content

Commit 768f942

Browse files
author
Elitezen
committed
1.0.2 Switched to Node Fetch API
1 parent a4e40e0 commit 768f942

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Changelog
2+
3+
## 1.0.2
4+
- Switched from https module to Node Fetch API

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ Documentation: https://github.com/Elitezen/open-trivia-db-wrapper/wiki/Documenta
66

77
Support me: https://www.paypal.com/paypalme/alejandromuratalla
88

9+
## Updates
10+
### 1.0.2
11+
- Switched from https module to Node Fetch API (now requires Node 18)
12+
913
# Installation
1014
Ensure you are using Node version 14 or higher and that your enviroment contains the `https` module.
1115
```sh-session
12-
npm i open-trivia-db
16+
npm i open-trivia-db // Requires NodeJS 18 or higher
17+
18+
npm i [email protected] // Below NodeJS 18
1319
```
1420

1521
# Example Usage

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "open-trivia-db",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A wrapper for the Open Trivia Database API. Built with TypeScript, works with VanillaJS.",
55
"keywords": [
66
"opentriviadatabase",

src/Classes/OpenTDBUtil.ts

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { get } from "https";
21
import { Question, QuestionOptions, RawQuestion } from "../Typings/interfaces";
32
import {
43
OpenTDBResponseCode,
@@ -67,35 +66,17 @@ export default class OpenTDBUtil {
6766
);
6867

6968
return new Promise((resolve, reject) => {
70-
let data = "";
71-
const req = get(url, (res) => {
72-
res.on("data", (chunk) => (data += chunk));
73-
res.on("error", reject);
74-
res.on("end", () => {
75-
if (data.length > 0) {
76-
try {
77-
const body = JSON.parse(data);
78-
const responseCode = (body?.response_code?.toString?.() ||
79-
null) as OpenTDBResponseCode | null;
80-
if (responseCode) {
81-
if (responseCode > 0) throw new OpenTDBResponse(responseCode);
82-
}
83-
84-
resolve(body);
85-
} catch (err) {
86-
reject(err);
87-
}
88-
} else {
89-
throw new OpenTDBError(
90-
"API responded with no data",
91-
OpenTDBError.errors.headers.EMPTY_RESPONSE
92-
);
69+
fetch(url)
70+
.then(req => req.json())
71+
.then(data => {
72+
const responseCode = (data?.response_code?.toString?.() ||
73+
null) as OpenTDBResponseCode | null;
74+
if (responseCode) {
75+
if (responseCode > 0) throw new OpenTDBResponse(responseCode);
9376
}
94-
});
95-
});
9677

97-
req.on("error", reject);
98-
req.end();
78+
resolve(data);
79+
});
9980
});
10081
}
10182

0 commit comments

Comments
 (0)