Add option for debug API

This commit is contained in:
hzrd149 2024-11-16 14:55:42 -06:00
parent b7bf4a3d1c
commit dd37773f25
5 changed files with 83 additions and 0 deletions

@ -0,0 +1,5 @@
---
"nostrudel": minor
---
Add option for debug API

@ -8,6 +8,7 @@ console.log("Funding", funding);
import "./services/user-event-sync";
import "./services/username-search";
import "./services/page-api";
// setup bitcoin connect
import { init, onConnected } from "@getalby/bitcoin-connect-react";

@ -54,6 +54,7 @@ const enableKeyboardShortcuts = new BooleanLocalStorageEntry("enable-keyboard-sh
// privacy
const defaultAuthenticationMode = new LocalStorageEntry("default-relay-auth-mode", "ask"); // ask, always, never
const proactivelyAuthenticate = new BooleanLocalStorageEntry("proactively-authenticate", false);
const debugApi = new BooleanLocalStorageEntry("debug-api", false);
// display settings
const showBrandLogo = new BooleanLocalStorageEntry("show-brand-logo", true);
@ -72,6 +73,7 @@ const localSettings = {
showBrandLogo,
defaultAuthenticationMode,
proactivelyAuthenticate,
debugApi,
};
if (import.meta.env.DEV) {

46
src/services/page-api.ts Normal file

@ -0,0 +1,46 @@
import accountService from "./account";
import channelMetadataService from "./channel-metadata";
import { eventStore, queryStore } from "./event-store";
import localSettings from "./local-settings";
import readStatusService from "./read-status";
import relayInfoService from "./relay-info";
import relayPoolService from "./relay-pool";
import replaceableEventsService from "./replaceable-events";
import signingService from "./signing";
import timelineCacheService from "./timeline-cache";
import { userSearchDirectory } from "./username-search";
const noStrudel = {
/**
* Internal applesauce EventStore
* @see https://hzrd149.github.io/applesauce/classes/applesauce_core.EventStore.html
*/
eventStore,
/**
* Internal applesauce QueryStore
* @see https://hzrd149.github.io/applesauce/classes/applesauce_core.QueryStore.html
*/
queryStore,
/** Account management */
accountService,
/** Signing queue */
signingService,
// other internal services
replaceableEventsService,
userSearchDirectory,
readStatusService,
relayInfoService,
relayPoolService,
channelMetadataService,
timelineCacheService,
localSettings,
};
localSettings.debugApi.subscribe((enabled) => {
if (enabled) Reflect.set(window, "noStrudel", noStrudel);
// @ts-expect-error
else delete window.noStrudel;
});

@ -11,6 +11,7 @@ import {
Button,
Heading,
FormLabel,
Text,
} from "@chakra-ui/react";
import { useObservable } from "applesauce-react/hooks";
@ -48,6 +49,7 @@ export default function PrivacySettings() {
const defaultAuthenticationMode = useObservable(localSettings.defaultAuthenticationMode);
const proactivelyAuthenticate = useObservable(localSettings.proactivelyAuthenticate);
const debugApi = useObservable(localSettings.debugApi);
return (
<VerticalPageLayout as="form" onSubmit={submit} flex={1}>
@ -205,6 +207,33 @@ export default function PrivacySettings() {
</span>
</FormHelperText>
</FormControl>
<FormControl>
<Flex alignItems="center">
<FormLabel htmlFor="debugApi" mb="0">
Enable debug api
</FormLabel>
<Switch
id="debugApi"
isChecked={debugApi}
onChange={(e) => localSettings.debugApi.next(e.currentTarget.checked)}
/>
</Flex>
<FormHelperText>
<Text>
Adds a window.noStrudel to the page with access to internal methods{" "}
<Link
href="https://github.com/hzrd149/nostrudel/blob/master/src/services/page-api.ts"
target="_blank"
color="blue.500"
>
see source
</Link>
</Text>
<Text color="orange.500" mt="1">
WARNING: this can expose your secret keys and signer.
</Text>
</FormHelperText>
</FormControl>
<Button
ml="auto"
isLoading={formState.isLoading || formState.isValidating || formState.isSubmitting}