Skip to content

Commit

Permalink
fix: swr types
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Jul 5, 2023
1 parent 7b77145 commit 07ecafd
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-islands-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-swr": patch
---

fix: swr types
2 changes: 1 addition & 1 deletion packages/postgrest-swr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"directory": "packages/postgrest-swr"
},
"peerDependencies": {
"swr": "^2.0.0",
"swr": "^2.2.0",
"react": "^16.11.0 || ^17.0.0 || ^18.0.0",
"@supabase/postgrest-js": "^1.0.0"
},
Expand Down
21 changes: 0 additions & 21 deletions packages/postgrest-swr/src/lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PostgrestParser } from '@supabase-cache-helpers/postgrest-filter';
import { isPostgrestBuilder } from '@supabase-cache-helpers/postgrest-shared';
import { BareFetcher, Key, Middleware, SWRConfiguration, SWRHook } from 'swr';
import {
SWRInfiniteConfiguration,
SWRInfiniteFetcher,
Expand All @@ -10,26 +9,6 @@ import {

import { encode } from './encode';

export const middleware: Middleware = <Result>(useSWRNext: SWRHook) => {
return (
key: Key,
fetcher: BareFetcher<Result> | null,
config: SWRConfiguration
) => {
if (!fetcher) throw new Error('No fetcher provided');

if (key !== null && !isPostgrestBuilder<Result>(key)) {
throw new Error('Key is not a PostgrestBuilder');
}

return useSWRNext(
key ? encode(new PostgrestParser<Result>(key), false) : null,
() => fetcher(key),
config
);
};
};

export const infiniteMiddleware = <Result>(
useSWRInfiniteNext: SWRInfiniteHook
) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/postgrest-swr/src/mutate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ export type UsePostgrestSWRMutationOpts<
SWRMutationConfiguration<
GetReturnType<S, T, Relationships, O, Q, R>,
PostgrestError,
GetInputType<T, O>,
string
string,
GetInputType<T, O>
> & { disableAutoQuery?: boolean } & GetFetcherOptions<S, T, O>;
2 changes: 1 addition & 1 deletion packages/postgrest-swr/src/mutate/use-delete-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function useDeleteMutation<
primaryKeys: (keyof T['Row'])[],
query?: QueryWithoutWildcard<Q> | null,
opts?: UsePostgrestSWRMutationOpts<S, T, Re, 'DeleteOne', Q, R>
): SWRMutationResponse<R | null, PostgrestError, Partial<T['Row']>> {
): SWRMutationResponse<R | null, PostgrestError, string, Partial<T['Row']>> {
const key = useRandomKey();
const queriesForTable = useQueriesForTableLoader(getTable(qb));
const deleteItem = useDeleteItem({
Expand Down
2 changes: 1 addition & 1 deletion packages/postgrest-swr/src/mutate/use-insert-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function useInsertMutation<
primaryKeys: (keyof T['Row'])[],
query?: QueryWithoutWildcard<Q> | null,
opts?: UsePostgrestSWRMutationOpts<S, T, Re, 'Insert', Q, R>
): SWRMutationResponse<R[] | null, PostgrestError, T['Insert'][]> {
): SWRMutationResponse<R[] | null, PostgrestError, string, T['Insert'][]> {
const key = useRandomKey();
const queriesForTable = useQueriesForTableLoader(getTable(qb));
const upsertItem = useUpsertItem({
Expand Down
2 changes: 1 addition & 1 deletion packages/postgrest-swr/src/mutate/use-update-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function useUpdateMutation<
primaryKeys: (keyof T['Row'])[],
query?: QueryWithoutWildcard<Q> | null,
opts?: UsePostgrestSWRMutationOpts<S, T, Re, 'UpdateOne', Q, R>
): SWRMutationResponse<R | null, PostgrestError, T['Update']> {
): SWRMutationResponse<R | null, PostgrestError, string, T['Update']> {
const key = useRandomKey();
const queriesForTable = useQueriesForTableLoader(getTable(qb));
const upsertItem = useUpsertItem({
Expand Down
2 changes: 1 addition & 1 deletion packages/postgrest-swr/src/mutate/use-upsert-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function useUpsertMutation<
primaryKeys: (keyof T['Row'])[],
query?: QueryWithoutWildcard<Q> | null,
opts?: UsePostgrestSWRMutationOpts<S, T, Re, 'Upsert', Q, R>
): SWRMutationResponse<R[] | null, PostgrestError, T['Insert'][], string> {
): SWRMutationResponse<R[] | null, PostgrestError, string, T['Insert'][]> {
const key = useRandomKey();
const queriesForTable = useQueriesForTableLoader(getTable(qb));
const upsertItem = useUpsertItem({
Expand Down
23 changes: 21 additions & 2 deletions packages/postgrest-swr/src/query/use-query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PostgrestParser } from '@supabase-cache-helpers/postgrest-filter';
import {
AnyPostgrestResponse,
isPostgrestBuilder,
Expand All @@ -10,7 +11,7 @@ import {
} from '@supabase/postgrest-js';
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';

import { middleware } from '../lib';
import { encode } from '../lib';

/**
* The return type of `useQuery` for `.single()` record results
Expand Down Expand Up @@ -112,7 +113,25 @@ function useQuery<Result>(
},
{
...config,
use: [...(config?.use ?? []), middleware],
use: [
...(config?.use ?? []),
(useSWRNext) => {
return (key, fetcher, config) => {
if (!fetcher) throw new Error('No fetcher provided');

if (key !== null && !isPostgrestBuilder<Result>(key)) {
throw new Error('Key is not a PostgrestBuilder');
}

// eslint-disable-next-line react-hooks/rules-of-hooks
return useSWRNext(
key ? encode(new PostgrestParser<Result>(key), false) : null,
() => fetcher(key),
config
);
};
},
],
}
);

Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07ecafd

Please sign in to comment.