@stack-spot/portal-network 0.14.1 → 0.15.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 +12 -0
- package/dist/api/cloudServices.d.ts +37 -656
- package/dist/api/cloudServices.d.ts.map +1 -1
- package/dist/api/cloudServices.js +26 -386
- package/dist/api/cloudServices.js.map +1 -1
- package/dist/client/cloud-services.d.ts +111 -0
- package/dist/client/cloud-services.d.ts.map +1 -0
- package/dist/client/cloud-services.js +136 -0
- package/dist/client/cloud-services.js.map +1 -0
- package/dist/client/types.d.ts +67 -1
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/workspace.d.ts +6 -5
- package/dist/client/workspace.d.ts.map +1 -1
- package/dist/client/workspace.js.map +1 -1
- package/dist/index.d.ts +3 -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 -2
- package/dist/network/AutoInfiniteQuery.d.ts.map +1 -1
- package/dist/network/AutoInfiniteQuery.js.map +1 -1
- package/dist/network/AutoQuery.d.ts +4 -4
- package/dist/network/AutoQuery.d.ts.map +1 -1
- package/dist/network/AutoQuery.js.map +1 -1
- package/dist/network/ManualInfiniteQuery.d.ts +2 -2
- package/dist/network/ManualInfiniteQuery.d.ts.map +1 -1
- package/dist/network/ManualInfiniteQuery.js.map +1 -1
- package/dist/network/ManualQuery.d.ts +4 -4
- package/dist/network/ManualQuery.d.ts.map +1 -1
- package/dist/network/ManualQuery.js.map +1 -1
- package/dist/network/types.d.ts +16 -5
- package/dist/network/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/api/cloudServices.ts +280 -1707
- package/src/client/cloud-services.ts +84 -0
- package/src/client/types.ts +85 -1
- package/src/client/workspace.ts +6 -5
- package/src/index.ts +4 -0
- package/src/network/AutoInfiniteQuery.ts +7 -3
- package/src/network/AutoQuery.ts +10 -10
- package/src/network/ManualInfiniteQuery.ts +3 -3
- package/src/network/ManualQuery.ts +8 -8
- package/src/network/types.ts +22 -9
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { HttpError } from '@oazapfts/runtime'
|
|
2
|
+
import {
|
|
3
|
+
BillingAccountSummaryResponse,
|
|
4
|
+
defaults, downloadCsvFiles, enableGuardrail, getAlerts, getAllocationCostCsv, getAllocationCostFilters, getAllTags,
|
|
5
|
+
getCostOverview, getGuardrails, getInstanceDetails, listBillingAccounts, listInstances, listServices,
|
|
6
|
+
processAllocationCostRequest,
|
|
7
|
+
} from '../api/cloudServices'
|
|
8
|
+
import apis from '../apis.json'
|
|
9
|
+
import { DefaultAPIError } from '../error/DefaultAPIError'
|
|
10
|
+
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
11
|
+
import { ReactQueryNetworkClient } from '../network/ReactQueryNetworkClient'
|
|
12
|
+
import { CostAllocationResult, CostAllocationVariables, FixedManagedService, OazapftsFunction, ReplaceResult } from './types'
|
|
13
|
+
|
|
14
|
+
class CloudServicesClient extends ReactQueryNetworkClient {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(apis.cloudServices.url, defaults)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
protected buildStackSpotError(error: HttpError): StackspotAPIError {
|
|
20
|
+
return new DefaultAPIError(error.data, error.status, { en: {}, pt: {} }, error.headers)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Gets overview of costs over the current month, last month and last 3 months.
|
|
25
|
+
*/
|
|
26
|
+
costOverview = this.query(getCostOverview)
|
|
27
|
+
/**
|
|
28
|
+
* Gets a CSV file with the data of `costOverview`.
|
|
29
|
+
*/
|
|
30
|
+
downloadCostOverviewCSV = this.mutation(downloadCsvFiles)
|
|
31
|
+
/**
|
|
32
|
+
* Gets the alerts in a workspace and environment (paginated).
|
|
33
|
+
*/
|
|
34
|
+
alerts = this.infiniteQuery(getAlerts, {
|
|
35
|
+
accumulator: 'data',
|
|
36
|
+
defaultVariables: { pageSize: 40 },
|
|
37
|
+
initialPageParam: 1,
|
|
38
|
+
getNextPageParam: ({ lastPage }) => lastPage.nextPage,
|
|
39
|
+
})
|
|
40
|
+
/**
|
|
41
|
+
* Lists instances of a particular service resource running in a workspace.
|
|
42
|
+
*/
|
|
43
|
+
allInstancesOfResource = this.query(listInstances)
|
|
44
|
+
/**
|
|
45
|
+
* Lists all managed services.
|
|
46
|
+
*/
|
|
47
|
+
allServices = this.query(listServices as unknown as ReplaceResult<typeof listServices, FixedManagedService[]>)
|
|
48
|
+
/**
|
|
49
|
+
* Gets the description of an instance.
|
|
50
|
+
*/
|
|
51
|
+
instance = this.query(getInstanceDetails as unknown as ReplaceResult<typeof getInstanceDetails, string>)
|
|
52
|
+
/**
|
|
53
|
+
* Gets all Guard Rails.
|
|
54
|
+
*/
|
|
55
|
+
guardRails = this.query(getGuardrails)
|
|
56
|
+
/**
|
|
57
|
+
* Enables a Guard Rail in any level
|
|
58
|
+
*/
|
|
59
|
+
enableGuardRail = this.mutation(enableGuardrail)
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves cost and usage metrics for the current account.
|
|
62
|
+
*/
|
|
63
|
+
costAllocation = this.query(
|
|
64
|
+
processAllocationCostRequest as unknown as OazapftsFunction<CostAllocationVariables, CostAllocationResult>,
|
|
65
|
+
)
|
|
66
|
+
/**
|
|
67
|
+
* Lists the filters related to the given type.
|
|
68
|
+
*/
|
|
69
|
+
costAllocationFilters = this.query(getAllocationCostFilters)
|
|
70
|
+
/**
|
|
71
|
+
* Gets a CSV file with the data of `costAllocation`.
|
|
72
|
+
*/
|
|
73
|
+
downloadCostAllocationCSV = this.query(getAllocationCostCsv)
|
|
74
|
+
/**
|
|
75
|
+
* Lists all tags.
|
|
76
|
+
*/
|
|
77
|
+
allTags = this.query(getAllTags)
|
|
78
|
+
/**
|
|
79
|
+
* Retrieves a summary for each billing account of the account with the id passed as parameter.
|
|
80
|
+
*/
|
|
81
|
+
billingAccounts = this.query(listBillingAccounts as unknown as ReplaceResult<typeof listBillingAccounts, BillingAccountSummaryResponse[]>)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const cloudServicesClient = new CloudServicesClient()
|
package/src/client/types.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RequestOpts } from '@oazapfts/runtime'
|
|
2
|
+
import { AccountScmInfoSaveRequest, AccountScmInfoUpdateRequest, AccountScmStatusResponse, IdResponse } from '../api/account'
|
|
3
|
+
import { AllocationCostRequest, AllocationCostResponse, ChargePeriod, getAllocationCostFilters, ManagedService, ServiceResource } from '../api/cloudServices'
|
|
4
|
+
import { EnvironmentReadResponse, WorkspaceReadResponse } from '../api/workspace'
|
|
2
5
|
|
|
3
6
|
interface BaseSMCStatus {
|
|
4
7
|
/**
|
|
@@ -42,3 +45,84 @@ export interface UpdateSCMRequest {
|
|
|
42
45
|
value?: SCMAuthGitValue | SCMAuthPATValue,
|
|
43
46
|
},
|
|
44
47
|
}
|
|
48
|
+
|
|
49
|
+
export type AllocationCostType = Parameters<typeof getAllocationCostFilters>[0]['$type']
|
|
50
|
+
|
|
51
|
+
export interface CostAllocationVariables {
|
|
52
|
+
allocationCostRequest: Omit<AllocationCostRequest, 'stackSpotAccountId' | 'billingAccountId'> & {
|
|
53
|
+
stackSpotAccountId: string,
|
|
54
|
+
billingAccountId: string[],
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface ServiceNameResponse {
|
|
59
|
+
type: 'ServiceNameResponse',
|
|
60
|
+
chargePeriod?: ChargePeriod,
|
|
61
|
+
/** Represents an offering that can be purchased from a cloud provider. */
|
|
62
|
+
serviceName?: string,
|
|
63
|
+
/** Effective cost post discounts and amortizations. */
|
|
64
|
+
effectiveCost?: number,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface TagResponse {
|
|
68
|
+
type: 'TagResponse',
|
|
69
|
+
chargePeriod?: ChargePeriod,
|
|
70
|
+
/** Tag key: Identifies a grouping attribute or category for resources or constructs. */
|
|
71
|
+
tagKey?: string,
|
|
72
|
+
/** Tag value: Specifies the group or category detail that the resource or construct belongs to. */
|
|
73
|
+
tagValue?: string,
|
|
74
|
+
/** Effective cost post discounts and amortizations. */
|
|
75
|
+
effectiveCost?: number,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface UsageUnitResponse {
|
|
79
|
+
type: 'UsageUnitResponse',
|
|
80
|
+
chargePeriod?: ChargePeriod,
|
|
81
|
+
/** Represents an offering that can be purchased from a cloud provider. */
|
|
82
|
+
serviceName?: string,
|
|
83
|
+
/** Represents the volume of a given resource or service used or purchased based on the Usage Unit. */
|
|
84
|
+
usageQuantity?: number,
|
|
85
|
+
/** Defines the unit of measurement for the resource or service. */
|
|
86
|
+
usageUnit?: string,
|
|
87
|
+
/** Effective cost post discounts and amortizations. */
|
|
88
|
+
effectiveCost?: number,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type CostAllocationData = ServiceNameResponse | TagResponse | UsageUnitResponse
|
|
92
|
+
|
|
93
|
+
export interface CostAllocationResult extends Omit<AllocationCostResponse, 'data'> {
|
|
94
|
+
data: CostAllocationData[],
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
interface FixedServiceResource extends Omit<ServiceResource, 'id'> {
|
|
98
|
+
id: string,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface FixedManagedService extends Omit<ManagedService, 'resources'> {
|
|
102
|
+
resources: FixedServiceResource[],
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type OazapftsFunction<Variables = any, Result = any> = (variables: Variables, opts?: RequestOpts) => Promise<Result>
|
|
106
|
+
|
|
107
|
+
export type FixVariables<
|
|
108
|
+
T extends OazapftsFunction,
|
|
109
|
+
Fix extends Record<string, any>,
|
|
110
|
+
> = OazapftsFunction<Omit<Parameters<T>[0], keyof Fix> & { [K in keyof Fix as Fix[K] extends never ? never : K]: Fix[K] }, ReturnType<T>>
|
|
111
|
+
|
|
112
|
+
export type ReplaceResult<T extends (...args: any[]) => Promise<any>, Fix> = (...args: Parameters<T>) => Promise<Fix>
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
export interface FixedWorkspaceReadResponse extends Omit<WorkspaceReadResponse, 'id' | 'name'> {
|
|
116
|
+
id: string,
|
|
117
|
+
name: string,
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface FixedEnvironmentReadResponse extends Omit<EnvironmentReadResponse, 'id' | 'name'> {
|
|
121
|
+
id: string,
|
|
122
|
+
name: string,
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface FixedIdResponse extends Omit<IdResponse, 'id'> {
|
|
126
|
+
id: string,
|
|
127
|
+
}
|
|
128
|
+
|
package/src/client/workspace.ts
CHANGED
|
@@ -34,6 +34,7 @@ import { DefaultAPIError } from '../error/DefaultAPIError'
|
|
|
34
34
|
import { workspaceDictionary } from '../error/dictionary/workspace'
|
|
35
35
|
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
36
36
|
import { ReactQueryNetworkClient } from '../network/ReactQueryNetworkClient'
|
|
37
|
+
import { FixedEnvironmentReadResponse, FixedIdResponse, FixedWorkspaceReadResponse, ReplaceResult } from './types'
|
|
37
38
|
|
|
38
39
|
class WorkspaceClient extends ReactQueryNetworkClient {
|
|
39
40
|
constructor() {
|
|
@@ -77,12 +78,12 @@ class WorkspaceClient extends ReactQueryNetworkClient {
|
|
|
77
78
|
/**
|
|
78
79
|
* Gets all environments
|
|
79
80
|
*/
|
|
80
|
-
environments = this.query(getEnvironments)
|
|
81
|
+
environments = this.query(getEnvironments as unknown as ReplaceResult<typeof getEnvironments, FixedEnvironmentReadResponse[]>)
|
|
81
82
|
|
|
82
83
|
/**
|
|
83
84
|
* Gets environment by Id
|
|
84
85
|
*/
|
|
85
|
-
environment = this.query(getEnvironment)
|
|
86
|
+
environment = this.query(getEnvironment as unknown as ReplaceResult<typeof getEnvironment, FixedEnvironmentReadResponse>)
|
|
86
87
|
|
|
87
88
|
/**
|
|
88
89
|
* Creates an environment
|
|
@@ -157,17 +158,17 @@ class WorkspaceClient extends ReactQueryNetworkClient {
|
|
|
157
158
|
/**
|
|
158
159
|
* Gets all workspaces
|
|
159
160
|
*/
|
|
160
|
-
workspaces = this.query(getWorkspaces)
|
|
161
|
+
workspaces = this.query(getWorkspaces as unknown as ReplaceResult<typeof getWorkspaces, FixedWorkspaceReadResponse[]>)
|
|
161
162
|
|
|
162
163
|
/**
|
|
163
164
|
* Gets a workspace
|
|
164
165
|
*/
|
|
165
|
-
workspace = this.query(getWorkspaceForId)
|
|
166
|
+
workspace = this.query(getWorkspaceForId as unknown as ReplaceResult<typeof getWorkspaceForId, FixedWorkspaceReadResponse>)
|
|
166
167
|
|
|
167
168
|
/**
|
|
168
169
|
* Creates a workspace
|
|
169
170
|
*/
|
|
170
|
-
createWorkspace = this.mutation(save)
|
|
171
|
+
createWorkspace = this.mutation(save as unknown as ReplaceResult<typeof save, FixedIdResponse>)
|
|
171
172
|
|
|
172
173
|
/**
|
|
173
174
|
* Updates a workspace
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { accountClient } from './client/account'
|
|
2
|
+
export { cloudServicesClient } from './client/cloud-services'
|
|
2
3
|
export { secretsClient } from './client/secrets'
|
|
3
4
|
export { workspaceClient } from './client/workspace'
|
|
4
5
|
export { runtimeManagerClient } from './client/runtime-manager'
|
|
@@ -6,8 +7,11 @@ export { workspaceSearchClient } from './client/workspace-search'
|
|
|
6
7
|
export { workflowClient } from './client/workflow'
|
|
7
8
|
export { eventBusClient } from './client/event-bus'
|
|
8
9
|
export * from './client/types'
|
|
10
|
+
export { UseQueryObjectOptions } from './network/types'
|
|
9
11
|
export { DefaultAPIError } from './error/DefaultAPIError'
|
|
10
12
|
export { StackspotAPIError } from './error/StackspotAPIError'
|
|
11
13
|
export { NetworkClient } from './network/NetworkClient'
|
|
12
14
|
export { queryClient } from './network/react-query-client'
|
|
15
|
+
export { OperationResult, OperationVariables } from './network/types'
|
|
13
16
|
export { useExtendedList } from './utils/use-extended-list'
|
|
17
|
+
|
|
@@ -50,7 +50,11 @@ export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Va
|
|
|
50
50
|
).flat() as Accumulator extends keyof Result ? Result[Accumulator] : Result
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
private useInfiniteQueryResult(
|
|
53
|
+
private useInfiniteQueryResult(
|
|
54
|
+
suspense: boolean,
|
|
55
|
+
variables = {} as Variables,
|
|
56
|
+
options?: UseInfiniteQueryObjectOptions<Result>,
|
|
57
|
+
) {
|
|
54
58
|
variables = { ...variables, ...this.options.defaultVariables }
|
|
55
59
|
const use = suspense ? useSuspenseInfiniteQuery : useInfiniteQuery
|
|
56
60
|
return use<any, any, any, any, any>({
|
|
@@ -65,7 +69,7 @@ export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Va
|
|
|
65
69
|
|
|
66
70
|
useInfiniteQuery(
|
|
67
71
|
variables?: Variables,
|
|
68
|
-
options?: UseInfiniteQueryObjectOptions
|
|
72
|
+
options?: UseInfiniteQueryObjectOptions<Result>,
|
|
69
73
|
): [
|
|
70
74
|
Accumulator extends keyof Result ? Result[Accumulator] : Result,
|
|
71
75
|
UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
|
|
@@ -79,7 +83,7 @@ export class AutoInfiniteQuery<Variables, Result, PageParamName extends keyof Va
|
|
|
79
83
|
|
|
80
84
|
useStatefulInfiniteQuery(
|
|
81
85
|
variables?: Variables,
|
|
82
|
-
options?: UseInfiniteQueryObjectOptions
|
|
86
|
+
options?: UseInfiniteQueryObjectOptions<Result>,
|
|
83
87
|
): [
|
|
84
88
|
Accumulator extends keyof Result ? Result[Accumulator] : Result,
|
|
85
89
|
boolean,
|
package/src/network/AutoQuery.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* eslint-disable react-hooks/rules-of-hooks */
|
|
2
2
|
|
|
3
|
-
import { DefinedUseQueryResult,
|
|
3
|
+
import { DefinedUseQueryResult, UseSuspenseQueryResult, useQuery, useSuspenseQuery } from '@tanstack/react-query'
|
|
4
4
|
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
5
5
|
import { AutoOperation } from './AutoOperation'
|
|
6
6
|
import { queryClient } from './react-query-client'
|
|
7
|
-
import { AutoQueryObjectParams, QueryObject } from './types'
|
|
7
|
+
import { AutoQueryObjectParams, QueryObject, UseQueryObjectOptions } from './types'
|
|
8
8
|
|
|
9
9
|
export class AutoQuery<Variables, Result> extends AutoOperation<Variables> implements QueryObject<Variables, Result> {
|
|
10
10
|
/**
|
|
@@ -35,16 +35,16 @@ export class AutoQuery<Variables, Result> extends AutoOperation<Variables> imple
|
|
|
35
35
|
#useQueryResult<T extends boolean>(
|
|
36
36
|
suspense: T,
|
|
37
37
|
...args: Variables extends void
|
|
38
|
-
? [options?:
|
|
39
|
-
: [variables: Variables, options?:
|
|
38
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
39
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
40
40
|
) {
|
|
41
41
|
/* `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
|
|
42
42
|
variables and the 2nd is the RequestOpts. If it doesn't accept variables, its arity is one: it accepts only the RequestOpts.
|
|
43
43
|
We can use this information to determine what the type of `args` actually is at runtime. If variables are accepted, than the 1st
|
|
44
44
|
argument is the variables and the 2nd is the query options, otherwise, it has a single argument, which is the query options. */
|
|
45
45
|
const [variables, options] = this.fn.length > 1
|
|
46
|
-
? args as [Variables,
|
|
47
|
-
: [undefined, args[0] as
|
|
46
|
+
? args as [Variables, UseQueryObjectOptions<Result> | undefined]
|
|
47
|
+
: [undefined, args[0] as UseQueryObjectOptions<Result> | undefined]
|
|
48
48
|
const use = suspense ? useSuspenseQuery : useQuery
|
|
49
49
|
return use({
|
|
50
50
|
...options,
|
|
@@ -55,8 +55,8 @@ export class AutoQuery<Variables, Result> extends AutoOperation<Variables> imple
|
|
|
55
55
|
|
|
56
56
|
useQuery(
|
|
57
57
|
...args: Variables extends void
|
|
58
|
-
? [options?:
|
|
59
|
-
: [variables: Variables, options?:
|
|
58
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
59
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
60
60
|
) {
|
|
61
61
|
const result = this.#useQueryResult(true, ...args)
|
|
62
62
|
return result.data
|
|
@@ -64,8 +64,8 @@ export class AutoQuery<Variables, Result> extends AutoOperation<Variables> imple
|
|
|
64
64
|
|
|
65
65
|
useStatefulQuery(
|
|
66
66
|
...args: Variables extends void
|
|
67
|
-
? [options?:
|
|
68
|
-
: [variables: Variables, options?:
|
|
67
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
68
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
69
69
|
) {
|
|
70
70
|
const result = this.#useQueryResult(false, ...args)
|
|
71
71
|
return [result.data, result.isPending, result.error as StackspotAPIError | undefined, result] as const
|
|
@@ -44,7 +44,7 @@ export class ManualInfiniteQuery<
|
|
|
44
44
|
).flat() as Accumulator extends keyof Result ? Result[Accumulator] : Result
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
private useInfiniteQueryResult(suspense: boolean, variables = {} as Variables, options?: UseInfiniteQueryObjectOptions) {
|
|
47
|
+
private useInfiniteQueryResult(suspense: boolean, variables = {} as Variables, options?: UseInfiniteQueryObjectOptions<Result>) {
|
|
48
48
|
variables = { ...variables, ...this.getConfig().defaultVariables }
|
|
49
49
|
const use = suspense ? useSuspenseInfiniteQuery : useInfiniteQuery
|
|
50
50
|
return use<any, any, any, any, any>({
|
|
@@ -59,7 +59,7 @@ export class ManualInfiniteQuery<
|
|
|
59
59
|
|
|
60
60
|
useInfiniteQuery(
|
|
61
61
|
variables?: Variables,
|
|
62
|
-
options?: UseInfiniteQueryObjectOptions
|
|
62
|
+
options?: UseInfiniteQueryObjectOptions<Result>,
|
|
63
63
|
): [
|
|
64
64
|
Accumulator extends keyof Result ? Result[Accumulator] : Result,
|
|
65
65
|
UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
|
|
@@ -73,7 +73,7 @@ export class ManualInfiniteQuery<
|
|
|
73
73
|
|
|
74
74
|
useStatefulInfiniteQuery(
|
|
75
75
|
variables?: Variables,
|
|
76
|
-
options?: UseInfiniteQueryObjectOptions
|
|
76
|
+
options?: UseInfiniteQueryObjectOptions<Result>,
|
|
77
77
|
): [
|
|
78
78
|
Accumulator extends keyof Result ? Result[Accumulator] : Result,
|
|
79
79
|
boolean,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* eslint-disable react-hooks/rules-of-hooks */
|
|
2
2
|
|
|
3
|
-
import { DefinedUseQueryResult,
|
|
3
|
+
import { DefinedUseQueryResult, UseSuspenseQueryResult, useQuery, useSuspenseQuery } from '@tanstack/react-query'
|
|
4
4
|
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
5
5
|
import { ManualOperation } from './ManualOperation'
|
|
6
6
|
import { queryClient } from './react-query-client'
|
|
7
|
-
import { FullOperationConfig, QueryObject } from './types'
|
|
7
|
+
import { FullOperationConfig, QueryObject, UseQueryObjectOptions } from './types'
|
|
8
8
|
|
|
9
9
|
export class ManualQuery<
|
|
10
10
|
Variables extends Record<string, any> | void,
|
|
@@ -51,8 +51,8 @@ export class ManualQuery<
|
|
|
51
51
|
#useQueryResult<T extends boolean>(
|
|
52
52
|
suspense: T,
|
|
53
53
|
...args: Variables extends void
|
|
54
|
-
? [options?:
|
|
55
|
-
: [variables: Variables, options?:
|
|
54
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
55
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
56
56
|
) {
|
|
57
57
|
/* `this.config.request` is a function with arity 1 or 2. If it accepts variables, its arity is 2: the 1st parameter is the
|
|
58
58
|
AbortSignal and the 2nd is the variables. If it doesn't accept variables, its arity is one: it accepts only the AbortSignal.
|
|
@@ -69,8 +69,8 @@ export class ManualQuery<
|
|
|
69
69
|
|
|
70
70
|
useQuery(
|
|
71
71
|
...args: Variables extends void
|
|
72
|
-
? [options?:
|
|
73
|
-
: [variables: Variables, options?:
|
|
72
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
73
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
74
74
|
) {
|
|
75
75
|
const result = this.#useQueryResult(false, ...args)
|
|
76
76
|
return result.data
|
|
@@ -78,8 +78,8 @@ export class ManualQuery<
|
|
|
78
78
|
|
|
79
79
|
useStatefulQuery(
|
|
80
80
|
...args: Variables extends void
|
|
81
|
-
? [options?:
|
|
82
|
-
: [variables: Variables, options?:
|
|
81
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
82
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
83
83
|
) {
|
|
84
84
|
const result = this.#useQueryResult(false, ...args)
|
|
85
85
|
return [result.data, result.isPending, result.error as StackspotAPIError | undefined, result] as const
|
package/src/network/types.ts
CHANGED
|
@@ -144,6 +144,8 @@ export interface OperationObject<Variables> {
|
|
|
144
144
|
cancel: (variables?: Partial<Variables>) => boolean,
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
export type UseQueryObjectOptions<Result> = Omit<UseQueryOptions<Result, StackspotAPIError>, 'queryFn' | 'queryKey'>
|
|
148
|
+
|
|
147
149
|
export interface QueryObject<Variables, Result> extends OperationObject<Variables> {
|
|
148
150
|
/**
|
|
149
151
|
* Runs the query operation.
|
|
@@ -165,8 +167,8 @@ export interface QueryObject<Variables, Result> extends OperationObject<Variable
|
|
|
165
167
|
*/
|
|
166
168
|
useQuery: (
|
|
167
169
|
...args: Variables extends void
|
|
168
|
-
? [options?:
|
|
169
|
-
: [variables: Variables, options?:
|
|
170
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
171
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
170
172
|
) => Result | undefined,
|
|
171
173
|
/**
|
|
172
174
|
* Same as `query`, but a React Hook instead.
|
|
@@ -180,8 +182,8 @@ export interface QueryObject<Variables, Result> extends OperationObject<Variable
|
|
|
180
182
|
*/
|
|
181
183
|
useStatefulQuery: (
|
|
182
184
|
...args: Variables extends void
|
|
183
|
-
? [options?:
|
|
184
|
-
: [variables: Variables, options?:
|
|
185
|
+
? [options?: UseQueryObjectOptions<Result>]
|
|
186
|
+
: [variables: Variables, options?: UseQueryObjectOptions<Result>]
|
|
185
187
|
) => Readonly<[Result | undefined, boolean, StackspotAPIError | null | undefined, UseQueryResult<Result, StackspotAPIError>]>,
|
|
186
188
|
/**
|
|
187
189
|
* Invalidates the cache for this query. A specific query can be invalidated by passing an argument (variables).
|
|
@@ -197,7 +199,10 @@ export interface QueryObject<Variables, Result> extends OperationObject<Variable
|
|
|
197
199
|
getKey: (...args: Variables extends void ? [] : [variables?: Partial<Variables>]) => any[],
|
|
198
200
|
}
|
|
199
201
|
|
|
200
|
-
export type UseInfiniteQueryObjectOptions = Omit<
|
|
202
|
+
export type UseInfiniteQueryObjectOptions<Result> = Omit<
|
|
203
|
+
UseInfiniteQueryOptions<Result, StackspotAPIError>,
|
|
204
|
+
'queryFn' | 'queryKey' | 'getNextPageParam' | 'initialPageParam'
|
|
205
|
+
>
|
|
201
206
|
|
|
202
207
|
export interface InfiniteQueryObject<Variables, Result, Accumulator extends keyof Result | ''> extends QueryObject<Variables, Result> {
|
|
203
208
|
/**
|
|
@@ -212,8 +217,8 @@ export interface InfiniteQueryObject<Variables, Result, Accumulator extends keyo
|
|
|
212
217
|
*/
|
|
213
218
|
useInfiniteQuery: (
|
|
214
219
|
...args: Partial<Variables> extends Variables
|
|
215
|
-
? [variables?: Variables, options?: UseInfiniteQueryObjectOptions]
|
|
216
|
-
: [variables: Variables, options?: UseInfiniteQueryObjectOptions]
|
|
220
|
+
? [variables?: Variables, options?: UseInfiniteQueryObjectOptions<Result>]
|
|
221
|
+
: [variables: Variables, options?: UseInfiniteQueryObjectOptions<Result>]
|
|
217
222
|
) => Readonly<[
|
|
218
223
|
(Accumulator extends keyof Result ? Result[Accumulator] : Result) | undefined,
|
|
219
224
|
UseInfiniteQueryResult<InfiniteData<Result>, StackspotAPIError>,
|
|
@@ -232,8 +237,8 @@ export interface InfiniteQueryObject<Variables, Result, Accumulator extends keyo
|
|
|
232
237
|
*/
|
|
233
238
|
useStatefulInfiniteQuery: (
|
|
234
239
|
...args: Partial<Variables> extends Variables
|
|
235
|
-
? [variables?: Variables, options?:
|
|
236
|
-
: [variables: Variables, options?:
|
|
240
|
+
? [variables?: Variables, options?: UseInfiniteQueryObjectOptions<Result>]
|
|
241
|
+
: [variables: Variables, options?: UseInfiniteQueryObjectOptions<Result>]
|
|
237
242
|
) => Readonly<[
|
|
238
243
|
(Accumulator extends keyof Result ? Result[Accumulator] : Result) | undefined,
|
|
239
244
|
boolean,
|
|
@@ -277,3 +282,11 @@ export interface MutationObject<Variables, Result> extends OperationObject<Varia
|
|
|
277
282
|
UseMutationResult<Result, StackspotAPIError, Variables>,
|
|
278
283
|
]>,
|
|
279
284
|
}
|
|
285
|
+
|
|
286
|
+
export type OperationVariables<T extends Pick<OperationObject<any>, 'cancel'>> = T extends Pick<OperationObject<infer R>, 'cancel'>
|
|
287
|
+
? R
|
|
288
|
+
: never
|
|
289
|
+
export type OperationResult<T extends { query: (variables?: any) => Promise<any> } | { mutate: (variables?: any) => Promise<any> }> =
|
|
290
|
+
T extends { query: (variables?: any) => Promise<infer R> } ? R : (
|
|
291
|
+
T extends { mutate: (variables?: any) => Promise<infer R> } ? R : never
|
|
292
|
+
)
|