Skip to content

Commit cbfb2fa

Browse files
committed
migrate tests
1 parent 633b54f commit cbfb2fa

File tree

12 files changed

+2509
-36
lines changed

12 files changed

+2509
-36
lines changed

packages/clients/tanstack-query/package.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"watch": "tsup-node --watch",
1111
"lint": "eslint src --ext ts",
1212
"test": "vitest run",
13-
"pack": "pnpm pack"
13+
"pack": "pnpm pack",
14+
"test:generate": "tsx scripts/generate.ts"
1415
},
1516
"keywords": [
1617
"tanstack-query",
@@ -35,17 +36,24 @@
3536
},
3637
"dependencies": {
3738
"@zenstackhq/common-helpers": "workspace:*",
38-
"@zenstackhq/schema": "workspace:*",
3939
"@zenstackhq/orm": "workspace:*",
40-
"superjson": "^2.2.3",
41-
"decimal.js": "catalog:"
40+
"@zenstackhq/schema": "workspace:*",
41+
"decimal.js": "catalog:",
42+
"superjson": "^2.2.3"
4243
},
4344
"devDependencies": {
45+
"@tanstack/react-query": "catalog:",
46+
"@testing-library/dom": "^10.4.1",
47+
"@testing-library/react": "^16.3.0",
48+
"@types/react": "catalog:",
4449
"@zenstackhq/eslint-config": "workspace:*",
50+
"@zenstackhq/language": "workspace:*",
51+
"@zenstackhq/sdk": "workspace:*",
4552
"@zenstackhq/typescript-config": "workspace:*",
46-
"react": "catalog:",
47-
"@types/react": "catalog:",
48-
"@tanstack/react-query": "catalog:"
53+
"@zenstackhq/vitest-config": "workspace:*",
54+
"jsdom": "^27.1.0",
55+
"nock": "^14.0.10",
56+
"react": "catalog:"
4957
},
5058
"peerDependencies": {
5159
"@tanstack/react-query": "^5.0.0",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { loadDocument } from '@zenstackhq/language';
2+
import { TsSchemaGenerator } from '@zenstackhq/sdk';
3+
import { glob } from 'glob';
4+
import path from 'node:path';
5+
import { fileURLToPath } from 'node:url';
6+
7+
const dir = path.dirname(fileURLToPath(import.meta.url));
8+
9+
async function main() {
10+
const zmodelFiles = glob.sync(path.resolve(dir, '../test/**/*.zmodel'));
11+
for (const file of zmodelFiles) {
12+
console.log(`Generating TS schema for: ${file}`);
13+
await generate(file);
14+
}
15+
}
16+
17+
async function generate(schemaPath: string) {
18+
const generator = new TsSchemaGenerator();
19+
const outputDir = path.dirname(schemaPath);
20+
const result = await loadDocument(schemaPath);
21+
if (!result.success) {
22+
throw new Error(`Failed to load schema from ${schemaPath}: ${result.errors}`);
23+
}
24+
await generator.generate(result.model, outputDir);
25+
}
26+
27+
main();

packages/clients/tanstack-query/src/react.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useSuspenseQuery,
88
type DefaultError,
99
type InfiniteData,
10+
type QueryKey,
1011
type UseInfiniteQueryOptions,
1112
type UseInfiniteQueryResult,
1213
type UseMutationOptions,
@@ -87,19 +88,29 @@ function useHooksContext() {
8788

8889
export type ModelQueryOptions<T> = Omit<UseQueryOptions<T, DefaultError>, 'queryKey'> & ExtraQueryOptions;
8990

91+
export type ModelQueryResult<T> = UseQueryResult<T, DefaultError> & { queryKey: QueryKey };
92+
9093
export type ModelSuspenseQueryOptions<T> = Omit<UseSuspenseQueryOptions<T, DefaultError>, 'queryKey'> &
9194
ExtraQueryOptions;
9295

96+
export type ModelSuspenseQueryResult<T> = UseSuspenseQueryResult<T, DefaultError> & { queryKey: QueryKey };
97+
9398
export type ModelInfiniteQueryOptions<T> = Omit<
9499
UseInfiniteQueryOptions<T, DefaultError, InfiniteData<T>>,
95100
'queryKey' | 'initialPageParam'
96101
>;
97102

103+
export type ModelInfiniteQueryResult<T> = UseInfiniteQueryResult<T, DefaultError> & { queryKey: QueryKey };
104+
98105
export type ModelSuspenseInfiniteQueryOptions<T> = Omit<
99106
UseSuspenseInfiniteQueryOptions<T, DefaultError, InfiniteData<T>>,
100107
'queryKey' | 'initialPageParam'
101108
>;
102109

110+
export type ModelSuspenseInfiniteQueryResult<T> = UseSuspenseInfiniteQueryResult<T, DefaultError> & {
111+
queryKey: QueryKey;
112+
};
113+
103114
export type ModelMutationOptions<T, TArgs> = Omit<UseMutationOptions<T, DefaultError, TArgs>, 'mutationFn'> &
104115
ExtraMutationOptions;
105116

@@ -113,48 +124,48 @@ export type ModelQueryHooks<Schema extends SchemaDef, Model extends GetModels<Sc
113124
useFindUnique<T extends FindUniqueArgs<Schema, Model>>(
114125
args: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
115126
options?: ModelQueryOptions<ModelResult<Schema, Model, T> | null>,
116-
): UseQueryResult<ModelResult<Schema, Model, T> | null>;
127+
): ModelQueryResult<ModelResult<Schema, Model, T> | null>;
117128

118129
useSuspenseFindUnique<T extends FindUniqueArgs<Schema, Model>>(
119130
args: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
120131
options?: ModelSuspenseQueryOptions<ModelResult<Schema, Model, T> | null>,
121-
): UseSuspenseQueryResult<ModelResult<Schema, Model, T> | null>;
132+
): ModelSuspenseQueryResult<ModelResult<Schema, Model, T> | null>;
122133

