@stack-spot/portal-network 0.184.0 → 0.185.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.
Files changed (136) hide show
  1. package/CHANGELOG.md +2419 -2412
  2. package/dist/api/account.js +1 -1
  3. package/dist/api/agent-tools.js +1 -1
  4. package/dist/api/agent.js +1 -1
  5. package/dist/api/ai.js +1 -1
  6. package/dist/api/apiManagement.js +1 -1
  7. package/dist/api/apiRuntime.js +1 -1
  8. package/dist/api/cloudAccount.js +1 -1
  9. package/dist/api/cloudPlatform.js +1 -1
  10. package/dist/api/cloudPlatformHorizon.js +1 -1
  11. package/dist/api/cloudRuntimes.js +1 -1
  12. package/dist/api/cloudServices.js +1 -1
  13. package/dist/api/codeShift.d.ts +63 -4
  14. package/dist/api/codeShift.d.ts.map +1 -1
  15. package/dist/api/codeShift.js +14 -1
  16. package/dist/api/codeShift.js.map +1 -1
  17. package/dist/api/content.js +1 -1
  18. package/dist/api/dataIntegration.js +1 -1
  19. package/dist/api/discover.js +1 -1
  20. package/dist/api/genAiInference.js +1 -1
  21. package/dist/api/insights.js +1 -1
  22. package/dist/api/notification.js +1 -1
  23. package/dist/api/secrets.js +1 -1
  24. package/dist/api/serviceCatalog.js +1 -1
  25. package/dist/api/workspace-ai.js +1 -1
  26. package/dist/api/workspace.js +1 -1
  27. package/dist/api/workspaceManager.js +1 -1
  28. package/dist/api/workspaceSearchEngine.js +1 -1
  29. package/dist/client/code-shift.d.ts +22 -0
  30. package/dist/client/code-shift.d.ts.map +1 -1
  31. package/dist/client/code-shift.js +28 -1
  32. package/dist/client/code-shift.js.map +1 -1
  33. package/package.json +6 -6
  34. package/scripts/generate-apis.ts +134 -134
  35. package/src/api/account.ts +8367 -8367
  36. package/src/api/agent-tools.ts +2169 -2169
  37. package/src/api/agent.ts +1083 -1083
  38. package/src/api/ai.ts +3388 -3388
  39. package/src/api/apiManagement.ts +570 -570
  40. package/src/api/apiRuntime.ts +2103 -2103
  41. package/src/api/cloudAccount.ts +1239 -1239
  42. package/src/api/cloudPlatform.ts +927 -927
  43. package/src/api/cloudPlatformHorizon.ts +2655 -2655
  44. package/src/api/cloudRuntimes.ts +2043 -2043
  45. package/src/api/cloudServices.ts +1445 -1445
  46. package/src/api/codeShift.ts +3567 -3481
  47. package/src/api/content.ts +9785 -9785
  48. package/src/api/dataIntegration.ts +1657 -1657
  49. package/src/api/discover.ts +435 -435
  50. package/src/api/eventBus.ts +171 -171
  51. package/src/api/genAiInference.ts +603 -603
  52. package/src/api/insights.ts +310 -310
  53. package/src/api/notification.ts +334 -334
  54. package/src/api/secrets.ts +342 -342
  55. package/src/api/serviceCatalog.ts +2908 -2908
  56. package/src/api/workflows.ts +1669 -1669
  57. package/src/api/workspace-ai.ts +677 -677
  58. package/src/api/workspace.ts +5889 -5889
  59. package/src/api/workspaceManager.ts +2951 -2951
  60. package/src/api/workspaceSearchEngine.ts +153 -153
  61. package/src/api-addresses.ts +120 -120
  62. package/src/apis-itau.json +225 -225
  63. package/src/apis.json +225 -225
  64. package/src/client/account.ts +902 -902
  65. package/src/client/agent-tools.ts +210 -210
  66. package/src/client/agent.ts +81 -81
  67. package/src/client/ai.ts +395 -395
  68. package/src/client/api-management.ts +40 -40
  69. package/src/client/cloud-account.ts +70 -70
  70. package/src/client/cloud-platform-horizon.ts +113 -113
  71. package/src/client/cloud-platform.ts +163 -163
  72. package/src/client/cloud-runtimes.ts +129 -129
  73. package/src/client/cloud-services.ts +94 -94
  74. package/src/client/code-shift.ts +364 -349
  75. package/src/client/content.ts +538 -538
  76. package/src/client/data-integration.ts +191 -191
  77. package/src/client/discover.ts +89 -89
  78. package/src/client/event-bus.ts +84 -84
  79. package/src/client/gen-ai-inference.ts +65 -65
  80. package/src/client/insights.ts +28 -28
  81. package/src/client/notification.ts +32 -32
  82. package/src/client/runtime-manager.ts +76 -76
  83. package/src/client/secrets.ts +60 -60
  84. package/src/client/types.ts +377 -377
  85. package/src/client/workflow.ts +83 -83
  86. package/src/client/workspace-ai.ts +191 -191
  87. package/src/client/workspace-manager.ts +564 -564
  88. package/src/client/workspace-search.ts +39 -39
  89. package/src/client/workspace.ts +480 -480
  90. package/src/error/DefaultAPIError.ts +151 -151
  91. package/src/error/FileUploadError.ts +18 -18
  92. package/src/error/IgnoredErrorCodes.ts +3 -3
  93. package/src/error/StackspotAPIError.ts +101 -101
  94. package/src/error/StreamCanceledError.ts +10 -10
  95. package/src/error/StreamError.ts +7 -7
  96. package/src/error/StreamJsonError.ts +10 -10
  97. package/src/error/dictionary/account.ts +58 -58
  98. package/src/error/dictionary/action-details.ts +20 -20
  99. package/src/error/dictionary/action.ts +211 -211
  100. package/src/error/dictionary/agent-tools.ts +75 -75
  101. package/src/error/dictionary/ai-inference.ts +28 -28
  102. package/src/error/dictionary/base.ts +22 -22
  103. package/src/error/dictionary/cloud-platform.ts +82 -82
  104. package/src/error/dictionary/cnt-fields.ts +14 -14
  105. package/src/error/dictionary/cnt.ts +103 -103
  106. package/src/error/dictionary/code-shift.ts +12 -12
  107. package/src/error/dictionary/rte.ts +24 -24
  108. package/src/error/dictionary/rtm.ts +10 -10
  109. package/src/error/dictionary/secrets.ts +14 -14
  110. package/src/error/dictionary/workspace-ai.ts +10 -10
  111. package/src/error/dictionary/workspace-details.ts +15 -15
  112. package/src/error/dictionary/workspace-fields.ts +10 -10
  113. package/src/error/dictionary/workspace.ts +209 -209
  114. package/src/error/types.ts +21 -21
  115. package/src/index.ts +43 -43
  116. package/src/network/AutoInfiniteQuery.ts +115 -115
  117. package/src/network/AutoMutation.ts +27 -27
  118. package/src/network/AutoOperation.ts +73 -73
  119. package/src/network/AutoQuery.ts +75 -75
  120. package/src/network/ManualInfiniteQuery.ts +95 -95
  121. package/src/network/ManualMutation.ts +40 -40
  122. package/src/network/ManualOperation.ts +52 -52
  123. package/src/network/ManualQuery.ts +82 -82
  124. package/src/network/NetworkClient.ts +167 -167
  125. package/src/network/ReactQueryNetworkClient.ts +312 -312
  126. package/src/network/react-query-client.ts +14 -14
  127. package/src/network/types.ts +294 -294
  128. package/src/types.ts +1 -1
  129. package/src/utils/StreamedArray.tsx +146 -146
  130. package/src/utils/StreamedJson.tsx +166 -166
  131. package/src/utils/remove-authorization-param.ts +6 -6
  132. package/src/utils/string.ts +19 -19
  133. package/src/utils/use-extended-list.ts +80 -80
  134. package/src/utils/use-streamed-array.ts +17 -17
  135. package/tsconfig.build.json +4 -4
  136. package/tsconfig.json +10 -10
