use-gh-repo

安装

npx shadcn@latest add https://ssp-ui-registry-staging.sh3.agoralab.co/r/use-gh-repo.json
yarn dlx shadcn@latest add https://ssp-ui-registry-staging.sh3.agoralab.co/r/use-gh-repo.json
pnpx shadcn@latest add https://ssp-ui-registry-staging.sh3.agoralab.co/r/use-gh-repo.json
bunx shadcn@latest add https://ssp-ui-registry-staging.sh3.agoralab.co/r/use-gh-repo.json

参考

示例

"use client"import type { SWRConfiguration } from "swr"import z from "zod"import { useCancelableSwr } from "@/registry/ssp/hook/use-cancelable-swr"export const API_GH_ROOT = "https://api.github.com"export const template = {id: "gh-repos-owner-repo",// https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#get-a-repositoryurl: `${API_GH_ROOT}/repos/:owner/:repo`,method: "GET",pathParams: ["owner", "repo"],// Partial schema, add more fields as needed.responseSchema: z.object({  id: z.number(),  name: z.string(),  full_name: z.string(),  private: z.boolean(),  html_url: z.string(),  description: z.string(),  stargazers_count: z.number(),  watchers_count: z.number(),  forks_count: z.number(),  open_issues_count: z.number(),}),}export const useGhRepo = (owner: string,repo: string,fetchOpts?: Omit<RequestInit, "signal">,swrOpts?: SWRConfiguration) => {const url = template.url.replace(":owner", owner).replace(":repo", repo)const [{ data, error, isLoading, mutate }, controller] = useCancelableSwr(  { url, key: [template.id, owner, repo] },  fetchOpts,  swrOpts)return {  data: (data ? template.responseSchema.parse(data) : data) as    | z.infer<typeof template.responseSchema>    | undefined,  error,  isLoading,  mutate,  controller,}}
const [{ data, error, isLoading, mutate }, controller] = useGhRepo({
  owner: "TEN-framework",
  repo: "ten-framework",
})
const stargazersCount = data?.stargazers_count