mirror of
https://github.com/Sevichecc/raycast-akkoma-extension.git
synced 2025-04-30 14:49:29 +08:00
fix: could not send status
This commit is contained in:
parent
803283e037
commit
44599de9b2
2 changed files with 12 additions and 11 deletions
13
src/api.ts
13
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<OAuth.TokenResponse> => {
|
||||
const { instance } = getPreferenceValues<Preference>();
|
||||
|
@ -30,7 +30,7 @@ export const createApp = async (): Promise<Credentials> => {
|
|||
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,
|
||||
content_type,
|
||||
}: Partial<Status>): Promise<StatusResponse> => {
|
||||
const { instance } = getPreferenceValues<Preference>();
|
||||
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;
|
||||
|
|
|
@ -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<void> => {
|
|||
}
|
||||
|
||||
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));
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue