From d8ecc4269f2049f64ee60f518076ccd162857a36 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 13 Jan 2023 08:52:24 -0500 Subject: replace node-fetch with wretch --- src/connections/Facebook/index.ts | 55 +++++++++------------------------------ 1 file changed, 12 insertions(+), 43 deletions(-) (limited to 'src/connections/Facebook/index.ts') diff --git a/src/connections/Facebook/index.ts b/src/connections/Facebook/index.ts index 67f8da79..5413f867 100644 --- a/src/connections/Facebook/index.ts +++ b/src/connections/Facebook/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 { FacebookSettings } from "./FacebookSettings"; @@ -83,59 +82,29 @@ export default class FacebookConnection extends Connection { const url = this.getTokenUrl(code); - return fetch(url.toString(), { - method: "GET", - headers: { + return wretch(url.toString()) + .headers({ Accept: "application/json", - }, - }) - .then((res) => { - if (!res.ok) { - throw new ApiError("Failed to exchange code", 0, 400); - } - - return res.json(); }) - .then( - ( - res: ConnectedAccountCommonOAuthTokenResponse & - FacebookErrorResponse, - ) => { - if (res.error) throw new Error(res.error.message); - return res; - }, - ) + .get() + .json() .catch((e) => { - console.error( - `Error exchanging token for ${this.id} connection: ${e}`, - ); + console.error(e); throw DiscordApiErrors.GENERAL_ERROR; }); } async getUser(token: string): Promise { 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(); - }) - .then((res: UserResponse & FacebookErrorResponse) => { - if (res.error) throw new Error(res.error.message); - return res; }) + .get() + .json() .catch((e) => { - console.error( - `Error fetching user for ${this.id} connection: ${e}`, - ); + console.error(e); throw DiscordApiErrors.GENERAL_ERROR; }); } -- cgit 1.5.1