mirror of
https://github.com/Sevichecc/raycast-akkoma-extension.git
synced 2025-04-30 22:49:30 +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
15
src/api.ts
15
src/api.ts
|
@ -1,7 +1,7 @@
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import { OAuth, getPreferenceValues } from "@raycast/api";
|
import { OAuth, getPreferenceValues } from "@raycast/api";
|
||||||
import { Credentials, Preference, Status ,StatusResponse} from "./types";
|
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> => {
|
export const fetchToken = async (params: URLSearchParams, errorMessage: string): Promise<OAuth.TokenResponse> => {
|
||||||
const { instance } = getPreferenceValues<Preference>();
|
const { instance } = getPreferenceValues<Preference>();
|
||||||
|
@ -30,7 +30,7 @@ export const createApp = async (): Promise<Credentials> => {
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
client_name: "raycast-akkoma-extension",
|
client_name: "raycast-akkoma-extension",
|
||||||
redirect_uris: "https://raycast.com/redirect?packageName=Extension",
|
redirect_uris: "https://raycast.com/redirect?packageName=Extension",
|
||||||
scopes: "read write push",
|
scopes: "read write",
|
||||||
website: "https://raycast.com",
|
website: "https://raycast.com",
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
@ -48,26 +48,29 @@ export const postNewStatus = async ({
|
||||||
spoiler_text,
|
spoiler_text,
|
||||||
sensitive,
|
sensitive,
|
||||||
scheduled_at,
|
scheduled_at,
|
||||||
}: Partial<Status>) : Promise<StatusResponse> => {
|
content_type,
|
||||||
|
}: Partial<Status>): Promise<StatusResponse> => {
|
||||||
const { instance } = getPreferenceValues<Preference>();
|
const { instance } = getPreferenceValues<Preference>();
|
||||||
const token = await authorize();
|
const tokenSet = await client.getTokens();
|
||||||
|
|
||||||
const response = await fetch(`https://${instance}/api/v1/statuses`, {
|
const response = await fetch(`https://${instance}/api/v1/statuses`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": "Bearer" + token,
|
"Authorization": "Bearer " + tokenSet?.accessToken,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
status,
|
status,
|
||||||
visibility,
|
visibility,
|
||||||
spoiler_text,
|
spoiler_text,
|
||||||
sensitive,
|
sensitive,
|
||||||
|
content_type,
|
||||||
scheduled_at,
|
scheduled_at,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
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;
|
return (await response.json()) as StatusResponse;
|
||||||
|
|
|
@ -2,12 +2,12 @@ import { OAuth, getPreferenceValues } from "@raycast/api";
|
||||||
import { Preference } from "./types";
|
import { Preference } from "./types";
|
||||||
import { fetchToken,createApp} from "./api";
|
import { fetchToken,createApp} from "./api";
|
||||||
|
|
||||||
const client = new OAuth.PKCEClient({
|
export const client = new OAuth.PKCEClient({
|
||||||
redirectMethod: OAuth.RedirectMethod.Web,
|
redirectMethod: OAuth.RedirectMethod.Web,
|
||||||
providerName: "Akkoma",
|
providerName: "Akkoma",
|
||||||
providerIcon: "akkoma-icon.png",
|
providerIcon: "akkoma-icon.png",
|
||||||
providerId: "akkoma",
|
providerId: "akkoma",
|
||||||
description: "Connect to your Akkoma | Pleroma | Mastodon account",
|
description: "Connect to your Akkoma / Pleroma acount",
|
||||||
});
|
});
|
||||||
|
|
||||||
const requestAccessToken = async (
|
const requestAccessToken = async (
|
||||||
|
@ -59,14 +59,12 @@ export const authorize = async (): Promise<void> => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const { client_id, client_secret } = await createApp();
|
const { client_id, client_secret } = await createApp();
|
||||||
|
|
||||||
const authRequest = await client.authorizationRequest({
|
const authRequest = await client.authorizationRequest({
|
||||||
endpoint: `https://${instance}/oauth/authorize`,
|
endpoint: `https://${instance}/oauth/authorize`,
|
||||||
clientId: client_id,
|
clientId: client_id,
|
||||||
scope: "read write push",
|
scope: "read write",
|
||||||
});
|
});
|
||||||
|
|
||||||
const { authorizationCode } = await client.authorize(authRequest);
|
const { authorizationCode } = await client.authorize(authRequest);
|
||||||
|
|
||||||
await client.setTokens(await requestAccessToken(client_id, client_secret, authRequest, authorizationCode));
|
await client.setTokens(await requestAccessToken(client_id, client_secret, authRequest, authorizationCode));
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue