refactor: to api client (#45)

* refactor: api client

* chore: lint

* chore: lint

Signed-off-by: Innei <i@innei.in>

* fix: update

Signed-off-by: Innei <i@innei.in>

* fix: combine api fetch

Signed-off-by: Innei <i@innei.in>

---------

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-06-06 19:32:48 +08:00
committed by GitHub
parent 2d7e1219ff
commit 085a266a0f
25 changed files with 264 additions and 279 deletions
+5
View File
@@ -112,6 +112,11 @@
"typescript": "^5.4.5",
"vite": "^5.2.12"
},
"pnpm": {
"patchedDependencies": {
"hono@4.4.2": "patches/hono@4.4.2.patch"
}
},
"simple-git-hooks": {
"pre-commit": "pnpm exec lint-staged"
},
+13
View File
@@ -0,0 +1,13 @@
diff --git a/dist/types/client/types.d.ts b/dist/types/client/types.d.ts
index 14af56d34248f101aecdebdd2e56278eac4b0c0d..00f2921985e0fabc90df8397a0c18abd8041c1e7 100644
--- a/dist/types/client/types.d.ts
+++ b/dist/types/client/types.d.ts
@@ -41,7 +41,7 @@ type ClientResponseOfEndpoint<T extends Endpoint = Endpoint> = T extends {
output: infer O;
outputFormat: infer F;
status: infer S;
-} ? ClientResponse<O, S extends number ? S : never, F extends ResponseFormat ? F : never> : never;
+} ? O : never;
export interface ClientResponse<T, U extends number = StatusCode, F extends ResponseFormat = ResponseFormat> extends globalThis.Response {
readonly body: ReadableStream | null;
readonly bodyUsed: boolean;
+9 -4
View File
@@ -4,6 +4,11 @@ settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
patchedDependencies:
hono@4.4.2:
hash: ofhxnfpal3nvwlen5lybasjjdm
path: patches/hono@4.4.2.patch
importers:
.:
@@ -22,7 +27,7 @@ importers:
version: 5.0.1
'@hono/auth-js':
specifier: 1.0.8
version: 1.0.8(@auth/core@0.31.0)(hono@4.4.2)(react@18.3.1)
version: 1.0.8(@auth/core@0.31.0)(hono@4.4.2(patch_hash=ofhxnfpal3nvwlen5lybasjjdm))(react@18.3.1)
'@hookform/resolvers':
specifier: 3.4.2
version: 3.4.2(react-hook-form@7.51.5(react@18.3.1))
@@ -6298,10 +6303,10 @@ snapshots:
'@gar/promisify@1.1.3': {}
'@hono/auth-js@1.0.8(@auth/core@0.31.0)(hono@4.4.2)(react@18.3.1)':
'@hono/auth-js@1.0.8(@auth/core@0.31.0)(hono@4.4.2(patch_hash=ofhxnfpal3nvwlen5lybasjjdm))(react@18.3.1)':
dependencies:
'@auth/core': 0.31.0
hono: 4.4.2
hono: 4.4.2(patch_hash=ofhxnfpal3nvwlen5lybasjjdm)
react: 18.3.1
'@hookform/resolvers@3.4.2(react-hook-form@7.51.5(react@18.3.1))':
@@ -9150,7 +9155,7 @@ snapshots:
dependencies:
hermes-estree: 0.20.1
hono@4.4.2: {}
hono@4.4.2(patch_hash=ofhxnfpal3nvwlen5lybasjjdm): {}
hosted-git-info@2.8.9: {}
@@ -16,8 +16,7 @@ import {
} from "@renderer/components/ui/form"
import { Image } from "@renderer/components/ui/image"
import { Input } from "@renderer/components/ui/input"
import type { DiscoverResponse } from "@renderer/models"
import { apiFetch } from "@renderer/queries/api-fetch"
import { apiClient } from "@renderer/lib/api-fetch"
import { DEEPLINK_SCHEME } from "@shared/constants"
import { openElectronWindow } from "@shared/electron"
import { useMutation } from "@tanstack/react-query"
@@ -64,15 +63,13 @@ export function DiscoverForm({ type }: { type: string }) {
})
const mutation = useMutation({
mutationFn: async (keyword: string) => {
const { data } = await apiFetch<{
data: DiscoverResponse
}>("/discover", {
method: "POST",
body: {
const { data } = await apiClient.discover.$post({
json: {
keyword,
type: type === "rss" ? "rss" : "auto",
},
})
return data
},
})
@@ -10,10 +10,10 @@ import {
FormMessage,
} from "@renderer/components/ui/form"
import { Input } from "@renderer/components/ui/input"
import { apiFetch } from "@renderer/lib/api-fetch"
import { cn } from "@renderer/lib/utils"
import type { FeedResponse } from "@renderer/models"
import { Queries } from "@renderer/queries"
import { apiFetch } from "@renderer/queries/api-fetch"
import { useMutation } from "@tanstack/react-query"
import { useForm } from "react-hook-form"
import { z } from "zod"
@@ -59,6 +59,8 @@ export function DiscoverImport() {
mutationFn: async (file: File) => {
const formData = new FormData()
formData.append("file", file)
// FIXME: if post data is form data, hono hc not support this.
const { data } = await apiFetch<{
data: {
successfulItems: FeedResponseList
@@ -69,6 +71,7 @@ export function DiscoverImport() {
method: "POST",
body: formData,
})
return data
},
onSuccess: () => {
@@ -5,12 +5,12 @@ import {
PopoverContent,
PopoverTrigger,
} from "@renderer/components/ui/popover"
import { apiClient } from "@renderer/lib/api-fetch"
import { views } from "@renderer/lib/constants"
import { FeedViewType } from "@renderer/lib/enum"
import { buildStorageNS } from "@renderer/lib/ns"
import { getEntriesParams } from "@renderer/lib/utils"
import type { EntryModel } from "@renderer/models"
import { apiClient } from "@renderer/queries/api-fetch"
import { useEntries } from "@renderer/queries/entries"
import {
feedActions,
@@ -1,8 +1,8 @@
import { useEntryActions } from "@renderer/hooks/useEntryActions"
import { apiClient } from "@renderer/lib/api-fetch"
import { views } from "@renderer/lib/constants"
import { showNativeMenu } from "@renderer/lib/native-menu"
import { cn } from "@renderer/lib/utils"
import { apiClient } from "@renderer/queries/api-fetch"
import { feedActions, useFeedStore } from "@renderer/store"
import { entryActions, useEntry } from "@renderer/store/entry"
import { useMutation } from "@tanstack/react-query"
@@ -7,8 +7,8 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from "@renderer/components/ui/alert-dialog"
import { apiClient } from "@renderer/lib/api-fetch"
import { Queries } from "@renderer/queries"
import { apiFetch } from "@renderer/queries/api-fetch"
import { useMutation } from "@tanstack/react-query"
export function CategoryRemoveDialog({
@@ -24,9 +24,9 @@ export function CategoryRemoveDialog({
}) {
const renameMutation = useMutation({
mutationFn: async () =>
apiFetch("/categories", {
method: "DELETE",
body: {
apiClient.categories.$delete({
json: {
feedIdList,
deleteSubscriptions: false,
},
@@ -13,8 +13,8 @@ import {
FormMessage,
} from "@renderer/components/ui/form"
import { Input } from "@renderer/components/ui/input"
import { apiClient } from "@renderer/lib/api-fetch"
import { Queries } from "@renderer/queries"
import { apiFetch } from "@renderer/queries/api-fetch"
import { useMutation } from "@tanstack/react-query"
import { useForm } from "react-hook-form"
import { z } from "zod"
@@ -43,9 +43,9 @@ export function CategoryRenameDialog({
const renameMutation = useMutation({
mutationFn: async (values: z.infer<typeof formSchema>) =>
apiFetch("/categories", {
method: "PATCH",
body: {
apiClient.categories.$patch({
json: {
feedIdList,
category: values.category,
},
@@ -58,7 +58,6 @@ export function CategoryRenameDialog({
})
function onSubmit(values: z.infer<typeof formSchema>) {
console.log("onSubmit", values)
renameMutation.mutate(values)
}
@@ -3,13 +3,13 @@ import {
CollapsibleTrigger,
} from "@renderer/components/ui/collapsible"
import { Dialog } from "@renderer/components/ui/dialog"
import { apiClient } from "@renderer/lib/api-fetch"
import { client } from "@renderer/lib/client"
import { levels } from "@renderer/lib/constants"
import { showNativeMenu } from "@renderer/lib/native-menu"
import { cn } from "@renderer/lib/utils"
import type { FeedListModel } from "@renderer/models"
import { Queries } from "@renderer/queries"
import { apiFetch } from "@renderer/queries/api-fetch"
import {
feedActions,
useFeedActiveList,
@@ -41,13 +41,13 @@ export function FeedCategory({
const feedIdList = data.list.map((feed) => feed.feedId)
const deleteMutation = useMutation({
mutationFn: async () =>
apiFetch("/categories", {
method: "DELETE",
body: {
apiClient.categories.$delete({
json: {
feedIdList,
deleteSubscriptions: false,
},
}),
onSuccess: () => {
Queries.subscription.byView(view).invalidate()
},
@@ -8,13 +8,13 @@ import {
TooltipTrigger,
} from "@renderer/components/ui/tooltip"
import { useToast } from "@renderer/components/ui/use-toast"
import { apiClient } from "@renderer/lib/api-fetch"
import { levels } from "@renderer/lib/constants"
import dayjs from "@renderer/lib/dayjs"
import { showNativeMenu } from "@renderer/lib/native-menu"
import { cn } from "@renderer/lib/utils"
import type { SubscriptionResponse } from "@renderer/models"
import { Queries } from "@renderer/queries"
import { apiFetch } from "@renderer/queries/api-fetch"
import {
feedActions,
useFeedActiveList,
@@ -49,12 +49,12 @@ export function FeedItem({
const deleteMutation = useMutation({
mutationFn: async (feed: SubscriptionResponse[number]) =>
apiFetch("/subscriptions", {
method: "DELETE",
body: {
apiClient.subscriptions.$delete({
json: {
feedId: feed.feedId,
},
}),
onSuccess: (_, variables) => {
Queries.subscription.byView(variables.view).invalidate()
@@ -73,11 +73,10 @@ export function FeedItem({
<ToastAction
altText="Undo"
onClick={async () => {
await apiFetch("/subscriptions", {
method: "POST",
body: {
await apiClient.subscriptions.$post({
json: {
url: variables.feeds.url,
view: feed.view,
view: variables.view,
category: variables.category,
isPrivate: variables.isPrivate,
},
+15 -16
View File
@@ -1,7 +1,7 @@
import { useToast } from "@renderer/components/ui/use-toast"
import { apiClient } from "@renderer/lib/api-fetch"
import { client } from "@renderer/lib/client"
import type { EntriesResponse } from "@renderer/models"
import { apiClient, apiFetch } from "@renderer/queries/api-fetch"
import type { EntryModel } from "@renderer/models"
import { entryActions } from "@renderer/store/entry"
import { useMutation, useQuery } from "@tanstack/react-query"
import type { FetchError } from "ofetch"
@@ -12,7 +12,7 @@ export const useEntryActions = ({
entry,
}: {
view?: number
entry?: EntriesResponse[number]
entry?: EntryModel
}) => {
const checkEagle = useQuery({
queryKey: ["check-eagle"],
@@ -29,12 +29,12 @@ export const useEntryActions = ({
const collect = useMutation({
mutationFn: async () =>
apiFetch("/collections", {
method: "POST",
body: {
entry && apiClient.collections.$post({
json: {
entryId: entry?.entries.id,
},
}),
onMutate() {
if (!entry) return
entryActions.optimisticUpdate(entry.entries.id, {
@@ -52,12 +52,12 @@ export const useEntryActions = ({
})
const uncollect = useMutation({
mutationFn: async () =>
apiFetch("/collections", {
method: "DELETE",
body: {
entry && apiClient.collections.$delete({
json: {
entryId: entry?.entries.id,
},
}),
onMutate() {
if (!entry) return
entryActions.optimisticUpdate(entry.entries.id, {
@@ -87,13 +87,12 @@ export const useEntryActions = ({
},
})
const unread = useMutation({
mutationFn: async () =>
apiFetch("/reads", {
method: "DELETE",
body: {
entryId: entry?.entries.id,
},
}),
mutationFn: async () => entry && apiClient.reads.$delete({
json: {
entryId: entry.entries.id,
},
}),
onMutate: () => {
if (!entry) return
+30
View File
@@ -0,0 +1,30 @@
import { getCsrfToken } from "@hono/auth-js/react"
import type { AppType } from "@renderer/hono"
import { hc } from "hono/client"
import { ofetch } from "ofetch"
export const apiFetch = ofetch.create({
baseURL: import.meta.env.VITE_API_URL,
credentials: "include",
onRequest: async ({ options }) => {
if (options.method && options.method.toLowerCase() !== "get") {
const csrfToken = await getCsrfToken()
if (typeof options.body === "string") {
options.body = JSON.parse(options.body)
}
if (!options.body) {
options.body = {}
}
if (options.body instanceof FormData) {
options.body.append("csrfToken", csrfToken)
} else {
(options.body as Record<string, unknown>).csrfToken = csrfToken
}
}
},
})
export const apiClient = hc<AppType>("", {
fetch: async (input, options = {}) => apiFetch(input.toString(), options),
})
-1
View File
@@ -1,4 +1,3 @@
// eslint-disable-next-line no-restricted-syntax
export enum FeedViewType {
Articles = 0,
SocialMedia = 1,
+16 -20
View File
@@ -1,5 +1,9 @@
import type { apiClient } from "@renderer/queries/api-fetch"
import type { InferResponseType } from "hono/client"
import type { apiClient } from "@renderer/lib/api-fetch"
export type ExtractBizResponse<T extends (...args: any[]) => any> = Exclude<
Awaited<ReturnType<T>>,
undefined
>
export type ActiveList = {
level: string
@@ -11,24 +15,19 @@ export type ActiveList = {
export type FeedResponse = SubscriptionResponse[number]["feeds"]
export type FeedModel = Extract<
InferResponseType<typeof apiClient.feeds.$get>,
{ code: 0 }
export type FeedModel = ExtractBizResponse<
typeof apiClient.feeds.$get
>["data"]["feed"]
export type SubscriptionResponse = Array<
Exclude<
Extract<
InferResponseType<typeof apiClient.subscriptions.$get>,
{ code: 0 }
>["data"],
undefined
>[number] & { unread?: number }
ExtractBizResponse<typeof apiClient.subscriptions.$get>["data"][number] & {
unread?: number
}
>
export type EntryResponse = Exclude<
Extract<
InferResponseType<typeof apiClient.entries.$get>,
ExtractBizResponse<typeof apiClient.entries.$get>,
{ code: 0 }
>["data"],
undefined
@@ -36,18 +35,15 @@ export type EntryResponse = Exclude<
export type EntriesResponse = Array<
Exclude<
Extract<
InferResponseType<typeof apiClient.entries.$post>,
{ code: 0 }
>["data"],
Awaited<ReturnType<typeof apiClient.entries.$post>>["data"],
undefined
>[number]
>
>
>[number]
export type EntryModel = EntriesResponse[number]
export type DiscoverResponse = Array<
Exclude<
InferResponseType<typeof apiClient.discover.$post>["data"],
ExtractBizResponse<typeof apiClient.discover.$post>["data"],
undefined
>[number]
>
+4 -10
View File
@@ -1,7 +1,7 @@
import { Button } from "@renderer/components/ui/button"
import { UserButton } from "@renderer/components/user-button"
import { apiClient } from "@renderer/lib/api-fetch"
import { APP_NAME } from "@renderer/lib/constants"
import { apiFetch } from "@renderer/queries/api-fetch"
import { DEEPLINK_SCHEME } from "@shared/constants"
import { useEffect } from "react"
import { useNavigate } from "react-router-dom"
@@ -10,13 +10,7 @@ export function Component() {
const navigate = useNavigate()
const getCallbackUrl = async () => {
const { data } = await apiFetch<{
data: {
sessionToken: string
}
}>("/auth-app/new-session", {
method: "POST",
})
const { data } = await apiClient["auth-app"]["new-session"].$post({})
return `${DEEPLINK_SCHEME}auth?token=${data.sessionToken}`
}
@@ -44,8 +38,8 @@ export function Component() {
{" "}
{APP_NAME}
{" "}
Account. Now is the time to
open
Account. Now is the time
to open
{" "}
{APP_NAME}
{" "}
+120 -119
View File
@@ -22,11 +22,11 @@ import {
import { Switch } from "@renderer/components/ui/switch"
import { useToast } from "@renderer/components/ui/use-toast"
import { useBizQuery } from "@renderer/hooks/useBizQuery"
import { apiClient } from "@renderer/lib/api-fetch"
import { client } from "@renderer/lib/client"
import { views } from "@renderer/lib/constants"
import { cn } from "@renderer/lib/utils"
import { Queries } from "@renderer/queries"
import { apiFetch } from "@renderer/queries/api-fetch"
import { useFeed } from "@renderer/queries/feed"
import { useMutation } from "@tanstack/react-query"
import { useEffect } from "react"
@@ -68,17 +68,20 @@ export function Component() {
const { toast } = useToast()
const followMutation = useMutation({
mutationFn: async (values: z.infer<typeof formSchema>) =>
apiFetch("/subscriptions", {
method: isSubscribed ? "PATCH" : "POST",
body: {
url: feed.data?.feed.url,
view: Number.parseInt(values.view),
category: values.category,
isPrivate: values.isPrivate,
...(isSubscribed && { feedId: feed.data?.feed.id }),
},
}),
mutationFn: async (values: z.infer<typeof formSchema>) => {
const body = {
url: feed.data?.feed.url,
view: Number.parseInt(values.view),
category: values.category,
isPrivate: values.isPrivate,
...(isSubscribed && { feedId: feed.data?.feed.id }),
}
const $method = isSubscribed ? apiClient.subscriptions.$patch : apiClient.subscriptions.$post
$method({
// @ts-expect-error
json: body,
})
},
onSuccess: (_, variables) => {
if (
isSubscribed &&
@@ -122,116 +125,114 @@ export function Component() {
<img src="../icon.svg" alt="logo" className="size-8" />
Add follow
</div>
{feed.isLoading ?
{feed.isLoading ? (
<div className="flex flex-1 items-center justify-center">
Loading...
</div>
) : !feed.data?.feed ?
(
<div className="flex flex-1 items-center justify-center">
Loading...
<div className="flex flex-1 flex-col items-center justify-center gap-2">
<p>Feed not found.</p>
<p>{url}</p>
</div>
) :
!feed.data?.feed ?
(
<div className="flex flex-1 flex-col items-center justify-center gap-2">
<p>Feed not found.</p>
<p>{url}</p>
</div>
) :
(
<div className="space-y-4">
<Card>
<CardHeader>
<FollowSummary feed={feed.data?.feed} />
</CardHeader>
</Card>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="view"
render={({ field }) => (
<FormItem>
<FormLabel>View</FormLabel>
<Select onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
{views.map((view, index) => (
<SelectItem key={view.name} value={`${index}`}>
<div className="flex items-center gap-2">
<span className={cn(view.className, "flex")}>
{view.icon}
</span>
<span>{view.name}</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="category"
render={({ field }) => (
<FormItem>
<div>
<FormLabel>Category</FormLabel>
<FormDescription>
By default, your follows will be grouped by website.
</FormDescription>
</div>
<FormControl>
<AutoComplete
options={categories.data || []}
emptyMessage="No results."
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="isPrivate"
render={({ field }) => (
<FormItem>
<div className="flex items-center justify-between">
<div>
<FormLabel>Private Follow</FormLabel>
<FormDescription>
Whether this follow is publicly visible on your
profile page.
</FormDescription>
</div>
<FormControl>
<Switch
className="shrink-0"
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</div>
</FormItem>
)}
/>
<div className="flex justify-end">
<Button
size="sm"
type="submit"
isLoading={followMutation.isPending}
>
{isSubscribed ? "Update" : "Follow"}
</Button>
</div>
</form>
</Form>
</div>
)}
(
<div className="space-y-4">
<Card>
<CardHeader>
<FollowSummary feed={feed.data?.feed} />
</CardHeader>
</Card>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="view"
render={({ field }) => (
<FormItem>
<FormLabel>View</FormLabel>
<Select onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
</FormControl>
<SelectContent>
{views.map((view, index) => (
<SelectItem key={view.name} value={`${index}`}>
<div className="flex items-center gap-2">
<span className={cn(view.className, "flex")}>
{view.icon}
</span>
<span>{view.name}</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="category"
render={({ field }) => (
<FormItem>
<div>
<FormLabel>Category</FormLabel>
<FormDescription>
By default, your follows will be grouped by website.
</FormDescription>
</div>
<FormControl>
<AutoComplete
options={categories.data || []}
emptyMessage="No results."
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="isPrivate"
render={({ field }) => (
<FormItem>
<div className="flex items-center justify-between">
<div>
<FormLabel>Private Follow</FormLabel>
<FormDescription>
Whether this follow is publicly visible on your
profile page.
</FormDescription>
</div>
<FormControl>
<Switch
className="shrink-0"
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</div>
</FormItem>
)}
/>
<div className="flex justify-end">
<Button
size="sm"
type="submit"
isLoading={followMutation.isPending}
>
{isSubscribed ? "Update" : "Follow"}
</Button>
</div>
</form>
</Form>
</div>
)}
</div>
)
}
+3 -4
View File
@@ -12,7 +12,7 @@ import {
} from "@renderer/components/ui/form"
import { Input } from "@renderer/components/ui/input"
import { useToast } from "@renderer/components/ui/use-toast"
import { apiFetch } from "@renderer/queries/api-fetch"
import { apiClient } from "@renderer/lib/api-fetch"
import { useMutation } from "@tanstack/react-query"
import { useForm } from "react-hook-form"
import { z } from "zod"
@@ -39,9 +39,8 @@ export function Component() {
const updateMutation = useMutation({
mutationFn: async (values: z.infer<typeof formSchema>) =>
apiFetch("/auth-app/update-account", {
method: "PATCH",
body: values,
apiClient["auth-app"]["update-account"].$patch({
json: values,
}),
onSuccess: () => {
toast({
-46
View File
@@ -1,46 +0,0 @@
import { getCsrfToken } from "@hono/auth-js/react"
import type { AppType } from "@renderer/hono"
import { hc } from "hono/client"
import { ofetch } from "ofetch"
export const apiFetch = ofetch.create({
baseURL: import.meta.env.VITE_API_URL,
credentials: "include",
onRequest: async ({ options }) => {
if (options.method && options.method.toLowerCase() !== "get") {
const csrfToken = await getCsrfToken()
if (!options.body) {
options.body = {}
}
if (options.body instanceof FormData) {
options.body.append("csrfToken", csrfToken)
} else {
;(options.body as Record<string, unknown>).csrfToken = csrfToken
}
}
},
})
export const apiClient = hc<AppType>(
import.meta.env.VITE_API_URL,
{
fetch: async (...args) => {
const options = args[1] as RequestInit
options.credentials = "include"
if (options.method && options.method.toLowerCase() !== "get") {
const csrfToken = await getCsrfToken()
if (!options.body) {
options.body = JSON.stringify({})
}
if (options.body instanceof FormData) {
options.body.append("csrfToken", csrfToken)
} else {
if (typeof options.body === "string") {
options.body = JSON.stringify({ csrfToken, ...JSON.parse(options.body) })
}
}
}
return fetch(args[0], options)
},
},
)
+3 -5
View File
@@ -1,7 +1,7 @@
import { UnprocessableEntityError } from "@renderer/biz/error"
import { useBizInfiniteQuery, useBizQuery } from "@renderer/hooks/useBizQuery"
import { apiClient } from "@renderer/lib/api-fetch"
import { defineQuery } from "@renderer/lib/defineQuery"
import { apiClient } from "@renderer/queries/api-fetch"
import { entryActions } from "@renderer/store/entry"
export const entries = {
@@ -42,9 +42,8 @@ export const entries = {
id,
},
})
const json = await res.json()
return json.data
return res.data
},
{
rootKey: ["entries"],
@@ -59,9 +58,8 @@ export const entries = {
id,
},
})
const json = await res.json()
return json.data
return res.data
},
{
rootKey: ["entries-preview"],
+2 -3
View File
@@ -2,8 +2,8 @@ import {
UnprocessableFeedError,
} from "@renderer/biz/error"
import { useBizQuery } from "@renderer/hooks/useBizQuery"
import { apiClient } from "@renderer/lib/api-fetch"
import { defineQuery } from "@renderer/lib/defineQuery"
import { apiClient } from "@renderer/queries/api-fetch"
export const feed = {
byId: ({
@@ -25,9 +25,8 @@ export const feed = {
url,
},
})
const json = await res.json()
return json.data
return res.data
},
{
rootKey: ["feed"],
+4 -6
View File
@@ -1,6 +1,6 @@
import { apiClient } from "@renderer/lib/api-fetch"
import { defineQuery } from "@renderer/lib/defineQuery"
import type { FeedViewType } from "@renderer/lib/enum"
import { apiClient } from "@renderer/queries/api-fetch"
import { subscriptionActions, unreadActions } from "@renderer/store"
export const subscription = {
@@ -23,11 +23,9 @@ export const subscription = {
),
categories: (view?: number) =>
defineQuery(["subscription-categories", view], async () => {
const res = await (
await apiClient.categories.$get({
query: { view: String(view) },
})
).json()
const res = await apiClient.categories.$get({
query: { view: String(view) },
})
return res.data
}),
+3 -6
View File
@@ -1,7 +1,6 @@
import { apiClient } from "@renderer/lib/api-fetch"
import { getEntriesParams } from "@renderer/lib/utils"
import type { EntryModel } from "@renderer/models"
import { apiClient } from "@renderer/queries/api-fetch"
import type { InferResponseType } from "hono/client"
import { produce } from "immer"
import { omit } from "lodash-es"
@@ -26,7 +25,7 @@ interface EntryActions {
read?: boolean
pageParam?: string
}) => Promise<InferResponseType<typeof apiClient.entries.$post>>
}) => Promise<Awaited<ReturnType<typeof apiClient.entries.$post>>>
upsertMany: (entries: EntryModel[]) => void
optimisticUpdate: (entryId: string, changed: Partial<EntryModel>) => void
@@ -54,7 +53,7 @@ export const useEntryStore = createZustandStore<EntryState & EntryActions>(
pageParam,
}) => {
const res = await apiClient.entries.$post({
const data = await apiClient.entries.$post({
json: {
publishedAfter: pageParam as string,
read,
@@ -66,8 +65,6 @@ export const useEntryStore = createZustandStore<EntryState & EntryActions>(
},
})
const data = await res.json()
if (data.data) {
get().upsertMany(data.data)
}
+5 -5
View File
@@ -1,6 +1,6 @@
import { apiClient } from "@renderer/lib/api-fetch"
import type { FeedViewType } from "@renderer/lib/enum"
import type { SubscriptionModel } from "@renderer/models"
import { apiClient } from "@renderer/queries/api-fetch"
import { produce } from "immer"
import { omit } from "lodash-es"
@@ -23,11 +23,11 @@ export const useSubscriptionStore = createZustandStore<
data: {},
async fetchByView(view) {
const res = await (
await apiClient.subscriptions.$get({ query: { view: String(view) } })
).json()
const res = await apiClient.subscriptions.$get({
query: { view: String(view) },
})
res.data.forEach((subscription: SubscriptionModel) => {
res.data.forEach((subscription) => {
set((state) =>
produce(state, (state) => {
state.data[subscription.feeds.id] = subscription
+3 -3
View File
@@ -1,5 +1,5 @@
import { apiClient } from "@renderer/lib/api-fetch"
import type { FeedViewType } from "@renderer/lib/enum"
import { apiClient } from "@renderer/queries/api-fetch"
import { produce } from "immer"
import { omit } from "lodash-es"
@@ -19,9 +19,9 @@ export const useUnreadStore = createZustandStore<UnreadState & UnreadActions>(
data: {},
async fetchUnreadByView(view) {
const unread = await (
const unread = (
await apiClient.reads.$get({ query: { view: String(view) } })
).json()
)
const { data } = unread
for (const feedId in data) {