@stack-spot/portal-network 0.1.0 → 0.2.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 (160) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/package.json +7 -6
  3. package/src/apis.json +2 -10
  4. package/src/client/account.ts +44 -2
  5. package/src/error/CanceledError.ts +3 -0
  6. package/src/error/DefaultAPIError.ts +27 -1
  7. package/src/error/StackspotAPIError.ts +33 -1
  8. package/src/network/AutoInfiniteQuery.ts +97 -0
  9. package/src/network/AutoQuery.ts +2 -1
  10. package/src/network/ManualInfiniteQuery.ts +88 -0
  11. package/src/network/ManualQuery.ts +2 -2
  12. package/src/network/NetworkClient.ts +79 -13
  13. package/src/network/ReactQueryNetworkClient.ts +130 -12
  14. package/src/network/react-query-client.ts +3 -0
  15. package/src/network/types.ts +208 -2
  16. package/.generate-api.failed.json +0 -1
  17. package/dist/api/account.d.ts +0 -2368
  18. package/dist/api/account.d.ts.map +0 -1
  19. package/dist/api/account.js +0 -1521
  20. package/dist/api/account.js.map +0 -1
  21. package/dist/api/ai.d.ts +0 -1432
  22. package/dist/api/ai.d.ts.map +0 -1
  23. package/dist/api/ai.js +0 -1342
  24. package/dist/api/ai.js.map +0 -1
  25. package/dist/api/apiRuntime.d.ts +0 -922
  26. package/dist/api/apiRuntime.d.ts.map +0 -1
  27. package/dist/api/apiRuntime.js +0 -599
  28. package/dist/api/apiRuntime.js.map +0 -1
  29. package/dist/api/cloudAccount.d.ts +0 -473
  30. package/dist/api/cloudAccount.d.ts.map +0 -1
  31. package/dist/api/cloudAccount.js +0 -300
  32. package/dist/api/cloudAccount.js.map +0 -1
  33. package/dist/api/cloudServices.d.ts +0 -1233
  34. package/dist/api/cloudServices.d.ts.map +0 -1
  35. package/dist/api/cloudServices.js +0 -715
  36. package/dist/api/cloudServices.js.map +0 -1
  37. package/dist/api/insights.d.ts +0 -123
  38. package/dist/api/insights.d.ts.map +0 -1
  39. package/dist/api/insights.js +0 -112
  40. package/dist/api/insights.js.map +0 -1
  41. package/dist/api/plugin.d.ts +0 -368
  42. package/dist/api/plugin.d.ts.map +0 -1
  43. package/dist/api/plugin.js +0 -218
  44. package/dist/api/plugin.js.map +0 -1
  45. package/dist/api/serviceCatalog.d.ts +0 -737
  46. package/dist/api/serviceCatalog.d.ts.map +0 -1
  47. package/dist/api/serviceCatalog.js +0 -611
  48. package/dist/api/serviceCatalog.js.map +0 -1
  49. package/dist/api/workflows.d.ts +0 -366
  50. package/dist/api/workflows.d.ts.map +0 -1
  51. package/dist/api/workflows.js +0 -175
  52. package/dist/api/workflows.js.map +0 -1
  53. package/dist/api/workspace.js +0 -1476
  54. package/dist/api/workspace.js.map +0 -1
  55. package/dist/api/workspaceManager.d.ts +0 -1121
  56. package/dist/api/workspaceManager.d.ts.map +0 -1
  57. package/dist/api/workspaceManager.js +0 -357
  58. package/dist/api/workspaceManager.js.map +0 -1
  59. package/dist/api/workspaceSearchEngine.d.ts +0 -93
  60. package/dist/api/workspaceSearchEngine.d.ts.map +0 -1
  61. package/dist/api/workspaceSearchEngine.js +0 -55
  62. package/dist/api/workspaceSearchEngine.js.map +0 -1
  63. package/dist/apis.json +0 -129
  64. package/dist/client/account.d.ts +0 -46
  65. package/dist/client/account.d.ts.map +0 -1
  66. package/dist/client/account.js +0 -104
  67. package/dist/client/account.js.map +0 -1
  68. package/dist/error/CanceledError.d.ts +0 -5
  69. package/dist/error/CanceledError.d.ts.map +0 -1
  70. package/dist/error/CanceledError.js +0 -7
  71. package/dist/error/CanceledError.js.map +0 -1
  72. package/dist/error/DefaultAPIError.d.ts +0 -9
  73. package/dist/error/DefaultAPIError.d.ts.map +0 -1
  74. package/dist/error/DefaultAPIError.js +0 -58
  75. package/dist/error/DefaultAPIError.js.map +0 -1
  76. package/dist/error/StackspotAPIError.d.ts +0 -19
  77. package/dist/error/StackspotAPIError.d.ts.map +0 -1
  78. package/dist/error/StackspotAPIError.js +0 -39
  79. package/dist/error/StackspotAPIError.js.map +0 -1
  80. package/dist/error/dictionary/account.d.ts +0 -55
  81. package/dist/error/dictionary/account.d.ts.map +0 -1
  82. package/dist/error/dictionary/account.js +0 -55
  83. package/dist/error/dictionary/account.js.map +0 -1
  84. package/dist/error/dictionary/action.d.ts +0 -163
  85. package/dist/error/dictionary/action.d.ts.map +0 -1
  86. package/dist/error/dictionary/action.js +0 -163
  87. package/dist/error/dictionary/action.js.map +0 -1
  88. package/dist/error/dictionary/base.d.ts +0 -21
  89. package/dist/error/dictionary/base.d.ts.map +0 -1
  90. package/dist/error/dictionary/base.js +0 -21
  91. package/dist/error/dictionary/base.js.map +0 -1
  92. package/dist/error/dictionary/cnt-fields.d.ts +0 -13
  93. package/dist/error/dictionary/cnt-fields.d.ts.map +0 -1
  94. package/dist/error/dictionary/cnt-fields.js +0 -13
  95. package/dist/error/dictionary/cnt-fields.js.map +0 -1
  96. package/dist/error/dictionary/cnt.d.ts +0 -79
  97. package/dist/error/dictionary/cnt.d.ts.map +0 -1
  98. package/dist/error/dictionary/cnt.js +0 -79
  99. package/dist/error/dictionary/cnt.js.map +0 -1
  100. package/dist/error/dictionary/rte.d.ts +0 -23
  101. package/dist/error/dictionary/rte.d.ts.map +0 -1
  102. package/dist/error/dictionary/rte.js +0 -23
  103. package/dist/error/dictionary/rte.js.map +0 -1
  104. package/dist/error/dictionary/rtm.d.ts +0 -9
  105. package/dist/error/dictionary/rtm.d.ts.map +0 -1
  106. package/dist/error/dictionary/rtm.js +0 -9
  107. package/dist/error/dictionary/rtm.js.map +0 -1
  108. package/dist/error/dictionary/workspace-fields.d.ts +0 -9
  109. package/dist/error/dictionary/workspace-fields.d.ts.map +0 -1
  110. package/dist/error/dictionary/workspace-fields.js +0 -9
  111. package/dist/error/dictionary/workspace-fields.js.map +0 -1
  112. package/dist/error/dictionary/workspace.d.ts +0 -99
  113. package/dist/error/dictionary/workspace.d.ts.map +0 -1
  114. package/dist/error/dictionary/workspace.js +0 -99
  115. package/dist/error/dictionary/workspace.js.map +0 -1
  116. package/dist/index.d.ts +0 -6
  117. package/dist/index.d.ts.map +0 -1
  118. package/dist/index.js +0 -6
  119. package/dist/index.js.map +0 -1
  120. package/dist/network/AutoMutation.d.ts +0 -10
  121. package/dist/network/AutoMutation.d.ts.map +0 -1
  122. package/dist/network/AutoMutation.js +0 -20
  123. package/dist/network/AutoMutation.js.map +0 -1
  124. package/dist/network/AutoOperation.d.ts +0 -19
  125. package/dist/network/AutoOperation.d.ts.map +0 -1
  126. package/dist/network/AutoOperation.js +0 -99
  127. package/dist/network/AutoOperation.js.map +0 -1
  128. package/dist/network/AutoQuery.d.ts +0 -19
  129. package/dist/network/AutoQuery.d.ts.map +0 -1
  130. package/dist/network/AutoQuery.js +0 -70
  131. package/dist/network/AutoQuery.js.map +0 -1
  132. package/dist/network/ManualMutation.d.ts +0 -11
  133. package/dist/network/ManualMutation.d.ts.map +0 -1
  134. package/dist/network/ManualMutation.js +0 -32
  135. package/dist/network/ManualMutation.js.map +0 -1
  136. package/dist/network/ManualOperation.d.ts +0 -13
  137. package/dist/network/ManualOperation.d.ts.map +0 -1
  138. package/dist/network/ManualOperation.js +0 -53
  139. package/dist/network/ManualOperation.js.map +0 -1
  140. package/dist/network/ManualQuery.d.ts +0 -20
  141. package/dist/network/ManualQuery.d.ts.map +0 -1
  142. package/dist/network/ManualQuery.js +0 -77
  143. package/dist/network/ManualQuery.js.map +0 -1
  144. package/dist/network/NetworkClient.d.ts +0 -20
  145. package/dist/network/NetworkClient.d.ts.map +0 -1
  146. package/dist/network/NetworkClient.js +0 -85
  147. package/dist/network/NetworkClient.js.map +0 -1
  148. package/dist/network/ReactQueryNetworkClient.d.ts +0 -16
  149. package/dist/network/ReactQueryNetworkClient.d.ts.map +0 -1
  150. package/dist/network/ReactQueryNetworkClient.js +0 -125
  151. package/dist/network/ReactQueryNetworkClient.js.map +0 -1
  152. package/dist/network/react-query-client.d.ts +0 -3
  153. package/dist/network/react-query-client.d.ts.map +0 -1
  154. package/dist/network/react-query-client.js +0 -3
  155. package/dist/network/react-query-client.js.map +0 -1
  156. package/dist/network/types.d.ts +0 -55
  157. package/dist/network/types.d.ts.map +0 -1
  158. package/dist/network/types.js +0 -2
  159. package/dist/network/types.js.map +0 -1
  160. package/src/api/plugin.ts +0 -685