123134
useFindFirst<T extends FindArgs<Schema, Model, false>>(
124135
args?: SelectSubset<T, FindArgs<Schema, Model, false>>,
125136
options?: ModelQueryOptions<ModelResult<Schema, Model, T> | null>,
126-
): UseQueryResult<ModelResult<Schema, Model, T> | null>;
137+
): ModelQueryResult<ModelResult<Schema, Model, T> | null>;
127138

128139
useSuspenseFindFirst<T extends FindArgs<Schema, Model, false>>(
129140
args?: SelectSubset<T, FindArgs<Schema, Model, false>>,
130141
options?: ModelSuspenseQueryOptions<ModelResult<Schema, Model, T> | null>,
131-
): UseSuspenseQueryResult<ModelResult<Schema, Model, T> | null>;
142+
): ModelSuspenseQueryResult<ModelResult<Schema, Model, T> | null>;
132143

133144
useFindMany<T extends FindArgs<Schema, Model, true>>(
134145
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
135146
options?: ModelQueryOptions<ModelResult<Schema, Model, T>[]>,
136-
): UseQueryResult<ModelResult<Schema, Model, T>[]>;
147+
): ModelQueryResult<ModelResult<Schema, Model, T>[]>;
137148

138149
useSuspenseFindMany<T extends FindArgs<Schema, Model, true>>(
139150
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
140151
options?: ModelSuspenseQueryOptions<ModelResult<Schema, Model, T>[]>,
141-
): UseSuspenseQueryResult<ModelResult<Schema, Model, T>[]>;
142-
143-
useSuspenseInfiniteFindMany<T extends FindArgs<Schema, Model, true>>(
144-
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
145-
options?: ModelSuspenseInfiniteQueryOptions<ModelResult<Schema, Model, T>[]>,
146-
): UseSuspenseInfiniteQueryResult<InfiniteData<ModelResult<Schema, Model, T>[]>>;
152+
): ModelSuspenseQueryResult<ModelResult<Schema, Model, T>[]>;
147153

148154
useInfiniteFindMany<T extends FindArgs<Schema, Model, true>>(
149155
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
150156
options?: ModelInfiniteQueryOptions<ModelResult<Schema, Model, T>[]>,
151-
): UseInfiniteQueryResult<InfiniteData<ModelResult<Schema, Model, T>[]>>;
157+
): ModelInfiniteQueryResult<InfiniteData<ModelResult<Schema, Model, T>[]>>;
158+
159+
useSuspenseInfiniteFindMany<T extends FindArgs<Schema, Model, true>>(
160+
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
161+
options?: ModelSuspenseInfiniteQueryOptions<ModelResult<Schema, Model, T>[]>,
162+
): ModelSuspenseInfiniteQueryResult<InfiniteData<ModelResult<Schema, Model, T>[]>>;
152163

