@stack-spot/portal-network 0.45.1 → 0.46.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/CHANGELOG.md +7 -0
- package/dist/api/ai.d.ts +60 -34
- package/dist/api/ai.d.ts.map +1 -1
- package/dist/api/ai.js +38 -0
- package/dist/api/ai.js.map +1 -1
- package/dist/client/ai.d.ts +21 -0
- package/dist/client/ai.d.ts.map +1 -1
- package/dist/client/ai.js +19 -1
- package/dist/client/ai.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/network/AutoInfiniteQuery.d.ts +2 -0
- package/dist/network/AutoInfiniteQuery.d.ts.map +1 -1
- package/dist/network/AutoInfiniteQuery.js +11 -8
- package/dist/network/AutoInfiniteQuery.js.map +1 -1
- package/dist/network/AutoMutation.d.ts +4 -2
- package/dist/network/AutoMutation.d.ts.map +1 -1
- package/dist/network/AutoMutation.js +5 -3
- package/dist/network/AutoMutation.js.map +1 -1
- package/dist/network/AutoOperation.d.ts +1 -3
- package/dist/network/AutoOperation.d.ts.map +1 -1
- package/dist/network/AutoOperation.js +4 -31
- package/dist/network/AutoOperation.js.map +1 -1
- package/dist/network/AutoQuery.d.ts +2 -6
- package/dist/network/AutoQuery.d.ts.map +1 -1
- package/dist/network/AutoQuery.js +7 -20
- package/dist/network/AutoQuery.js.map +1 -1
- package/dist/network/ManualInfiniteQuery.d.ts.map +1 -1
- package/dist/network/ManualInfiniteQuery.js +2 -6
- package/dist/network/ManualInfiniteQuery.js.map +1 -1
- package/dist/network/ManualMutation.d.ts +4 -2
- package/dist/network/ManualMutation.d.ts.map +1 -1
- package/dist/network/ManualMutation.js +6 -11
- package/dist/network/ManualMutation.js.map +1 -1
- package/dist/network/ManualOperation.d.ts +0 -2
- package/dist/network/ManualOperation.d.ts.map +1 -1
- package/dist/network/ManualOperation.js +0 -18
- package/dist/network/ManualOperation.js.map +1 -1
- package/dist/network/ManualQuery.d.ts +3 -7
- package/dist/network/ManualQuery.d.ts.map +1 -1
- package/dist/network/ManualQuery.js +7 -26
- package/dist/network/ManualQuery.js.map +1 -1
- package/dist/network/ReactQueryNetworkClient.d.ts.map +1 -1
- package/dist/network/ReactQueryNetworkClient.js +2 -0
- package/dist/network/ReactQueryNetworkClient.js.map +1 -1
- package/dist/network/react-query-client.d.ts.map +1 -1
- package/dist/network/react-query-client.js +9 -1
- package/dist/network/react-query-client.js.map +1 -1
- package/dist/network/types.d.ts +11 -12
- package/dist/network/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/api/ai.ts +108 -34
- package/src/client/ai.ts +6 -0
- package/src/index.ts +1 -0
- package/src/network/AutoInfiniteQuery.ts +13 -7
- package/src/network/AutoMutation.ts +6 -4
- package/src/network/AutoOperation.ts +4 -25
- package/src/network/AutoQuery.ts +8 -16
- package/src/network/ManualInfiniteQuery.ts +2 -5
- package/src/network/ManualMutation.ts +8 -11
- package/src/network/ManualOperation.ts +0 -14
- package/src/network/ManualQuery.ts +9 -22
- package/src/network/ReactQueryNetworkClient.ts +1 -0
- package/src/network/react-query-client.ts +9 -1
- package/src/network/types.ts +9 -12
- package/dist/error/CanceledError.d.ts +0 -8
- package/dist/error/CanceledError.d.ts.map +0 -1
- package/dist/error/CanceledError.js +0 -10
- package/dist/error/CanceledError.js.map +0 -1
- package/src/error/CanceledError.ts +0 -10
package/src/client/ai.ts
CHANGED
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
deleteConversationV1ConversationsConversationIdDelete,
|
|
7
7
|
downloadConversationV1ConversationsConversationIdDownloadGet,
|
|
8
8
|
findKnowledgeObjectByCustomIdV1KnowledgeSourcesSlugObjectsCustomIdGet,
|
|
9
|
+
formatFetchStepV1QuickCommandsSlugStepsStepSlugFetchFormatPost,
|
|
10
|
+
formatResultV1QuickCommandsSlugResultFormatPost,
|
|
9
11
|
getQuickCommandV1QuickCommandsSlugGet,
|
|
10
12
|
HttpValidationError,
|
|
11
13
|
listAiStacksV1AiStacksGet,
|
|
@@ -15,6 +17,7 @@ import {
|
|
|
15
17
|
listKnowledgeSourcesV1KnowledgeSourcesGet,
|
|
16
18
|
postEventV1EventsPost,
|
|
17
19
|
quickActionsV1QuickActionsPost,
|
|
20
|
+
quickCommandsRunV2V2QuickCommandsSlugStepsStepSlugRunPost,
|
|
18
21
|
updateTitleV1ConversationsConversationIdPatch,
|
|
19
22
|
} from '../api/ai'
|
|
20
23
|
import apis from '../apis.json'
|
|
@@ -56,6 +59,9 @@ class AIClient extends ReactQueryNetworkClient {
|
|
|
56
59
|
downloadChat = this.mutation(removeAuthorizationParam(downloadConversationV1ConversationsConversationIdDownloadGet))
|
|
57
60
|
renameChat = this.mutation(removeAuthorizationParam(updateTitleV1ConversationsConversationIdPatch))
|
|
58
61
|
createEvent = this.mutation(removeAuthorizationParam(postEventV1EventsPost))
|
|
62
|
+
fetchStepOfQuickCommand = this.mutation(removeAuthorizationParam(formatFetchStepV1QuickCommandsSlugStepsStepSlugFetchFormatPost))
|
|
63
|
+
llmStepOfQuickCommand = this.mutation(removeAuthorizationParam(quickCommandsRunV2V2QuickCommandsSlugStepsStepSlugRunPost))
|
|
64
|
+
formatResultOfQuickCommand = this.mutation(removeAuthorizationParam(formatResultV1QuickCommandsSlugResultFormatPost))
|
|
59
65
|
|
|
60
66
|
sendChatMessage(request: FixedChatRequest, minChangeIntervalMS?: number): StreamedJson<ChatResponse3> {
|
|
61
67
|
const abortController = new AbortController()
|
package/src/index.ts
CHANGED
|
@@ -32,15 +32,12 @@ export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Va
|
|
|
32
32
|
private createInfiniteQueryFn(
|
|
33
33
|
variables: Variables | undefined,
|
|
34
34
|
): (context: QueryFunctionContext<QueryKey, Variables[PageParamName]>) => Promise<Result> {
|
|
35
|
-
return
|
|
35
|
+
return ({ pageParam, signal }) => {
|
|
36
36
|
const paginatedVariables = {
|
|
37
37
|
...variables,
|
|
38
38
|
[this.options.pageParamName]: pageParam ?? variables?.[this.options.pageParamName],
|
|
39
39
|
} as Variables
|
|
40
|
-
|
|
41
|
-
const result = await this.currentRequests.get(paginatedVariables)
|
|
42
|
-
this.currentRequests.delete(paginatedVariables)
|
|
43
|
-
return result!
|
|
40
|
+
return this.callFn(paginatedVariables, signal)
|
|
44
41
|
}
|
|
45
42
|
}
|
|
46
43
|
|
|
@@ -59,7 +56,7 @@ export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Va
|
|
|
59
56
|
const use = suspense ? useSuspenseInfiniteQuery : useInfiniteQuery
|
|
60
57
|
return use<any, any, any, any, any>({
|
|
61
58
|
...options,
|
|
62
|
-
queryKey:
|
|
59
|
+
queryKey: this.getInfiniteKey(variables),
|
|
63
60
|
queryFn: this.createInfiniteQueryFn(variables),
|
|
64
61
|
initialPageParam: this.options.initialPageParam,
|
|
65
62
|
getNextPageParam: (lastPage, allPages, lastPageParam, allPageParams) =>
|
|
@@ -102,7 +99,16 @@ export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Va
|
|
|
102
99
|
async invalidate(variables?: Partial<Variables> | undefined): Promise<void> {
|
|
103
100
|
await Promise.all([
|
|
104
101
|
super.invalidate(variables),
|
|
105
|
-
queryClient.invalidateQueries({ queryKey:
|
|
102
|
+
queryClient.invalidateQueries({ queryKey: this.getInfiniteKey(variables) }),
|
|
106
103
|
])
|
|
107
104
|
}
|
|
105
|
+
|
|
106
|
+
private getInfiniteKey(variables?: Partial<Variables> | undefined) {
|
|
107
|
+
return ['infinite', ...this.getKey(variables)]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async cancel(variables?: Partial<Variables> | undefined) {
|
|
111
|
+
super.cancel(variables)
|
|
112
|
+
queryClient.cancelQueries({ queryKey: this.getInfiniteKey() })
|
|
113
|
+
}
|
|
108
114
|
}
|
|
@@ -11,14 +11,16 @@ export class AutoMutation<Variables, Result> extends AutoOperation<Variables> im
|
|
|
11
11
|
super(params)
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
mutate(...
|
|
15
|
-
|
|
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)
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
useMutation(options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'>) {
|
|
20
|
+
useMutation(options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'> & { signal?: AbortSignal }) {
|
|
19
21
|
const result = useMutation<Result, StackspotAPIError, Variables>({
|
|
20
22
|
...options,
|
|
21
|
-
mutationFn: variables => this.callFn(variables),
|
|
23
|
+
mutationFn: (variables) => this.callFn(variables, options?.signal),
|
|
22
24
|
}, queryClient)
|
|
23
25
|
return [result.mutateAsync as any, result.isPending, result.error as StackspotAPIError | undefined, result as any] as const
|
|
24
26
|
}
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
import { type RequestOpts } from '@oazapfts/runtime'
|
|
4
4
|
import { UseQueryOptions, UseQueryResult, useQuery } from '@tanstack/react-query'
|
|
5
|
-
import { CanceledError } from '../error/CanceledError'
|
|
6
5
|
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
7
6
|
import { queryClient } from './react-query-client'
|
|
8
7
|
import { AutoQueryObjectParams, OperationObject } from './types'
|
|
@@ -12,7 +11,6 @@ export abstract class AutoOperation<Variables> implements OperationObject<Variab
|
|
|
12
11
|
protected apiName: string
|
|
13
12
|
protected onFetchPermission: typeof fetch
|
|
14
13
|
private transformError?: (error: any) => StackspotAPIError
|
|
15
|
-
private abortMap = new Map<Partial<Variables> | undefined, AbortController[]>()
|
|
16
14
|
|
|
17
15
|
constructor({ apiName, onFetchPermission, fn, transformError }: AutoQueryObjectParams<Variables, any>) {
|
|
18
16
|
this.fn = fn
|
|
@@ -24,29 +22,22 @@ export abstract class AutoOperation<Variables> implements OperationObject<Variab
|
|
|
24
22
|
}
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
protected async callFn(variables: Partial<Variables> | undefined, fetchFn?: typeof fetch) {
|
|
28
|
-
const abortController = new AbortController()
|
|
29
|
-
if (fetchFn !== this.onFetchPermission) {
|
|
30
|
-
if (!this.abortMap.has(variables)) this.abortMap.set(variables, [])
|
|
31
|
-
this.abortMap.get(variables)?.push(abortController)
|
|
32
|
-
}
|
|
25
|
+
protected async callFn(variables: Partial<Variables> | undefined, signal: AbortSignal | undefined, fetchFn?: typeof fetch) {
|
|
33
26
|
const fn = this.fn as (...args: any[]) => Promise<any>
|
|
34
27
|
try {
|
|
35
28
|
const result = await (
|
|
36
29
|
fn.length === 2
|
|
37
|
-
? fn(variables ?? {}, fetchFn ? { fetch: fetchFn, signal:
|
|
38
|
-
: fn(fetchFn ? { fetch: fetchFn, signal:
|
|
30
|
+
? fn(variables ?? {}, fetchFn ? { fetch: fetchFn, signal } : { signal })
|
|
31
|
+
: fn(fetchFn ? { fetch: fetchFn, signal } : { signal })
|
|
39
32
|
)
|
|
40
33
|
return result
|
|
41
34
|
} catch (error) {
|
|
42
35
|
throw this.transformError ? this.transformError(error) : error
|
|
43
|
-
} finally {
|
|
44
|
-
this.abortMap.delete(variables)
|
|
45
36
|
}
|
|
46
37
|
}
|
|
47
38
|
|
|
48
39
|
private createPermissionQueryFn(variables: Partial<Variables> | undefined) {
|
|
49
|
-
return () => this.callFn(variables, this.onFetchPermission)
|
|
40
|
+
return () => this.callFn(variables, undefined, this.onFetchPermission)
|
|
50
41
|
}
|
|
51
42
|
|
|
52
43
|
isAllowed(...[variables]: Variables extends void ? [] : [variables?: Partial<Variables>]) {
|
|
@@ -79,16 +70,4 @@ export abstract class AutoOperation<Variables> implements OperationObject<Variab
|
|
|
79
70
|
getPermissionKey(variables?: Partial<Variables>) {
|
|
80
71
|
return [this.apiName, `${this.fn.name}.permission`, variables]
|
|
81
72
|
}
|
|
82
|
-
|
|
83
|
-
cancel(variables?: Partial<Variables> | undefined) {
|
|
84
|
-
if (variables && this.abortMap.has(variables)) {
|
|
85
|
-
this.abortMap.get(variables)?.forEach(c => c.abort(new CanceledError()))
|
|
86
|
-
return true
|
|
87
|
-
}
|
|
88
|
-
if (!variables && this.abortMap.size) {
|
|
89
|
-
this.abortMap.forEach(controllers => controllers.forEach(c => c.abort(new CanceledError())))
|
|
90
|
-
return true
|
|
91
|
-
}
|
|
92
|
-
return false
|
|
93
|
-
}
|
|
94
73
|
}
|
package/src/network/AutoQuery.ts
CHANGED
|
@@ -7,28 +7,14 @@ import { queryClient } from './react-query-client'
|
|
|
7
7
|
import { AutoQueryObjectParams, QueryObject, UseQueryObjectOptions } from './types'
|
|
8
8
|
|
|
9
9
|
export class AutoQuery<Variables, Result> extends AutoOperation<Variables> implements QueryObject<Variables, Result> {
|
|
10
|
-
/**
|
|
11
|
-
* Prevents the same request from being triggered more than once if subsequent calls are made before the first ends.
|
|
12
|
-
*/
|
|
13
|
-
protected currentRequests = new Map<Variables | undefined, Promise<Result>>()
|
|
14
|
-
|
|
15
10
|
constructor(params: AutoQueryObjectParams<Variables, Result>) {
|
|
16
11
|
super(params)
|
|
17
12
|
}
|
|
18
13
|
|
|
19
|
-
private createQueryFn(variables: Variables | undefined) {
|
|
20
|
-
return async () => {
|
|
21
|
-
if (!this.currentRequests.has(variables)) this.currentRequests.set(variables, this.callFn(variables))
|
|
22
|
-
const result = await this.currentRequests.get(variables)
|
|
23
|
-
this.currentRequests.delete(variables)
|
|
24
|
-
return result!
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
14
|
query(...[variables]: Variables extends void ? [] : [variables: Variables]) {
|
|
29
15
|
return queryClient.fetchQuery({
|
|
30
16
|
queryKey: this.getKey(variables),
|
|
31
|
-
queryFn: this.
|
|
17
|
+
queryFn: ({ signal }) => this.callFn(variables, signal),
|
|
32
18
|
})
|
|
33
19
|
}
|
|
34
20
|
|
|
@@ -48,8 +34,10 @@ export class AutoQuery<Variables, Result> extends AutoOperation<Variables> imple
|
|
|
48
34
|
const use = suspense ? useSuspenseQuery : useQuery
|
|
49
35
|
return use({
|
|
50
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,
|
|
51
39
|
queryKey: this.getKey(variables),
|
|
52
|
-
queryFn: this.
|
|
40
|
+
queryFn: ({ signal }) => this.callFn(variables, signal),
|
|
53
41
|
}, queryClient) as T extends true ? UseSuspenseQueryResult<Result, StackspotAPIError> : DefinedUseQueryResult<Result, StackspotAPIError>
|
|
54
42
|
}
|
|
55
43
|
|
|
@@ -80,4 +68,8 @@ export class AutoQuery<Variables, Result> extends AutoOperation<Variables> imple
|
|
|
80
68
|
if (variables) key.push(variables)
|
|
81
69
|
return key
|
|
82
70
|
}
|
|
71
|
+
|
|
72
|
+
cancel(variables?: Partial<Variables>) {
|
|
73
|
+
queryClient.cancelQueries({ queryKey: this.getKey(variables) })
|
|
74
|
+
}
|
|
83
75
|
}
|
|
@@ -26,15 +26,12 @@ export class ManualInfiniteQuery<
|
|
|
26
26
|
private createInfiniteQueryFn(
|
|
27
27
|
variables: Variables,
|
|
28
28
|
): (context: QueryFunctionContext<QueryKey, Variables[PageParamName]>) => Promise<Result> {
|
|
29
|
-
return
|
|
29
|
+
return ({ pageParam, signal }) => {
|
|
30
30
|
const paginatedVariables = {
|
|
31
31
|
...variables,
|
|
32
32
|
[this.getConfig().pageParamName]: pageParam ?? variables?.[this.getConfig().pageParamName],
|
|
33
33
|
} as Variables
|
|
34
|
-
|
|
35
|
-
const result = await this.currentRequests.get(paginatedVariables)
|
|
36
|
-
this.currentRequests.delete(paginatedVariables)
|
|
37
|
-
return result!
|
|
34
|
+
return this.makeRequest(paginatedVariables, signal)
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
37
|
|
|
@@ -14,29 +14,26 @@ export class ManualMutation<
|
|
|
14
14
|
super(config)
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
private async makeRequest(variables?: Record<string, any
|
|
18
|
-
const abortController = new AbortController()
|
|
19
|
-
if (!this.abortMap.has(variables)) this.abortMap.set(variables, [])
|
|
20
|
-
this.abortMap.get(variables)?.push(abortController)
|
|
17
|
+
private async makeRequest(variables?: Record<string, any>, signal: AbortSignal = new AbortController().signal) {
|
|
21
18
|
try {
|
|
22
19
|
return await this.config.request(
|
|
23
|
-
...[
|
|
20
|
+
...[signal, variables] as Variables extends void ? [AbortSignal] : [AbortSignal, Variables],
|
|
24
21
|
)
|
|
25
22
|
} catch (error) {
|
|
26
23
|
throw this.config.transformError(error)
|
|
27
|
-
} finally {
|
|
28
|
-
this.abortMap.delete(variables)
|
|
29
24
|
}
|
|
30
25
|
}
|
|
31
26
|
|
|
32
|
-
mutate(...args: Variables extends void ? [] : [variables: Variables]) {
|
|
33
|
-
|
|
27
|
+
mutate(...args: Variables extends void ? [signal?: AbortSignal] : [variables: Variables, signal?: AbortSignal]) {
|
|
28
|
+
const variables = args[0] instanceof AbortSignal ? undefined : args[0]
|
|
29
|
+
const signal = args[0] instanceof AbortSignal ? args[0] : args[1]
|
|
30
|
+
return this.makeRequest(variables, signal)
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
useMutation(options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'>) {
|
|
33
|
+
useMutation(options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'> & { signal?: AbortSignal }) {
|
|
37
34
|
const result = useMutation<Result, StackspotAPIError, Variables>({
|
|
38
35
|
...options,
|
|
39
|
-
mutationFn: (
|
|
36
|
+
mutationFn: (variables: any) => this.makeRequest(variables, options?.signal),
|
|
40
37
|
}, queryClient)
|
|
41
38
|
return [result.mutateAsync as any, result.isPending, result.error as StackspotAPIError | undefined, result as any] as const
|
|
42
39
|
}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
/* eslint-disable react-hooks/rules-of-hooks */
|
|
2
2
|
|
|
3
3
|
import { UseQueryOptions, UseQueryResult, useQuery } from '@tanstack/react-query'
|
|
4
|
-
import { CanceledError } from '../error/CanceledError'
|
|
5
4
|
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
6
5
|
import { queryClient } from './react-query-client'
|
|
7
6
|
import { FullOperationConfig, OperationObject } from './types'
|
|
8
7
|
|
|
9
8
|
export abstract class ManualOperation<Variables extends Record<string, any> | void> implements OperationObject<Variables> {
|
|
10
9
|
protected config: FullOperationConfig<Variables extends void ? [AbortSignal] : [AbortSignal, Variables], any>
|
|
11
|
-
protected abortMap = new Map<Record<string, any> | undefined, AbortController[]>()
|
|
12
10
|
|
|
13
11
|
constructor(config: FullOperationConfig<Variables extends void ? [AbortSignal] : [AbortSignal, Variables], any>) {
|
|
14
12
|
this.config = config
|
|
@@ -51,16 +49,4 @@ export abstract class ManualOperation<Variables extends Record<string, any> | vo
|
|
|
51
49
|
getPermissionKey(variables?: Partial<Variables>) {
|
|
52
50
|
return [this.config.apiName, `${this.config.name}.permission`, variables]
|
|
53
51
|
}
|
|
54
|
-
|
|
55
|
-
cancel(variables?: Partial<Variables> | undefined) {
|
|
56
|
-
if (variables && this.abortMap.has(variables)) {
|
|
57
|
-
this.abortMap.get(variables)?.forEach(c => c.abort(new CanceledError()))
|
|
58
|
-
return true
|
|
59
|
-
}
|
|
60
|
-
if (!variables && this.abortMap.size) {
|
|
61
|
-
this.abortMap.forEach(controllers => controllers.forEach(c => c.abort(new CanceledError())))
|
|
62
|
-
return true
|
|
63
|
-
}
|
|
64
|
-
return false
|
|
65
|
-
}
|
|
66
52
|
}
|
|
@@ -10,41 +10,24 @@ export class ManualQuery<
|
|
|
10
10
|
Variables extends Record<string, any> | void,
|
|
11
11
|
Result
|
|
12
12
|
> extends ManualOperation<Variables> implements QueryObject<Variables, Result> {
|
|
13
|
-
/**
|
|
14
|
-
* Prevents the same request from being triggered more than once if subsequent calls are made before the first ends.
|
|
15
|
-
*/
|
|
16
|
-
protected currentRequests = new Map<Record<string, any> | undefined, Promise<Result>>()
|
|
17
|
-
|
|
18
13
|
constructor(config: FullOperationConfig<Variables extends void ? [AbortSignal] : [AbortSignal, Variables], Result>) {
|
|
19
14
|
super(config)
|
|
20
15
|
}
|
|
21
16
|
|
|
22
|
-
protected async makeRequest(variables: Record<string, any> | undefined) {
|
|
23
|
-
const abortController = new AbortController()
|
|
24
|
-
this.abortMap.set(variables, [abortController])
|
|
17
|
+
protected async makeRequest(variables: Record<string, any> | undefined, signal: AbortSignal) {
|
|
25
18
|
try {
|
|
26
19
|
return await this.config.request(
|
|
27
|
-
...[
|
|
20
|
+
...[signal, variables] as Variables extends void ? [AbortSignal] : [AbortSignal, Variables],
|
|
28
21
|
)
|
|
29
22
|
} catch (error) {
|
|
30
23
|
throw this.config.transformError(error)
|
|
31
|
-
} finally {
|
|
32
|
-
this.abortMap.delete(variables)
|
|
33
|
-
this.currentRequests.delete(variables)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private createQueryFn(variables: Record<string, any> | undefined) {
|
|
38
|
-
return () => {
|
|
39
|
-
if (!this.currentRequests.has(variables)) this.currentRequests.set(variables, this.makeRequest(variables))
|
|
40
|
-
return this.currentRequests.get(variables)!
|
|
41
24
|
}
|
|
42
25
|
}
|
|
43
26
|
|
|
44
27
|
query(...[variables]: Variables extends void ? [] : [variables: Variables]) {
|
|
45
28
|
return queryClient.fetchQuery({
|
|
46
29
|
queryKey: this.getKey(variables as Variables),
|
|
47
|
-
queryFn: this.
|
|
30
|
+
queryFn: ({ signal }) => this.makeRequest(variables, signal),
|
|
48
31
|
})
|
|
49
32
|
}
|
|
50
33
|
|
|
@@ -63,7 +46,7 @@ export class ManualQuery<
|
|
|
63
46
|
return use({
|
|
64
47
|
...options,
|
|
65
48
|
queryKey: this.getKey(variables as Variables),
|
|
66
|
-
queryFn: this.
|
|
49
|
+
queryFn: ({ signal }) => this.makeRequest(variables, signal),
|
|
67
50
|
}, queryClient) as T extends true ? UseSuspenseQueryResult<Result, StackspotAPIError> : DefinedUseQueryResult<Result, StackspotAPIError>
|
|
68
51
|
}
|
|
69
52
|
|
|
@@ -89,7 +72,11 @@ export class ManualQuery<
|
|
|
89
72
|
return queryClient.invalidateQueries({ queryKey: this.getKey(variables) })
|
|
90
73
|
}
|
|
91
74
|
|
|
92
|
-
getKey(variables?: Partial<Variables>)
|
|
75
|
+
getKey(variables?: Partial<Variables>) {
|
|
93
76
|
return [this.config.apiName, this.config.name, variables]
|
|
94
77
|
}
|
|
78
|
+
|
|
79
|
+
cancel(variables?: Partial<Variables>) {
|
|
80
|
+
queryClient.cancelQueries({ queryKey: this.getKey(variables) })
|
|
81
|
+
}
|
|
95
82
|
}
|
|
@@ -39,6 +39,7 @@ export abstract class ReactQueryNetworkClient extends NetworkClient {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
#transformError(error: any): StackspotAPIError {
|
|
42
|
+
if (error instanceof StackspotAPIError) return error
|
|
42
43
|
if (!(error instanceof HttpError)) throw new StackspotAPIError({ status: 0, message: error?.message || `${error}`, stack: error.stack })
|
|
43
44
|
const data = this.#parseErrorData(error)
|
|
44
45
|
if (data.type === PERMISSION_ERROR) {
|
|
@@ -3,4 +3,12 @@ import { QueryClient } from '@tanstack/react-query'
|
|
|
3
3
|
/**
|
|
4
4
|
* The global, unique, Query Client for React Query.
|
|
5
5
|
*/
|
|
6
|
-
export const queryClient = new QueryClient({
|
|
6
|
+
export const queryClient = new QueryClient({
|
|
7
|
+
defaultOptions: {
|
|
8
|
+
queries: {
|
|
9
|
+
refetchOnWindowFocus: false,
|
|
10
|
+
retry: false,
|
|
11
|
+
staleTime: Infinity,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
})
|
package/src/network/types.ts
CHANGED
|
@@ -136,15 +136,6 @@ export interface OperationObject<Variables> {
|
|
|
136
136
|
* @returns the query key.
|
|
137
137
|
*/
|
|
138
138
|
getPermissionKey: (...args: Variables extends void ? [] : [variables?: Partial<Variables>]) => any[],
|
|
139
|
-
/**
|
|
140
|
-
* Cancels the request if it hasn't finished yet.
|
|
141
|
-
*
|
|
142
|
-
* A canceled request will fail by throwing a `CanceledError`.
|
|
143
|
-
*
|
|
144
|
-
* @param variables the variables for the operation. If not provided, this will cancel all requests in progress for this operation.
|
|
145
|
-
* @returns true if any request has been canceled, false otherwise.
|
|
146
|
-
*/
|
|
147
|
-
cancel: (variables?: Partial<Variables>) => boolean,
|
|
148
139
|
}
|
|
149
140
|
|
|
150
141
|
export type UseQueryObjectOptions<Result> = Omit<UseQueryOptions<Result, StackspotAPIError>, 'queryFn' | 'queryKey'>
|
|
@@ -200,6 +191,12 @@ export interface QueryObject<Variables, Result> extends OperationObject<Variable
|
|
|
200
191
|
* @returns the query key.
|
|
201
192
|
*/
|
|
202
193
|
getKey: (...args: Variables extends void ? [] : [variables?: Partial<Variables>]) => any[],
|
|
194
|
+
/**
|
|
195
|
+
* Cancels the query if it hasn't finished yet.
|
|
196
|
+
*
|
|
197
|
+
* @param variables the variables for the operation. If not provided, this will cancel all requests in progress for this query.
|
|
198
|
+
*/
|
|
199
|
+
cancel: (variables?: Partial<Variables>) => void,
|
|
203
200
|
}
|
|
204
201
|
|
|
205
202
|
export type UseInfiniteQueryObjectOptions<Result> = Omit<
|
|
@@ -263,7 +260,7 @@ export interface MutationObject<Variables, Result> extends OperationObject<Varia
|
|
|
263
260
|
* @param args the variables for the mutation.
|
|
264
261
|
* @returns a promise that resolves to the response's data.
|
|
265
262
|
*/
|
|
266
|
-
mutate: (...args: Variables extends void ? [] : [variables: Variables]) => Promise<Result>,
|
|
263
|
+
mutate: (...args: Variables extends void ? [signal?: AbortSignal] : [variables: Variables, signal?: AbortSignal]) => Promise<Result>,
|
|
267
264
|
/**
|
|
268
265
|
* A React hook equivalent to React Query's `useMutation`. It creates a mutation function and the mutation states.
|
|
269
266
|
* @param options the options for `useMutation`
|
|
@@ -274,7 +271,7 @@ export interface MutationObject<Variables, Result> extends OperationObject<Varia
|
|
|
274
271
|
* - [3]: the original UseMutationResult object, from ReactQuery.
|
|
275
272
|
*
|
|
276
273
|
*/
|
|
277
|
-
useMutation: (options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'>) =>
|
|
274
|
+
useMutation: (options?: Omit<UseMutationOptions<Result, StackspotAPIError, Variables>, 'mutationFn'> & { signal?: AbortSignal }) =>
|
|
278
275
|
Readonly<[
|
|
279
276
|
(...args: Variables extends void
|
|
280
277
|
? [options?: MutateOptions<Result, StackspotAPIError>]
|
|
@@ -286,7 +283,7 @@ export interface MutationObject<Variables, Result> extends OperationObject<Varia
|
|
|
286
283
|
]>,
|
|
287
284
|
}
|
|
288
285
|
|
|
289
|
-
export type OperationVariables<T extends Pick<OperationObject<any>, '
|
|
286
|
+
export type OperationVariables<T extends Pick<OperationObject<any>, 'isAllowed'>> = T extends Pick<OperationObject<infer R>, 'isAllowed'>
|
|
290
287
|
? R
|
|
291
288
|
: never
|
|
292
289
|
export type OperationResult<T extends { query: (variables?: any) => Promise<any> } | { mutate: (variables?: any) => Promise<any> }> =
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { StackspotAPIError } from './StackspotAPIError.js';
|
|
2
|
-
/**
|
|
3
|
-
* This error is thrown when a request is canceled by the user before it completes.
|
|
4
|
-
*/
|
|
5
|
-
export declare class CanceledError extends StackspotAPIError {
|
|
6
|
-
constructor();
|
|
7
|
-
}
|
|
8
|
-
//# sourceMappingURL=CanceledError.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CanceledError.d.ts","sourceRoot":"","sources":["../../src/error/CanceledError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAEvD;;GAEG;AACH,qBAAa,aAAc,SAAQ,iBAAiB;;CAInD"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { StackspotAPIError } from './StackspotAPIError.js';
|
|
2
|
-
/**
|
|
3
|
-
* This error is thrown when a request is canceled by the user before it completes.
|
|
4
|
-
*/
|
|
5
|
-
export class CanceledError extends StackspotAPIError {
|
|
6
|
-
constructor() {
|
|
7
|
-
super({ status: 0, message: lang => lang === 'en' ? 'Canceled by the user' : 'Cancelado pelo usuário' });
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=CanceledError.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"CanceledError.js","sourceRoot":"","sources":["../../src/error/CanceledError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAEvD;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,iBAAiB;IAClD;QACE,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,CAAC,wBAAwB,EAAE,CAAC,CAAA;IAC1G,CAAC;CACF"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { StackspotAPIError } from './StackspotAPIError'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This error is thrown when a request is canceled by the user before it completes.
|
|
5
|
-
*/
|
|
6
|
-
export class CanceledError extends StackspotAPIError {
|
|
7
|
-
constructor() {
|
|
8
|
-
super({ status: 0, message: lang => lang === 'en' ? 'Canceled by the user' : 'Cancelado pelo usuário' })
|
|
9
|
-
}
|
|
10
|
-
}
|