feat: use ofetch
This commit is contained in:
@@ -18,10 +18,10 @@ import {
|
||||
ContextMenuSeparator,
|
||||
} from "@renderer/components/ui/context-menu"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { getCsrfToken } from "@hono/auth-js/react"
|
||||
import { useToast } from "@renderer/components/ui/use-toast"
|
||||
import { ToastAction } from "@renderer/components/ui/toast"
|
||||
import { SubscriptionResponse } from "@renderer/lib/types"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
|
||||
export function FeedList({
|
||||
className,
|
||||
@@ -93,23 +93,13 @@ function FeedCategory({
|
||||
|
||||
const queryClient = useQueryClient()
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: async (feed: SubscriptionResponse[number]) => {
|
||||
return (
|
||||
await (
|
||||
await fetch(`${import.meta.env.VITE_API_URL}/subscriptions`, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({
|
||||
feedId: feed.feedId,
|
||||
csrfToken: await getCsrfToken(),
|
||||
}),
|
||||
})
|
||||
).json()
|
||||
).data
|
||||
},
|
||||
mutationFn: async (feed: SubscriptionResponse[number]) =>
|
||||
apiFetch("/subscriptions", {
|
||||
method: "DELETE",
|
||||
body: {
|
||||
feedId: feed.feedId,
|
||||
},
|
||||
}),
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["subscriptions", view],
|
||||
@@ -126,19 +116,14 @@ function FeedCategory({
|
||||
<ToastAction
|
||||
altText="Undo"
|
||||
onClick={async () => {
|
||||
await fetch(`${import.meta.env.VITE_API_URL}/subscriptions`, {
|
||||
await apiFetch("/subscriptions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({
|
||||
body: {
|
||||
url: variables.feeds.url,
|
||||
view,
|
||||
category: variables.category,
|
||||
// private: variables.private,
|
||||
csrfToken: await getCsrfToken(),
|
||||
}),
|
||||
},
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["subscriptions", view],
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
} from "@renderer/components/ui/form"
|
||||
import { Input } from "@renderer/components/ui/input"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { getCsrfToken } from "@hono/auth-js/react"
|
||||
import {
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
@@ -31,6 +30,7 @@ import { views } from "@renderer/lib/constants"
|
||||
import { cn } from "@renderer/lib/utils"
|
||||
import { Switch } from "@renderer/components/ui/switch"
|
||||
import { FollowSummary } from "../feed-summary"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
|
||||
const formSchema = z.object({
|
||||
view: z.enum(["0", "1", "2", "3", "4", "5"]),
|
||||
@@ -54,26 +54,16 @@ export function FollowDialog({
|
||||
|
||||
const queryClient = useQueryClient()
|
||||
const followMutation = useMutation({
|
||||
mutationFn: async (values: z.infer<typeof formSchema>) => {
|
||||
return (
|
||||
await (
|
||||
await fetch(`${import.meta.env.VITE_API_URL}/subscriptions`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({
|
||||
url: feed.url,
|
||||
view: parseInt(values.view),
|
||||
category: values.category,
|
||||
private: values.private,
|
||||
csrfToken: await getCsrfToken(),
|
||||
}),
|
||||
})
|
||||
).json()
|
||||
).data
|
||||
},
|
||||
mutationFn: async (values: z.infer<typeof formSchema>) =>
|
||||
apiFetch("/subscriptions", {
|
||||
method: "POST",
|
||||
body: {
|
||||
url: feed.url,
|
||||
view: parseInt(values.view),
|
||||
category: values.category,
|
||||
private: values.private,
|
||||
},
|
||||
}),
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["subscriptions", parseInt(variables.view)],
|
||||
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
import { Input } from "@renderer/components/ui/input"
|
||||
import { useEffect } from "react"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { getCsrfToken } from "@hono/auth-js/react"
|
||||
import { Image } from "@renderer/components/ui/image"
|
||||
import { FeedResponse, EntriesResponse } from "@renderer/lib/types"
|
||||
import {
|
||||
@@ -24,6 +23,7 @@ import {
|
||||
} from "@renderer/components/ui/card"
|
||||
import { FollowButton } from "./button"
|
||||
import { FollowSummary } from "../feed-summary"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
|
||||
const formSchema = z.object({
|
||||
keyword: z.string().min(1),
|
||||
@@ -61,28 +61,22 @@ export function FollowForm({ type }: { type: string }) {
|
||||
})
|
||||
const mutation = useMutation({
|
||||
mutationFn: async (keyword: string) => {
|
||||
return (
|
||||
await (
|
||||
await fetch(`${import.meta.env.VITE_API_URL}/discover`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({
|
||||
csrfToken: await getCsrfToken(),
|
||||
keyword,
|
||||
type: type === "rss" ? "rss" : "auto",
|
||||
}),
|
||||
})
|
||||
).json()
|
||||
).data as {
|
||||
feed: Partial<FeedResponse>
|
||||
docs?: string
|
||||
entries?: Partial<EntriesResponse>
|
||||
isSubscribed?: boolean
|
||||
subscriptionCount?: number
|
||||
}[]
|
||||
const { data } = await apiFetch<{
|
||||
data: {
|
||||
feed: Partial<FeedResponse>
|
||||
docs?: string
|
||||
entries?: Partial<EntriesResponse>
|
||||
isSubscribed?: boolean
|
||||
subscriptionCount?: number
|
||||
}[]
|
||||
}>("/discover", {
|
||||
method: "POST",
|
||||
body: {
|
||||
keyword,
|
||||
type: type === "rss" ? "rss" : "auto",
|
||||
},
|
||||
})
|
||||
return data
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { ofetch } from "ofetch"
|
||||
import { getCsrfToken } from "@hono/auth-js/react"
|
||||
|
||||
export const apiFetch = ofetch.create({
|
||||
baseURL: import.meta.env.VITE_API_URL,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
onRequest: async ({ options }) => {
|
||||
if (options.method && options.method.toLowerCase() !== "get") {
|
||||
const csrfToken = await getCsrfToken()
|
||||
if (!options.body) {
|
||||
options.body = {}
|
||||
}
|
||||
;(options.body as Record<string, any>).csrfToken = csrfToken
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useInfiniteQuery } from "@tanstack/react-query"
|
||||
import { levels } from "@renderer/lib/constants"
|
||||
import { EntriesResponse, ListResponse } from "../types"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
|
||||
export const useEntries = ({
|
||||
level,
|
||||
@@ -13,8 +14,6 @@ export const useEntries = ({
|
||||
queryKey: ["entries", level, id],
|
||||
enabled: level !== undefined && id !== undefined,
|
||||
queryFn: async ({ pageParam }) => {
|
||||
let entries: ListResponse<EntriesResponse> | null = null
|
||||
const baseUrl = `${import.meta.env.VITE_API_URL}/entries?`
|
||||
const params: {
|
||||
category?: string
|
||||
view?: string
|
||||
@@ -27,26 +26,12 @@ export const useEntries = ({
|
||||
} else if (level === levels.feed) {
|
||||
params.feedId = id + ""
|
||||
}
|
||||
entries = await (
|
||||
await fetch(
|
||||
baseUrl +
|
||||
new URLSearchParams({
|
||||
offset: pageParam + "",
|
||||
...params,
|
||||
}),
|
||||
{
|
||||
credentials: "include",
|
||||
},
|
||||
)
|
||||
).json()
|
||||
|
||||
if (!entries) {
|
||||
entries = {
|
||||
code: -1,
|
||||
}
|
||||
}
|
||||
|
||||
return entries
|
||||
return await apiFetch<ListResponse<EntriesResponse>>("/entries", {
|
||||
query: {
|
||||
offset: pageParam + "",
|
||||
...params,
|
||||
},
|
||||
})
|
||||
},
|
||||
getNextPageParam: (lastPage, _, lastPageParam) =>
|
||||
lastPageParam + (lastPage.data?.length || 0),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { SubscriptionResponse } from "../types"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
|
||||
export type Response = {
|
||||
list: {
|
||||
@@ -14,17 +15,13 @@ export const useSubscriptions = (view?: number) =>
|
||||
useQuery({
|
||||
queryKey: ["subscriptions", view],
|
||||
queryFn: async () => {
|
||||
const subscriptions = (
|
||||
await (
|
||||
await fetch(
|
||||
`${import.meta.env.VITE_API_URL}/subscriptions` +
|
||||
(view !== undefined ? `?view=${view}` : ``),
|
||||
{
|
||||
credentials: "include",
|
||||
},
|
||||
)
|
||||
).json()
|
||||
).data as SubscriptionResponse
|
||||
const { data: subscriptions } = await apiFetch<{
|
||||
data: SubscriptionResponse
|
||||
}>("/subscriptions", {
|
||||
query: {
|
||||
view,
|
||||
},
|
||||
})
|
||||
|
||||
const categories = {
|
||||
list: {},
|
||||
|
||||
@@ -1,29 +1,21 @@
|
||||
import { Button } from "@renderer/components/ui/button"
|
||||
import { UserButton } from "@renderer/components/user-button"
|
||||
import { getCsrfToken, authConfigManager } from "@hono/auth-js/react"
|
||||
import { useEffect } from "react"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
import { apiFetch } from "@renderer/lib/queries/api-fetch"
|
||||
|
||||
export function Component() {
|
||||
const navigate = useNavigate()
|
||||
|
||||
const getCallbackUrl = async () => {
|
||||
const response = await (
|
||||
await fetch(
|
||||
`${authConfigManager.getConfig().baseUrl}/auth-app/new-session`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({
|
||||
csrfToken: await getCsrfToken(),
|
||||
}),
|
||||
},
|
||||
)
|
||||
).json()
|
||||
return `follow://auth?token=${response.data.sessionToken}`
|
||||
const { data } = await apiFetch<{
|
||||
data: {
|
||||
sessionToken: string
|
||||
}
|
||||
}>("/auth-app/new-session", {
|
||||
method: "POST",
|
||||
})
|
||||
return `follow://auth?token=${data.sessionToken}`
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user