@orval/query 8.6.2 → 8.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +163 -38
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -4
package/dist/index.mjs
CHANGED
|
@@ -9,6 +9,8 @@ const normalizeQueryOptions = (queryOptions = {}, outputWorkspace) => {
|
|
|
9
9
|
return {
|
|
10
10
|
...queryOptions.usePrefetch ? { usePrefetch: true } : {},
|
|
11
11
|
...queryOptions.useInvalidate ? { useInvalidate: true } : {},
|
|
12
|
+
...queryOptions.useSetQueryData ? { useSetQueryData: true } : {},
|
|
13
|
+
...queryOptions.useGetQueryData ? { useGetQueryData: true } : {},
|
|
12
14
|
...queryOptions.useQuery ? { useQuery: true } : {},
|
|
13
15
|
...queryOptions.useInfinite ? { useInfinite: true } : {},
|
|
14
16
|
...queryOptions.useInfiniteQueryParam ? { useInfiniteQueryParam: queryOptions.useInfiniteQueryParam } : {},
|
|
@@ -340,19 +342,24 @@ const getSignalDefinition = ({ hasSignal, hasSignalParam = false }) => {
|
|
|
340
342
|
if (!hasSignal) return "";
|
|
341
343
|
return `${hasSignalParam ? "querySignal" : "signal"}?: AbortSignal\n`;
|
|
342
344
|
};
|
|
343
|
-
const getQueryArgumentsRequestType = (httpClient, mutator) => {
|
|
344
|
-
if (!mutator)
|
|
345
|
+
const getQueryArgumentsRequestType = (httpClient, mutator, useRuntimeFetcher) => {
|
|
346
|
+
if (!mutator) {
|
|
347
|
+
if (httpClient === OutputHttpClient.AXIOS) return `axios?: AxiosRequestConfig`;
|
|
348
|
+
if (httpClient === OutputHttpClient.FETCH && useRuntimeFetcher) return "fetch?: RequestInit, fetcher?: typeof globalThis.fetch";
|
|
349
|
+
return "fetch?: RequestInit";
|
|
350
|
+
}
|
|
345
351
|
if (mutator.hasSecondArg && !mutator.isHook) return `request?: SecondParameter<typeof ${mutator.name}>`;
|
|
346
352
|
if (mutator.hasSecondArg && mutator.isHook) return `request?: SecondParameter<ReturnType<typeof ${mutator.name}>>`;
|
|
347
353
|
return "";
|
|
348
354
|
};
|
|
349
|
-
const getQueryOptions = ({ isRequestOptions, mutator, isExactOptionalPropertyTypes, hasSignal, httpClient, hasSignalParam = false }) => {
|
|
355
|
+
const getQueryOptions = ({ isRequestOptions, mutator, isExactOptionalPropertyTypes, hasSignal, httpClient, hasSignalParam = false, useRuntimeFetcher = false }) => {
|
|
350
356
|
const signalVar = hasSignalParam ? "querySignal" : "signal";
|
|
351
357
|
const signalProp = hasSignalParam ? `signal: ${signalVar}` : "signal";
|
|
352
358
|
if (!mutator && isRequestOptions) {
|
|
353
359
|
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
354
|
-
|
|
355
|
-
|
|
360
|
+
const fetcherArg = httpClient === OutputHttpClient.FETCH && useRuntimeFetcher ? ", fetcherFn" : "";
|
|
361
|
+
if (!hasSignal) return `${options}${fetcherArg}`;
|
|
362
|
+
return `{ ${isExactOptionalPropertyTypes ? `...(${signalVar} ? { ${signalProp} } : {})` : signalProp}, ...${options} }${fetcherArg}`;
|
|
356
363
|
}
|
|
357
364
|
if (mutator?.hasSecondArg && httpClient === OutputHttpClient.ANGULAR) {
|
|
358
365
|
if (!hasSignal) return "http";
|
|
@@ -369,13 +376,12 @@ const getQueryOptions = ({ isRequestOptions, mutator, isExactOptionalPropertyTyp
|
|
|
369
376
|
}
|
|
370
377
|
return "";
|
|
371
378
|
};
|
|
372
|
-
const getHookOptions = ({ isRequestOptions, httpClient, mutator }) => {
|
|
379
|
+
const getHookOptions = ({ isRequestOptions, httpClient, mutator, useRuntimeFetcher = false }) => {
|
|
373
380
|
if (!isRequestOptions) return "";
|
|
374
381
|
let value = "const {query: queryOptions";
|
|
375
|
-
if (!mutator)
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
382
|
+
if (!mutator) if (httpClient === OutputHttpClient.AXIOS) value += ", axios: axiosOptions";
|
|
383
|
+
else if (httpClient === OutputHttpClient.FETCH && useRuntimeFetcher) value += ", fetch: fetchOptions, fetcher: fetcherFn";
|
|
384
|
+
else value += ", fetch: fetchOptions";
|
|
379
385
|
if (mutator?.hasSecondArg) value += ", request: requestOptions";
|
|
380
386
|
value += "} = options ?? {};";
|
|
381
387
|
return value;
|
|
@@ -389,8 +395,9 @@ const getQueryErrorType = (operationName, response, httpClient, mutator) => {
|
|
|
389
395
|
if (mutator) return mutator.hasErrorType ? `${mutator.default ? pascal(operationName) : ""}ErrorType<${errorsType}>` : errorsType;
|
|
390
396
|
else return httpClient === OutputHttpClient.AXIOS ? `AxiosError<${errorsType}>` : errorsType;
|
|
391
397
|
};
|
|
392
|
-
const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator) => {
|
|
393
|
-
const
|
|
398
|
+
const getHooksOptionImplementation = (isRequestOptions, httpClient, operationName, mutator, useRuntimeFetcher) => {
|
|
399
|
+
const fetcherOption = httpClient === OutputHttpClient.FETCH && useRuntimeFetcher ? ", fetcher: fetcherFn" : "";
|
|
400
|
+
const options = httpClient === OutputHttpClient.AXIOS ? ", axios: axiosOptions" : `, fetch: fetchOptions${fetcherOption}`;
|
|
394
401
|
return isRequestOptions ? `const mutationKey = ['${operationName}'];
|
|
395
402
|
const {mutation: mutationOptions${mutator ? mutator.hasSecondArg ? ", request: requestOptions" : "" : options}} = options ?
|
|
396
403
|
options.mutation && 'mutationKey' in options.mutation && options.mutation.mutationKey ?
|
|
@@ -398,10 +405,11 @@ const {mutation: mutationOptions${mutator ? mutator.hasSecondArg ? ", request: r
|
|
|
398
405
|
: {...options, mutation: {...options.mutation, mutationKey}}
|
|
399
406
|
: {mutation: { mutationKey, }${mutator?.hasSecondArg ? ", request: undefined" : ""}${mutator ? "" : httpClient === OutputHttpClient.AXIOS ? ", axios: undefined" : ", fetch: undefined"}};` : "";
|
|
400
407
|
};
|
|
401
|
-
const getMutationRequestArgs = (isRequestOptions, httpClient, mutator) => {
|
|
408
|
+
const getMutationRequestArgs = (isRequestOptions, httpClient, mutator, useRuntimeFetcher) => {
|
|
402
409
|
const options = httpClient === OutputHttpClient.AXIOS ? "axiosOptions" : "fetchOptions";
|
|
410
|
+
const fetcherArg = httpClient === OutputHttpClient.FETCH && useRuntimeFetcher ? ", fetcherFn" : "";
|
|
403
411
|
if (mutator?.hasSecondArg && httpClient === OutputHttpClient.ANGULAR) return "http";
|
|
404
|
-
return isRequestOptions ? mutator ? mutator.hasSecondArg ? "requestOptions" : "" : options : "";
|
|
412
|
+
return isRequestOptions ? mutator ? mutator.hasSecondArg ? "requestOptions" : "" : `${options}${fetcherArg}` : "";
|
|
405
413
|
};
|
|
406
414
|
const getQueryHeader = (params) => {
|
|
407
415
|
if (params.output.httpClient === OutputHttpClient.FETCH) return generateFetchHeader(params);
|
|
@@ -1000,7 +1008,7 @@ const createAngularAdapter = ({ hasQueryV5, hasQueryV5WithDataTagError, hasQuery
|
|
|
1000
1008
|
getQueryOptionsDefinitionPrefix() {
|
|
1001
1009
|
return prefix;
|
|
1002
1010
|
},
|
|
1003
|
-
generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation }) {
|
|
1011
|
+
generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation, useRuntimeFetcher }) {
|
|
1004
1012
|
const definition = getQueryOptionsDefinition({
|
|
1005
1013
|
operationName,
|
|
1006
1014
|
mutator,
|
|
@@ -1015,7 +1023,7 @@ const createAngularAdapter = ({ hasQueryV5, hasQueryV5WithDataTagError, hasQuery
|
|
|
1015
1023
|
initialData
|
|
1016
1024
|
});
|
|
1017
1025
|
if (!isRequestOptions) return `${type ? "queryOptions" : "mutationOptions"}${initialData === "defined" ? "" : "?"}: ${definition}`;
|
|
1018
|
-
const requestType = getQueryArgumentsRequestType(httpClient, mutator);
|
|
1026
|
+
const requestType = getQueryArgumentsRequestType(httpClient, mutator, useRuntimeFetcher);
|
|
1019
1027
|
const isQueryRequired = initialData === "defined";
|
|
1020
1028
|
const optionsType = `{ ${type ? "query" : "mutation"}${isQueryRequired ? "" : "?"}:${definition}, ${!type && hasInvalidation ? "skipInvalidation?: boolean, " : ""}${requestType}}`;
|
|
1021
1029
|
if (type !== void 0 && !forQueryOptions) return `options${isQueryRequired ? "" : "?"}: ${optionsType} | (() => ${optionsType})\n`;
|
|
@@ -1217,7 +1225,7 @@ const createSvelteAdapter = ({ hasSvelteQueryV4, hasSvelteQueryV6, hasQueryV5, h
|
|
|
1217
1225
|
getQueryOptionsDefinitionPrefix() {
|
|
1218
1226
|
return prefix;
|
|
1219
1227
|
},
|
|
1220
|
-
generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation }) {
|
|
1228
|
+
generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, forQueryOptions = false, hasInvalidation, useRuntimeFetcher }) {
|
|
1221
1229
|
const definition = getQueryOptionsDefinition({
|
|
1222
1230
|
operationName,
|
|
1223
1231
|
mutator,
|
|
@@ -1232,7 +1240,7 @@ const createSvelteAdapter = ({ hasSvelteQueryV4, hasSvelteQueryV6, hasQueryV5, h
|
|
|
1232
1240
|
initialData
|
|
1233
1241
|
});
|
|
1234
1242
|
if (!isRequestOptions) return `${type ? "queryOptions" : "mutationOptions"}${initialData === "defined" ? "" : "?"}: ${definition}`;
|
|
1235
|
-
const requestType = getQueryArgumentsRequestType(httpClient, mutator);
|
|
1243
|
+
const requestType = getQueryArgumentsRequestType(httpClient, mutator, useRuntimeFetcher);
|
|
1236
1244
|
const isQueryRequired = initialData === "defined";
|
|
1237
1245
|
const optionsType = `{ ${type ? "query" : "mutation"}${isQueryRequired ? "" : "?"}:${definition}, ${!type && hasInvalidation ? "skipInvalidation?: boolean, " : ""}${requestType}}`;
|
|
1238
1246
|
return `options${isQueryRequired ? "" : "?"}: ${hasSvelteQueryV6 && !forQueryOptions ? "() => " : ""}${optionsType}\n`;
|
|
@@ -1384,7 +1392,7 @@ const withDefaults = (adapter) => ({
|
|
|
1384
1392
|
getOptionalQueryClientArgument() {
|
|
1385
1393
|
return adapter.hasQueryV5 ? ", queryClient?: QueryClient" : "";
|
|
1386
1394
|
},
|
|
1387
|
-
generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, hasInvalidation }) {
|
|
1395
|
+
generateQueryArguments({ operationName, definitions, mutator, isRequestOptions, type, queryParams, queryParam, initialData, httpClient, hasInvalidation, useRuntimeFetcher }) {
|
|
1388
1396
|
const definition = getQueryOptionsDefinition({
|
|
1389
1397
|
operationName,
|
|
1390
1398
|
mutator,
|
|
@@ -1399,7 +1407,7 @@ const withDefaults = (adapter) => ({
|
|
|
1399
1407
|
initialData
|
|
1400
1408
|
});
|
|
1401
1409
|
if (!isRequestOptions) return `${type ? "queryOptions" : "mutationOptions"}${initialData === "defined" ? "" : "?"}: ${definition}`;
|
|
1402
|
-
const requestType = getQueryArgumentsRequestType(httpClient, mutator);
|
|
1410
|
+
const requestType = getQueryArgumentsRequestType(httpClient, mutator, useRuntimeFetcher);
|
|
1403
1411
|
const isQueryRequired = initialData === "defined";
|
|
1404
1412
|
const optionsType = `{ ${type ? "query" : "mutation"}${isQueryRequired ? "" : "?"}:${definition}, ${!type && hasInvalidation ? "skipInvalidation?: boolean, " : ""}${requestType}}`;
|
|
1405
1413
|
return `options${isQueryRequired ? "" : "?"}: ${optionsType}\n`;
|
|
@@ -1502,6 +1510,72 @@ const serializeTarget = (target) => JSON.stringify({
|
|
|
1502
1510
|
invalidateMode: target.invalidateMode,
|
|
1503
1511
|
file: target.file ?? ""
|
|
1504
1512
|
});
|
|
1513
|
+
const HTTP_METHODS = [
|
|
1514
|
+
"get",
|
|
1515
|
+
"post",
|
|
1516
|
+
"put",
|
|
1517
|
+
"delete",
|
|
1518
|
+
"patch",
|
|
1519
|
+
"options",
|
|
1520
|
+
"head",
|
|
1521
|
+
"trace"
|
|
1522
|
+
];
|
|
1523
|
+
/**
|
|
1524
|
+
* Look up an operation's route and path-parameter metadata from the OpenAPI
|
|
1525
|
+
* spec. Matches against both the raw `operationId` and its camelCase form
|
|
1526
|
+
* so that renamed/overridden operations are still found.
|
|
1527
|
+
*/
|
|
1528
|
+
const findOperationInfo = (spec, operationName) => {
|
|
1529
|
+
const paths = spec?.paths;
|
|
1530
|
+
if (!paths || typeof paths !== "object") return void 0;
|
|
1531
|
+
for (const [routePath, rawPathItem] of Object.entries(paths)) {
|
|
1532
|
+
if (!rawPathItem || typeof rawPathItem !== "object") continue;
|
|
1533
|
+
const pathItem = rawPathItem;
|
|
1534
|
+
for (const method of HTTP_METHODS) {
|
|
1535
|
+
const operation = pathItem[method];
|
|
1536
|
+
const opId = operation?.operationId;
|
|
1537
|
+
if (!opId) continue;
|
|
1538
|
+
if (opId !== operationName && camel(opId) !== operationName) continue;
|
|
1539
|
+
if (!routePath.includes("{")) return {
|
|
1540
|
+
route: routePath,
|
|
1541
|
+
hasRequiredPathParams: false
|
|
1542
|
+
};
|
|
1543
|
+
return {
|
|
1544
|
+
route: routePath,
|
|
1545
|
+
hasRequiredPathParams: [...Array.isArray(pathItem.parameters) ? pathItem.parameters : [], ...Array.isArray(operation.parameters) ? operation.parameters : []].filter((p) => p.in === "path").some((p) => p.schema?.default === void 0 && p.default === void 0)
|
|
1546
|
+
};
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
};
|
|
1550
|
+
/**
|
|
1551
|
+
* Extract the static route prefix before the first path parameter.
|
|
1552
|
+
* e.g. "/pets/{petId}" → "/pets/", "/pets" → "/pets"
|
|
1553
|
+
*
|
|
1554
|
+
* Returns `undefined` when the prefix contains no meaningful literal
|
|
1555
|
+
* segments (e.g. "/{tenantId}/pets") to avoid overly-broad invalidation.
|
|
1556
|
+
*/
|
|
1557
|
+
const getStaticRoutePrefix = (route) => {
|
|
1558
|
+
const idx = route.indexOf("{");
|
|
1559
|
+
if (idx === -1) return route;
|
|
1560
|
+
const prefix = route.slice(0, idx);
|
|
1561
|
+
return prefix.split("/").some((segment) => segment.length > 0) ? prefix : void 0;
|
|
1562
|
+
};
|
|
1563
|
+
/**
|
|
1564
|
+
* Check whether the target invalidation needs to call the query key function.
|
|
1565
|
+
* Returns false when no params are specified and the route has required path
|
|
1566
|
+
* parameters (without defaults), meaning we should use predicate-based broad
|
|
1567
|
+
* invalidation instead of calling the function without the required arguments.
|
|
1568
|
+
*/
|
|
1569
|
+
const hasNonEmptyParams = (params) => {
|
|
1570
|
+
if (!params) return false;
|
|
1571
|
+
if (Array.isArray(params)) return params.length > 0;
|
|
1572
|
+
return Object.keys(params).length > 0;
|
|
1573
|
+
};
|
|
1574
|
+
const needsQueryKeyFnCall = (target, spec) => {
|
|
1575
|
+
if (hasNonEmptyParams(target.params)) return true;
|
|
1576
|
+
if (findOperationInfo(spec, target.query)?.hasRequiredPathParams) return false;
|
|
1577
|
+
return true;
|
|
1578
|
+
};
|
|
1505
1579
|
const generateVariableRef = (varName) => {
|
|
1506
1580
|
const parts = varName.split(".");
|
|
1507
1581
|
if (parts.length === 1) return `variables.${varName}`;
|
|
@@ -1511,10 +1585,25 @@ const generateParamArgs = (params) => {
|
|
|
1511
1585
|
if (Array.isArray(params)) return params.map((v) => generateVariableRef(v)).join(", ");
|
|
1512
1586
|
return Object.values(params).map((v) => generateVariableRef(v)).join(", ");
|
|
1513
1587
|
};
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1588
|
+
/**
|
|
1589
|
+
* Create a generateInvalidateCall function that has access to the OpenAPI spec
|
|
1590
|
+
* for intelligent route-based invalidation when params are not specified.
|
|
1591
|
+
*/
|
|
1592
|
+
const createGenerateInvalidateCall = (spec, shouldSplitQueryKey) => {
|
|
1593
|
+
return (target) => {
|
|
1594
|
+
const method = target.invalidateMode === "reset" ? "resetQueries" : "invalidateQueries";
|
|
1595
|
+
const queryKeyFn = camel(`get-${target.query}-query-key`);
|
|
1596
|
+
if (hasNonEmptyParams(target.params)) return ` queryClient.${method}({ queryKey: ${queryKeyFn}(${generateParamArgs(target.params)}) });`;
|
|
1597
|
+
const info = findOperationInfo(spec, target.query);
|
|
1598
|
+
if (info?.hasRequiredPathParams) {
|
|
1599
|
+
const prefix = getStaticRoutePrefix(info.route);
|
|
1600
|
+
if (prefix !== void 0) {
|
|
1601
|
+
if (shouldSplitQueryKey) return ` queryClient.${method}({ queryKey: [${prefix.split("/").filter((s) => s !== "").map((s) => `'${s}'`).join(", ")}] });`;
|
|
1602
|
+
return ` queryClient.${method}({ predicate: (query) => typeof query.queryKey[0] === 'string' && query.queryKey[0].startsWith('${prefix}') });`;
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
return ` queryClient.${method}({ queryKey: ${queryKeyFn}() });`;
|
|
1606
|
+
};
|
|
1518
1607
|
};
|
|
1519
1608
|
const generateMutationHook = async ({ verbOptions, options, isRequestOptions, httpClient, doc, adapter }) => {
|
|
1520
1609
|
const { operationName, body, props, mutator, response, operationId, override } = verbOptions;
|
|
@@ -1550,13 +1639,15 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
|
|
|
1550
1639
|
return true;
|
|
1551
1640
|
});
|
|
1552
1641
|
const hasInvalidation = uniqueInvalidates.length > 0 && adapter.supportsMutationInvalidation();
|
|
1642
|
+
const useRuntimeFetcher = override.fetch.useRuntimeFetcher;
|
|
1553
1643
|
const mutationArguments = adapter.generateQueryArguments({
|
|
1554
1644
|
operationName,
|
|
1555
1645
|
definitions,
|
|
1556
1646
|
mutator,
|
|
1557
1647
|
isRequestOptions,
|
|
1558
1648
|
httpClient,
|
|
1559
|
-
hasInvalidation
|
|
1649
|
+
hasInvalidation,
|
|
1650
|
+
useRuntimeFetcher
|
|
1560
1651
|
});
|
|
1561
1652
|
const mutationArgumentsForOptions = adapter.generateQueryArguments({
|
|
1562
1653
|
operationName,
|
|
@@ -1565,10 +1656,11 @@ const generateMutationHook = async ({ verbOptions, options, isRequestOptions, ht
|
|
|
1565
1656
|
isRequestOptions,
|
|
1566
1657
|
httpClient,
|
|
1567
1658
|
forQueryOptions: true,
|
|
1568
|
-
hasInvalidation
|
|
1659
|
+
hasInvalidation,
|
|
1660
|
+
useRuntimeFetcher
|
|
1569
1661
|
});
|
|
1570
1662
|
const mutationOptionsFnName = camel(mutationOptionsMutator || mutator?.isHook ? `use-${operationName}-mutationOptions` : `get-${operationName}-mutationOptions`);
|
|
1571
|
-
const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator);
|
|
1663
|
+
const hooksOptionImplementation = getHooksOptionImplementation(isRequestOptions, httpClient, camel(operationName), mutator, useRuntimeFetcher);
|
|
1572
1664
|
const mutationOptionsFn = `export const ${mutationOptionsFnName} = <TError = ${errorType},
|
|
1573
1665
|
TContext = unknown>(${adapter.getHttpFirstParam(mutator)}${hasInvalidation ? "queryClient: QueryClient, " : ""}${mutationArgumentsForOptions}): ${mutationOptionFnReturnType} => {
|
|
1574
1666
|
|
|
@@ -1580,14 +1672,14 @@ ${hooksOptionImplementation}
|
|
|
1580
1672
|
const mutationFn: MutationFunction<Awaited<ReturnType<${dataType}>>, ${definitions ? `{${definitions}}` : "void"}> = (${properties ? "props" : ""}) => {
|
|
1581
1673
|
${properties ? `const {${properties}} = props ?? {};` : ""}
|
|
1582
1674
|
|
|
1583
|
-
return ${operationName}(${adapter.getMutationHttpPrefix(mutator)}${properties}${properties ? "," : ""}${getMutationRequestArgs(isRequestOptions, httpClient, mutator)})
|
|
1675
|
+
return ${operationName}(${adapter.getMutationHttpPrefix(mutator)}${properties}${properties ? "," : ""}${getMutationRequestArgs(isRequestOptions, httpClient, mutator, useRuntimeFetcher)})
|
|
1584
1676
|
}
|
|
1585
1677
|
|
|
1586
1678
|
${hasInvalidation ? adapter.generateMutationOnSuccess({
|
|
1587
1679
|
operationName,
|
|
1588
1680
|
definitions,
|
|
1589
1681
|
isRequestOptions,
|
|
1590
|
-
generateInvalidateCall,
|
|
1682
|
+
generateInvalidateCall: createGenerateInvalidateCall(context.spec, !!query.shouldSplitQueryKey),
|
|
1591
1683
|
uniqueInvalidates
|
|
1592
1684
|
}) : ""}
|
|
1593
1685
|
|
|
@@ -1631,7 +1723,7 @@ ${mutationHookBody}
|
|
|
1631
1723
|
}
|
|
1632
1724
|
`,
|
|
1633
1725
|
mutators: mutationOptionsMutator ? [mutationOptionsMutator] : void 0,
|
|
1634
|
-
imports: hasInvalidation ? uniqueInvalidates.filter((i) => !!i.file).map((i) => ({
|
|
1726
|
+
imports: hasInvalidation ? uniqueInvalidates.filter((i) => !!i.file && needsQueryKeyFnCall(i, context.spec)).map((i) => ({
|
|
1635
1727
|
name: camel(`get-${i.query}-query-key`),
|
|
1636
1728
|
importPath: i.file,
|
|
1637
1729
|
values: true
|
|
@@ -1671,7 +1763,7 @@ const generatePrefetch = ({ usePrefetch, type, useQuery, useInfinite, operationN
|
|
|
1671
1763
|
return queryClient;
|
|
1672
1764
|
}\n`;
|
|
1673
1765
|
};
|
|
1674
|
-
const generateQueryImplementation = ({ queryOption: { name, queryParam, options, type, queryKeyFnName }, operationName, queryProperties, queryKeyProperties, queryParams, params, props, mutator, queryOptionsMutator, queryKeyMutator, isRequestOptions, response, httpClient, isExactOptionalPropertyTypes, hasSignal, route, doc, usePrefetch, useQuery, useInfinite, useInvalidate, adapter }) => {
|
|
1766
|
+
const generateQueryImplementation = ({ queryOption: { name, queryParam, options, type, queryKeyFnName }, operationName, queryProperties, queryKeyProperties, queryParams, params, props, mutator, queryOptionsMutator, queryKeyMutator, isRequestOptions, response, httpClient, isExactOptionalPropertyTypes, hasSignal, useRuntimeFetcher, route, doc, usePrefetch, useQuery, useInfinite, useInvalidate, useSetQueryData, useGetQueryData, adapter }) => {
|
|
1675
1767
|
const { hasQueryV5, hasQueryV5WithDataTagError, hasQueryV5WithInfiniteQueryOptionsError } = adapter;
|
|
1676
1768
|
const hasSignalParam = props.some((prop) => prop.name === "signal");
|
|
1677
1769
|
const queryPropDefinitions = toObjectString(props, "definition");
|
|
@@ -1713,7 +1805,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
1713
1805
|
queryParams,
|
|
1714
1806
|
queryParam,
|
|
1715
1807
|
initialData: "defined",
|
|
1716
|
-
httpClient
|
|
1808
|
+
httpClient,
|
|
1809
|
+
useRuntimeFetcher
|
|
1717
1810
|
});
|
|
1718
1811
|
const undefinedInitialDataQueryArguments = adapter.generateQueryArguments({
|
|
1719
1812
|
operationName,
|
|
@@ -1724,7 +1817,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
1724
1817
|
queryParams,
|
|
1725
1818
|
queryParam,
|
|
1726
1819
|
initialData: "undefined",
|
|
1727
|
-
httpClient
|
|
1820
|
+
httpClient,
|
|
1821
|
+
useRuntimeFetcher
|
|
1728
1822
|
});
|
|
1729
1823
|
const queryArguments = adapter.generateQueryArguments({
|
|
1730
1824
|
operationName,
|
|
@@ -1734,7 +1828,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
1734
1828
|
type,
|
|
1735
1829
|
queryParams,
|
|
1736
1830
|
queryParam,
|
|
1737
|
-
httpClient
|
|
1831
|
+
httpClient,
|
|
1832
|
+
useRuntimeFetcher
|
|
1738
1833
|
});
|
|
1739
1834
|
const queryArgumentsForOptions = adapter.generateQueryArguments({
|
|
1740
1835
|
operationName,
|
|
@@ -1745,7 +1840,8 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
1745
1840
|
queryParams,
|
|
1746
1841
|
queryParam,
|
|
1747
1842
|
httpClient,
|
|
1748
|
-
forQueryOptions: true
|
|
1843
|
+
forQueryOptions: true,
|
|
1844
|
+
useRuntimeFetcher
|
|
1749
1845
|
});
|
|
1750
1846
|
const queryOptions = getQueryOptions({
|
|
1751
1847
|
isRequestOptions,
|
|
@@ -1753,12 +1849,14 @@ const generateQueryImplementation = ({ queryOption: { name, queryParam, options,
|
|
|
1753
1849
|
mutator,
|
|
1754
1850
|
hasSignal,
|
|
1755
1851
|
httpClient,
|
|
1756
|
-
hasSignalParam
|
|
1852
|
+
hasSignalParam,
|
|
1853
|
+
useRuntimeFetcher
|
|
1757
1854
|
});
|
|
1758
1855
|
const hookOptions = getHookOptions({
|
|
1759
1856
|
isRequestOptions,
|
|
1760
1857
|
httpClient,
|
|
1761
|
-
mutator
|
|
1858
|
+
mutator,
|
|
1859
|
+
useRuntimeFetcher
|
|
1762
1860
|
});
|
|
1763
1861
|
const queryFnArguments = getQueryFnArguments({
|
|
1764
1862
|
hasQueryParam: !!queryParam && props.some(({ type }) => type === "queryParam"),
|
|
@@ -1829,8 +1927,18 @@ export function ${queryHookName}<TData = ${TData}, TError = ${errorType}>(\n ${q
|
|
|
1829
1927
|
isRequestOptions,
|
|
1830
1928
|
doc
|
|
1831
1929
|
});
|
|
1832
|
-
const
|
|
1930
|
+
const isPrimaryQueryType = type === QueryType.QUERY || type === QueryType.INFINITE || type === QueryType.SUSPENSE_QUERY && !useQuery || type === QueryType.SUSPENSE_INFINITE && !useInfinite;
|
|
1931
|
+
const shouldGenerateInvalidate = useInvalidate && isPrimaryQueryType;
|
|
1833
1932
|
const invalidateFnName = camel(`invalidate-${name}`);
|
|
1933
|
+
const shouldGenerateSetQueryData = useSetQueryData && isPrimaryQueryType;
|
|
1934
|
+
const isReactQuery = adapter.outputClient === OutputClient.REACT_QUERY;
|
|
1935
|
+
const setQueryDataFnName = isReactQuery ? camel(`use-set-${name}-query-data`) : camel(`set-${name}-query-data`);
|
|
1936
|
+
const setQueryDataKeyExpr = queryKeyMutator ? `${queryKeyMutator.name}({ ${queryProperties} }${queryKeyMutator.hasSecondArg ? `, { url: \`${route}\` }` : ""})` : `${queryKeyFnName}(${queryKeyProperties})`;
|
|
1937
|
+
const setQueryDataProps = toObjectString(props.filter((prop) => prop.type !== GetterPropType.HEADER), "implementation").replaceAll("?:", ":");
|
|
1938
|
+
const shouldGenerateGetQueryData = useGetQueryData && isPrimaryQueryType;
|
|
1939
|
+
const getQueryDataFnName = isReactQuery ? camel(`use-get-${name}-query-data`) : camel(`get-${name}-query-data`);
|
|
1940
|
+
const getQueryDataKeyExpr = setQueryDataKeyExpr;
|
|
1941
|
+
const getQueryDataProps = setQueryDataProps;
|
|
1834
1942
|
const queryInit = adapter.generateQueryInit({
|
|
1835
1943
|
queryOptionsFnName,
|
|
1836
1944
|
queryProperties,
|
|
@@ -1877,6 +1985,20 @@ ${shouldGenerateInvalidate ? `${doc}export const ${invalidateFnName} = async (\n
|
|
|
1877
1985
|
|
|
1878
1986
|
return queryClient;
|
|
1879
1987
|
}\n` : ""}
|
|
1988
|
+
${shouldGenerateSetQueryData ? isReactQuery ? `${doc}export const ${setQueryDataFnName} = () => {
|
|
1989
|
+
const queryClient = useQueryClient();
|
|
1990
|
+
return (${setQueryDataProps}updater: ${TData} | undefined | ((old: ${TData} | undefined) => ${TData} | undefined)) => {
|
|
1991
|
+
queryClient.setQueryData(${setQueryDataKeyExpr}, updater);
|
|
1992
|
+
};
|
|
1993
|
+
}\n` : `${doc}export const ${setQueryDataFnName} = (queryClient: QueryClient, ${setQueryDataProps}updater: ${TData} | undefined | ((old: ${TData} | undefined) => ${TData} | undefined)) => {
|
|
1994
|
+
queryClient.setQueryData(${setQueryDataKeyExpr}, updater);
|
|
1995
|
+
}\n` : ""}
|
|
1996
|
+
${shouldGenerateGetQueryData ? isReactQuery ? `${doc}export const ${getQueryDataFnName} = () => {
|
|
1997
|
+
const queryClient = useQueryClient();
|
|
1998
|
+
return (${getQueryDataProps}) =>
|
|
1999
|
+
queryClient.getQueryData<${TData}>(${getQueryDataKeyExpr});
|
|
2000
|
+
}\n` : `${doc}export const ${getQueryDataFnName} = (queryClient: QueryClient, ${getQueryDataProps}) =>
|
|
2001
|
+
queryClient.getQueryData<${TData}>(${getQueryDataKeyExpr});\n` : ""}
|
|
1880
2002
|
`;
|
|
1881
2003
|
};
|
|
1882
2004
|
const generateQueryHook = async (verbOptions, options, outputClient, adapter) => {
|
|
@@ -2004,6 +2126,7 @@ ${queryKeyFns}`;
|
|
|
2004
2126
|
httpClient,
|
|
2005
2127
|
isExactOptionalPropertyTypes,
|
|
2006
2128
|
hasSignal: getHasSignal({ overrideQuerySignal: override.query.signal }),
|
|
2129
|
+
useRuntimeFetcher: override.fetch.useRuntimeFetcher,
|
|
2007
2130
|
queryOptionsMutator,
|
|
2008
2131
|
queryKeyMutator,
|
|
2009
2132
|
route,
|
|
@@ -2012,6 +2135,8 @@ ${queryKeyFns}`;
|
|
|
2012
2135
|
useQuery: query.useQuery,
|
|
2013
2136
|
useInfinite: query.useInfinite,
|
|
2014
2137
|
useInvalidate: query.useInvalidate,
|
|
2138
|
+
useSetQueryData: operationQueryOptions?.useSetQueryData ?? query.useSetQueryData,
|
|
2139
|
+
useGetQueryData: operationQueryOptions?.useGetQueryData ?? query.useGetQueryData,
|
|
2015
2140
|
adapter
|
|
2016
2141
|
});
|
|
2017
2142
|
implementation += `
|