@openapi-qraft/react 2.0.0-next.1 → 2.0.0-next.2
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/CHANGELOG.md +7 -0
- package/dist/types/migrate-to-v2-codemod.d.ts +3 -0
- package/dist/types/migrate-to-v2-codemod.d.ts.map +1 -0
- package/dist/types/qraftPredefinedParametersRequestFn.d.ts +1 -1
- package/dist/types/qraftPredefinedParametersRequestFn.d.ts.map +1 -1
- package/dist/types/service-operation/ServiceOperationGetQueryState.d.ts +4 -3
- package/dist/types/service-operation/ServiceOperationGetQueryState.d.ts.map +1 -1
- package/package.json +5 -3
- package/src/migrate-to-v2-codemod.ts +118 -0
- package/src/qraftPredefinedParametersRequestFn.ts +17 -8
- package/src/service-operation/ServiceOperationGetQueryState.ts +8 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @openapi-qraft/react
|
|
2
2
|
|
|
3
|
+
## 2.0.0-next.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 56191e1: Updated the `getQueryState(...)` function to support being invoked without parameters if they are not required. This enhancement simplifies the usage in scenarios where the parameters are optional, providing a more flexible API.
|
|
8
|
+
- bbcac65: Refined all types related to value in the `qraftPredefinedParametersRequestFn(...)` function.
|
|
9
|
+
|
|
3
10
|
## 2.0.0-next.1
|
|
4
11
|
|
|
5
12
|
### Minor Changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate-to-v2-codemod.d.ts","sourceRoot":"","sources":["../../src/migrate-to-v2-codemod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAGH,QAAQ,EAER,OAAO,EACR,MAAM,aAAa,CAAC;AAErB,MAAM,CAAC,OAAO,UAAU,WAAW,CACjC,IAAI,EAAE,QAAQ,EACd,GAAG,EAAE,GAAG,EACR,OAAO,EAAE,OAAO,UA2EjB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RequestFn } from './lib/requestFn.js';
|
|
2
|
-
export type QraftPredefinedParameterValue<
|
|
2
|
+
export type QraftPredefinedParameterValue<TValue> = TValue | (() => Promise<TValue | undefined>) | undefined;
|
|
3
3
|
export type InputPredefinedParametersItem<TValue> = {
|
|
4
4
|
requestPattern: string;
|
|
5
5
|
parameters: Array<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"qraftPredefinedParametersRequestFn.d.ts","sourceRoot":"","sources":["../../src/qraftPredefinedParametersRequestFn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,SAAS,EAIV,MAAM,oBAAoB,CAAC;AAG5B,MAAM,MAAM,6BAA6B,CAAC,
|
|
1
|
+
{"version":3,"file":"qraftPredefinedParametersRequestFn.d.ts","sourceRoot":"","sources":["../../src/qraftPredefinedParametersRequestFn.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,SAAS,EAIV,MAAM,oBAAoB,CAAC;AAG5B,MAAM,MAAM,6BAA6B,CAAC,MAAM,IAC5C,MAAM,GACN,CAAC,MAAM,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC,GACnC,SAAS,CAAC;AAEd,MAAM,MAAM,6BAA6B,CAAC,MAAM,IAAI;IAClD,cAAc,EAAE,MAAM,CAAC;IACvB,UAAU,EAAE,KAAK,CAAC;QAChB,EAAE,EAAE,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;QAClC,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,6BAA6B,CAAC,MAAM,CAAC,CAAC;KAC9C,CAAC,CAAC;CACJ,CAAC;AAEF,KAAK,iCAAiC,GAClC,KAAK,GACL,MAAM,GACN,KAAK,GACL,OAAO,GACP,QAAQ,GACR,SAAS,GACT,MAAM,GACN,OAAO,CAAC;AAEZ,MAAM,MAAM,8BAA8B,GAAG;IAC3C,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EACH,KAAK,CAAC,iCAAiC,CAAC,GACxC,aAAa,CAAC,QAAQ,CAAC,iCAAiC,CAAC,CAAC,CAAC;IAC/D,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;CACxD,CAAC;AAEF,wBAAgB,kCAAkC,CAChD,8BAA8B,SAAS,6BAA6B,CAAC,GAAG,CAAC,EACzE,KAAK,EACL,MAAM,EAEN,yBAAyB,EACrB,KAAK,CAAC,8BAA8B,CAAC,GACrC,aAAa,CAAC,QAAQ,CAAC,8BAA8B,CAAC,CAAC,EAC3D,0BAA0B,EACtB,KAAK,CAAC,8BAA8B,CAAC,GACrC,aAAa,CAAC,QAAQ,CAAC,8BAA8B,CAAC,CAAC,EAC3D,SAAS,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,GAClC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CA6C1B"}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { DefaultError, QueryState } from '@tanstack/query-core';
|
|
2
2
|
import type { OperationInfiniteData } from './OperationInfiniteData.js';
|
|
3
3
|
import type { ServiceOperationInfiniteQueryKey, ServiceOperationQueryKey } from './ServiceOperationKey.js';
|
|
4
|
+
import { AreAllOptional } from '../lib/AreAllOptional.js';
|
|
4
5
|
export interface ServiceOperationGetQueryState<TSchema extends {
|
|
5
6
|
url: string;
|
|
6
7
|
method: string;
|
|
7
|
-
}, TData, TParams
|
|
8
|
-
getQueryState(parameters: TParams | ServiceOperationQueryKey<TSchema, TParams>): QueryState<TData, TError> | undefined;
|
|
9
|
-
getInfiniteQueryState(parameters: TParams | ServiceOperationInfiniteQueryKey<TSchema, TParams>): QueryState<OperationInfiniteData<TData, TParams>, TError> | undefined;
|
|
8
|
+
}, TData, TParams, TError = DefaultError> {
|
|
9
|
+
getQueryState(parameters: AreAllOptional<TParams> extends true ? TParams | ServiceOperationQueryKey<TSchema, TParams> | void : TParams | ServiceOperationQueryKey<TSchema, TParams>): QueryState<TData, TError> | undefined;
|
|
10
|
+
getInfiniteQueryState(parameters: AreAllOptional<TParams> extends true ? TParams | ServiceOperationInfiniteQueryKey<TSchema, TParams> | void : TParams | ServiceOperationInfiniteQueryKey<TSchema, TParams>): QueryState<OperationInfiniteData<TData, TParams>, TError> | undefined;
|
|
10
11
|
}
|
|
11
12
|
//# sourceMappingURL=ServiceOperationGetQueryState.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServiceOperationGetQueryState.d.ts","sourceRoot":"","sources":["../../../src/service-operation/ServiceOperationGetQueryState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,EACV,gCAAgC,EAChC,wBAAwB,EACzB,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"ServiceOperationGetQueryState.d.ts","sourceRoot":"","sources":["../../../src/service-operation/ServiceOperationGetQueryState.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,KAAK,EACV,gCAAgC,EAChC,wBAAwB,EACzB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE1D,MAAM,WAAW,6BAA6B,CAC5C,OAAO,SAAS;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC/C,KAAK,EACL,OAAO,EACP,MAAM,GAAG,YAAY;IAErB,aAAa,CACX,UAAU,EAAE,cAAc,CAAC,OAAO,CAAC,SAAS,IAAI,GAC5C,OAAO,GAAG,wBAAwB,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,IAAI,GAC3D,OAAO,GAAG,wBAAwB,CAAC,OAAO,EAAE,OAAO,CAAC,GACvD,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;IAEzC,qBAAqB,CACnB,UAAU,EAAE,cAAc,CAAC,OAAO,CAAC,SAAS,IAAI,GAC5C,OAAO,GAAG,gCAAgC,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,IAAI,GACnE,OAAO,GAAG,gCAAgC,CAAC,OAAO,EAAE,OAAO,CAAC,GAC/D,UAAU,CAAC,qBAAqB,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;CAC1E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openapi-qraft/react",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.2",
|
|
4
4
|
"description": "API client for React, providing type-safe requests and dynamic Tanstack Query React Hooks via a modular, Proxy-based architecture.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "NODE_ENV=production rollup --config rollup.config.mjs && tsc --project tsconfig.build.json --emitDeclarationOnly",
|
|
@@ -15,20 +15,22 @@
|
|
|
15
15
|
},
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@openapi-qraft/cli": "2.0.0-next.
|
|
18
|
+
"@openapi-qraft/cli": "2.0.0-next.2",
|
|
19
19
|
"@openapi-qraft/eslint-config": "1.0.0",
|
|
20
|
-
"@openapi-qraft/openapi-typescript-plugin": "^1.2.0-next.
|
|
20
|
+
"@openapi-qraft/openapi-typescript-plugin": "^1.2.0-next.2",
|
|
21
21
|
"@openapi-qraft/rollup-config": "1.1.0-next.0",
|
|
22
22
|
"@openapi-qraft/test-fixtures": "1.0.0",
|
|
23
23
|
"@tanstack/react-query": "^5.17.3",
|
|
24
24
|
"@testing-library/jest-dom": "^5.17.0",
|
|
25
25
|
"@testing-library/react": "^13.4.0",
|
|
26
26
|
"@testing-library/user-event": "^14.5.2",
|
|
27
|
+
"@types/jscodeshift": "^0.11.11",
|
|
27
28
|
"@types/react": "^18.2.47",
|
|
28
29
|
"@vitejs/plugin-react": "latest",
|
|
29
30
|
"@vitest/coverage-v8": "^1.1.3",
|
|
30
31
|
"eslint": "^9.0.0",
|
|
31
32
|
"globals": "^15.0.0",
|
|
33
|
+
"jscodeshift": "^17.0.0",
|
|
32
34
|
"jsdom": "latest",
|
|
33
35
|
"msw": "^2.0.12",
|
|
34
36
|
"prettier": "^3.3.3",
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
API,
|
|
3
|
+
ASTPath,
|
|
4
|
+
CallExpression,
|
|
5
|
+
FileInfo,
|
|
6
|
+
MemberExpression,
|
|
7
|
+
Options,
|
|
8
|
+
} from 'jscodeshift';
|
|
9
|
+
|
|
10
|
+
export default function transformer(
|
|
11
|
+
file: FileInfo,
|
|
12
|
+
api: API,
|
|
13
|
+
options: Options
|
|
14
|
+
) {
|
|
15
|
+
const j = api.jscodeshift;
|
|
16
|
+
const root = j(file.source);
|
|
17
|
+
|
|
18
|
+
const methodNames = [
|
|
19
|
+
'fetchQuery',
|
|
20
|
+
'fetchInfiniteQuery',
|
|
21
|
+
'prefetchQuery',
|
|
22
|
+
'prefetchInfiniteQuery',
|
|
23
|
+
'getQueryData',
|
|
24
|
+
'getQueriesData',
|
|
25
|
+
'setQueryData',
|
|
26
|
+
'getQueryState',
|
|
27
|
+
'setQueriesData',
|
|
28
|
+
'invalidateQueries',
|
|
29
|
+
'refetchQueries',
|
|
30
|
+
'cancelQueries',
|
|
31
|
+
'removeQueries',
|
|
32
|
+
'resetQueries',
|
|
33
|
+
'isFetching',
|
|
34
|
+
'isMutating',
|
|
35
|
+
'setInfiniteQueryData',
|
|
36
|
+
'getInfiniteQueryData',
|
|
37
|
+
'getInfiniteQueryState',
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const hooksWithTwoArgs = [
|
|
41
|
+
'useSuspenseQueries',
|
|
42
|
+
'useIsFetching',
|
|
43
|
+
'useMutationState',
|
|
44
|
+
'useIsMutating',
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const hooksWithThreeArgs = [
|
|
48
|
+
'useQuery',
|
|
49
|
+
'useMutation',
|
|
50
|
+
'useInfiniteQuery',
|
|
51
|
+
'useQueries',
|
|
52
|
+
'useSuspenseQuery',
|
|
53
|
+
'useSuspenseInfiniteQuery',
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
const apiClientVariableNames = getAPIClientVariableNames(options);
|
|
57
|
+
|
|
58
|
+
return root
|
|
59
|
+
.find(j.CallExpression)
|
|
60
|
+
.forEach((path: ASTPath<CallExpression>) => {
|
|
61
|
+
const callee = path.node.callee;
|
|
62
|
+
if (callee.type === 'MemberExpression') {
|
|
63
|
+
const apiClientCallMethodName = getAPIClientCallMethodName(
|
|
64
|
+
callee,
|
|
65
|
+
apiClientVariableNames
|
|
66
|
+
);
|
|
67
|
+
if (apiClientCallMethodName) {
|
|
68
|
+
if (methodNames.includes(apiClientCallMethodName)) {
|
|
69
|
+
// Remove the last argument `queryClient`
|
|
70
|
+
path.node.arguments = path.node.arguments.slice(0, -1);
|
|
71
|
+
} else if (
|
|
72
|
+
hooksWithTwoArgs.includes(apiClientCallMethodName) &&
|
|
73
|
+
path.node.arguments.length === 2
|
|
74
|
+
) {
|
|
75
|
+
// Remove the second argument for specific hooks if there are exactly two arguments
|
|
76
|
+
path.node.arguments = path.node.arguments.slice(0, 1);
|
|
77
|
+
} else if (
|
|
78
|
+
hooksWithThreeArgs.includes(apiClientCallMethodName) &&
|
|
79
|
+
path.node.arguments.length === 3
|
|
80
|
+
) {
|
|
81
|
+
// Remove the third argument for specific hooks if there are exactly three arguments
|
|
82
|
+
path.node.arguments = path.node.arguments.slice(0, 2);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
.toSource();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function getAPIClientCallMethodName(
|
|
91
|
+
node: MemberExpression,
|
|
92
|
+
apiClientNames: string[]
|
|
93
|
+
): string | undefined {
|
|
94
|
+
if (
|
|
95
|
+
node.object.type === 'MemberExpression' &&
|
|
96
|
+
node.object.object.type === 'MemberExpression' &&
|
|
97
|
+
node.object.object.object.type === 'Identifier' &&
|
|
98
|
+
node.object.object.property.type === 'Identifier' &&
|
|
99
|
+
node.object.property.type === 'Identifier' &&
|
|
100
|
+
node.property.type === 'Identifier'
|
|
101
|
+
) {
|
|
102
|
+
const apiClientVariableName = node.object.object.object.name;
|
|
103
|
+
|
|
104
|
+
if (apiClientNames.includes(apiClientVariableName))
|
|
105
|
+
return node.property.name;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function getAPIClientVariableNames(options: Options) {
|
|
110
|
+
const apiClientVariableNameOption = options['api-client-variable-name'];
|
|
111
|
+
|
|
112
|
+
if (!apiClientVariableNameOption) return ['api', 'qraft'];
|
|
113
|
+
|
|
114
|
+
return String(apiClientVariableNameOption)
|
|
115
|
+
.split(',')
|
|
116
|
+
.map((name) => name.trim())
|
|
117
|
+
.filter(Boolean);
|
|
118
|
+
}
|
|
@@ -7,9 +7,10 @@ import type {
|
|
|
7
7
|
} from './lib/requestFn.js';
|
|
8
8
|
import { shelfMerge } from './lib/shelfMerge.js';
|
|
9
9
|
|
|
10
|
-
export type QraftPredefinedParameterValue<
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
export type QraftPredefinedParameterValue<TValue> =
|
|
11
|
+
| TValue
|
|
12
|
+
| (() => Promise<TValue | undefined>)
|
|
13
|
+
| undefined;
|
|
13
14
|
|
|
14
15
|
export type InputPredefinedParametersItem<TValue> = {
|
|
15
16
|
requestPattern: string;
|
|
@@ -131,10 +132,18 @@ async function resolveQraftPredefinedParameters<T>(
|
|
|
131
132
|
);
|
|
132
133
|
}
|
|
133
134
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
type QraftPredefinedParameterResolvedValue<TData> = TData extends () => Promise<
|
|
136
|
+
infer TAsyncData
|
|
137
|
+
>
|
|
138
|
+
? TAsyncData
|
|
139
|
+
: TData;
|
|
140
|
+
|
|
141
|
+
async function resolveQraftPredefinedParameterValue<TData>(
|
|
142
|
+
value: QraftPredefinedParameterValue<TData>
|
|
143
|
+
): Promise<QraftPredefinedParameterResolvedValue<TData>> {
|
|
137
144
|
if (value && typeof value === 'function')
|
|
138
|
-
return (await
|
|
139
|
-
|
|
145
|
+
return (await (
|
|
146
|
+
value as () => Promise<TData | undefined>
|
|
147
|
+
)()) as QraftPredefinedParameterResolvedValue<TData>;
|
|
148
|
+
return value as QraftPredefinedParameterResolvedValue<TData>;
|
|
140
149
|
}
|
|
@@ -4,18 +4,23 @@ import type {
|
|
|
4
4
|
ServiceOperationInfiniteQueryKey,
|
|
5
5
|
ServiceOperationQueryKey,
|
|
6
6
|
} from './ServiceOperationKey.js';
|
|
7
|
+
import { AreAllOptional } from '../lib/AreAllOptional.js';
|
|
7
8
|
|
|
8
9
|
export interface ServiceOperationGetQueryState<
|
|
9
10
|
TSchema extends { url: string; method: string },
|
|
10
11
|
TData,
|
|
11
|
-
TParams
|
|
12
|
+
TParams,
|
|
12
13
|
TError = DefaultError,
|
|
13
14
|
> {
|
|
14
15
|
getQueryState(
|
|
15
|
-
parameters: TParams
|
|
16
|
+
parameters: AreAllOptional<TParams> extends true
|
|
17
|
+
? TParams | ServiceOperationQueryKey<TSchema, TParams> | void
|
|
18
|
+
: TParams | ServiceOperationQueryKey<TSchema, TParams>
|
|
16
19
|
): QueryState<TData, TError> | undefined;
|
|
17
20
|
|
|
18
21
|
getInfiniteQueryState(
|
|
19
|
-
parameters: TParams
|
|
22
|
+
parameters: AreAllOptional<TParams> extends true
|
|
23
|
+
? TParams | ServiceOperationInfiniteQueryKey<TSchema, TParams> | void
|
|
24
|
+
: TParams | ServiceOperationInfiniteQueryKey<TSchema, TParams>
|
|
20
25
|
): QueryState<OperationInfiniteData<TData, TParams>, TError> | undefined;
|
|
21
26
|
}
|