Skip to content

Commit 5721fe2

Browse files
committed
Add optional API key support in settings
1 parent 10998e0 commit 5721fe2

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,13 @@
764764
"type": "textfield",
765765
"default": "http://127.0.0.1:31009",
766766
"required": false
767+
},
768+
{
769+
"name": "apiKey",
770+
"title": "API Key",
771+
"description": "Optional API key for the Anytype API. Leave empty to use the built-in authentication flow with your Anytype desktop app, or enter an API key (e.g., from a headless Anytype instance) to use manual authentication.",
772+
"type": "password",
773+
"required": false
767774
}
768775
],
769776
"dependencies": {

src/components/EnsureAuthenticated.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export function EnsureAuthenticated({ placeholder, viewType, children }: EnsureA
6464

6565
useEffect(() => {
6666
const retrieveAndValidateToken = async () => {
67-
const token = await LocalStorage.getItem<string>(localStorageKeys.appKey);
67+
const token = getPreferenceValues().apiKey || (await LocalStorage.getItem(localStorageKeys.appKey));
6868
if (token) {
6969
const isValid = await checkApiTokenValidity();
7070
setHasToken(true);
@@ -125,6 +125,20 @@ export function EnsureAuthenticated({ placeholder, viewType, children }: EnsureA
125125
return <>{children}</>;
126126
}
127127

128+
// If API key is set in settings, no need for pairing flow
129+
const settingsApiKey = getPreferenceValues().apiKey;
130+
if (settingsApiKey) {
131+
return (
132+
<List searchBarPlaceholder="Invalid API Key">
133+
<List.EmptyView
134+
icon={Icon.XMarkCircle}
135+
title="Invalid API Key"
136+
description="The API key provided in settings is invalid. Please check your settings."
137+
/>
138+
</List>
139+
);
140+
}
141+
128142
return challengeId ? (
129143
<Form
130144
isLoading={isLoading}

src/utils/api.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LocalStorage } from "@raycast/api";
1+
import { getPreferenceValues, LocalStorage } from "@raycast/api";
22
import fetch, { Headers as FetchHeaders } from "node-fetch";
33
import { currentApiVersion, errorConnectionMessage, localStorageKeys } from "./constant";
44
import { checkResponseError } from "./error";
@@ -22,11 +22,12 @@ export interface ApiResponse<T> {
2222
*/
2323
export async function apiFetch<T>(url: string, options: FetchOptions): Promise<ApiResponse<T>> {
2424
try {
25-
const token = await LocalStorage.getItem(localStorageKeys.appKey);
25+
const token = getPreferenceValues().apiKey || (await LocalStorage.getItem(localStorageKeys.appKey));
26+
2627
const response = await fetch(url, {
2728
method: options.method,
2829
headers: {
29-
Authorization: token ? `Bearer ${token}` : "",
30+
Authorization: `Bearer ${token}`,
3031
"Content-Type": "application/json",
3132
"Anytype-Version": currentApiVersion,
3233
...options.headers,

0 commit comments

Comments
 (0)