153164
useCreate<T extends CreateArgs<Schema, Model>>(
154-
options?: UseMutationOptions<ModelResult<Schema, Model, T>, DefaultError, T>,
165+
options?: ModelMutationOptions<ModelResult<Schema, Model, T>, T>,
155166
): ModelMutationResult<ModelResult<Schema, Model, T>, T>;
156167

157-
useCreateMany<T extends CreateManyArgs<Schema, Model>[]>(
168+
useCreateMany<T extends CreateManyArgs<Schema, Model>>(
158169
options?: ModelMutationOptions<BatchResult, T>,
159170
): ModelMutationResult<BatchResult, T>;
160171

@@ -188,27 +199,27 @@ export type ModelQueryHooks<Schema extends SchemaDef, Model extends GetModels<Sc
188199

189200
useCount<T extends CountArgs<Schema, Model>>(
190201
options?: ModelQueryOptions<CountResult<Schema, Model, T>>,
191-
): UseQueryResult<CountResult<Schema, Model, T>>;
202+
): ModelQueryResult<CountResult<Schema, Model, T>>;
192203

193204
useSuspenseCount<T extends CountArgs<Schema, Model>>(
194205
options?: ModelSuspenseQueryOptions<CountResult<Schema, Model, T>>,
195-
): UseSuspenseQueryResult<CountResult<Schema, Model, T>>;
206+
): ModelSuspenseQueryResult<CountResult<Schema, Model, T>>;
196207

197208
useAggregate<T extends AggregateArgs<Schema, Model>>(
198209
options?: ModelQueryOptions<AggregateResult<Schema, Model, T>>,
199-
): UseQueryResult<AggregateResult<Schema, Model, T>>;
210+
): ModelQueryResult<AggregateResult<Schema, Model, T>>;
200211

201212
useSuspenseAggregate<T extends AggregateArgs<Schema, Model>>(
202213
options?: ModelSuspenseQueryOptions<AggregateResult<Schema, Model, T>>,
203-
): UseSuspenseQueryResult<AggregateResult<Schema, Model, T>>;
214+
): ModelSuspenseQueryResult<AggregateResult<Schema, Model, T>>;
204215

205216
useGroupBy<T extends GroupByArgs<Schema, Model>>(
206217
options?: ModelQueryOptions<GroupByResult<Schema, Model, T>>,
207-
): UseQueryResult<GroupByResult<Schema, Model, T>>;
218+
): ModelQueryResult<GroupByResult<Schema, Model, T>>;
208219

209220
useSuspenseGroupBy<T extends GroupByArgs<Schema, Model>>(
210221
options?: ModelSuspenseQueryOptions<GroupByResult<Schema, Model, T>>,
211-
): UseSuspenseQueryResult<GroupByResult<Schema, Model, T>>;
222+
): ModelSuspenseQueryResult<GroupByResult<Schema, Model, T>>;
212223
};
213224

214225
/**
@@ -233,7 +244,7 @@ export function useModelQueries<Schema extends SchemaDef, Model extends GetModel
233244
throw new Error(`Model "${model}" not found in schema`);
234245
}
235246

236-
const modelName = lowerCaseFirst(modelDef.name);
247+
const modelName = modelDef.name;
237248

238249
return {
239250
useFindUnique: (args: any, options?: any) => {

packages/clients/tanstack-query/src/utils/common.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { lowerCaseFirst } from '@zenstackhq/common-helpers';
12
import type { SchemaDef } from '@zenstackhq/schema';
23
import { applyMutation } from './mutator';
34
import { getMutatedModels, getReadModels } from './query-analysis';
@@ -208,12 +209,13 @@ export function unmarshal(value: string) {
208209
}
209210

210211
export function makeUrl(url: string, model: string, operation: string, args?: unknown) {
212+
const baseUrl = `${url}/${lowerCaseFirst(model)}/${operation}`;
211213
if (!args) {
212-
return `${url}/${model}/${operation}`;
214+
return baseUrl;
213215
}
214216

215217
const { data, meta } = serialize(args);
216-
let result = `${url}/${model}/${operation}?q=${encodeURIComponent(JSON.stringify(data))}`;
218+
let result = `${baseUrl}?q=${encodeURIComponent(JSON.stringify(data))}`;
217219
if (meta) {
218220
result += `&meta=${encodeURIComponent(JSON.stringify({ serialization: meta }))}`;
219221
}

0 commit comments

Comments
 (0)