mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	We're migrating to ESM, which will allow us to import the latest versions of our dependencies. Co-Authored-By: Christopher Kolstad <chriswk@getunleash.io>
		
			
				
	
	
		
			37 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import useSWR, { type SWRConfiguration } from 'swr';
 | |
| import { useCallback, useMemo } from 'react';
 | |
| import { formatApiPath } from 'utils/formatPath';
 | |
| import handleErrorResponses from '../httpErrorResponseHandler.js';
 | |
| import type { IApiToken } from '../useApiTokens/useApiTokens.js';
 | |
| 
 | |
| export const useProjectApiTokens = (
 | |
|     project: string,
 | |
|     options: SWRConfiguration = {},
 | |
| ) => {
 | |
|     const path = formatApiPath(`api/admin/projects/${project}/api-tokens`);
 | |
|     const { data, error, mutate } = useSWR<IApiToken[]>(path, fetcher, options);
 | |
| 
 | |
|     const tokens = useMemo(() => {
 | |
|         return data ?? [];
 | |
|     }, [data]);
 | |
| 
 | |
|     const refetch = useCallback(() => {
 | |
|         mutate().catch(console.warn);
 | |
|     }, [mutate]);
 | |
| 
 | |
|     return {
 | |
|         tokens,
 | |
|         error,
 | |
|         loading: !error && !data,
 | |
|         refetch,
 | |
|     };
 | |
| };
 | |
| 
 | |
| const fetcher = async (path: string): Promise<IApiToken[]> => {
 | |
|     const res = await fetch(path).then(
 | |
|         handleErrorResponses('Project Api tokens'),
 | |
|     );
 | |
|     const data = await res.json();
 | |
|     return data.tokens;
 | |
| };
 |