@@ -2,24 +2,31 @@
2
2
 
3
3
  import { Defaults, HttpError, RequestOpts } from '@oazapfts/runtime'
4
4
  import { StackspotAPIError } from '../error/StackspotAPIError'
5
+ import { AutoInfiniteQuery } from './AutoInfiniteQuery'
5
6
  import { AutoMutation } from './AutoMutation'
6
7
  import { AutoQuery } from './AutoQuery'
8
+ import { ManualInfiniteQuery } from './ManualInfiniteQuery'
7
9
  import { ManualMutation } from './ManualMutation'
8
10
  import { ManualQuery } from './ManualQuery'
9
11
  import { NetworkClient } from './NetworkClient'
10
- import { Env, HTTPMethod, MutationObject, OperationConfig, OperationObject, QueryObject } from './types'
12
+ import { Env, HTTPMethod, InfiniteQueryConfig, InfiniteQueryObject, InfiniteQueryOptions, MutationObject, OperationConfig, OperationObject, QueryObject } from './types'
11
13
 
12
14
  const DUMMY_ULID = '01HSTZ5471CBG4AW9BFJ0VTVG9'
13
15
  const PERMISSION_ERROR = 'permission-error'
16
+ export const DEFAULT_PAGE_SIZE = 20
14
17
 
