@stack-spot/portal-network 0.208.3 → 0.210.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 (44) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/api/accountAssetManager.d.ts +279 -0
  3. package/dist/api/accountAssetManager.d.ts.map +1 -0
  4. package/dist/api/accountAssetManager.js +167 -0
  5. package/dist/api/accountAssetManager.js.map +1 -0
  6. package/dist/api-addresses.d.ts +13 -20
  7. package/dist/api-addresses.d.ts.map +1 -1
  8. package/dist/api-addresses.js +59 -17
  9. package/dist/api-addresses.js.map +1 -1
  10. package/dist/apis-itau.json +8 -0
  11. package/dist/apis.json +8 -0
  12. package/dist/client/account-asset-manager.d.ts +110 -0
  13. package/dist/client/account-asset-manager.d.ts.map +1 -0
  14. package/dist/client/account-asset-manager.js +160 -0
  15. package/dist/client/account-asset-manager.js.map +1 -0
  16. package/dist/client/ai.d.ts +24 -1
  17. package/dist/client/ai.d.ts.map +1 -1
  18. package/dist/client/ai.js +19 -1
  19. package/dist/client/ai.js.map +1 -1
  20. package/dist/error/dictionary/accountAssetManager.d.ts +11 -0
  21. package/dist/error/dictionary/accountAssetManager.d.ts.map +1 -0
  22. package/dist/error/dictionary/accountAssetManager.js +11 -0
  23. package/dist/error/dictionary/accountAssetManager.js.map +1 -0
  24. package/dist/index.d.ts +2 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +2 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/network/NetworkClient.d.ts.map +1 -1
  29. package/dist/network/NetworkClient.js +11 -2
  30. package/dist/network/NetworkClient.js.map +1 -1
  31. package/dist/network/types.d.ts +1 -0
  32. package/dist/network/types.d.ts.map +1 -1
  33. package/package.json +1 -1
  34. package/readme.md +42 -1
  35. package/src/api/accountAssetManager.ts +655 -0
  36. package/src/api-addresses.ts +92 -37
  37. package/src/apis-itau.json +8 -0
  38. package/src/apis.json +8 -0
  39. package/src/client/account-asset-manager.ts +100 -0
  40. package/src/client/ai.ts +10 -0
  41. package/src/error/dictionary/accountAssetManager.ts +12 -0
  42. package/src/index.ts +2 -1
  43. package/src/network/NetworkClient.ts +12 -3
  44. package/src/network/types.ts +1 -0
@@ -1,3 +1,4 @@
1
+ import { APIMap } from '@stack-spot/opa'
1
2
  import apisItauJson from './apis-itau.json' assert { type: 'json' }
2
3
  import apisJson from './apis.json' assert { type: 'json' }
3
4
  import { NetworkClient } from './network/NetworkClient'
@@ -5,26 +6,29 @@ import { Env, Tenant } from './network/types'
5
6
 
6
7
  type ApisKeys = keyof typeof apisJson;
7
8
 
9
+ type APIName = APIMap[string]
10
+ type ExceptionAPIName = Exclude<APIName, keyof Apis>
11
+
12
+ declare global {
13
+ interface ImportMeta {
14
+ readonly env: {
15
+ VITE_NETWORK_OVERRIDES: string,
16
+ DEV: boolean,
17
+ PROD: boolean,
18
+ },
19
+ }
20
+ }
21
+
8
22
  type ApiConfig = {
9
23
  url: Record<Env, string>,
10
24
  docs?: string,
11
25
  }
12
26
 
13
- type PartialApiConfig = {
14
- url: Partial<Record<Env, string>>,
15
- docs?: string
16
- }
17
-
18
27
  type Apis = Record<ApisKeys, ApiConfig>;
19
28
 
20
- type PartialApis = Partial<Record<ApisKeys, PartialApiConfig>>;
21
-
22
- type ApiAddress = {
23
- [api: string]: string,
24
- }
29
+ type ApiAddress = Record<ApisKeys, string>
25
30
 
26
- let defaultApis: Apis = apisJson;
27
- let overrideApis: PartialApis = {};
31
+ const defaultApis: Apis = apisJson
28
32
 
