daily-advice-appmain.tsx4 matches
7const [loading, setLoading] = useState(false);
89async function fetchAdvice() {
10setLoading(true);
11try {
12const res = await fetch("https://api.adviceslip.com/advice");
13const data = await res.json();
14setAdvice(data.slip.advice);
2122useEffect(() => {
23fetchAdvice();
24}, []);
2532</p>
33<button
34onClick={fetchAdvice}
35className="mt-6 bg-blue-500 hover:bg-blue-600 text-white font-semibold py-2 px-4 rounded w-full"
36disabled={loading}
mini-remixrender.tsx1 match
56e.preventDefault()
57const form = e.currentTarget as HTMLFormElement
58const res = await fetch(form.action || window.location.href, {
59method: form.method || "POST",
60body: new FormData(form),
moiPosterImprovedMoiEditor.tsx7 matches
8disabled?: boolean;
9username?: string; // Username prop to set default author
10valId?: string; // Val ID for fetching endpoints
11apiToken?: string; // Token for API requests
12}
62const initializedRef = useRef(false);
63
64// Fetch HTTP endpoints if available
65useEffect(() => {
66if (!valId || !apiToken) return;
67
68const fetchEndpoints = async () => {
69setLoadingEndpoints(true);
70setEndpointsError(null);
71
72try {
73const response = await fetch(`/api/vals/${valId}/endpoints`, {
74headers: { 'Authorization': `Bearer ${apiToken}` }
75});
76
77if (!response.ok) {
78throw new Error(`Failed to fetch endpoints: ${response.statusText}`);
79}
80
82setEndpoints(data.endpoints || []);
83} catch (error) {
84console.error('Error fetching endpoints:', error);
85setEndpointsError(error instanceof Error ? error.message : 'Unknown error');
86} finally {
89};
90
91fetchEndpoints();
92}, [valId, apiToken]);
93
moiPosterImprovedApp.tsx29 matches
34const [tokenError, setTokenError] = useState<string | null>(null);
35const [username, setUsername] = useState<string>("");
36// Flag to track if we've already tried to fetch data
37const [initialFetchDone, setInitialFetchDone] = useState(false);
38// Editor key for forcing re-render
39const [editorKey, setEditorKey] = useState<string>("editor-0");
57}, [apiToken]);
5859// Fetch user information to get username
60const fetchUserInfo = useCallback(async () => {
61if (!apiToken) return;
6263try {
64const response = await fetch(`/api/user`, {
65headers: { "Authorization": `Bearer ${apiToken}` },
66});
74}
75} catch (error) {
76console.warn("Could not fetch user info:", error);
77// Non-critical error, don't show to user
78}
79}, [apiToken]);
8081const fetchVals = useCallback(async () => {
82if (!apiToken) {
83setError("Val Town API Key is required to list your vals.");
84setLoading(false);
85setVals([]);
86setInitialFetchDone(true);
87return;
88}
97try {
98const queryParams = filterPrivacy !== "all" ? `?privacy=${filterPrivacy}` : "";
99const response = await fetch(`/api/vals${queryParams}`, {
100headers: { "Authorization": `Bearer ${apiToken}` },
101});
108setVals([]);
109} else {
110setError(`Failed to fetch vals: ${errorData.error || response.statusText}`);
111}
112throw new Error(`Failed to fetch vals (Status: ${response.status})`);
113}
114const data = await response.json();
121}
122} catch (err) {
123console.error(`Error fetching vals:`, err);
124if (!error && !tokenError) {
125setError(`Failed to load vals. Please try again later.`);
127} finally {
128setLoading(false);
129setInitialFetchDone(true);
130}
131}, [apiToken, filterPrivacy, initialFetchDone, error, tokenError]);
132133// Run initial fetch only once when API token is available
134useEffect(() => {
135if (apiToken && !initialFetchDone && !loading) {
136fetchUserInfo();
137fetchVals();
138}
139}, [apiToken, initialFetchDone, loading, fetchUserInfo, fetchVals]);
140141const handleValSelect = async (val: Val) => {
165try {
166const valId = val.id;
167const response = await fetch(`/api/vals/${valId}/moi`);
168if (!response.ok) {
169const errorData = await response.json().catch(() => ({ error: `HTTP error ${response.status}` }));
170throw new Error(errorData.error || "Failed to fetch moi.md");
171}
172const data: MoiFile = await response.json();
179pendingSaveContentRef.current = initialContent;
180} catch (err) {
181console.error("Error fetching moi.md:", err);
182setItemError(`Failed to fetch moi.md content: ${err instanceof Error ? err.message : String(err)}`);
183if (selectedVal) {
184const defaultContent = generateDefaultMoiContent(selectedVal);
274console.log("Updating existing moi.md file");
275// Update existing moi.md file using the current API endpoint
276const response = await fetch(`/api/vals/${valId}/moi`, {
277method: "POST",
278headers: { "Content-Type": "application/json", "Authorization": `Bearer ${apiToken}` },
296console.log("Creating new moi.md file");
297// Create new moi.md file for vals that don't have one yet
298const response = await fetch(`/api/vals/${valId}/file`, {
299method: "POST",
300headers: { "Content-Type": "application/json", "Authorization": `Bearer ${apiToken}` },
327
328// Refresh the vals list to show updated status
329fetchVals();
330
331setTimeout(() => setSaveSuccess(false), 3000);
351setApiToken(newToken);
352353// Reset the initialFetchDone flag if the token changes
354if (newToken !== apiToken) {
355setInitialFetchDone(false);
356}
357};
443{/* Refresh Button: Aqua background, Black text */}
444<button
445onClick={fetchVals}
446disabled={loading || !apiToken}
447className={`px-3 py-1.5 rounded-md text-sm transition-colors text-[#000000] ${
moiPosterImprovedQuickEditor.tsx5 matches
142}, [username, metadata.author, loaded]);
143
144// Fetch the actual username from the API if we have a valId
145useEffect(() => {
146if (!valId || !apiToken || metadata.author) return;
147
148const fetchUsername = async () => {
149try {
150const response = await fetch(`/api/username/${username}`, {
151headers: { 'Authorization': `Bearer ${apiToken}` }
152});
162}
163} catch (error) {
164console.warn('Error fetching username:', error);
165}
166};
167
168if (username) {
169fetchUsername();
170}
171}, [valId, apiToken, username, metadata.author]);
213try {
214// ---- START: Replace this block in Val Town ----
215const response = await fetch("https://api.openai.com/v1/chat/completions", {
216method: "POST",
217headers: {
claudeCodeLoaderindex.tsx2 matches
25});
2627// HTTP vals expect an exported "fetch handler"
28// This is how you "run the server" in Val Town with Hono
29export default app.fetch;
FixItWandgenerate.ts1 match
34// Only transcribe the audio if it's provided
35if (audioB64) {
36const audioResponse = await fetch(audioB64);
37const audioBlob = await audioResponse.blob();
38
MiniAppStarterneynar.ts14 matches
1const baseUrl = "https://api.neynar.com/v2/farcaster/";
23export async function fetchNeynarGet(path: string) {
4const res = await fetch(baseUrl + path, {
5method: "GET",
6headers: {
14}
1516export function fetchUser(username: string) {
17return fetchNeynarGet(`user/by_username?username=${username}`).then(r => r.user);
18}
19export function fetchUsersById(fids: string) {
20return fetchNeynarGet(`user/bulk?fids=${fids}`).then(r => r.users);
21}
2223export function fetchUserFeed(fid: number) {
24return fetchNeynarGet(
25`feed?feed_type=filter&filter_type=fids&fids=${fid}&with_recasts=false&with_replies=false&limit=100&cursor=`,
26).then(r => r.casts);
27}
2829export function fetchChannel(channelId: string) {
30return fetchNeynarGet(`channel?id=${channelId}`).then(r => r.channel);
31}
3233export function fetchChannelFeed(channelId: string) {
34return fetchNeynarGet(
35`feed/channels?channel_ids=${channelId}&with_recasts=false&limit=100`,
36).then(r => r.casts);
37}
3839export function fetchChannelsFeed(channelIds: array) {
40return fetchNeynarGet(
41`feed/channels?channel_ids=${channelIds.join(",")}&with_recasts=false&limit=100`,
42).then(r => r.casts);
MiniAppStarterindex.tsx2 matches
63});
6465// HTTP vals expect an exported "fetch handler"
66// This is how you "run the server" in Val Town with Hono
67export default app.fetch;