Skip to content

Commit

Permalink
Merge pull request #458 from psteinroe/feat/abort-signal
Browse files Browse the repository at this point in the history
feat: support abort signal in react query
  • Loading branch information
psteinroe authored Jun 5, 2024
2 parents 30fce63 + eefa8e6 commit 0cda3d6
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-otters-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-react-query": minor
---

feat: support abort signal
5 changes: 5 additions & 0 deletions .changeset/tricky-donuts-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@supabase-cache-helpers/postgrest-core": patch
---

feat: is transform builder helper
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@changesets/cli": "2.27.0",
"eslint": "8.54.0",
"prettier": "3.2.0",
"supabase": "^1.145.4",
"supabase": "latest",
"turbo": "1.10.16"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/postgrest-core/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import * as Import from '../src';

describe('index exports', () => {
it('should export', () => {
expect(Object.keys(Import)).toHaveLength(38);
expect(Object.keys(Import)).toHaveLength(39);
});
});
1 change: 1 addition & 0 deletions packages/postgrest-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './lib/cache-data-types';
export * from './lib/response-types';
export * from './lib/encode-object';
export * from './lib/is-postgrest-builder';
export * from './lib/is-postgrest-transform-builder';
export * from './lib/get';
export * from './lib/set-filter-value';
export * from './lib/parse-value';
Expand Down
30 changes: 30 additions & 0 deletions packages/postgrest-core/src/lib/is-postgrest-transform-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PostgrestTransformBuilder } from '@supabase/postgrest-js';
import { GenericSchema } from '@supabase/postgrest-js/dist/module/types';

export const isPostgrestTransformBuilder = <
Schema extends GenericSchema,
Row extends Record<string, unknown>,
Result,
RelationName = unknown,
Relationships = unknown,
>(
q: unknown,
): q is PostgrestTransformBuilder<
Schema,
Row,
Result,
RelationName,
Relationships
> => {
return (
typeof (
q as PostgrestTransformBuilder<
Schema,
Row,
Result,
RelationName,
Relationships
>
).abortSignal === 'function'
);
};
6 changes: 5 additions & 1 deletion packages/postgrest-react-query/src/query/build-query-opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PostgrestError } from '@supabase/postgrest-js';
import {
AnyPostgrestResponse,
isPostgrestBuilder,
isPostgrestTransformBuilder,
} from '@supabase-cache-helpers/postgrest-core';
import { UseQueryOptions as UseReactQueryOptions } from '@tanstack/react-query';

Expand All @@ -16,7 +17,10 @@ export function buildQueryOpts<Result>(
): UseReactQueryOptions<AnyPostgrestResponse<Result>, PostgrestError> {
return {
queryKey: encode<Result>(query, false),
queryFn: async () => {
queryFn: async ({ signal }) => {
if (isPostgrestTransformBuilder(query)) {
query = query.abortSignal(signal);
}
if (isPostgrestBuilder(query)) {
query = query.throwOnError();
}
Expand Down
108 changes: 49 additions & 59 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 0cda3d6

Please sign in to comment.