@stack-spot/portal-network 0.184.0 → 0.185.0-beta.1
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 +2444 -2412
- package/dist/api/account.js +1 -1
- package/dist/api/agent-tools.js +1 -1
- package/dist/api/agent.js +1 -1
- package/dist/api/ai.js +1 -1
- package/dist/api/apiManagement.js +1 -1
- package/dist/api/apiRuntime.js +1 -1
- package/dist/api/cloudAccount.js +1 -1
- package/dist/api/cloudPlatform.js +1 -1
- package/dist/api/cloudPlatformHorizon.js +1 -1
- package/dist/api/cloudRuntimes.js +1 -1
- package/dist/api/cloudServices.js +1 -1
- package/dist/api/codeShift.d.ts +63 -4
- package/dist/api/codeShift.d.ts.map +1 -1
- package/dist/api/codeShift.js +14 -1
- package/dist/api/codeShift.js.map +1 -1
- package/dist/api/content.js +1 -1
- package/dist/api/dataIntegration.js +1 -1
- package/dist/api/discover.js +1 -1
- package/dist/api/genAiInference.js +1 -1
- package/dist/api/insights.js +1 -1
- package/dist/api/notification.js +1 -1
- package/dist/api/secrets.js +1 -1
- package/dist/api/serviceCatalog.js +1 -1
- package/dist/api/workspace-ai.js +1 -1
- package/dist/api/workspace.js +1 -1
- package/dist/api/workspaceManager.js +1 -1
- package/dist/api/workspaceSearchEngine.js +1 -1
- package/dist/client/ai.d.ts.map +1 -1
- package/dist/client/ai.js +84 -14
- package/dist/client/ai.js.map +1 -1
- package/dist/client/code-shift.d.ts +30 -0
- package/dist/client/code-shift.d.ts.map +1 -1
- package/dist/client/code-shift.js +37 -1
- package/dist/client/code-shift.js.map +1 -1
- package/dist/client/types.d.ts +26 -6
- package/dist/client/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/readme.md +1 -1
- package/scripts/generate-apis.ts +134 -134
- package/src/api/account.ts +8368 -8367
- package/src/api/agent-tools.ts +2172 -2169
- package/src/api/agent.ts +1085 -1083
- package/src/api/ai.ts +3388 -3388
- package/src/api/apiManagement.ts +570 -570
- package/src/api/apiRuntime.ts +2103 -2103
- package/src/api/cloudAccount.ts +1239 -1239
- package/src/api/cloudPlatform.ts +927 -927
- package/src/api/cloudPlatformHorizon.ts +2655 -2655
- package/src/api/cloudRuntimes.ts +2043 -2043
- package/src/api/cloudServices.ts +1445 -1445
- package/src/api/codeShift.ts +3567 -3481
- package/src/api/content.ts +9785 -9785
- package/src/api/dataIntegration.ts +1657 -1657
- package/src/api/discover.ts +435 -435
- package/src/api/eventBus.ts +171 -171
- package/src/api/genAiInference.ts +603 -603
- package/src/api/insights.ts +310 -310
- package/src/api/notification.ts +336 -334
- package/src/api/secrets.ts +342 -342
- package/src/api/serviceCatalog.ts +2908 -2908
- package/src/api/workflows.ts +1669 -1669
- package/src/api/workspace-ai.ts +677 -677
- package/src/api/workspace.ts +5889 -5889
- package/src/api/workspaceManager.ts +2951 -2951
- package/src/api/workspaceSearchEngine.ts +153 -153
- package/src/api-addresses.ts +120 -120
- package/src/apis-itau.json +225 -225
- package/src/apis.json +225 -225
- package/src/client/account.ts +902 -902
- package/src/client/agent-tools.ts +210 -210
- package/src/client/agent.ts +81 -81
- package/src/client/ai.ts +469 -395
- package/src/client/api-management.ts +40 -40
- package/src/client/cloud-account.ts +70 -70
- package/src/client/cloud-platform-horizon.ts +113 -113
- package/src/client/cloud-platform.ts +163 -163
- package/src/client/cloud-runtimes.ts +129 -129
- package/src/client/cloud-services.ts +94 -94
- package/src/client/code-shift.ts +371 -349
- package/src/client/content.ts +538 -538
- package/src/client/data-integration.ts +191 -191
- package/src/client/discover.ts +89 -89
- package/src/client/event-bus.ts +84 -84
- package/src/client/gen-ai-inference.ts +65 -65
- package/src/client/insights.ts +28 -28
- package/src/client/notification.ts +32 -32
- package/src/client/runtime-manager.ts +76 -76
- package/src/client/secrets.ts +60 -60
- package/src/client/types.ts +398 -377
- package/src/client/workflow.ts +83 -83
- package/src/client/workspace-ai.ts +191 -191
- package/src/client/workspace-manager.ts +564 -564
- package/src/client/workspace-search.ts +39 -39
- package/src/client/workspace.ts +480 -480
- package/src/error/DefaultAPIError.ts +151 -151
- package/src/error/FileUploadError.ts +18 -18
- package/src/error/IgnoredErrorCodes.ts +3 -3
- package/src/error/StackspotAPIError.ts +101 -101
- package/src/error/StreamCanceledError.ts +10 -10
- package/src/error/StreamError.ts +7 -7
- package/src/error/StreamJsonError.ts +10 -10
- package/src/error/dictionary/account.ts +58 -58
- package/src/error/dictionary/action-details.ts +20 -20
- package/src/error/dictionary/action.ts +211 -211
- package/src/error/dictionary/agent-tools.ts +75 -75
- package/src/error/dictionary/ai-inference.ts +28 -28
- package/src/error/dictionary/base.ts +22 -22
- package/src/error/dictionary/cloud-platform.ts +82 -82
- package/src/error/dictionary/cnt-fields.ts +14 -14
- package/src/error/dictionary/cnt.ts +103 -103
- package/src/error/dictionary/code-shift.ts +12 -12
- package/src/error/dictionary/rte.ts +24 -24
- package/src/error/dictionary/rtm.ts +10 -10
- package/src/error/dictionary/secrets.ts +14 -14
- package/src/error/dictionary/workspace-ai.ts +10 -10
- package/src/error/dictionary/workspace-details.ts +15 -15
- package/src/error/dictionary/workspace-fields.ts +10 -10
- package/src/error/dictionary/workspace.ts +209 -209
- package/src/error/types.ts +21 -21
- package/src/index.ts +43 -43
- package/src/network/AutoInfiniteQuery.ts +115 -115
- package/src/network/AutoMutation.ts +27 -27
- package/src/network/AutoOperation.ts +73 -73
- package/src/network/AutoQuery.ts +75 -75
- package/src/network/ManualInfiniteQuery.ts +95 -95
- package/src/network/ManualMutation.ts +40 -40
- package/src/network/ManualOperation.ts +52 -52
- package/src/network/ManualQuery.ts +82 -82
- package/src/network/NetworkClient.ts +167 -167
- package/src/network/ReactQueryNetworkClient.ts +312 -312
- package/src/network/react-query-client.ts +14 -14
- package/src/network/types.ts +294 -294
- package/src/types.ts +1 -1
- package/src/utils/StreamedArray.tsx +146 -146
- package/src/utils/StreamedJson.tsx +166 -166
- package/src/utils/remove-authorization-param.ts +6 -6
- package/src/utils/string.ts +19 -19
- package/src/utils/use-extended-list.ts +80 -80
- package/src/utils/use-streamed-array.ts +17 -17
- package/tsconfig.build.json +4 -4
- package/tsconfig.json +10 -10
|
@@ -1,167 +1,167 @@
|
|
|
1
|
-
import { AuthenticationError, SessionExpiredError } from '@stack-spot/auth'
|
|
2
|
-
import { requestPermission } from '@stack-spot/opa'
|
|
3
|
-
import { events } from 'fetch-event-stream'
|
|
4
|
-
import { getBaseUrlByTenantWithOverride } from '../api-addresses'
|
|
5
|
-
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
6
|
-
import { Env, FetchEventStream, HTTPMethod, SessionManager, Tenant } from './types'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A set of methods for performing network requests to an API.
|
|
10
|
-
*
|
|
11
|
-
* The requests are authenticated unless there's no session available.
|
|
12
|
-
*
|
|
13
|
-
* In order for a network client to work properly, the general setup for the class `NetworkClient` must be made before any request is
|
|
14
|
-
* attempted:
|
|
15
|
-
*
|
|
16
|
-
* ```
|
|
17
|
-
* NetworkClient.setup(mySessionManager, currentEnv)
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
export abstract class NetworkClient {
|
|
21
|
-
private baseURL: Record<Env, string>
|
|
22
|
-
static readonly sessionManager?: SessionManager
|
|
23
|
-
private static env?: Env
|
|
24
|
-
private static tenant?: Tenant
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @param baseURL An object with the keys "dev", "stg" and "prd". The values must be the url for each of these environments.
|
|
28
|
-
*/
|
|
29
|
-
constructor(baseURL: Record<Env, string>) {
|
|
30
|
-
this.baseURL = baseURL
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Sets up all network clients. Must be called before attempting to make any request.
|
|
35
|
-
* @param sessionManager An object with functions capable of checking, retrieving and ending the current session.
|
|
36
|
-
* @param env The environment to send the requests to.
|
|
37
|
-
* @param tenant - {@link Tenant} tenant identifier used to scope requests.
|
|
38
|
-
*/
|
|
39
|
-
static setup(sessionManager: SessionManager, env: Env, tenant?: Tenant) {
|
|
40
|
-
//@ts-ignore This is the only place we would link session manager to be set.
|
|
41
|
-
NetworkClient.sessionManager = sessionManager
|
|
42
|
-
NetworkClient.env = env
|
|
43
|
-
NetworkClient.tenant = tenant
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Retrieves the current environment configuration.
|
|
48
|
-
*
|
|
49
|
-
* This method returns the environment setting for the `NetworkClient`.
|
|
50
|
-
* If no environment is explicitly set, it defaults to `'prd'` (production).
|
|
51
|
-
*
|
|
52
|
-
* @returns {Env} The current environment configuration, either the value of `NetworkClient.env` or `'prd'` if not set.
|
|
53
|
-
*/
|
|
54
|
-
static getEnv(): Env {
|
|
55
|
-
return NetworkClient.env || 'prd'
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private uninitializedError() {
|
|
59
|
-
return new Error('Please, call "NetworkClient.setup(sessionManager, env)" before attempting to make a request.')
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Resolves the final base URL for the current environment.
|
|
64
|
-
* If a local API override exists for the current environment and current resource (api name), it will be use instead.
|
|
65
|
-
* @returns the final baseURL for the current environment, considering overrides if available.
|
|
66
|
-
*/
|
|
67
|
-
protected getBaseURL(): string {
|
|
68
|
-
return getBaseUrlByTenantWithOverride(this.baseURL, NetworkClient.env, NetworkClient.tenant);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Builds a URL with the `baseUrl` of this network client and the `path` passed as parameter.
|
|
73
|
-
* @param path the path to the resource.
|
|
74
|
-
* @returns a full URL.
|
|
75
|
-
*/
|
|
76
|
-
protected resolveURL(path: string) {
|
|
77
|
-
if (!NetworkClient.env) throw this.uninitializedError()
|
|
78
|
-
// paths must not start with "/", otherwise, the base url will not be fully appended to it.
|
|
79
|
-
const fixedPath = path.replace(/^\//, '')
|
|
80
|
-
// the baseUrl must end with "/", otherwise, the last of its part will get replaced by the path, instead of concatenated with.
|
|
81
|
-
const fixedBaseUrl = this.getBaseURL().replace(/([^/])$/, '$1/')
|
|
82
|
-
return new URL(fixedPath, fixedBaseUrl)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Verifies if the current user is allowed to send the given request.
|
|
87
|
-
* @param method the request's method.
|
|
88
|
-
* @param path the path to the resource.
|
|
89
|
-
* @param body the request's body.
|
|
90
|
-
* @returns a promise that resolves to true if it's allowed or false otherwise.
|
|
91
|
-
*/
|
|
92
|
-
protected async requestPermission(method: HTTPMethod, path: string, body?: string | object): Promise<boolean> {
|
|
93
|
-
try {
|
|
94
|
-
return await requestPermission(method, this.resolveURL(path).toString(), body)
|
|
95
|
-
} catch (error) {
|
|
96
|
-
if (error instanceof AuthenticationError) {
|
|
97
|
-
this.getSessionManager().endSession()
|
|
98
|
-
}
|
|
99
|
-
return false
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
private getSessionManager() {
|
|
104
|
-
const sessionManager = NetworkClient.sessionManager
|
|
105
|
-
if (!sessionManager) throw this.uninitializedError()
|
|
106
|
-
return sessionManager
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Makes a request (same signature as `globalThis.fetch`). This request will prepend the base url to the url and, if there's an active
|
|
111
|
-
* session, include authentication headers.
|
|
112
|
-
* @param input the url or request object.
|
|
113
|
-
* @param init the fetch options.
|
|
114
|
-
* @returns a promise with the Response.
|
|
115
|
-
*/
|
|
116
|
-
protected fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
|
|
117
|
-
const sessionManager = this.getSessionManager()
|
|
118
|
-
let inputWithBaseUrl: string | URL | Request = ''
|
|
119
|
-
if (typeof input === 'string') inputWithBaseUrl = this.resolveURL(input)
|
|
120
|
-
else if (input instanceof URL) inputWithBaseUrl = this.resolveURL(input.toString())
|
|
121
|
-
else inputWithBaseUrl = { ...input, url: this.resolveURL(input.url).toString() }
|
|
122
|
-
// some APIs throw errors if the method is lowercase, the following line prevents it
|
|
123
|
-
if (init?.method) init.method = init.method.toUpperCase()
|
|
124
|
-
try {
|
|
125
|
-
return sessionManager.hasSession() ? sessionManager.getSession().fetch(inputWithBaseUrl, init) : fetch(inputWithBaseUrl, init)
|
|
126
|
-
} catch (error) {
|
|
127
|
-
if (error instanceof AuthenticationError || error instanceof SessionExpiredError) sessionManager.endSession()
|
|
128
|
-
throw error
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Reads an EventSource from the endpoint. Differently than the original specification, this allows common HTTP requests with method and
|
|
134
|
-
* body to be made.
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* ```
|
|
138
|
-
* let events = this.stream('url', options)
|
|
139
|
-
* for await (let event of events) {
|
|
140
|
-
* console.log('<<', event.data)
|
|
141
|
-
* }
|
|
142
|
-
* ```
|
|
143
|
-
* @param input the url or request object.
|
|
144
|
-
* @param init the fetch options.
|
|
145
|
-
* @returns a promise with a FetchEventStream, which is an AsyncGenerator.
|
|
146
|
-
*/
|
|
147
|
-
protected async stream(input: string | URL | Request, init?: RequestInit): Promise<FetchEventStream> {
|
|
148
|
-
const response = await this.fetch(input, init)
|
|
149
|
-
if (!response.ok) {
|
|
150
|
-
let message = `Failed to get stream response. Error status: ${response.status}.`
|
|
151
|
-
try {
|
|
152
|
-
message = await response.text()
|
|
153
|
-
} catch { /* empty */ }
|
|
154
|
-
throw new StackspotAPIError({ status: response.status, headers: response.headers, message })
|
|
155
|
-
}
|
|
156
|
-
return events(response, init?.signal)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Checks whether or not the current account is freemium.
|
|
161
|
-
* @returns true if it's a freemium account, false otherwise.
|
|
162
|
-
*/
|
|
163
|
-
protected isFreemium() {
|
|
164
|
-
const sessionManager = this.getSessionManager()
|
|
165
|
-
return sessionManager.hasSession() && !!sessionManager.getSession().getTokenData().freemium_status
|
|
166
|
-
}
|
|
167
|
-
}
|
|
1
|
+
import { AuthenticationError, SessionExpiredError } from '@stack-spot/auth'
|
|
2
|
+
import { requestPermission } from '@stack-spot/opa'
|
|
3
|
+
import { events } from 'fetch-event-stream'
|
|
4
|
+
import { getBaseUrlByTenantWithOverride } from '../api-addresses'
|
|
5
|
+
import { StackspotAPIError } from '../error/StackspotAPIError'
|
|
6
|
+
import { Env, FetchEventStream, HTTPMethod, SessionManager, Tenant } from './types'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A set of methods for performing network requests to an API.
|
|
10
|
+
*
|
|
11
|
+
* The requests are authenticated unless there's no session available.
|
|
12
|
+
*
|
|
13
|
+
* In order for a network client to work properly, the general setup for the class `NetworkClient` must be made before any request is
|
|
14
|
+
* attempted:
|
|
15
|
+
*
|
|
16
|
+
* ```
|
|
17
|
+
* NetworkClient.setup(mySessionManager, currentEnv)
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export abstract class NetworkClient {
|
|
21
|
+
private baseURL: Record<Env, string>
|
|
22
|
+
static readonly sessionManager?: SessionManager
|
|
23
|
+
private static env?: Env
|
|
24
|
+
private static tenant?: Tenant
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param baseURL An object with the keys "dev", "stg" and "prd". The values must be the url for each of these environments.
|
|
28
|
+
*/
|
|
29
|
+
constructor(baseURL: Record<Env, string>) {
|
|
30
|
+
this.baseURL = baseURL
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Sets up all network clients. Must be called before attempting to make any request.
|
|
35
|
+
* @param sessionManager An object with functions capable of checking, retrieving and ending the current session.
|
|
36
|
+
* @param env The environment to send the requests to.
|
|
37
|
+
* @param tenant - {@link Tenant} tenant identifier used to scope requests.
|
|
38
|
+
*/
|
|
39
|
+
static setup(sessionManager: SessionManager, env: Env, tenant?: Tenant) {
|
|
40
|
+
//@ts-ignore This is the only place we would link session manager to be set.
|
|
41
|
+
NetworkClient.sessionManager = sessionManager
|
|
42
|
+
NetworkClient.env = env
|
|
43
|
+
NetworkClient.tenant = tenant
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Retrieves the current environment configuration.
|
|
48
|
+
*
|
|
49
|
+
* This method returns the environment setting for the `NetworkClient`.
|
|
50
|
+
* If no environment is explicitly set, it defaults to `'prd'` (production).
|
|
51
|
+
*
|
|
52
|
+
* @returns {Env} The current environment configuration, either the value of `NetworkClient.env` or `'prd'` if not set.
|
|
53
|
+
*/
|
|
54
|
+
static getEnv(): Env {
|
|
55
|
+
return NetworkClient.env || 'prd'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private uninitializedError() {
|
|
59
|
+
return new Error('Please, call "NetworkClient.setup(sessionManager, env)" before attempting to make a request.')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Resolves the final base URL for the current environment.
|
|
64
|
+
* If a local API override exists for the current environment and current resource (api name), it will be use instead.
|
|
65
|
+
* @returns the final baseURL for the current environment, considering overrides if available.
|
|
66
|
+
*/
|
|
67
|
+
protected getBaseURL(): string {
|
|
68
|
+
return getBaseUrlByTenantWithOverride(this.baseURL, NetworkClient.env, NetworkClient.tenant);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Builds a URL with the `baseUrl` of this network client and the `path` passed as parameter.
|
|
73
|
+
* @param path the path to the resource.
|
|
74
|
+
* @returns a full URL.
|
|
75
|
+
*/
|
|
76
|
+
protected resolveURL(path: string) {
|
|
77
|
+
if (!NetworkClient.env) throw this.uninitializedError()
|
|
78
|
+
// paths must not start with "/", otherwise, the base url will not be fully appended to it.
|
|
79
|
+
const fixedPath = path.replace(/^\//, '')
|
|
80
|
+
// the baseUrl must end with "/", otherwise, the last of its part will get replaced by the path, instead of concatenated with.
|
|
81
|
+
const fixedBaseUrl = this.getBaseURL().replace(/([^/])$/, '$1/')
|
|
82
|
+
return new URL(fixedPath, fixedBaseUrl)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Verifies if the current user is allowed to send the given request.
|
|
87
|
+
* @param method the request's method.
|
|
88
|
+
* @param path the path to the resource.
|
|
89
|
+
* @param body the request's body.
|
|
90
|
+
* @returns a promise that resolves to true if it's allowed or false otherwise.
|
|
91
|
+
*/
|
|
92
|
+
protected async requestPermission(method: HTTPMethod, path: string, body?: string | object): Promise<boolean> {
|
|
93
|
+
try {
|
|
94
|
+
return await requestPermission(method, this.resolveURL(path).toString(), body)
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (error instanceof AuthenticationError) {
|
|
97
|
+
this.getSessionManager().endSession()
|
|
98
|
+
}
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private getSessionManager() {
|
|
104
|
+
const sessionManager = NetworkClient.sessionManager
|
|
105
|
+
if (!sessionManager) throw this.uninitializedError()
|
|
106
|
+
return sessionManager
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Makes a request (same signature as `globalThis.fetch`). This request will prepend the base url to the url and, if there's an active
|
|
111
|
+
* session, include authentication headers.
|
|
112
|
+
* @param input the url or request object.
|
|
113
|
+
* @param init the fetch options.
|
|
114
|
+
* @returns a promise with the Response.
|
|
115
|
+
*/
|
|
116
|
+
protected fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> {
|
|
117
|
+
const sessionManager = this.getSessionManager()
|
|
118
|
+
let inputWithBaseUrl: string | URL | Request = ''
|
|
119
|
+
if (typeof input === 'string') inputWithBaseUrl = this.resolveURL(input)
|
|
120
|
+
else if (input instanceof URL) inputWithBaseUrl = this.resolveURL(input.toString())
|
|
121
|
+
else inputWithBaseUrl = { ...input, url: this.resolveURL(input.url).toString() }
|
|
122
|
+
// some APIs throw errors if the method is lowercase, the following line prevents it
|
|
123
|
+
if (init?.method) init.method = init.method.toUpperCase()
|
|
124
|
+
try {
|
|
125
|
+
return sessionManager.hasSession() ? sessionManager.getSession().fetch(inputWithBaseUrl, init) : fetch(inputWithBaseUrl, init)
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error instanceof AuthenticationError || error instanceof SessionExpiredError) sessionManager.endSession()
|
|
128
|
+
throw error
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Reads an EventSource from the endpoint. Differently than the original specification, this allows common HTTP requests with method and
|
|
134
|
+
* body to be made.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```
|
|
138
|
+
* let events = this.stream('url', options)
|
|
139
|
+
* for await (let event of events) {
|
|
140
|
+
* console.log('<<', event.data)
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
* @param input the url or request object.
|
|
144
|
+
* @param init the fetch options.
|
|
145
|
+
* @returns a promise with a FetchEventStream, which is an AsyncGenerator.
|
|
146
|
+
*/
|
|
147
|
+
protected async stream(input: string | URL | Request, init?: RequestInit): Promise<FetchEventStream> {
|
|
148
|
+
const response = await this.fetch(input, init)
|
|
149
|
+
if (!response.ok) {
|
|
150
|
+
let message = `Failed to get stream response. Error status: ${response.status}.`
|
|
151
|
+
try {
|
|
152
|
+
message = await response.text()
|
|
153
|
+
} catch { /* empty */ }
|
|
154
|
+
throw new StackspotAPIError({ status: response.status, headers: response.headers, message })
|
|
155
|
+
}
|
|
156
|
+
return events(response, init?.signal)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Checks whether or not the current account is freemium.
|
|
161
|
+
* @returns true if it's a freemium account, false otherwise.
|
|
162
|
+
*/
|
|
163
|
+
protected isFreemium() {
|
|
164
|
+
const sessionManager = this.getSessionManager()
|
|
165
|
+
return sessionManager.hasSession() && !!sessionManager.getSession().getTokenData().freemium_status
|
|
166
|
+
}
|
|
167
|
+
}
|