useInstalledPlugins.ts 912 B

123456789101112131415161718192021222324252627
  1. import useSWR, { SWRResponse } from 'swr';
  2. import type { SearchResult, SearchResultItem } from '../interfaces/github-api';
  3. const pluginFetcher = (owner: string, repo: string) => {
  4. return async() => {
  5. const reqUrl = `/api/fetch_repository?owner=${owner}&repo=${repo}`;
  6. const data = await fetch(reqUrl).then(res => res.json());
  7. return data.searchResultItem;
  8. };
  9. };
  10. export const useInstalledPlugin = (owner: string, repo: string): SWRResponse<SearchResultItem | null, Error> => {
  11. return useSWR(`${owner}/{repo}`, pluginFetcher(owner, repo));
  12. };
  13. const pluginsFetcher = () => {
  14. return async() => {
  15. const reqUrl = '/api/fetch_repositories';
  16. const data = await fetch(reqUrl).then(res => res.json());
  17. return data.searchResult;
  18. };
  19. };
  20. export const useInstalledPlugins = (): SWRResponse<SearchResult | null, Error> => {
  21. return useSWR('/api/fetch_repositories', pluginsFetcher());
  22. };