18
+ /**
19
+ * A Network Client that uses oazapfts for making requests and React Query for managing responses.
20
+ */
15
21
  export abstract class ReactQueryNetworkClient extends NetworkClient {
22
+ /**
23
+ * @param baseURL An object with the keys "dev", "stg" and "prd". The values must be the url for each of these environments.
24
+ * @param defaults the `default` object of the API this client is for (provided by the code generated by oazapfts).
25
+ */
16
26
  constructor(baseURL: Record<Env, string>, defaults: Defaults<any>) {
17
27
  super(baseURL)
18
28
  defaults.baseUrl = ''
19
- // eslint-disable-next-line arrow-body-style
20
- defaults.fetch = (...args) => {
21
- return this.#onFetch(...args)
22
- }
29
+ defaults.fetch = (...args) => this.#onFetch(...args)
23
30
  }
24
31
 
25
32
  #getMappedHeaders(headers: HeadersInit | undefined) {
@@ -77,9 +84,13 @@ export abstract class ReactQueryNetworkClient extends NetworkClient {
77
84
  const [path, method, body, headers] = input instanceof Request
78
85
  ? [input.url, input.method as HTTPMethod, input.body, input.headers]
79
86
  : [`${input}`, (init?.method?.toLowerCase() || 'get') as HTTPMethod, init?.body, init?.headers]
80
- return this[method](path, { body: body as any, headers: this.#getMappedHeaders(headers) })
87
+ return this[method](path, { body: body as any, headers: this.#getMappedHeaders(headers), signal: init?.signal })
81
88
  }
82
89
 
90
+ /**
91
+ * Receives an HttpError and returns a StackspotAPIError.
92
+ * @param error the original HttpError created by oazapfts.
93
+ */
83
94
  protected abstract buildStackSpotError(error: HttpError): StackspotAPIError
84
95
 
85
96
  async #onFetchPermission(input: string | URL | Request, init?: RequestInit | undefined): Promise<Response> {
@@ -97,15 +108,27 @@ export abstract class ReactQueryNetworkClient extends NetworkClient {
97
108
  }
98
109
  }
99
110
 
100
- protected query<Args extends [opts?: RequestOpts] | [variables: Record<string, any>, opts?: RequestOpts], Result>(
101
- fn: (...args: Args) => Promise<Result>
102
- ): Args extends [variables: infer Variables, opts?: RequestOpts] ? QueryObject<Variables, Result> : QueryObject<void, Result>
111
+ /**
112
+ * Builds a query manually by using a configuration object.
113
+ * @param config the configuration object containing the name, a request function and a permission function.
114
+ */
103
115
  protected query<Args extends [AbortSignal, Record<string, any>] | [AbortSignal], Result>(
104
116
  config: OperationConfig<Args, Result>,
105
117
  ): QueryObject<Args extends [AbortSignal] ? void : Args[1], Result>
118
+ /**
119
+ * Builds a query manually by using a configuration object.
120
+ * @param config the configuration object containing the name and a request function.
121
+ */
106
122
  protected query<Args extends [AbortSignal, Record<string, any>] | [AbortSignal], Result>(
107
123
  config: Omit<OperationConfig<Args, Result>, 'permission'>,
108
124
  ): Omit<QueryObject<Args extends [AbortSignal] ? void : Args[1], Result>, 'isAllowed' | 'useAllowed' | 'getPermissionKey'>
125
+ /**
126
+ * Builds a query automatically by using a function generated by oazapfts.
127
+ * @param fn the oazapfts function.
128
+ */
129
+ protected query<Args extends [opts?: RequestOpts] | [variables: Record<string, any>, opts?: RequestOpts], Result>(
130
+ fn: (...args: Args) => Promise<Result>
131
+ ): Args extends [variables: infer Variables, opts?: RequestOpts] ? QueryObject<Variables, Result> : QueryObject<void, Result>
109
132
  protected query(fnOrConfig: any): QueryObject<any, any> {
110
133
  return typeof fnOrConfig === 'function'
111
134
  ? new AutoQuery(
@@ -124,15 +147,110 @@ export abstract class ReactQueryNetworkClient extends NetworkClient {
124
147
  })
125
148
  }
126
149
 
127
- protected mutation<Args extends [opts?: RequestOpts] | [variables: Record<string, any>, opts?: RequestOpts], Result>(
128
- fn: (...args: Args) => Promise<Result>,
129
- ): Args extends [variables: infer Variables, opts?: RequestOpts] ? MutationObject<Variables, Result> : MutationObject<void, Result>
150
+ /**
151
+ * Builds a query manually by using a configuration object.
152
+ * @param config the configuration object containing the name, a request function and a permission function.
153
+ */
154
+ protected infiniteQuery<
155
+ Variables extends Record<string, any>,
156
+ Result,
157
+ PageParamName extends keyof Variables,
158
+ Accumulator extends keyof Result | '',
159
+ >(config: InfiniteQueryConfig<Variables, Result, PageParamName, Accumulator>): InfiniteQueryObject<Variables, Result, Accumulator>
160
+ /**
161
+ * Builds a query manually by using a configuration object.
162
+ * @param config the configuration object containing the name and a request function.
163
+ */
164
+ protected infiniteQuery<
165
+ Variables extends Record<string, any>,
166
+ Result,
167
+ PageParamName extends keyof Variables,
168
+ Accumulator extends keyof Result | '',
169
+ >(
170
+ config: Omit<InfiniteQueryConfig<Variables, Result, PageParamName, Accumulator>, 'permission'>,
171
+ ): Omit<InfiniteQueryObject<Variables, Result, Accumulator>, 'isAllowed' | 'useAllowed' | 'getPermissionKey'>
172
+ /**
173
+ * Builds a query automatically by using a function generated by oazapfts with the variables `page` and `size`.
174
+ * @param fn the oazapfts function that returns an array.
175
+ * @param options optional configuration for the infinite query. By default, it will use the variables page and size of the function
176
+ * passed in the first parameter.
177
+ */
178
+ protected infiniteQuery<
179
+ Variables extends { page?: number, size?: number },
180
+ Result extends any[],
181
+ PageParamName extends keyof Variables = 'page',
182
+ Accumulator extends keyof Result | '' = '',
183
+ >(
184
+ fn: (variables: Variables, opts?: RequestOpts) => Promise<Result>,
185
+ options?: Partial<InfiniteQueryOptions<Variables, Result, PageParamName, Accumulator>>,
186
+ ): InfiniteQueryObject<Variables, Result, Accumulator>
187
+ /**
188
+ * Builds a query automatically by using a function generated by oazapfts.
189
+ * @param fn the oazapfts function.
190
+ * @param options the configuration for the infinite query.
191
+ */
192
+ protected infiniteQuery<
193
+ Variables extends Record<string, any>,
194
+ Result,
195
+ PageParamName extends keyof Variables,
196
+ Accumulator extends keyof Result,
197
+ >(
198
+ fn: (variables: Variables, opts?: RequestOpts) => Promise<Result>,
199
+ options: InfiniteQueryOptions<Variables, Result, PageParamName, Accumulator>,
200
+ ): InfiniteQueryObject<Variables, Result, Accumulator>
201
+ protected infiniteQuery(fnOrConfig: any, options?: InfiniteQueryOptions<any, any, any, any>): InfiniteQueryObject<any, any, any> {
202
+ return typeof fnOrConfig === 'function'
203
+ ? new AutoInfiniteQuery(
204
+ {
205
+ apiName: this.constructor.name,
206
+ fn: fnOrConfig,
207
+ onFetchPermission: (...args) => this.#onFetchPermission(...args),
208
+ transformError: error => this.#transformError(error),
209
+ },
210
+ {
211
+ accumulator: options?.accumulator ?? '',
212
+ pageParamName: options?.pageParamName ?? 'page',
213
+ initialPageParam: options?.initialPageParam ?? 1,
214
+ defaultVariables: options?.defaultVariables ?? { size: DEFAULT_PAGE_SIZE },
215
+ getNextPageParam: options?.getNextPageParam ?? (({ variables, lastPage, lastPageParam }) => {
216
+ const size = variables.size ?? 1
217
+ const isLastPageTheLast = lastPage && (
218
+ (Array.isArray(lastPage) && lastPage.length < size)
219
+ || (options?.accumulator !== undefined && options?.accumulator !== '' && lastPage[options?.accumulator]?.length < size)
220
+ )
221
+ return isLastPageTheLast ? undefined : lastPageParam + 1
222
+ }),
223
+ },
224
+ )
225
+ : new ManualInfiniteQuery({
226
+ ...fnOrConfig,
227
+ apiName: this.constructor.name,
228
+ request: this.#withErrorTreatment(fnOrConfig.request),
229
+ permission: fnOrConfig.permission ? this.#withErrorTreatment(fnOrConfig.permission) : undefined,
230
+ })
231
+ }
232
+
233
+ /**
234
+ * Builds a mutation manually by using a configuration object.
235
+ * @param config the configuration object containing the name, a request function and a permission function.
236
+ */
130
237
  protected mutation<Args extends [AbortSignal, Record<string, any>] | [AbortSignal], Result>(
131
238
  config: OperationConfig<Args, Result>,
132
239
  ): MutationObject<Args extends [AbortSignal] ? void : Args[1], Result>
240
+ /**
241
+ * Builds a mutation manually by using a configuration object.
242
+ * @param config the configuration object containing the name and a request function.
243
+ */
133
244
  protected mutation<Args extends [AbortSignal, Record<string, any>] | [AbortSignal], Result>(
134
245
  config: Omit<OperationConfig<Args, Result>, 'permission'>,
135
246
  ): Omit<MutationObject<Args extends [AbortSignal] ? void : Args[1], Result>, keyof OperationObject<any>>
247
+ /**
248
+ * Builds a mutation automatically by using a function generated by oazapfts.
249
+ * @param fn the oazapfts function.
250
+ */
251
+ protected mutation<Args extends [opts?: RequestOpts] | [variables: Record<string, any>, opts?: RequestOpts], Result>(
252
+ fn: (...args: Args) => Promise<Result>,
253
+ ): Args extends [variables: infer Variables, opts?: RequestOpts] ? MutationObject<Variables, Result> : MutationObject<void, Result>
136
254
  protected mutation(fnOrConfig: any): MutationObject<any, any> {
137
255
  if (typeof fnOrConfig === 'function') {
138
256
  return new AutoMutation(
@@ -1,3 +1,6 @@
1
1
  import { QueryClient } from '@tanstack/react-query'
2
2
 
3
+ /**
4
+ * The global, unique, Query Client for React Query.
5
+ */
3
6
  export const queryClient = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false, retry: 3 } } })
@@ -1,6 +1,6 @@
1
1
  import type { RequestOpts } from '@oazapfts/runtime'
2
2
  import { Session } from '@stack-spot/auth'
3
- import { MutateOptions, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult } from '@tanstack/react-query'
3
+ import { InfiniteData, MutateOptions, UseInfiniteQueryOptions, UseInfiniteQueryResult, UseMutationOptions, UseMutationResult, UseQueryOptions, UseQueryResult } from '@tanstack/react-query'
4
4
  import { StackspotAPIError } from '../error/StackspotAPIError'
5
5
 
6
6
  export interface SessionManager {
@@ -12,6 +12,7 @@ export interface SessionManager {
12
12
  export interface RequestOptions {
13
13
  params?: Record<string, string | undefined>,
14
14
  headers?: Record<string, string>,
15
+ signal?: AbortSignal | null,
15
16
  }
16
17
 
17
18
  export interface RequestWithBody extends RequestOptions {
@@ -23,44 +24,233 @@ export type Env = 'dev' | 'stg' | 'prd'
23
24
  export type HTTPMethod = 'post' | 'patch' | 'delete' | 'put' | 'get'
24
25
 
25
26
  export interface OperationConfig<Args extends [AbortSignal, Record<string, any>] | [AbortSignal], Result> {
27
+ /**
28
+ * The operation name. To be used as key in React Query.
29
+ */
26
30
  name: string,
31
+ /**
32
+ * The function to run once the operation is called.
33
+ * @param abortSignal the AbortSignal to pass to any request made.
34
+ * @param variables if this operation accepts variables, the second parameter is the variables object.
35
+ * @returns the operation result (this should not be a Response object, but it could be its body).
36
+ */
27
37
  request: (...args: Args) => Promise<Result>,
38
+ /**
39
+ * The function to run once one asks if the operation is allowed.
40
+ * @param variables if this operation accepts variables, the single parameter is the variables object.
41
+ * @returns a promise that resolves to true if the operation is allowed and false otherwise.
42
+ */
28
43
  permission: (...args: Args extends [AbortSignal] ? [] : [Args[1]]) => Promise<boolean>,
29
44
  }
30
45
 
46
+
47
+ export interface InfiniteQueryOptions<Variables, Result, PageParamName extends keyof Variables, Accumulator extends keyof Result | ''> {
48
+ /**
49
+ * The name of the variable that controls the current page.
50
+ */
51
+ pageParamName: PageParamName,
52
+ /**
53
+ * The name of the property in the Result that contains the items of a page. If the result itself is the array of items, use an empty
54
+ * string ('').
55
+ */
56
+ accumulator?: Accumulator,
57
+ /**
58
+ * The initial value for page variable.
59
+ */
60
+ initialPageParam: Variables[PageParamName],
61
+ /**
62
+ * Some defaults to be used when a variable is not specified.
63
+ */
64
+ defaultVariables?: Partial<Variables>,
65
+ /**
66
+ * A function that determines the value of the page variable in order to get the next page.
67
+ * @parar context the current context with the variables and page details.
68
+ * @returns undefined or null if there are no more pages. The next value for the page variable otherwise.
69
+ */
70
+ getNextPageParam: (context: {
71
+ /**
72
+ * the original set of variables passed to the hook `useInfiniteQuery`.
73
+ */
74
+ variables: Variables,
75
+ /**
76
+ * the last page Result.
77
+ */
78
+ lastPage: Result,
79
+ /**
80
+ * the Results of each page.
81
+ */
82
+ allPages: Result[],
83
+ /**
84
+ * the value of the page variable used to retrieve the last page.
85
+ */
86
+ lastPageParam: Variables[PageParamName],
87
+ /**
88
+ * the values of the page variable for requesting each of the pages.
89
+ */
90
+ allPageParams: Variables[PageParamName][],
91
+ }) => Variables[PageParamName] | undefined | null,
92
+ }
93
+
31
94
  export interface FullOperationConfig<
32
95
  Args extends [AbortSignal, Record<string, any>] | [AbortSignal], Result
33
96
  > extends OperationConfig<Args, Result> {
97
+ /**
98
+ * The name of the API where this operation is called: used for composing a query key.
99
+ */
34
100
  apiName: string,
35
101
  }
36
102
 
103
+ export type InfiniteQueryConfig<
104
+ Variables extends Record<string, any>,
105
+ Result,
106
+ PageParamName extends keyof Variables,
107
+ Accumulator extends keyof Result | '',
108
+ > = OperationConfig<[AbortSignal, Variables], Result> & InfiniteQueryOptions<Variables, Result, PageParamName, Accumulator>
109
+
37
110
  export interface OperationObject<Variables> {
111
+ /**
112
+ * Checks if this operation is allowed for the user currently logged in.
113
+ * @param variables the variables for the operation, if a required url parameter is not provided, it's replaced by a default string.
114
+ * @returns a promise that resolves to true if the operation is allowed or false otherwise.
115
+ * @throws `StackspotAPIError`
116
+ */
38
117
  isAllowed: (...args: Variables extends void ? [] : [variables?: Partial<Variables>]) => Promise<boolean>,
118
+ /**
119
+ * Same as `isAllowed`, but a React Hook instead.
120
+ * @param args if this operation accepts variables, the first argument should be the variables and the second the query options
121
+ * (optional). If it doesn't accept variables, the single optional argument must be the query options. Moreover, if a required url
122
+ * parameter is not provided, it's replaced by a default string.
123
+ * @returns an array with 4 items:
124
+ * - [0]: true if allowed, false if not allowed, undefined if not fetched.
125
+ * - [1]: true if pending, false otherwise.
126
+ * - [2]: a StackspotAPIError if an error happens, null or undefined otherwise.
127
+ * - [3]: the original UseQueryResult object, from ReactQuery.
128
+ */
39
129
  useAllowed: (
40
130
  ...args: Variables extends void
41
131
  ? [options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
42
132
  : [variables?: Partial<Variables>, options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
43
133
  ) => Readonly<[boolean | undefined, boolean, StackspotAPIError | null | undefined, UseQueryResult<boolean, StackspotAPIError>]>,
134
+ /**
135
+ * Gets the key for the query used to verify if this operation is allowed.
136
+ *
137
+ * Attention: invalidating this key won't clear the cache, since the `@stack-spot/opa` library doesn't support individual cache
138
+ * invalidation.
139
+ * @param args if this operation accepts variables, the first argument should be the variables and the second the query options
140
+ * (optional). If it doesn't accept variables, the single optional argument must be the query options. Moreover, if a required url
141
+ * parameter is not provided, it's replaced by a default string.
142
+ * @returns the query key.
143
+ */
44
144
  getPermissionKey: (...args: Variables extends void ? [] : [variables?: Partial<Variables>]) => any[],
145
+ /**
146
+ * Cancels the request if it hasn't finished yet.
147
+ *
148
+ * A canceled request will fail by throwing a `CanceledError`.
149
+ *
150
+ * @param variables the variables for the operation. If not provided, this will cancel all requests in progress for this operation.
151
+ * @returns true if any request has been canceled, false otherwise.
152
+ */
45
153
  cancel: (variables?: Partial<Variables>) => boolean,
46
154
  }
47
155
 
48
156
  export interface QueryObject<Variables, Result> extends OperationObject<Variables> {
157
+ /**
158
+ * Runs the query operation.
159
+ * @param variables the request variables if this query accepts variables.
160
+ * @returns a Promise with the response data
161
+ * @throws `StackspotAPIError`
162
+ */
49
163
  query: (...args: Variables extends void ? [] : [variables: Variables]) => Promise<Result>,
164
+ /**
165
+ * Same as `query`, but, instead of an async function, this is a React Hook that expects a React Suspense environment.
166
+ *
167
+ * If you want a hook that performs a stateful query instead of relying in Suspense, call `useStatefulQuery` instead.
168
+ *
169
+ * @param args if this query accepts variables, the first argument should be the variables and the second the query options
170
+ * (optional). If it doesn't accept variables, the single optional argument must be the query options.
171
+ * @returns the response data or undefined if not fetched.
172
+ * @throws `StackspotAPIError`
173
+ * @throws `Promise<Result>`
174
+ */
50
175
  useQuery: (
51
176
  ...args: Variables extends void
52
177
  ? [options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
53
178
  : [variables: Variables, options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
54
179
  ) => Result | undefined,
180
+ /**
181
+ * Same as `query`, but a React Hook instead.
182
+ * @param args if this query accepts variables, the first argument should be the variables and the second the query options
183
+ * (optional). If it doesn't accept variables, the single optional argument must be the query options.
184
+ * @returns an array with 4 items:
185
+ * - [0]: the response data or undefined if not fetched.
186
+ * - [1]: true if pending, false otherwise.
187
+ * - [2]: a StackspotAPIError if an error happens, null or undefined otherwise.
188
+ * - [3]: the original UseQueryResult object, from ReactQuery.
189
+ */
55
190
  useStatefulQuery: (
56
191
  ...args: Variables extends void
57
192
  ? [options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
58
193
  : [variables: Variables, options?: Omit<UseQueryOptions, 'queryFn' | 'queryKey'>]
59
194
  ) => Readonly<[Result | undefined, boolean, StackspotAPIError | null | undefined, UseQueryResult<Result, StackspotAPIError>]>,
195
+ /**
196
+ * Invalidates the cache for this query. A specific query can be invalidated by passing an argument (variables).
197
+ * @param variables if this query accepts variables, a query with specific variables can be invalidated.
198
+ * @returns a promise that resolves once the cache is invalidated.
199
+ */
60
200
  invalidate: (...args: Variables extends void ? [] : [variables?: Partial<Variables>]) => Promise<void>,
201
+ /**
202
+ * Gets the key for this query.
203
+ * @param variables the variables for the query.
204
+ * @returns the query key.
205
+ */
61
206
  getKey: (...args: Variables extends void ? [] : [variables?: Partial<Variables>]) => any[],
62
207
  }
63
208
 
209
+ export type UseInfiniteQueryObjectOptions = Omit<UseInfiniteQueryOptions, 'queryFn' | 'queryKey' | 'getNextPageParam' | 'initialPageParam'>
210
+
211
+ export interface InfiniteQueryObject<Variables, Result, Accumulator extends keyof Result | ''> extends QueryObject<Variables, Result> {
212
+ /**
213
+ * Builds up a list with the result of multiple pages. This relies on React Suspense for error and first-loading feedbacks.
214
+ *
215
+ * Equivalent to React Query's `useSuspenseInfiniteQuery`.
216
+ * @param variables the variables for the query.
217
+ * @param options the options for the query.
218
+ * @returns an array with 2 items:
219
+ * - [0]: the list of items.
220
+ * - [1]: the original UseInfiniteQueryResult object, from ReactQuery.
221
+ */
222
+ useInfiniteQuery: (
223
+ ...args: Partial<Variables> extends Variables
224
+ ? [variables?: Variables, options?: UseInfiniteQueryObjectOptions]
225
+ : [variables: Variables, options?: UseInfiniteQueryObjectOptions]
226
+ ) => Readonly<[
227
+ (Accumulator extends keyof Result ? Result[Accumulator] : Result) | undefined,
228
+ UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
229
+ ]>,
230
+ /**
231
+ * Builds up a list with the result of multiple pages.
232
+ *
233
+ * Equivalent to React Query's `useInfiniteQuery`.
234
+ * @param variables the variables for the query.
235
+ * @param options the options for the query.
236
+ * @returns an array with 4 items:
237
+ * - [0]: the list of items.
238
+ * - [1]: true if fetching the first page, false otherwise.
239
+ * - [2]: a StackspotAPIError if an error happens, null or undefined otherwise.
240
+ * - [3]: the original UseInfiniteQueryResult object, from ReactQuery.
241
+ */
242
+ useStatefulInfiniteQuery: (
243
+ ...args: Partial<Variables> extends Variables
244
+ ? [variables?: Variables, options?: Omit<UseInfiniteQueryOptions, 'queryFn' | 'queryKey' | 'getNextPageParam' | 'initialPageParam'>]
245
+ : [variables: Variables, options?: Omit<UseInfiniteQueryOptions, 'queryFn' | 'queryKey' | 'getNextPageParam' | 'initialPageParam'>]
246
+ ) => Readonly<[
247
+ (Accumulator extends keyof Result ? Result[Accumulator] : Result) | undefined,
248
+ boolean,
249
+ StackspotAPIError | undefined | null,
250
+ UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
251
+ ]>,
252
+ }
253
+
64
254
  export interface AutoQueryObjectParams<Variables, Result> {
65
255
  apiName: string,
66
256
  fn: ((variables: Variables, opts?: RequestOpts) => Promise<Result>) | ((opts?: RequestOpts) => Promise<Result>),
@@ -69,14 +259,30 @@ export interface AutoQueryObjectParams<Variables, Result> {
69
259
  }
70
260
 
71
261
  export interface MutationObject<Variables, Result> extends OperationObject<Variables> {
262
+ /**
263
+ * Runs the mutation and returns a promise.
264
+ * @param args the variables for the mutation.
265
+ * @returns a promise that resolves to the response's data.
266
+ */
72
267
  mutate: (...args: Variables extends void ? [] : [variables: Variables]) => Promise<Result>,
268
+ /**
269
+ * A React hook equivalent to React Query's `useMutation`. It creates a mutation function and the mutation states.
270
+ * @param options the options for `useMutation`
271
+ * @returns an array with 4 items:
272
+ * - [0]: the mutation function (equivalent to `mutateAsync` from the result of a `useMutation`).
273
+ * - [1]: true if pending, false otherwise.
274
+ * - [2]: a StackspotAPIError if an error happens, undefined otherwise.
275
+ * - [3]: the original UseMutationResult object, from ReactQuery.
276
+ *
277
+ */
73
278
  useMutation: (options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'>) =>
74
279
  Readonly<[
75
280
  (...args: Variables extends void
76
281
  ? [options?: MutateOptions<Result, StackspotAPIError>]
77
282
  : [variables: Variables, options?: MutateOptions<Result, StackspotAPIError, Variables>]
78
283
  ) => Promise<Result>,
79
- boolean, StackspotAPIError | undefined,
284
+ boolean,
285
+ StackspotAPIError | undefined,
80
286
  UseMutationResult<Result, StackspotAPIError, Variables>,
81
287
  ]>,
82
288
  }
@@ -1 +0,0 @@
1
- ["apiManagement","content","dataPlatform"]