@approvio/ts-sdk 0.0.27 → 1.0.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/dist/index.mjs CHANGED
@@ -31,7 +31,7 @@ function validateURL(url) {
31
31
  function removeTrailingSlash(url) {
32
32
  return url.endsWith("/") ? url.slice(0, -1) : url;
33
33
  }
34
- const USER_AGENT = `Approvio-TS-SDK/0.0.27`;
34
+ const USER_AGENT = `Approvio-TS-SDK/1.0.0`;
35
35
  //#endregion
36
36
  //#region src/client/http.ts
37
37
  /**
@@ -217,13 +217,15 @@ var AuthHelper = class {
217
217
  * Handles automatic token renewal using a refresh token and DPoP proof when the access token expires.
218
218
  */
219
219
  var AgentAuthenticator = class {
220
+ organizationId;
220
221
  privateKey;
221
222
  accessToken;
222
223
  refreshToken;
223
224
  onTokenRefreshed;
224
225
  endpoint;
225
226
  axios;
226
- constructor(endpoint, privateKey, accessToken, refreshToken, onTokenRefreshed) {
227
+ constructor(endpoint, organizationId, privateKey, accessToken, refreshToken, onTokenRefreshed) {
228
+ this.organizationId = organizationId;
227
229
  this.privateKey = privateKey;
228
230
  this.accessToken = accessToken;
229
231
  this.refreshToken = refreshToken;
@@ -254,8 +256,9 @@ var AgentAuthenticator = class {
254
256
  }
255
257
  async renewToken() {
256
258
  const request = { refreshToken: this.refreshToken };
257
- const dpopProof = await this.generateDpopProof("POST", `${this.endpoint}/auth/agents/refresh`);
258
- return (await this.axios.post("/auth/agents/refresh", request, { headers: { DPoP: dpopProof } })).data;
259
+ const refreshPath = `/o/${encodeURIComponent(this.organizationId)}/auth/agents/refresh`;
260
+ const dpopProof = await this.generateDpopProof("POST", `${this.endpoint}${refreshPath}`);
261
+ return (await this.axios.post(refreshPath, request, { headers: { DPoP: dpopProof } })).data;
259
262
  }
260
263
  async generateDpopProof(method, url) {
261
264
  const privateKey = await jose.importPKCS8(this.privateKey, "RS256");
@@ -290,6 +293,20 @@ var UnexpectedEntityTypeError = class extends Error {
290
293
  this.received = received;
291
294
  }
292
295
  };
296
+ var OrganizationContextChangedError = class extends Error {
297
+ status;
298
+ constructor(status, message) {
299
+ super(message);
300
+ this.status = status;
301
+ }
302
+ };
303
+ var OrganizationSuspendedError = class extends Error {
304
+ status;
305
+ constructor(status, message) {
306
+ super(message);
307
+ this.status = status;
308
+ }
309
+ };
293
310
  //#endregion
294
311
  //#region src/client/base.client.ts
295
312
  const networkCodes = [
@@ -320,10 +337,14 @@ var BaseApprovioClient = class {
320
337
  if (networkCodes.includes(error.code ?? "")) return new NetworkError(`Network error ${error.code}`);
321
338
  const status = error.response?.status;
322
339
  const data = error.response?.data;
323
- if (status && isApprovioError(data)) return {
324
- ...data,
325
- status
326
- };
340
+ if (status && isApprovioError(data)) {
341
+ if (data.code === "ORGANIZATION_CONTEXT_CHANGED") return new OrganizationContextChangedError(status, data.message);
342
+ if (data.code === "ORGANIZATION_SUSPENDED") return new OrganizationSuspendedError(status, data.message);
343
+ return {
344
+ ...data,
345
+ status
346
+ };
347
+ }
327
348
  }
328
349
  if (error instanceof Error) return error;
329
350
  return new Error(String(error));
@@ -336,12 +357,34 @@ var BaseApprovioClient = class {
336
357
  return (await this.axios.get(url, { params })).data;
337
358
  }, (error) => this.handleError(error));
338
359
  }
360
+ getVersioned(url, params) {
361
+ return TE.tryCatch(async () => {
362
+ const response = await this.axios.get(url, { params });
363
+ const etag = response.headers.etag;
364
+ if (typeof etag !== "string" || !etag) throw new Error("ETag header was expected but not found");
365
+ return {
366
+ data: response.data,
367
+ etag
368
+ };
369
+ }, (error) => this.handleError(error));
370
+ }
339
371
  /**
340
372
  * Performs a POST request.
341
373
  */
342
- post(url, data) {
374
+ post(url, data, headers) {
375
+ return TE.tryCatch(async () => {
376
+ return (headers ? await this.axios.post(url, data, { headers }) : await this.axios.post(url, data)).data;
377
+ }, (error) => this.handleError(error));
378
+ }
379
+ postVersioned(url, data, headers) {
343
380
  return TE.tryCatch(async () => {
344
- return (await this.axios.post(url, data)).data;
381
+ const response = await this.axios.post(url, data, { headers });
382
+ const etag = response.headers.etag;
383
+ if (typeof etag !== "string" || !etag) throw new Error("ETag header was expected but not found");
384
+ return {
385
+ data: response.data,
386
+ etag
387
+ };
345
388
  }, (error) => this.handleError(error));
346
389
  }
347
390
  postWithLocation(url, data) {
@@ -358,76 +401,84 @@ var BaseApprovioClient = class {
358
401
  /**
359
402
  * Performs a PUT request.
360
403
  */
361
- put(url, data) {
404
+ put(url, data, headers) {
362
405
  return TE.tryCatch(async () => {
363
- return (await this.axios.put(url, data)).data;
406
+ return (headers ? await this.axios.put(url, data, { headers }) : await this.axios.put(url, data)).data;
407
+ }, (error) => this.handleError(error));
408
+ }
409
+ patch(url, data, headers) {
410
+ return TE.tryCatch(async () => {
411
+ return (headers ? await this.axios.patch(url, data, { headers }) : await this.axios.patch(url, data)).data;
364
412
  }, (error) => this.handleError(error));
365
413
  }
366
414
  /**
367
415
  * Performs a DELETE request.
368
416
  */
369
- delete(url, data) {
417
+ delete(url, data, headers) {
370
418
  return TE.tryCatch(async () => {
371
- const config = data !== void 0 ? { data } : void 0;
419
+ const config = data !== void 0 || headers ? {
420
+ data,
421
+ headers
422
+ } : void 0;
372
423
  return (await this.axios.delete(url, config)).data;
373
424
  }, (error) => this.handleError(error));
374
425
  }
375
- /**
376
- * Get workflow details.
377
- */
426
+ };
427
+ //#endregion
428
+ //#region src/client/organization.client.ts
429
+ /**
430
+ * A tenant handle whose organization cannot change after construction.
431
+ *
432
+ * Create another handle to use another organization. This prevents a browser
433
+ * context switch or a caller mistake from retargeting an in-flight client.
434
+ */
435
+ var OrganizationClient = class extends BaseApprovioClient {
436
+ organizationId;
437
+ constructor(config, authenticator, organizationId) {
438
+ super(config, authenticator);
439
+ this.organizationId = organizationId;
440
+ }
441
+ organizationPath(path) {
442
+ return `/o/${encodeURIComponent(this.organizationId)}${path}`;
443
+ }
378
444
  getWorkflow(workflowId, params) {
379
- return this.get(`/workflows/${workflowId}`, params);
445
+ return this.get(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}`), params);
380
446
  }
381
- /**
382
- * List workflows.
383
- */
384
447
  listWorkflows(params) {
385
- const queryParams = {
386
- page: params?.page,
387
- limit: params?.limit,
388
- includeOnlyNonTerminalState: params?.includeOnlyNonTerminalState,
389
- include: params?.include
390
- };
391
- return this.get("/workflows", queryParams);
448
+ return this.get(this.organizationPath("/workflows"), params);
392
449
  }
393
- /**
394
- * Create a new workflow.
395
- */
396
450
  createWorkflow(data) {
397
- return this.post("/workflows", data);
451
+ return this.post(this.organizationPath("/workflows"), data);
398
452
  }
399
- /**
400
- * Vote on a workflow.
401
- */
402
453
  voteOnWorkflow(workflowId, data) {
403
- return this.post(`/workflows/${workflowId}/vote`, data);
454
+ return this.post(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}/vote`), data);
404
455
  }
405
- /**
406
- * Check if the current entity can vote on a workflow.
407
- */
408
456
  canVoteOnWorkflow(workflowId) {
409
- return this.get(`/workflows/${workflowId}/canVote`);
457
+ return this.get(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}/canVote`));
410
458
  }
411
- /**
412
- * List role templates.
413
- */
414
459
  listRoleTemplates() {
415
- return this.get("/roles");
460
+ return this.get(this.organizationPath("/roles"));
416
461
  }
417
- /**
418
- * Get authenticated entity information.
419
- */
420
462
  getEntityInfo() {
421
- return this.get("/auth/info");
463
+ return this.get(this.organizationPath("/auth/info"));
422
464
  }
423
465
  listVotes(workflowId, params) {
424
- return this.get(`/workflows/${workflowId}/votes`, params);
466
+ return this.get(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}/votes`), params);
425
467
  }
426
- /**
427
- * Get available authentication providers.
428
- */
429
- getAuthProviders() {
430
- return this.get("/auth/providers");
468
+ generateAgentChallenge(data) {
469
+ return this.post(this.organizationPath("/auth/agents/challenge"), data);
470
+ }
471
+ exchangeAgentToken(data) {
472
+ return this.post(this.organizationPath("/auth/agents/token"), data);
473
+ }
474
+ initiateWebPrivilegeToken() {
475
+ return this.post(this.organizationPath("/auth/web/initiatePrivilegedTokenExchange"));
476
+ }
477
+ exchangeWebPrivilegeToken(data) {
478
+ return this.post(this.organizationPath("/auth/web/exchangePrivilegedToken"), data);
479
+ }
480
+ exchangeCliPrivilegeToken(data) {
481
+ return this.post(this.organizationPath("/auth/cli/exchangePrivilegedToken"), data);
431
482
  }
432
483
  };
433
484
  //#endregion
@@ -435,100 +486,91 @@ var BaseApprovioClient = class {
435
486
  /**
436
487
  * Client for Approvio API (Human/User).
437
488
  */
438
- var ApprovioUserClient = class extends BaseApprovioClient {
489
+ var ApprovioUserClient = class extends OrganizationClient {
439
490
  authenticator;
440
- constructor(config, authenticator) {
441
- super(config, authenticator);
491
+ constructor(config, authenticator, organizationId) {
492
+ super(config, authenticator, organizationId);
442
493
  this.authenticator = authenticator;
443
494
  }
444
495
  listUsers(params) {
445
- return this.get("/users", params);
496
+ return this.get(this.organizationPath("/users"), params);
446
497
  }
447
498
  registerAgent(data) {
448
- return this.post("/agents/register", data);
499
+ return this.post(this.organizationPath("/agents/register"), data);
449
500
  }
450
- assignAgentRoles(agentId, data) {
451
- return this.put(`/agents/${agentId}/roles`, data);
501
+ assignAgentRoles(agentId, data, etag) {
502
+ return this.put(this.organizationPath(`/agents/${encodeURIComponent(agentId)}/roles`), data, { "If-Match": etag });
452
503
  }
453
- removeAgentRoles(agentId, data) {
454
- return this.delete(`/agents/${agentId}/roles`, data);
504
+ removeAgentRoles(agentId, data, etag) {
505
+ return this.delete(this.organizationPath(`/agents/${encodeURIComponent(agentId)}/roles`), data, { "If-Match": etag });
455
506
  }
456
507
  createUser(data) {
457
- return pipe(this.postWithLocation("/users", data), TE.map(({ location }) => location), TE.chain((location) => {
508
+ return pipe(this.postWithLocation(this.organizationPath("/users"), data), TE.map(({ location }) => location), TE.chain((location) => {
458
509
  const id = location.split("/").pop();
459
510
  if (!id) return TE.left(/* @__PURE__ */ new Error("Invalid location"));
460
511
  return TE.right(id);
461
512
  }));
462
513
  }
463
514
  getUser(userId) {
464
- return this.get(`/users/${userId}`);
515
+ return this.get(this.organizationPath(`/users/${encodeURIComponent(userId)}`));
465
516
  }
466
- assignUserRoles(userId, data) {
467
- return this.put(`/users/${userId}/roles`, data);
517
+ assignUserRoles(userId, data, etag) {
518
+ return this.put(this.organizationPath(`/users/${encodeURIComponent(userId)}/roles`), data, { "If-Match": etag });
468
519
  }
469
- removeUserRoles(userId, data) {
470
- return this.delete(`/users/${userId}/roles`, data);
520
+ removeUserRoles(userId, data, etag) {
521
+ return this.delete(this.organizationPath(`/users/${encodeURIComponent(userId)}/roles`), data, { "If-Match": etag });
471
522
  }
472
523
  createWorkflowTemplate(data) {
473
- return this.post("/workflow-templates", data);
524
+ return this.post(this.organizationPath("/workflow-templates"), data);
474
525
  }
475
526
  listWorkflowTemplates(request) {
476
- return this.get("/workflow-templates", request);
527
+ return this.get(this.organizationPath("/workflow-templates"), request);
477
528
  }
478
529
  getWorkflowTemplate(templateIdentifier) {
479
- return this.get(`/workflow-templates/${templateIdentifier}`);
530
+ return this.get(this.organizationPath(`/workflow-templates/${encodeURIComponent(templateIdentifier)}`));
480
531
  }
481
- updateWorkflowTemplate(templateIdentifier, data) {
482
- return this.put(`/workflow-templates/${templateIdentifier}`, data);
532
+ updateWorkflowTemplate(templateIdentifier, data, etag) {
533
+ return this.put(this.organizationPath(`/workflow-templates/${encodeURIComponent(templateIdentifier)}`), data, { "If-Match": etag });
483
534
  }
484
535
  deprecateWorkflowTemplate(templateName, data) {
485
- return this.post(`/workflow-templates/${templateName}/deprecate`, data);
536
+ return this.post(this.organizationPath(`/workflow-templates/${encodeURIComponent(templateName)}/deprecate`), data);
486
537
  }
487
538
  createGroup(data) {
488
- return pipe(this.postWithLocation("/groups", data), TE.map(({ location }) => location), TE.chain((location) => {
539
+ return pipe(this.postWithLocation(this.organizationPath("/groups"), data), TE.map(({ location }) => location), TE.chain((location) => {
489
540
  const id = location.split("/").pop();
490
541
  if (!id) return TE.left(/* @__PURE__ */ new Error("Invalid location"));
491
542
  return TE.right(id);
492
543
  }));
493
544
  }
494
545
  listGroups(params) {
495
- return this.get("/groups", params);
546
+ return this.get(this.organizationPath("/groups"), params);
496
547
  }
497
548
  getGroup(groupIdentifier) {
498
- return this.get(`/groups/${groupIdentifier}`);
549
+ return this.get(this.organizationPath(`/groups/${encodeURIComponent(groupIdentifier)}`));
499
550
  }
500
551
  listGroupEntities(groupId, params) {
501
- return this.get(`/groups/${groupId}/entities`, params);
552
+ return this.get(this.organizationPath(`/groups/${encodeURIComponent(groupId)}/entities`), params);
502
553
  }
503
554
  addGroupEntities(groupId, data) {
504
- return this.post(`/groups/${groupId}/entities`, data);
555
+ return this.post(this.organizationPath(`/groups/${encodeURIComponent(groupId)}/entities`), data);
505
556
  }
506
557
  removeGroupEntities(groupId, data) {
507
- return this.delete(`/groups/${groupId}/entities`, data);
558
+ return this.delete(this.organizationPath(`/groups/${encodeURIComponent(groupId)}/entities`), data);
508
559
  }
509
560
  createSpace(data) {
510
- return this.post("/spaces", data);
561
+ return this.post(this.organizationPath("/spaces"), data);
511
562
  }
512
563
  listSpaces(params) {
513
- return this.get("/spaces", params);
564
+ return this.get(this.organizationPath("/spaces"), params);
514
565
  }
515
566
  getSpace(spaceId) {
516
- return this.get(`/spaces/${spaceId}`);
567
+ return this.get(this.organizationPath(`/spaces/${encodeURIComponent(spaceId)}`));
517
568
  }
518
569
  deleteSpace(spaceId) {
519
- return this.delete(`/spaces/${spaceId}`);
520
- }
521
- addOrganizationAdminToOrg(organizationName, data) {
522
- return this.post(`/organization/${organizationName}/admins`, data);
523
- }
524
- listOrganizationAdminsForOrg(organizationName, params) {
525
- return this.get(`/organization/${organizationName}/admins`, params);
526
- }
527
- removeOrganizationAdminFromOrg(organizationName, data) {
528
- return this.delete(`/organization/${organizationName}/admins`, data);
570
+ return this.delete(this.organizationPath(`/spaces/${encodeURIComponent(spaceId)}`));
529
571
  }
530
572
  getAgent(agentId) {
531
- return this.get(`/agents/${agentId}`);
573
+ return this.get(this.organizationPath(`/agents/${encodeURIComponent(agentId)}`));
532
574
  }
533
575
  getEntityInfo() {
534
576
  return pipe(super.getEntityInfo(), TE.chain((info) => {
@@ -537,13 +579,52 @@ var ApprovioUserClient = class extends BaseApprovioClient {
537
579
  }));
538
580
  }
539
581
  listAuditLogs(params) {
540
- return this.get("/audit-logs", params);
582
+ return this.get(this.organizationPath("/audit-logs"), params);
541
583
  }
542
584
  listMyAuditLogs(params) {
543
- return this.get("/audit-logs/me", params);
585
+ return this.get(this.organizationPath("/audit-logs/me"), params);
544
586
  }
545
587
  resolveResources(params) {
546
- return this.post("/resources/resolve", params);
588
+ return this.post(this.organizationPath("/resources/resolve"), params);
589
+ }
590
+ getOrganizationEntitlements() {
591
+ return this.get(this.organizationPath("/entitlements"));
592
+ }
593
+ getOrganizationUsage(params) {
594
+ return this.get(this.organizationPath("/usage"), params);
595
+ }
596
+ getOrganization() {
597
+ return this.get(this.organizationPath(""));
598
+ }
599
+ updateOrganization(data, etag) {
600
+ return this.patch(this.organizationPath(""), data, { "If-Match": etag });
601
+ }
602
+ deleteOrganization(etag) {
603
+ return this.delete(this.organizationPath(""), void 0, { "If-Match": etag });
604
+ }
605
+ suspendOrganization(etag) {
606
+ return this.post(this.organizationPath("/suspend"), void 0, { "If-Match": etag });
607
+ }
608
+ resumeOrganization(etag) {
609
+ return this.post(this.organizationPath("/resume"), void 0, { "If-Match": etag });
610
+ }
611
+ listMembers(params) {
612
+ return this.get(this.organizationPath("/members"), params);
613
+ }
614
+ changeMemberRole(membershipId, data, etag) {
615
+ return this.patch(this.organizationPath(`/members/${encodeURIComponent(membershipId)}`), data, { "If-Match": etag });
616
+ }
617
+ removeMember(membershipId, etag) {
618
+ return this.delete(this.organizationPath(`/members/${encodeURIComponent(membershipId)}`), void 0, { "If-Match": etag });
619
+ }
620
+ createInvitation(data) {
621
+ return this.post(this.organizationPath("/invitations"), data);
622
+ }
623
+ revokeInvitation(invitationId) {
624
+ return this.delete(this.organizationPath(`/invitations/${encodeURIComponent(invitationId)}`));
625
+ }
626
+ acceptInvitation(invitationId, data) {
627
+ return this.post(this.organizationPath(`/invitations/${encodeURIComponent(invitationId)}/accept`), data);
547
628
  }
548
629
  logout() {
549
630
  return this.post("/auth/web/logout");
@@ -554,10 +635,10 @@ var ApprovioUserClient = class extends BaseApprovioClient {
554
635
  /**
555
636
  * Client for Approvio API (Agent).
556
637
  */
557
- var ApprovioAgentClient = class extends BaseApprovioClient {
638
+ var ApprovioAgentClient = class extends OrganizationClient {
558
639
  authenticator;
559
640
  constructor(config, authenticator) {
560
- super(config, authenticator);
641
+ super(config, authenticator, authenticator.organizationId);
561
642
  this.authenticator = authenticator;
562
643
  }
563
644
  getAuthenticator() {
@@ -571,4 +652,36 @@ var ApprovioAgentClient = class extends BaseApprovioClient {
571
652
  }
572
653
  };
573
654
  //#endregion
574
- export { AgentAuthenticator, ApprovioAgentClient, ApprovioUserClient, AuthHelper, BaseApprovioClient, CliUserAuthenticator, WebAuthenticator, isApprovioError };
655
+ //#region src/client/platform.client.ts
656
+ /** Operations that do not select or access tenant data. */
657
+ var ApprovioPlatformClient = class extends BaseApprovioClient {
658
+ constructor(config, authenticator) {
659
+ super(config, authenticator);
660
+ }
661
+ getAuthProviders() {
662
+ return this.get("/auth/providers");
663
+ }
664
+ getOwnAccount() {
665
+ return this.get("/account");
666
+ }
667
+ listOwnOrganizations(params) {
668
+ return this.get("/o", params);
669
+ }
670
+ createOrganization(data) {
671
+ return this.post("/o", data);
672
+ }
673
+ getWebSession() {
674
+ return this.getVersioned("/auth/web/session");
675
+ }
676
+ switchWebOrganization(data, etag) {
677
+ return this.postVersioned("/auth/web/select-organization", data, { "If-Match": etag });
678
+ }
679
+ selectCliOrganization(data) {
680
+ return this.post("/auth/cli/select-organization", data);
681
+ }
682
+ logout() {
683
+ return this.post("/auth/web/logout");
684
+ }
685
+ };
686
+ //#endregion
687
+ export { AgentAuthenticator, ApprovioAgentClient, ApprovioPlatformClient, ApprovioUserClient, AuthHelper, BaseApprovioClient, CliUserAuthenticator, LocationNotFoundError, NetworkError, OrganizationClient, OrganizationContextChangedError, OrganizationSuspendedError, UnexpectedEntityTypeError, WebAuthenticator, isApprovioError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@approvio/ts-sdk",
3
- "version": "0.0.27",
3
+ "version": "1.0.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -34,7 +34,7 @@
34
34
  "prepublishOnly": "yarn format && yarn lint && yarn build"
35
35
  },
36
36
  "dependencies": {
37
- "@approvio/api": "0.0.57",
37
+ "@approvio/api": "1.0.0",
38
38
  "axios": "1.16.1",
39
39
  "jose": "6.2.3"
40
40
  },
@@ -51,7 +51,7 @@
51
51
  "eslint-config-prettier": "10.1.8",
52
52
  "eslint-plugin-jest": "28.14.0",
53
53
  "eslint-plugin-node": "11.1.0",
54
- "eslint-plugin-prettier": "5.5.4",
54
+ "eslint-plugin-prettier": "5.5.6",
55
55
  "fp-ts": "2.16.11",
56
56
  "io-ts": "2.2.20",
57
57
  "jest": "29.7.0",