@@ -1,115 +1,115 @@
1
- /* eslint-disable react-hooks/rules-of-hooks */
2
-
3
- import {
4
- InfiniteData, QueryFunctionContext, QueryKey,
5
- UseInfiniteQueryResult,
6
- useInfiniteQuery,
7
- useSuspenseInfiniteQuery,
8
- } from '@tanstack/react-query'
9
- import { get, set } from 'lodash'
10
- import { StackspotAPIError } from '../error/StackspotAPIError'
11
- import { AutoQuery } from './AutoQuery'
12
- import { queryClient } from './react-query-client'
13
- import { AutoQueryObjectParams, InfiniteQueryObject, InfiniteQueryOptions, UseInfiniteQueryObjectOptions } from './types'
14
-
15
- export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Variables, Accumulator extends keyof Result | ''>
16
- extends AutoQuery<Variables, Result>
17
- implements InfiniteQueryObject<Variables, Result, Accumulator>
18
- {
19
- private options: Required<InfiniteQueryOptions<Variables, Result, PageParamName, Accumulator | ''>>
20
-
21
- constructor(
22
- params: AutoQueryObjectParams<Variables, Result>,
23
- options: InfiniteQueryOptions<Variables, Result, PageParamName, Accumulator>,
24
- ) {
25
- super(params)
26
- this.options = {
27
- ...options,
28
- accumulator: options.accumulator ?? '',
29
- defaultVariables: options.defaultVariables ?? {},
30
- }
31
- }
32
-
33
- private createInfiniteQueryFn(
34
- variables: Variables | undefined,
35
- ): (context: QueryFunctionContext<QueryKey, Variables[PageParamName]>) => Promise<Result> {
36
- return ({ pageParam, signal }) => {
37
- const paginatedVariables = {
38
- ...variables,
39
- }
40
- set(paginatedVariables, this.options.pageParamName, pageParam ?? get(variables, this.options.pageParamName))
41
- return this.callFn(paginatedVariables, signal)
42
- }
43
- }
44
-
45
- private getListFromData(data: InfiniteData<Result> | undefined) {
46
- return data?.pages.map(
47
- page => this.options.accumulator ? page[this.options.accumulator as keyof Result] : page,
48
- ).flat() as Accumulator extends keyof Result ? Result[Accumulator] : Result
49
- }
50
-
51
- private useInfiniteQueryResult(
52
- suspense: boolean,
53
- variables = {} as Variables,
54
- options?: UseInfiniteQueryObjectOptions<Result>,
55
- ) {
56
- variables = { ...this.options.defaultVariables, ...variables }
57
- const use = suspense ? useSuspenseInfiniteQuery : useInfiniteQuery
58
- return use<any, any, any, any, any>({
59
- ...options,
60
- queryKey: this.getInfiniteKey(variables),
61
- queryFn: this.createInfiniteQueryFn(variables),
62
- initialPageParam: this.options.initialPageParam,
63
- getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
64
- this.options.getNextPageParam({ variables, lastPage, allPages, lastPageParam, allPageParams }),
65
- }, queryClient) as UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>
66
- }
67
-
68
- useInfiniteQuery(
69
- variables?: Variables,
70
- options?: UseInfiniteQueryObjectOptions<Result>,
71
- ): [
72
- Accumulator extends keyof Result ? Result[Accumulator] : Result,
73
- UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
74
- ] {
75
- const result = this.useInfiniteQueryResult(true, variables, options)
76
- return [
77
- this.getListFromData(result.data),
78
- result,
79
- ]
80
- }
81
-
82
- useStatefulInfiniteQuery(
83
- variables?: Variables,
84
- options?: UseInfiniteQueryObjectOptions<Result>,
85
- ): [
86
- Accumulator extends keyof Result ? Result[Accumulator] : Result,
87
- boolean,
88
- StackspotAPIError | undefined | null,
89
- UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
90
- ] {
91
- const result = this.useInfiniteQueryResult(false, variables, options)
92
- return [
93
- this.getListFromData(result.data),
94
- result.isPending,
95
- result.error,
96
- result,
97
- ]
98
- }
99
-
100
- async invalidate(variables?: Partial<Variables> | undefined): Promise<void> {
101
- await Promise.all([
102
- super.invalidate(variables),
103
- queryClient.invalidateQueries({ queryKey: this.getInfiniteKey(variables) }),
104
- ])
105
- }
106
-
107
- private getInfiniteKey(variables?: Partial<Variables> | undefined) {
108
- return ['infinite', ...this.getKey(variables)]
109
- }
110
-
111
- async cancel(variables?: Partial<Variables> | undefined) {
112
- super.cancel(variables)
113
- queryClient.cancelQueries({ queryKey: this.getInfiniteKey() })
114
- }
115
- }
1
+ /* eslint-disable react-hooks/rules-of-hooks */
2
+
3
+ import {
4
+ InfiniteData, QueryFunctionContext, QueryKey,
5
+ UseInfiniteQueryResult,
6
+ useInfiniteQuery,
7
+ useSuspenseInfiniteQuery,
8
+ } from '@tanstack/react-query'
9
+ import { get, set } from 'lodash'
10
+ import { StackspotAPIError } from '../error/StackspotAPIError'
11
+ import { AutoQuery } from './AutoQuery'
12
+ import { queryClient } from './react-query-client'
13
+ import { AutoQueryObjectParams, InfiniteQueryObject, InfiniteQueryOptions, UseInfiniteQueryObjectOptions } from './types'
14
+
15
+ export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Variables, Accumulator extends keyof Result | ''>
16
+ extends AutoQuery<Variables, Result>
17
+ implements InfiniteQueryObject<Variables, Result, Accumulator>
18
+ {
19
+ private options: Required<InfiniteQueryOptions<Variables, Result, PageParamName, Accumulator | ''>>
20
+
21
+ constructor(
22
+ params: AutoQueryObjectParams<Variables, Result>,
23
+ options: InfiniteQueryOptions<Variables, Result, PageParamName, Accumulator>,
24
+ ) {
25
+ super(params)
26
+ this.options = {
27
+ ...options,
28
+ accumulator: options.accumulator ?? '',
29
+ defaultVariables: options.defaultVariables ?? {},
30
+ }
31
+ }
32
+
33
+ private createInfiniteQueryFn(
34
+ variables: Variables | undefined,
35
+ ): (context: QueryFunctionContext<QueryKey, Variables[PageParamName]>) => Promise<Result> {
36
+ return ({ pageParam, signal }) => {
37
+ const paginatedVariables = {
38
+ ...variables,
39
+ }
40
+ set(paginatedVariables, this.options.pageParamName, pageParam ?? get(variables, this.options.pageParamName))
41
+ return this.callFn(paginatedVariables, signal)
42
+ }
43
+ }
44
+
45
+ private getListFromData(data: InfiniteData<Result> | undefined) {
46
+ return data?.pages.map(
47
+ page => this.options.accumulator ? page[this.options.accumulator as keyof Result] : page,
48
+ ).flat() as Accumulator extends keyof Result ? Result[Accumulator] : Result
49
+ }
50
+
51
+ private useInfiniteQueryResult(
52
+ suspense: boolean,
53
+ variables = {} as Variables,
54
+ options?: UseInfiniteQueryObjectOptions<Result>,
55
+ ) {
56
+ variables = { ...this.options.defaultVariables, ...variables }
57
+ const use = suspense ? useSuspenseInfiniteQuery : useInfiniteQuery
58
+ return use<any, any, any, any, any>({
59
+ ...options,
60
+ queryKey: this.getInfiniteKey(variables),
61
+ queryFn: this.createInfiniteQueryFn(variables),
62
+ initialPageParam: this.options.initialPageParam,
63
+ getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
64
+ this.options.getNextPageParam({ variables, lastPage, allPages, lastPageParam, allPageParams }),
65
+ }, queryClient) as UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>
66
+ }
67
+
68
+ useInfiniteQuery(
69
+ variables?: Variables,
70
+ options?: UseInfiniteQueryObjectOptions<Result>,
71
+ ): [
72
+ Accumulator extends keyof Result ? Result[Accumulator] : Result,
73
+ UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
74
+ ] {
75
+ const result = this.useInfiniteQueryResult(true, variables, options)
76
+ return [
77
+ this.getListFromData(result.data),
78
+ result,
79
+ ]
80
+ }
81
+
82
+ useStatefulInfiniteQuery(
83
+ variables?: Variables,
84
+ options?: UseInfiniteQueryObjectOptions<Result>,
85
+ ): [
86
+ Accumulator extends keyof Result ? Result[Accumulator] : Result,
87
+ boolean,
88
+ StackspotAPIError | undefined | null,
89
+ UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
90
+ ] {
91
+ const result = this.useInfiniteQueryResult(false, variables, options)
92
+ return [
93
+ this.getListFromData(result.data),
94
+ result.isPending,
95
+ result.error,
96
+ result,
97
+ ]
98
+ }
99
+
100
+ async invalidate(variables?: Partial<Variables> | undefined): Promise<void> {
101
+ await Promise.all([
102
+ super.invalidate(variables),
103
+ queryClient.invalidateQueries({ queryKey: this.getInfiniteKey(variables) }),
104
+ ])
105
+ }
106
+
107
+ private getInfiniteKey(variables?: Partial<Variables> | undefined) {
108
+ return ['infinite', ...this.getKey(variables)]
109
+ }
110
+
111
+ async cancel(variables?: Partial<Variables> | undefined) {
112
+ super.cancel(variables)
113
+ queryClient.cancelQueries({ queryKey: this.getInfiniteKey() })
114
+ }
115
+ }
@@ -1,27 +1,27 @@
1
- /* eslint-disable react-hooks/rules-of-hooks */
2
-
3
- import { UseMutationOptions, useMutation } from '@tanstack/react-query'
4
- import { StackspotAPIError } from '../error/StackspotAPIError'
5
- import { AutoOperation } from './AutoOperation'
6
- import { queryClient } from './react-query-client'
7
- import { AutoQueryObjectParams, MutationObject } from './types'
8
-
9
- export class AutoMutation<Variables, Result> extends AutoOperation<Variables> implements MutationObject<Variables, Result> {
10
- constructor(params: AutoQueryObjectParams<Variables, Result>) {
11
- super(params)
12
- }
13
-
14
- mutate(...args: Variables extends void ? [signal?: AbortSignal] : [variables: Variables, signal?: AbortSignal]) {
15
- const variables = args[0] instanceof AbortSignal ? undefined : args[0]
16
- const signal = args[0] instanceof AbortSignal ? args[0] : args[1]
17
- return this.callFn(variables, signal)
18
- }
19
-
20
- useMutation(options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'> & { signal?: AbortSignal }) {
21
- const result = useMutation<Result, StackspotAPIError, Variables>({
22
- ...options,
23
- mutationFn: (variables) => this.callFn(variables, options?.signal),
24
- }, queryClient)
25
- return [result.mutateAsync as any, result.isPending, result.error as StackspotAPIError | undefined, result as any] as const
26
- }
27
- }
1
+ /* eslint-disable react-hooks/rules-of-hooks */
2
+
3
+ import { UseMutationOptions, useMutation } from '@tanstack/react-query'
4
+ import { StackspotAPIError } from '../error/StackspotAPIError'
5
+ import { AutoOperation } from './AutoOperation'
6
+ import { queryClient } from './react-query-client'
7
+ import { AutoQueryObjectParams, MutationObject } from './types'
8
+
9
+ export class AutoMutation<Variables, Result> extends AutoOperation<Variables> implements MutationObject<Variables, Result> {
10
+ constructor(params: AutoQueryObjectParams<Variables, Result>) {
11
+ super(params)
12
+ }
13
+
14
+ mutate(...args: Variables extends void ? [signal?: AbortSignal] : [variables: Variables, signal?: AbortSignal]) {
15
+ const variables = args[0] instanceof AbortSignal ? undefined : args[0]
16
+ const signal = args[0] instanceof AbortSignal ? args[0] : args[1]
17
+ return this.callFn(variables, signal)
18
+ }
19
+
20
+ useMutation(options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'> & { signal?: AbortSignal }) {
21
+ const result = useMutation<Result, StackspotAPIError, Variables>({
22
+ ...options,
23
+ mutationFn: (variables) => this.callFn(variables, options?.signal),
24
+ }, queryClient)
25
+ return [result.mutateAsync as any, result.isPending, result.error as StackspotAPIError | undefined, result as any] as const
26
+ }
27
+ }
@@ -1,73 +1,73 @@
1
- /* eslint-disable react-hooks/rules-of-hooks */
2
-
3
- import { type RequestOpts } from '@oazapfts/runtime'
4
- import { UseQueryOptions, UseQueryResult, useQuery } from '@tanstack/react-query'
5
- import { StackspotAPIError } from '../error/StackspotAPIError'
6
- import { queryClient } from './react-query-client'
7
- import { AutoQueryObjectParams, OperationObject } from './types'
8
-
9
- export abstract class AutoOperation<Variables> implements OperationObject<Variables> {
10
- protected fn: ((variables: Variables, opts?: RequestOpts) => Promise<any>) | ((opts?: RequestOpts) => Promise<any>)
11
- protected apiName: string
12
- protected onFetchPermission: typeof fetch
13
- private transformError?: (error: any) => StackspotAPIError
14
-
15
- constructor({ apiName, onFetchPermission, fn, transformError }: AutoQueryObjectParams<Variables, any>) {
16
- this.fn = fn
17
- this.onFetchPermission = onFetchPermission
18
- this.apiName = apiName
19
- this.transformError = transformError
20
- if (typeof fn === 'function' && !fn.name) {
21
- throw new Error("Anonymous functions can't be automatically converted into React queries or mutations.")
22
- }
23
- }
24
-
25
- protected async callFn(variables: Partial<Variables> | undefined, signal: AbortSignal | undefined, fetchFn?: typeof fetch) {
26
- const fn = this.fn as (...args: any[]) => Promise<any>
27
- try {
28
- const result = await (
29
- fn.length === 2
30
- ? fn(variables ?? {}, fetchFn ? { fetch: fetchFn, signal } : { signal })
31
- : fn(fetchFn ? { fetch: fetchFn, signal } : { signal })
32
- )
33
- return result
34
- } catch (error) {
35
- throw this.transformError ? this.transformError(error) : error
36
- }
37
- }
38
-
39
- private createPermissionQueryFn(variables: Partial<Variables> | undefined) {
40
- return () => this.callFn(variables, undefined, this.onFetchPermission)
41
- }
42
-
43
- isAllowed(...[variables]: Variables extends void ? [] : [variables?: Partial<Variables>]) {
44
- return queryClient.fetchQuery({
45
- queryKey: this.getPermissionKey(variables as Variables),
46
- queryFn: this.createPermissionQueryFn(variables),
47
- })
48
- }
49
-
50
- useAllowed(
51
- ...args: Variables extends void
52
- ? [options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
53
- : [variables?: Partial<Variables>, options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
54
- ) {
55
- /* `this.fn` is a oazapfts function, i.e. it has arity 1 or 2. If it accepts variables, its arity is 2: the 1st parameter is the
56
- variables and the 2nd is the RequestOpts. If it doesn't accept variables, its arity is one: it accepts only the RequestOpts.
57
- We can use this information to determine what the type of `args` actually is at runtime. If variables are accepted, than the 1st
58
- argument is the variables and the 2nd is the query options, otherwise, it has a single argument, which is the query options. */
59
- const [variables, options] = this.fn.length > 1
60
- ? args as [Variables, Omit<UseQueryOptions, 'queryFn' | 'queryKey'> | undefined]
61
- : [undefined, args[0] as Omit<UseQueryOptions, 'queryFn' | 'queryKey'> | undefined]
62
- const result = useQuery({
63
- ...options,
64
- queryKey: this.getPermissionKey(variables as Variables),
65
- queryFn: this.createPermissionQueryFn(variables),
66
- }, queryClient) as UseQueryResult<boolean, StackspotAPIError>
67
- return [result.data, result.isPending, result.error, result] as const
68
- }
69
-
70
- getPermissionKey(variables?: Partial<Variables>) {
71
- return [this.apiName, `${this.fn.name}.permission`, variables]
72
- }
73
- }
1
+ /* eslint-disable react-hooks/rules-of-hooks */
2
+
3
+ import { type RequestOpts } from '@oazapfts/runtime'
4
+ import { UseQueryOptions, UseQueryResult, useQuery } from '@tanstack/react-query'
5
+ import { StackspotAPIError } from '../error/StackspotAPIError'
6
+ import { queryClient } from './react-query-client'
7
+ import { AutoQueryObjectParams, OperationObject } from './types'
8
+
9
+ export abstract class AutoOperation<Variables> implements OperationObject<Variables> {
10
+ protected fn: ((variables: Variables, opts?: RequestOpts) => Promise<any>) | ((opts?: RequestOpts) => Promise<any>)
11
+ protected apiName: string
12
+ protected onFetchPermission: typeof fetch
13
+ private transformError?: (error: any) => StackspotAPIError
14
+
15
+ constructor({ apiName, onFetchPermission, fn, transformError }: AutoQueryObjectParams<Variables, any>) {
16
+ this.fn = fn
17
+ this.onFetchPermission = onFetchPermission
18
+ this.apiName = apiName
19
+ this.transformError = transformError
20
+ if (typeof fn === 'function' && !fn.name) {
21
+ throw new Error("Anonymous functions can't be automatically converted into React queries or mutations.")
22
+ }
23
+ }
24
+
25
+ protected async callFn(variables: Partial<Variables> | undefined, signal: AbortSignal | undefined, fetchFn?: typeof fetch) {
26
+ const fn = this.fn as (...args: any[]) => Promise<any>
27
+ try {
28
+ const result = await (
29
+ fn.length === 2
30
+ ? fn(variables ?? {}, fetchFn ? { fetch: fetchFn, signal } : { signal })
31
+ : fn(fetchFn ? { fetch: fetchFn, signal } : { signal })
32
+ )
33
+ return result
34
+ } catch (error) {
35
+ throw this.transformError ? this.transformError(error) : error
36
+ }
37
+ }
38
+
39
+ private createPermissionQueryFn(variables: Partial<Variables> | undefined) {
40
+ return () => this.callFn(variables, undefined, this.onFetchPermission)
41
+ }
42
+
43
+ isAllowed(...[variables]: Variables extends void ? [] : [variables?: Partial<Variables>]) {
44
+ return queryClient.fetchQuery({
45
+ queryKey: this.getPermissionKey(variables as Variables),
46
+ queryFn: this.createPermissionQueryFn(variables),
47
+ })
48
+ }
49
+
50
+ useAllowed(
51
+ ...args: Variables extends void
52
+ ? [options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
53
+ : [variables?: Partial<Variables>, options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
54
+ ) {
55
+ /* `this.fn` is a oazapfts function, i.e. it has arity 1 or 2. If it accepts variables, its arity is 2: the 1st parameter is the
56
+ variables and the 2nd is the RequestOpts. If it doesn't accept variables, its arity is one: it accepts only the RequestOpts.
57
+ We can use this information to determine what the type of `args` actually is at runtime. If variables are accepted, than the 1st
58
+ argument is the variables and the 2nd is the query options, otherwise, it has a single argument, which is the query options. */
59
+ const [variables, options] = this.fn.length > 1
60
+ ? args as [Variables, Omit<UseQueryOptions, 'queryFn' | 'queryKey'> | undefined]
61
+ : [undefined, args[0] as Omit<UseQueryOptions, 'queryFn' | 'queryKey'> | undefined]
62
+ const result = useQuery({
63
+ ...options,
64
+ queryKey: this.getPermissionKey(variables as Variables),
65
+ queryFn: this.createPermissionQueryFn(variables),
66
+ }, queryClient) as UseQueryResult<boolean, StackspotAPIError>
67
+ return [result.data, result.isPending, result.error, result] as const
68
+ }
69
+
70
+ getPermissionKey(variables?: Partial<Variables>) {
71
+ return [this.apiName, `${this.fn.name}.permission`, variables]
72
+ }
73
+ }
@@ -1,75 +1,75 @@
1
- /* eslint-disable react-hooks/rules-of-hooks */
2
-
3
- import { DefinedUseQueryResult, UseSuspenseQueryResult, useQuery, useSuspenseQuery } from '@tanstack/react-query'
4
- import { StackspotAPIError } from '../error/StackspotAPIError'
5
- import { AutoOperation } from './AutoOperation'
6
- import { queryClient } from './react-query-client'
7
- import { AutoQueryObjectParams, QueryObject, UseQueryObjectOptions } from './types'
8
-
9
- export class AutoQuery<Variables, Result> extends AutoOperation<Variables> implements QueryObject<Variables, Result> {
10
- constructor(params: AutoQueryObjectParams<Variables, Result>) {
11
- super(params)
12
- }
13
-
14
- query(...[variables]: Variables extends void ? [] : [variables: Variables]) {
15
- return queryClient.fetchQuery({
16
- queryKey: this.getKey(variables),
17
- queryFn: ({ signal }) => this.callFn(variables, signal),
18
- })
19
- }
20
-
21
- #useQueryResult<T extends boolean>(
22
- suspense: T,
23
- ...args: Variables extends void
24
- ? [options?: UseQueryObjectOptions<Result>]
25
- : [variables: Variables, options?: UseQueryObjectOptions<Result>]
26
- ) {
27
- /* `this.fn` is a oazapfts function, i.e. it has arity 1 or 2. If it accepts variables, its arity is 2: the 1st parameter is the
28
- variables and the 2nd is the RequestOpts. If it doesn't accept variables, its arity is one: it accepts only the RequestOpts.
29
- We can use this information to determine what the type of `args` actually is at runtime. If variables are accepted, than the 1st
30
- argument is the variables and the 2nd is the query options, otherwise, it has a single argument, which is the query options. */
31
- const [variables, options] = this.fn.length > 1
32
- ? args as [Variables, UseQueryObjectOptions<Result> | undefined]
33
- : [undefined, args[0] as UseQueryObjectOptions<Result> | undefined]
34
- const use = suspense ? useSuspenseQuery : useQuery
35
- return use({
36
- ...options,
37
- // without this, this lib won't work in React's strict mode, since every request will be immediately aborted (component unmounted).
38
- gcTime: 60000,
39
- queryKey: this.getKey(variables),
40
- queryFn: ({ signal }) => this.callFn(variables, signal),
41
- }, queryClient) as T extends true ? UseSuspenseQueryResult<Result, StackspotAPIError> : DefinedUseQueryResult<Result, StackspotAPIError>
42
- }
43
-
44
- useQuery(
45
- ...args: Variables extends void
46
- ? [options?: UseQueryObjectOptions<Result>]
47
- : [variables: Variables, options?: UseQueryObjectOptions<Result>]
48
- ) {
49
- const result = this.#useQueryResult(true, ...args)
50
- return result.data
51
- }
52
-
53
- useStatefulQuery(
54
- ...args: Variables extends void
55
- ? [options?: UseQueryObjectOptions<Result>]
56
- : [variables: Variables, options?: UseQueryObjectOptions<Result>]
57
- ) {
58
- const result = this.#useQueryResult(false, ...args)
59
- return [result.data, result.isPending, result.error as StackspotAPIError | undefined, result] as const
60
- }
61
-
62
- invalidate(variables?: Partial<Variables>) {
63
- return queryClient.invalidateQueries({ queryKey: this.getKey(variables) })
64
- }
65
-
66
- getKey(variables?: Partial<Variables>) {
67
- const key: any[] = [this.apiName, this.fn.name]
68
- if (variables) key.push(variables)
69
- return key
70
- }
71
-
72
- cancel(variables?: Partial<Variables>) {
73
- queryClient.cancelQueries({ queryKey: this.getKey(variables) })
74
- }
75
- }
1
+ /* eslint-disable react-hooks/rules-of-hooks */
2
+
3
+ import { DefinedUseQueryResult, UseSuspenseQueryResult, useQuery, useSuspenseQuery } from '@tanstack/react-query'
4
+ import { StackspotAPIError } from '../error/StackspotAPIError'
5
+ import { AutoOperation } from './AutoOperation'
6
+ import { queryClient } from './react-query-client'
7
+ import { AutoQueryObjectParams, QueryObject, UseQueryObjectOptions } from './types'
8
+
9
+ export class AutoQuery<Variables, Result> extends AutoOperation<Variables> implements QueryObject<Variables, Result> {
10
+ constructor(params: AutoQueryObjectParams<Variables, Result>) {
11
+ super(params)
12
+ }
13
+
14
+ query(...[variables]: Variables extends void ? [] : [variables: Variables]) {
15
+ return queryClient.fetchQuery({
16
+ queryKey: this.getKey(variables),
17
+ queryFn: ({ signal }) => this.callFn(variables, signal),
18
+ })
19
+ }
20
+
21
+ #useQueryResult<T extends boolean>(
22
+ suspense: T,
23
+ ...args: Variables extends void
24
+ ? [options?: UseQueryObjectOptions<Result>]
25
+ : [variables: Variables, options?: UseQueryObjectOptions<Result>]
26
+ ) {
27
+ /* `this.fn` is a oazapfts function, i.e. it has arity 1 or 2. If it accepts variables, its arity is 2: the 1st parameter is the
28
+ variables and the 2nd is the RequestOpts. If it doesn't accept variables, its arity is one: it accepts only the RequestOpts.
29
+ We can use this information to determine what the type of `args` actually is at runtime. If variables are accepted, than the 1st
30
+ argument is the variables and the 2nd is the query options, otherwise, it has a single argument, which is the query options. */
31
+ const [variables, options] = this.fn.length > 1
32
+ ? args as [Variables, UseQueryObjectOptions<Result> | undefined]
33
+ : [undefined, args[0] as UseQueryObjectOptions<Result> | undefined]
34
+ const use = suspense ? useSuspenseQuery : useQuery
35
+ return use({
36
+ ...options,
37
+ // without this, this lib won't work in React's strict mode, since every request will be immediately aborted (component unmounted).
38
+ gcTime: 60000,
39
+ queryKey: this.getKey(variables),
40
+ queryFn: ({ signal }) => this.callFn(variables, signal),
41
+ }, queryClient) as T extends true ? UseSuspenseQueryResult<Result, StackspotAPIError> : DefinedUseQueryResult<Result, StackspotAPIError>
42
+ }
43
+
44
+ useQuery(
45
+ ...args: Variables extends void
46
+ ? [options?: UseQueryObjectOptions<Result>]
47
+ : [variables: Variables, options?: UseQueryObjectOptions<Result>]
48
+ ) {
49
+ const result = this.#useQueryResult(true, ...args)
50
+ return result.data
51
+ }
52
+
53
+ useStatefulQuery(
54
+ ...args: Variables extends void
55
+ ? [options?: UseQueryObjectOptions<Result>]
56
+ : [variables: Variables, options?: UseQueryObjectOptions<Result>]
57
+ ) {
58
+ const result = this.#useQueryResult(false, ...args)
59
+ return [result.data, result.isPending, result.error as StackspotAPIError | undefined, result] as const
60
+ }
61
+
62
+ invalidate(variables?: Partial<Variables>) {
63
+ return queryClient.invalidateQueries({ queryKey: this.getKey(variables) })
64
+ }
65
+
66
+ getKey(variables?: Partial<Variables>) {
67
+ const key: any[] = [this.apiName, this.fn.name]
68
+ if (variables) key.push(variables)
69
+ return key
70
+ }
71
+
72
+ cancel(variables?: Partial<Variables>) {
73
+ queryClient.cancelQueries({ queryKey: this.getKey(variables) })
74
+ }
75
+ }