mirror of
https://github.com/iflytek/skillhub.git
synced 2026-10-09 03:17:52 +00:00
fix(web): use plain fetch for Phase 2 API hooks
This commit is contained in:
parent
a3fe76eaeb
commit
8d223dc875
8 changed files with 49 additions and 114 deletions
|
|
@ -1,19 +1,16 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { Namespace } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
export function useMyNamespaces() {
|
||||
return useQuery({
|
||||
queryKey: ['my-namespaces'],
|
||||
queryFn: async () => {
|
||||
const { data, error, response } = await client.GET('/api/v1/namespaces' as any, {})
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
const res = await fetch('/api/v1/namespaces')
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
return data as unknown as Namespace[]
|
||||
const json = await res.json()
|
||||
return json as Namespace[]
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,16 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { Namespace } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
export function useNamespaceDetail(slug: string) {
|
||||
return useQuery({
|
||||
queryKey: ['namespace', slug],
|
||||
queryFn: async () => {
|
||||
const { data, error, response } = await client.GET('/api/v1/namespaces/{slug}' as any, {
|
||||
params: {
|
||||
path: {
|
||||
slug,
|
||||
},
|
||||
},
|
||||
})
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
const res = await fetch(`/api/v1/namespaces/${slug}`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
return data as unknown as Namespace
|
||||
const json = await res.json()
|
||||
return json as Namespace
|
||||
},
|
||||
enabled: !!slug,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,25 +1,16 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { NamespaceMember } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
export function useNamespaceMembers(slug: string) {
|
||||
return useQuery({
|
||||
queryKey: ['namespace-members', slug],
|
||||
queryFn: async () => {
|
||||
const { data, error, response } = await client.GET('/api/v1/namespaces/{slug}/members' as any, {
|
||||
params: {
|
||||
path: {
|
||||
slug,
|
||||
},
|
||||
},
|
||||
})
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
const res = await fetch(`/api/v1/namespaces/${slug}/members`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
return data as unknown as NamespaceMember[]
|
||||
const json = await res.json()
|
||||
return json as NamespaceMember[]
|
||||
},
|
||||
enabled: !!slug,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
import { useMutation } from '@tanstack/react-query'
|
||||
import type { PublishResult } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
function getCsrfToken(): string | null {
|
||||
const match = document.cookie.match(/(?:^|; )XSRF-TOKEN=([^;]+)/)
|
||||
|
|
@ -26,21 +22,18 @@ export function usePublishSkill() {
|
|||
...(csrfToken && { 'X-XSRF-TOKEN': csrfToken }),
|
||||
}
|
||||
|
||||
const { data, error, response } = await client.POST('/api/v1/skills/{namespace}/publish' as any, {
|
||||
params: {
|
||||
path: {
|
||||
namespace,
|
||||
},
|
||||
},
|
||||
body: formData as any,
|
||||
const res = await fetch(`/api/v1/skills/${namespace}/publish`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData,
|
||||
})
|
||||
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
|
||||
return data as unknown as PublishResult
|
||||
const json = await res.json()
|
||||
return json as PublishResult
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,23 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { SearchParams, PagedResponse, SkillSummary } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
export function useSearchSkills(params: SearchParams) {
|
||||
return useQuery({
|
||||
queryKey: ['skills', params],
|
||||
queryFn: async () => {
|
||||
const { data, error, response } = await client.GET('/api/v1/skills' as any, {
|
||||
params: {
|
||||
query: {
|
||||
q: params.q,
|
||||
namespace: params.namespace,
|
||||
sort: params.sort,
|
||||
page: params.page,
|
||||
size: params.size,
|
||||
},
|
||||
},
|
||||
})
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
const queryParams = new URLSearchParams()
|
||||
if (params.q) queryParams.append('q', params.q)
|
||||
if (params.namespace) queryParams.append('namespace', params.namespace)
|
||||
if (params.sort) queryParams.append('sort', params.sort)
|
||||
if (params.page !== undefined) queryParams.append('page', String(params.page))
|
||||
if (params.size !== undefined) queryParams.append('size', String(params.size))
|
||||
|
||||
const res = await fetch(`/api/v1/skills?${queryParams.toString()}`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
return data as unknown as PagedResponse<SkillSummary>
|
||||
const json = await res.json()
|
||||
return json as PagedResponse<SkillSummary>
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,16 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { SkillDetail } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
export function useSkillDetail(namespace: string, slug: string) {
|
||||
return useQuery({
|
||||
queryKey: ['skill', namespace, slug],
|
||||
queryFn: async () => {
|
||||
const { data, error, response } = await client.GET('/api/v1/skills/{namespace}/{slug}' as any, {
|
||||
params: {
|
||||
path: {
|
||||
namespace,
|
||||
slug,
|
||||
},
|
||||
},
|
||||
})
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
const res = await fetch(`/api/v1/skills/${namespace}/${slug}`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
return data as unknown as SkillDetail
|
||||
const json = await res.json()
|
||||
return json as SkillDetail
|
||||
},
|
||||
enabled: !!namespace && !!slug,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,27 +1,16 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { SkillFile } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
export function useSkillFiles(namespace: string, slug: string, version: string) {
|
||||
return useQuery({
|
||||
queryKey: ['skill-files', namespace, slug, version],
|
||||
queryFn: async () => {
|
||||
const { data, error, response } = await client.GET('/api/v1/skills/{namespace}/{slug}/versions/{version}/files' as any, {
|
||||
params: {
|
||||
path: {
|
||||
namespace,
|
||||
slug,
|
||||
version,
|
||||
},
|
||||
},
|
||||
})
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
const res = await fetch(`/api/v1/skills/${namespace}/${slug}/versions/${version}/files`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
return data as unknown as SkillFile[]
|
||||
const json = await res.json()
|
||||
return json as SkillFile[]
|
||||
},
|
||||
enabled: !!namespace && !!slug && !!version,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,26 +1,16 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { SkillVersion } from '@/api/types'
|
||||
import createClient from 'openapi-fetch'
|
||||
import type { paths } from '@/api/generated/schema'
|
||||
|
||||
const client = createClient<paths>({ baseUrl: '' })
|
||||
|
||||
export function useSkillVersions(namespace: string, slug: string) {
|
||||
return useQuery({
|
||||
queryKey: ['skill-versions', namespace, slug],
|
||||
queryFn: async () => {
|
||||
const { data, error, response } = await client.GET('/api/v1/skills/{namespace}/{slug}/versions' as any, {
|
||||
params: {
|
||||
path: {
|
||||
namespace,
|
||||
slug,
|
||||
},
|
||||
},
|
||||
})
|
||||
if (error || !data) {
|
||||
throw new Error(`HTTP ${response.status}`)
|
||||
const res = await fetch(`/api/v1/skills/${namespace}/${slug}/versions`)
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}`)
|
||||
}
|
||||
return data as unknown as SkillVersion[]
|
||||
const json = await res.json()
|
||||
return json as SkillVersion[]
|
||||
},
|
||||
enabled: !!namespace && !!slug,
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue