From 44599de9b29872f051b11aed6613b15b07cda783 Mon Sep 17 00:00:00 2001 From: sevichecc <91365763+Sevichecc@users.noreply.github.com> Date: Fri, 14 Apr 2023 21:37:51 +0800 Subject: [PATCH] fix: could not send status --- src/api.ts | 15 +++++++++------ src/oauth.ts | 8 +++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/api.ts b/src/api.ts index 930830b..de43429 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,7 +1,7 @@ import fetch from "node-fetch"; import { OAuth, getPreferenceValues } from "@raycast/api"; import { Credentials, Preference, Status ,StatusResponse} from "./types"; -import { authorize } from "./oauth"; +import { authorize, client } from "./oauth"; export const fetchToken = async (params: URLSearchParams, errorMessage: string): Promise => { const { instance } = getPreferenceValues(); @@ -30,7 +30,7 @@ export const createApp = async (): Promise => { body: JSON.stringify({ client_name: "raycast-akkoma-extension", redirect_uris: "https://raycast.com/redirect?packageName=Extension", - scopes: "read write push", + scopes: "read write", website: "https://raycast.com", }), }); @@ -48,26 +48,29 @@ export const postNewStatus = async ({ spoiler_text, sensitive, scheduled_at, -}: Partial) : Promise => { + content_type, +}: Partial): Promise => { const { instance } = getPreferenceValues(); - const token = await authorize(); + const tokenSet = await client.getTokens(); const response = await fetch(`https://${instance}/api/v1/statuses`, { method: "POST", headers: { "Content-Type": "application/json", - "Authorization": "Bearer" + token, + "Authorization": "Bearer " + tokenSet?.accessToken, }, body: JSON.stringify({ status, visibility, spoiler_text, sensitive, + content_type, scheduled_at, }), }); + if (!response.ok) { - throw new Error("Failed to pulish new status"); + throw new Error("Failed to pulish new status!"); } return (await response.json()) as StatusResponse; diff --git a/src/oauth.ts b/src/oauth.ts index 45f3750..40a6cd2 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -2,12 +2,12 @@ import { OAuth, getPreferenceValues } from "@raycast/api"; import { Preference } from "./types"; import { fetchToken,createApp} from "./api"; -const client = new OAuth.PKCEClient({ +export const client = new OAuth.PKCEClient({ redirectMethod: OAuth.RedirectMethod.Web, providerName: "Akkoma", providerIcon: "akkoma-icon.png", providerId: "akkoma", - description: "Connect to your Akkoma | Pleroma | Mastodon account", + description: "Connect to your Akkoma / Pleroma acount", }); const requestAccessToken = async ( @@ -59,14 +59,12 @@ export const authorize = async (): Promise => { } const { client_id, client_secret } = await createApp(); - const authRequest = await client.authorizationRequest({ endpoint: `https://${instance}/oauth/authorize`, clientId: client_id, - scope: "read write push", + scope: "read write", }); const { authorizationCode } = await client.authorize(authRequest); - await client.setTokens(await requestAccessToken(client_id, client_secret, authRequest, authorizationCode)); };