diff options
author | Puyodead1 <puyodead@proton.me> | 2023-01-13 08:52:24 -0500 |
---|---|---|
committer | Puyodead1 <puyodead@proton.me> | 2023-03-18 19:28:46 -0400 |
commit | d8ecc4269f2049f64ee60f518076ccd162857a36 (patch) | |
tree | cd14b6082bac049b26a8a493898b43ef3216c10b /src/connections/Discord | |
parent | add Xbox connection (diff) | |
download | server-d8ecc4269f2049f64ee60f518076ccd162857a36.tar.xz |
replace node-fetch with wretch
Diffstat (limited to 'src/connections/Discord')
-rw-r--r-- | src/connections/Discord/index.ts | 62 |
1 files changed, 23 insertions, 39 deletions
diff --git a/src/connections/Discord/index.ts b/src/connections/Discord/index.ts index 23f5d978..52fc9ffd 100644 --- a/src/connections/Discord/index.ts +++ b/src/connections/Discord/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 { DiscordSettings } from "./DiscordSettings"; @@ -66,56 +65,41 @@ export default class DiscordConnection extends Connection { this.validateState(state); const url = this.getTokenUrl(); - return fetch(url, { - method: "POST", - headers: { + return wretch(url.toString()) + .headers({ Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded", - }, - body: new URLSearchParams({ - client_id: this.settings.clientId!, - client_secret: this.settings.clientSecret!, - 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 exchange token", 0, 400); - } - - return res.json(); }) + .body( + new URLSearchParams({ + client_id: this.settings.clientId!, + client_secret: this.settings.clientSecret!, + 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 token 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; }); } |