Compare commits

...

3 commits

Author SHA1 Message Date
Dessalines
c0281d32d8 Moving return to else. 2023-05-29 19:08:47 -04:00
Dessalines
c5fb4b559e Simplifying 2023-05-29 19:07:06 -04:00
Dessalines
5c26a3835a Better error wrapping. 2023-05-29 19:00:47 -04:00
2 changed files with 10 additions and 25 deletions

View file

@ -7,9 +7,7 @@
"author": "Dessalines <tyhou13@gmx.com>",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"/dist"
],
"files": ["/dist"],
"scripts": {
"build": "tsc",
"docs": "typedoc src/index.ts",
@ -17,13 +15,8 @@
"prepare": "yarn run build && husky install"
},
"lint-staged": {
"*.{ts,tsx,js}": [
"prettier --write",
"eslint --fix"
],
"package.json": [
"sortpack"
]
"*.{ts,tsx,js}": ["prettier --write", "eslint --fix"],
"package.json": ["sortpack"]
},
"dependencies": {
"cross-fetch": "^3.1.5",

View file

@ -1295,18 +1295,15 @@ export class LemmyHttp {
endpoint: string,
form: BodyType
): Promise<ResponseType> {
let response: Response;
if (type_ === HttpType.Get) {
const getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
const response = await fetch(getUrl, {
response = await fetch(getUrl, {
method: HttpType.Get,
headers: this.headers,
});
await this.checkandThrowError(response);
return await response.json();
} else {
const response = await fetch(this.buildFullUrl(endpoint), {
response = await fetch(this.buildFullUrl(endpoint), {
method: type_,
headers: {
"Content-Type": "application/json",
@ -1314,18 +1311,13 @@ export class LemmyHttp {
},
body: JSON.stringify(form),
});
await this.checkandThrowError(response);
return await response.json();
}
}
const json = await response.json();
private async checkandThrowError(response: Response) {
if (!response.ok) {
const errJson = await response.json();
const errString: string = errJson["error"] ?? response.statusText;
throw new Error(errString);
throw json["error"] ?? response.statusText;
} else {
return json;
}
}
}