Skip to content

Commit 916d3c1

Browse files
authored
chore: add context helpers to the generated tanstack hooks (#767)
1 parent 8b1c0c0 commit 916d3c1

File tree

7 files changed

+99
-27
lines changed

7 files changed

+99
-27
lines changed

packages/plugins/tanstack-query/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"jest": "^29.5.0",
8484
"react": "18.2.0",
8585
"rimraf": "^3.0.2",
86+
"svelte": "^4.2.1",
8687
"swr": "^2.0.3",
8788
"ts-jest": "^29.0.5",
8889
"typescript": "^4.9.4",

packages/plugins/tanstack-query/src/generator.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,14 @@ function generateIndex(project: Project, outDir: string, models: DataModel[], ta
418418
sf.addStatements(`export { Provider } from '@zenstackhq/tanstack-query/runtime/react';`);
419419
break;
420420
case 'vue':
421-
sf.addStatements(`export { VueQueryContextKey } from '@zenstackhq/tanstack-query/runtime/vue';`);
421+
sf.addStatements(
422+
`export { VueQueryContextKey, provideHooksContext } from '@zenstackhq/tanstack-query/runtime/vue';`
423+
);
422424
break;
423425
case 'svelte':
424-
sf.addStatements(`export { SvelteQueryContextKey } from '@zenstackhq/tanstack-query/runtime/svelte';`);
426+
sf.addStatements(
427+
`export { SvelteQueryContextKey, setHooksContext } from '@zenstackhq/tanstack-query/runtime/svelte';`
428+
);
425429
break;
426430
}
427431
}

packages/plugins/tanstack-query/src/runtime/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type APIContext = {
2424
/**
2525
* The endpoint to use for the queries.
2626
*/
27-
endpoint: string;
27+
endpoint?: string;
2828

2929
/**
3030
* A custom fetch function for sending the HTTP requests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './prisma-types';
2+
export type { FetchFn } from './common';

packages/plugins/tanstack-query/src/runtime/svelte.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
type QueryClient,
1111
type QueryOptions,
1212
} from '@tanstack/svelte-query';
13-
import { FetchFn, QUERY_KEY_PREFIX, fetcher, makeUrl, marshal } from './common';
13+
import { setContext } from 'svelte';
14+
import { APIContext, FetchFn, QUERY_KEY_PREFIX, fetcher, makeUrl, marshal } from './common';
1415

1516
export { APIContext as RequestHandlerContext } from './common';
1617

@@ -19,6 +20,13 @@ export { APIContext as RequestHandlerContext } from './common';
1920
*/
2021
export const SvelteQueryContextKey = 'zenstack-svelte-query-context';
2122

23+
/**
24+
* Set context for the generated TanStack Query hooks.
25+
*/
26+
export function setHooksContext(context: APIContext) {
27+
setContext(SvelteQueryContextKey, context);
28+
}
29+
2230
/**
2331
* Creates a svelte-query query.
2432
*

packages/plugins/tanstack-query/src/runtime/vue.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ import {
1111
type UseMutationOptions,
1212
type UseQueryOptions,
1313
} from '@tanstack/vue-query';
14-
import { inject } from 'vue';
15-
import { DEFAULT_QUERY_ENDPOINT, FetchFn, QUERY_KEY_PREFIX, fetcher, makeUrl, marshal } from './common';
16-
import { RequestHandlerContext } from './svelte';
14+
import { inject, provide } from 'vue';
15+
import { APIContext, DEFAULT_QUERY_ENDPOINT, FetchFn, QUERY_KEY_PREFIX, fetcher, makeUrl, marshal } from './common';
1716

1817
export { APIContext as RequestHandlerContext } from './common';
1918

2019
export const VueQueryContextKey = 'zenstack-vue-query-context';
2120

21+
/**
22+
* Provide context for the generated TanStack Query hooks.
23+
*/
24+
export function provideHooksContext(context: APIContext) {
25+
provide<APIContext>(VueQueryContextKey, context);
26+
}
27+
2228
export function getContext() {
23-
return inject<RequestHandlerContext>(VueQueryContextKey, {
29+
return inject<APIContext>(VueQueryContextKey, {
2430
endpoint: DEFAULT_QUERY_ENDPOINT,
2531
fetch: undefined,
2632
});

pnpm-lock.yaml

Lines changed: 71 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)