29
33
  const apis: Record<Tenant, Apis> = {
30
34
  'stackspot': apisJson,
@@ -37,12 +41,12 @@ const apis: Record<Tenant, Apis> = {
37
41
  * @param tenant The current tenant (e.g., 'stackspot', 'itau').
38
42
  * @returns API catalog containing the URLs per environment.
39
43
  */
40
- export function getApisByTenant(tenant: Tenant) {
44
+ function getApisByTenant(tenant: Tenant) {
41
45
  return apis[tenant]
42
46
  }
43
47
 
44
48
  function transformApisToApiAddress(apis: Apis, env: Env): ApiAddress {
45
- const apiAddress: ApiAddress = {}
49
+ const apiAddress = {} as ApiAddress
46
50
 
47
51
  for (const api in apis) {
48
52
  const key = api as ApisKeys
@@ -59,14 +63,28 @@ function transformApisToApiAddress(apis: Apis, env: Env): ApiAddress {
59
63
  * @returns {boolean} true if the URL is found, otherwise false.
60
64
  */
61
65
  function matchesBaseUrl(baseUrl: Record<Env, string>, candidate: Record<Env, string>): boolean {
62
- const envs: Env[] = ['dev', 'stg', 'prd'];
66
+ const envs: Env[] = ['dev', 'stg', 'prd']
63
67
 
64
68
  return envs.every(e => {
65
69
  if (candidate[e]) {
66
- return baseUrl[e] === candidate[e];
70
+ return baseUrl[e] === candidate[e]
67
71
  }
68
- return true;
69
- });
72
+ return true
73
+ })
74
+ }
75
+
76
+ const getApiOverride = () => {
77
+ const envs = import.meta.env
78
+
79
+ let overrideAPIsURL = {} as ApiAddress
80
+ if (envs.PROD) return overrideAPIsURL
81
+
82
+ try {
83
+ overrideAPIsURL= JSON.parse(envs.VITE_NETWORK_OVERRIDES)
84
+ return overrideAPIsURL
85
+ } catch {
86
+ return overrideAPIsURL
87
+ }
70
88
  }
71
89
 
72
90
  /**
@@ -77,44 +95,81 @@ function matchesBaseUrl(baseUrl: Record<Env, string>, candidate: Record<Env, str
77
95
  * @param tenant the current tenant.
78
96
  * @returns {string} the base URL for the given environment and tenant, applying any overrides if available.
79
97
  */
80
- export const getBaseUrlByTenantWithOverride = (baseUrl: Record<Env, string>, targetEnv: Env = 'prd', tenant: Tenant = 'stackspot'): string => {
81
- let matchesApiName: ApisKeys | null = null;
98
+ export const getBaseUrlByTenantWithOverride = (baseUrl: Record<Env, string>, targetEnv: Env = 'prd',
99
+ tenant: Tenant = 'stackspot'): string => {
100
+ let matchesApiName: ApisKeys | null = null
82
101
 
83
102
  for (const [apiName, config] of Object.entries(defaultApis) as [ApisKeys, ApiConfig][]) {
84
103
  if (matchesBaseUrl(baseUrl, config.url)) {
85
- matchesApiName = apiName;
86
- break;
104
+ matchesApiName = apiName
105
+ break
87
106
  }
88
107
  }
89
108
 
90
109
  if (!matchesApiName) return ''
91
110
 
92
- const overrideUrl = overrideApis[matchesApiName]?.url?.[targetEnv]
93
- if (overrideUrl) {
94
- return overrideUrl;
95
- }
96
-
111
+ const overrides = getApiOverride()
97
112
  const apis = getApisByTenant(tenant)
98
- return apis[matchesApiName]?.url?.[targetEnv];
113
+ const api = apis[matchesApiName]
114
+ return overrides?.[matchesApiName] ?? api?.url?.[targetEnv]
99
115
  }
100
116
 
101
- /**
102
- * Sets the APIs addresses for override.
103
- * @param customApis a JSON object containing APIs address to override.
104
- */
105
- export const setApisOverride = (customApis: PartialApis) => {
106
- overrideApis = customApis;
117
+ export const getApisBaseUrlConfig = (env: Env = 'prd', tenant: Tenant = 'stackspot'): ApiAddress => {
118
+ const overrides = getApiOverride()
119
+ const apis = getApisByTenant(tenant)
120
+ const apiMap = {} as ApiAddress
121
+
122
+ for (const [key, value] of Object.entries(apis)) {
123
+ if (value?.url && value.url?.[env]) {
124
+ const url = value.url[env]
125
+ const api = key as ApisKeys
126
+ apiMap[api] = url
127
+ }
128
+ }
129
+
130
+ return { ...apiMap, ...overrides }
107
131
  }
108
132
 
133
+
109
134
  /**
110
135
  * Sets the default APIs addresses for each environments.
111
136
  * @returns {Apis} an object containing APIs URLs grouped by api name and enviroments.
112
137
  */
113
- export const getApiAddresses = (): Apis => {
114
- return defaultApis;
115
- }
138
+ export const getApiAddresses = (): Apis => defaultApis
116
139
 
117
140
  export const apiAddresses = () => {
118
141
  const env = NetworkClient.getEnv()
119
142
  return transformApisToApiAddress(defaultApis, env)
120
143
  }
144
+
145
+ const networkApiNameToOpaApiName = {
146
+ ai: 'code-buddy',
147
+ dataIntegration: 'data-integration',
148
+ workspaceManager: 'workspace-manager',
149
+ cloudServices: 'cloud-services',
150
+ cloudAccount: 'cloud-account',
151
+ cloudPlatformHorizon: 'cloud-platform-horizon',
152
+ codeShift: 'code-shift',
153
+ genAiInference: 'ai-inference',
154
+ apiRuntime: 'runtime-manager',
155
+ serviceCatalog: 'catalog',
156
+ } as const satisfies Partial<Record<keyof Apis, ExceptionAPIName>>
157
+
158
+ function resolveApiName(name: keyof Apis): APIName {
159
+ return (networkApiNameToOpaApiName as Record<string, ExceptionAPIName>)[name] ?? name
160
+ }
161
+
162
+ function generateApiMap(apis: ApiAddress) {
163
+ const apiMap: APIMap = {}
164
+ for (const [key, url] of Object.entries(apis)) {
165
+ if (url) {
166
+ apiMap[url] = resolveApiName(key as keyof Apis)
167
+ }
168
+ }
169
+ return apiMap
170
+ }
171
+
172
+ export function getPermissionsAPIMap(env: Env, tenant: Tenant = 'stackspot'): APIMap {
173
+ const apis = getApisBaseUrlConfig(env, tenant)
174
+ return generateApiMap(apis)
175
+ }
@@ -222,6 +222,14 @@
222
222
  },
223
223
  "docs": "/v3/api-docs"
224
224
  },
225
+ "accountAssetManager" : {
226
+ "url": {
227
+ "dev": "https://account-asset-manager.dev.stackspot.com",
228
+ "stg": "https://account-asset-manager.stg.stackspot.com",
229
+ "prd": "https://account-asset-manager.stackspot.com"
230
+ },
231
+ "docs": "/v3/api-docs"
232
+ },
225
233
  "edpBfa": {
226
234
  "url": {
227
235
  "dev": "https://adp-development-edp-bfa.dev.stackspot.com",
package/src/apis.json CHANGED
@@ -222,6 +222,14 @@
222
222
  },
223
223
  "docs": "/v3/api-docs"
224
224
  },
225
+ "accountAssetManager" : {
226
+ "url": {
227
+ "dev": "https://account-asset-manager.dev.stackspot.com",
228
+ "stg": "https://account-asset-manager.stg.stackspot.com",
229
+ "prd": "https://account-asset-manager.stackspot.com"
230
+ },
231
+ "docs": "/v3/api-docs"
232
+ },
225
233
  "edpBfa": {
226
234
  "url": {
227
235
  "dev": "https://adp-development-edp-bfa.dev.stackspot.com",
@@ -0,0 +1,100 @@
1
+ import { HttpError } from '@oazapfts/runtime'
2
+ import { getApiAddresses } from '../api-addresses'
3
+ import { defaults, deleteAssetType, deleteFolder, deleteProject, disassociateProjectAsset, getFolderDetails, getProjectDetails, listAssetTypes, listFolders, saveAssetType, saveFolder, saveProject, updateAsset, updateAssetType, updateFolders, updateProject } from '../api/accountAssetManager'
4
+ import { DefaultAPIError } from '../error/DefaultAPIError'
5
+ import { accountAssetManagerDictionary } from '../error/dictionary/accountAssetManager'
6
+ import { StackspotAPIError } from '../error/StackspotAPIError'
7
+ import { ReactQueryNetworkClient } from '../network/ReactQueryNetworkClient'
8
+ import { removeAuthorizationParam } from '../utils/remove-authorization-param'
9
+
10
+ class AccountAssetManagerClient extends ReactQueryNetworkClient {
11
+ constructor() {
12
+ super(getApiAddresses().accountAssetManager.url, defaults)
13
+ }
14
+
15
+ protected buildStackSpotError(error: HttpError): StackspotAPIError {
16
+ return new DefaultAPIError(error.data, error.status, accountAssetManagerDictionary, error.headers)
17
+ }
18
+
19
+ /**
20
+ * Save project
21
+ */
22
+ saveProject = this.mutation(removeAuthorizationParam(saveProject))
23
+
24
+ /**
25
+ * Get project details
26
+ */
27
+ getProjectDetails = this.query(removeAuthorizationParam(getProjectDetails))
28
+
29
+ /**
30
+ * Delete project
31
+ */
32
+ deleteProject = this.mutation(removeAuthorizationParam(deleteProject))
33
+
34
+ /**
35
+ * Update project
36
+ */
37
+ updateProject = this.mutation(removeAuthorizationParam(updateProject))
38
+
39
+ /**
40
+ * Disassociate project asset
41
+ */
42
+ disassociateProjectAsset = this.mutation(removeAuthorizationParam(disassociateProjectAsset))
43
+
44
+ /**
45
+ * List account folders
46
+ */
47
+ folders = this.query(removeAuthorizationParam(listFolders))
48
+
49
+ /**
50
+ * Create new folder in account
51
+ */
52
+ createFolder = this.mutation(removeAuthorizationParam(saveFolder))
53
+
54
+ /**
55
+ * Get folder details
56
+ */
57
+ folderDetails = this.query(removeAuthorizationParam(getFolderDetails))
58
+
59
+ /**
60
+ * Delete folder
61
+ */
62
+ deleteFolder = this.mutation(removeAuthorizationParam(deleteFolder))
63
+
64
+ /**
65
+ * Update folder
66
+ */
67
+ updateFolder = this.mutation(removeAuthorizationParam(updateFolders))
68
+
69
+ /**
70
+ * Create asset
71
+ */
72
+ createAsset = this.mutation(removeAuthorizationParam(saveAssetType))
73
+
74
+ /**
75
+ * Update asset
76
+ */
77
+ updateAsset = this.mutation(removeAuthorizationParam(updateAsset))
78
+
79
+ /**
80
+ * List all asset types
81
+ */
82
+ assetTypes = this.query(removeAuthorizationParam(listAssetTypes))
83
+
84
+ /**
85
+ * Save asset type
86
+ */
87
+ saveAssetType = this.mutation(removeAuthorizationParam(saveAssetType))
88
+
89
+ /**
90
+ * Delete asset type
91
+ */
92
+ deleteAssetType = this.mutation(removeAuthorizationParam(deleteAssetType))
93
+
94
+ /**
95
+ * Update asset type
96
+ */
97
+ updateAssetType = this.mutation(removeAuthorizationParam(updateAssetType))
98
+ }
99
+
100
+ export const accountAssetManagerClient = new AccountAssetManagerClient()
package/src/client/ai.ts CHANGED
@@ -45,6 +45,8 @@ import {
45
45
  resetKnowledgeObjectsV1KnowledgeSourcesSlugObjectsDelete,
46
46
  runFetchStepV1QuickCommandsSlugStepsStepSlugFetchRunPost,
47
47
  searchKnowledgeSourcesV1KnowledgeSourcesSearchPost,
48
+ tokensByUserV1AnalyticsTokensByUserGet,
49
+ tokensDailyUsageV1AnalyticsTokensDailyUsageGet,
48
50
  startScriptStepV1QuickCommandsSlugStepsStepSlugStartScriptPost,
49
51
  totalV1TokensUsageTotalGet,
50
52
  updateQuickCommandV1QuickCommandsSlugPatch,
@@ -277,6 +279,14 @@ class AIClient extends ReactQueryNetworkClient {
277
279
  */
278
280
  removeFavoriteQuickCommand = this.mutation(removeAuthorizationParam(deleteFavoriteV1QuickCommandsSlugFavoriteDelete))
279
281
  /**
282
+ * Get Tokens Daily usage
283
+ */
284
+ analyticsTokensDailyUsage = this.query(removeAuthorizationParam(tokensDailyUsageV1AnalyticsTokensDailyUsageGet))
285
+ /**
286
+ * Get Tokens By User
287
+ */
288
+ analyticsTokensByUser = this.query(removeAuthorizationParam(tokensByUserV1AnalyticsTokensByUserGet))
289
+ /*
280
290
  * Lists all the feature flags for this user.
281
291
  */
282
292
  flags = this.query(getFlagsV1FlagsGet)
@@ -0,0 +1,12 @@
1
+ import { Dictionary } from '@stack-spot/portal-translate'
2
+
3
+ export const accountAssetManagerDictionary = {
4
+ en: {
5
+ FOLDER_BAD_REQUEST_ERROR: 'The provided data is invalid or inconsistent.',
6
+ FOLDER_ALREADY_EXISTS: 'A folder with this name already exists.',
7
+ },
8
+ pt: {
9
+ FOLDER_BAD_REQUEST_ERROR: 'Os dados informados estão inválidos ou inconsistentes.',
10
+ FOLDER_ALREADY_EXISTS: 'Já existe uma pasta com esse nome.',
11
+ },
12
+ } satisfies Dictionary
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { CancelledError } from '@tanstack/react-query'
2
- export { apiAddresses, getApiAddresses, getApisByTenant, getBaseUrlByTenantWithOverride, setApisOverride } from './api-addresses'
2
+ export { apiAddresses, getApiAddresses, getApisBaseUrlConfig, getBaseUrlByTenantWithOverride } from './api-addresses'
3
3
  export { accountClient } from './client/account'
4
+ export { accountAssetManagerClient } from './client/account-asset-manager'
4
5
  export { agentClient } from './client/agent'
5
6
  export { agentToolsClient } from './client/agent-tools'
6
7
  export { aiClient } from './client/ai'
@@ -1,8 +1,9 @@
1
1
  import { AuthenticationError, SessionExpiredError } from '@stack-spot/auth'
2
- import { requestPermission } from '@stack-spot/opa'
2
+ import { requestPermission, setup as setupPermissions } from '@stack-spot/opa'
3
3
  import { events } from 'fetch-event-stream'
4
- import { getBaseUrlByTenantWithOverride } from '../api-addresses'
4
+ import { getApisBaseUrlConfig, getBaseUrlByTenantWithOverride, getPermissionsAPIMap } from '../api-addresses'
5
5
  import { StackspotAPIError } from '../error/StackspotAPIError'
6
+ import { queryClient } from './react-query-client'
6
7
  import { Env, FetchEventStream, HTTPMethod, SessionManager, Tenant } from './types'
7
8
 
8
9
  /**
@@ -41,6 +42,14 @@ export abstract class NetworkClient {
41
42
  NetworkClient.sessionManager = sessionManager
42
43
  NetworkClient.env = env
43
44
  NetworkClient.tenant = tenant
45
+ const url = getApisBaseUrlConfig(env, tenant).permissionValidation
46
+ const apiMap = getPermissionsAPIMap(env, tenant)
47
+ sessionManager.onChange?.((session) => {
48
+ if (session) {
49
+ queryClient.invalidateQueries()
50
+ setupPermissions({ url, session, apiMap })
51
+ }
52
+ })
44
53
  }
45
54
 
46
55
  /**
@@ -65,7 +74,7 @@ export abstract class NetworkClient {
65
74
  * @returns the final baseURL for the current environment, considering overrides if available.
66
75
  */
67
76
  protected getBaseURL(): string {
68
- return getBaseUrlByTenantWithOverride(this.baseURL, NetworkClient.env, NetworkClient.tenant);
77
+ return getBaseUrlByTenantWithOverride(this.baseURL, NetworkClient.env, NetworkClient.tenant)
69
78
  }
70
79
 
71
80
  /**
@@ -8,6 +8,7 @@ export interface SessionManager {
8
8
  hasSession(): boolean,
9
9
  endSession(): void,
10
10
  getSession(): Session,
11
+ onChange?: (listener: (session: Session | undefined) => void) => (() => void),
11
12
  }
12
13
 
13
14
  export type Tenant = 'stackspot' | 'itau'