use-context-swr.tsx 1.1 KB

1234567891011121314151617181920212223242526272829
  1. import assert from 'assert';
  2. import { useSWRStatic } from '@growi/core/dist/swr';
  3. import type {
  4. Key, SWRConfiguration, SWRResponse,
  5. } from 'swr';
  6. export function useContextSWR<Data, Error>(key: Key): SWRResponse<Data, Error>;
  7. export function useContextSWR<Data, Error>(key: Key, data: Data | undefined): SWRResponse<Data, Error>;
  8. export function useContextSWR<Data, Error>(key: Key, data: Data | undefined,
  9. configuration: SWRConfiguration<Data, Error> | undefined): SWRResponse<Data, Error>;
  10. export function useContextSWR<Data, Error>(
  11. ...args: readonly [Key]
  12. | readonly [Key, Data | undefined]
  13. | readonly [Key, Data | undefined, SWRConfiguration<Data, Error> | undefined]
  14. ): SWRResponse<Data, Error> {
  15. const [key, data, configuration] = args;
  16. assert.notStrictEqual(configuration?.fetcher, null, 'useContextSWR does not support \'configuration.fetcher\'');
  17. const swrResponse = useSWRStatic(key, data, configuration);
  18. // overwrite mutate
  19. const result = Object.assign(swrResponse, { mutate: () => { throw Error('mutate can not be used in context') } });
  20. return result;
  21. }