summary refs log tree commit diff
path: root/src/connections/Reddit/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/connections/Reddit/index.ts')
-rw-r--r--src/connections/Reddit/index.ts58
1 files changed, 21 insertions, 37 deletions
diff --git a/src/connections/Reddit/index.ts b/src/connections/Reddit/index.ts

index 06fbcbe5..70b4a8af 100644 --- a/src/connections/Reddit/index.ts +++ b/src/connections/Reddit/index.ts
@@ -1,5 +1,4 @@ import { - ApiError, Config, ConnectedAccount, ConnectedAccountCommonOAuthTokenResponse, @@ -7,7 +6,7 @@ import { ConnectionLoader, DiscordApiErrors, } from "@fosscord/util"; -import fetch from "node-fetch"; +import wretch from "wretch"; import Connection from "../../util/connections/Connection"; import { RedditSettings } from "./RedditSettings"; @@ -74,57 +73,42 @@ export default class RedditConnection extends Connection { const url = this.getTokenUrl(); - return fetch(url.toString(), { - method: "POST", - headers: { + return wretch(url.toString()) + .headers({ Accept: "application/json", Authorization: `Basic ${Buffer.from( `${this.settings.clientId}:${this.settings.clientSecret}`, ).toString("base64")}`, "Content-Type": "application/x-www-form-urlencoded", - }, - body: new URLSearchParams({ - grant_type: "authorization_code", - code: code, - redirect_uri: `${ - Config.get().cdn.endpointPrivate || "http://localhost:3001" - }/connections/${this.id}/callback`, - }), - }) - .then((res) => { - if (!res.ok) { - throw new ApiError("Failed to code", 0, 400); - } - - return res.json(); }) + .body( + new URLSearchParams({ + grant_type: "authorization_code", + code: code, + redirect_uri: `${ + Config.get().cdn.endpointPrivate || + "http://localhost:3001" + }/connections/${this.id}/callback`, + }), + ) + .post() + .json<ConnectedAccountCommonOAuthTokenResponse>() .catch((e) => { - console.error( - `Error exchanging code for ${this.id} connection: ${e}`, - ); + console.error(e); throw DiscordApiErrors.GENERAL_ERROR; }); } async getUser(token: string): Promise<UserResponse> { const url = new URL(this.userInfoUrl); - return fetch(url.toString(), { - method: "GET", - headers: { + return wretch(url.toString()) + .headers({ Authorization: `Bearer ${token}`, - }, - }) - .then((res) => { - if (!res.ok) { - throw new ApiError("Failed to fetch user", 0, 400); - } - - return res.json(); }) + .get() + .json<UserResponse>() .catch((e) => { - console.error( - `Error fetching user for ${this.id} connection: ${e}`, - ); + console.error(e); throw DiscordApiErrors.GENERAL_ERROR; }); }