@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.cjs CHANGED
@@ -57,7 +57,7 @@ function validateURL(url) {
57
57
  function removeTrailingSlash(url) {
58
58
  return url.endsWith("/") ? url.slice(0, -1) : url;
59
59
  }
60
- const USER_AGENT = `Approvio-TS-SDK/0.0.27`;
60
+ const USER_AGENT = `Approvio-TS-SDK/1.0.0`;
61
61
  //#endregion
62
62
  //#region src/client/http.ts
63
63
  /**
@@ -243,13 +243,15 @@ var AuthHelper = class {
243
243
  * Handles automatic token renewal using a refresh token and DPoP proof when the access token expires.
244
244
  */
245
245
  var AgentAuthenticator = class {
246
+ organizationId;
246
247
  privateKey;
247
248
  accessToken;
248
249
  refreshToken;
249
250
  onTokenRefreshed;
250
251
  endpoint;
251
252
  axios;
252
- constructor(endpoint, privateKey, accessToken, refreshToken, onTokenRefreshed) {
253
+ constructor(endpoint, organizationId, privateKey, accessToken, refreshToken, onTokenRefreshed) {
254
+ this.organizationId = organizationId;
253
255
  this.privateKey = privateKey;
254
256
  this.accessToken = accessToken;
255
257
  this.refreshToken = refreshToken;
@@ -280,8 +282,9 @@ var AgentAuthenticator = class {
280
282
  }
281
283
  async renewToken() {
282
284
  const request = { refreshToken: this.refreshToken };
283
- const dpopProof = await this.generateDpopProof("POST", `${this.endpoint}/auth/agents/refresh`);
284
- return (await this.axios.post("/auth/agents/refresh", request, { headers: { DPoP: dpopProof } })).data;
285
+ const refreshPath = `/o/${encodeURIComponent(this.organizationId)}/auth/agents/refresh`;
286
+ const dpopProof = await this.generateDpopProof("POST", `${this.endpoint}${refreshPath}`);
287
+ return (await this.axios.post(refreshPath, request, { headers: { DPoP: dpopProof } })).data;
285
288
  }
286
289
  async generateDpopProof(method, url) {
287
290
  const privateKey = await jose.importPKCS8(this.privateKey, "RS256");
@@ -316,6 +319,20 @@ var UnexpectedEntityTypeError = class extends Error {
316
319
  this.received = received;
317
320
  }
318
321
  };
322
+ var OrganizationContextChangedError = class extends Error {
323
+ status;
324
+ constructor(status, message) {
325
+ super(message);
326
+ this.status = status;
327
+ }
328
+ };
329
+ var OrganizationSuspendedError = class extends Error {
330
+ status;
331
+ constructor(status, message) {
332
+ super(message);
333
+ this.status = status;
334
+ }
335
+ };
319
336
  //#endregion
320
337
  //#region src/client/base.client.ts
321
338
  const networkCodes = [
@@ -346,10 +363,14 @@ var BaseApprovioClient = class {
346
363
  if (networkCodes.includes(error.code ?? "")) return new NetworkError(`Network error ${error.code}`);
347
364
  const status = error.response?.status;
348
365
  const data = error.response?.data;
349
- if (status && isApprovioError(data)) return {
350
- ...data,
351
- status
352
- };
366
+ if (status && isApprovioError(data)) {
367
+ if (data.code === "ORGANIZATION_CONTEXT_CHANGED") return new OrganizationContextChangedError(status, data.message);
368
+ if (data.code === "ORGANIZATION_SUSPENDED") return new OrganizationSuspendedError(status, data.message);
369
+ return {
370
+ ...data,
371
+ status
372
+ };
373
+ }
353
374
  }
354
375
  if (error instanceof Error) return error;
355
376
  return new Error(String(error));
@@ -362,12 +383,34 @@ var BaseApprovioClient = class {
362
383
  return (await this.axios.get(url, { params })).data;
363
384
  }, (error) => this.handleError(error));
364
385
  }
386
+ getVersioned(url, params) {
387
+ return fp_ts_lib_TaskEither_js.tryCatch(async () => {
388
+ const response = await this.axios.get(url, { params });
389
+ const etag = response.headers.etag;
390
+ if (typeof etag !== "string" || !etag) throw new Error("ETag header was expected but not found");
391
+ return {
392
+ data: response.data,
393
+ etag
394
+ };
395
+ }, (error) => this.handleError(error));
396
+ }
365
397
  /**
366
398
  * Performs a POST request.
367
399
  */
368
- post(url, data) {
400
+ post(url, data, headers) {
401
+ return fp_ts_lib_TaskEither_js.tryCatch(async () => {
402
+ return (headers ? await this.axios.post(url, data, { headers }) : await this.axios.post(url, data)).data;
403
+ }, (error) => this.handleError(error));
404
+ }
405
+ postVersioned(url, data, headers) {
369
406
  return fp_ts_lib_TaskEither_js.tryCatch(async () => {
370
- return (await this.axios.post(url, data)).data;
407
+ const response = await this.axios.post(url, data, { headers });
408
+ const etag = response.headers.etag;
409
+ if (typeof etag !== "string" || !etag) throw new Error("ETag header was expected but not found");
410
+ return {
411
+ data: response.data,
412
+ etag
413
+ };
371
414
  }, (error) => this.handleError(error));
372
415
  }
373
416
  postWithLocation(url, data) {
@@ -384,76 +427,84 @@ var BaseApprovioClient = class {
384
427
  /**
385
428
  * Performs a PUT request.
386
429
  */
387
- put(url, data) {
430
+ put(url, data, headers) {
388
431
  return fp_ts_lib_TaskEither_js.tryCatch(async () => {
389
- return (await this.axios.put(url, data)).data;
432
+ return (headers ? await this.axios.put(url, data, { headers }) : await this.axios.put(url, data)).data;
433
+ }, (error) => this.handleError(error));
434
+ }
435
+ patch(url, data, headers) {
436
+ return fp_ts_lib_TaskEither_js.tryCatch(async () => {
437
+ return (headers ? await this.axios.patch(url, data, { headers }) : await this.axios.patch(url, data)).data;
390
438
  }, (error) => this.handleError(error));
391
439
  }
392
440
  /**
393
441
  * Performs a DELETE request.
394
442
  */
395
- delete(url, data) {
443
+ delete(url, data, headers) {
396
444
  return fp_ts_lib_TaskEither_js.tryCatch(async () => {
397
- const config = data !== void 0 ? { data } : void 0;
445
+ const config = data !== void 0 || headers ? {
446
+ data,
447
+ headers
448
+ } : void 0;
398
449
  return (await this.axios.delete(url, config)).data;
399
450
  }, (error) => this.handleError(error));
400
451
  }
401
- /**
402
- * Get workflow details.
403
- */
452
+ };
453
+ //#endregion
454
+ //#region src/client/organization.client.ts
455
+ /**
456
+ * A tenant handle whose organization cannot change after construction.
457
+ *
458
+ * Create another handle to use another organization. This prevents a browser
459
+ * context switch or a caller mistake from retargeting an in-flight client.
460
+ */
461
+ var OrganizationClient = class extends BaseApprovioClient {
462
+ organizationId;
463
+ constructor(config, authenticator, organizationId) {
464
+ super(config, authenticator);
465
+ this.organizationId = organizationId;
466
+ }
467
+ organizationPath(path) {
468
+ return `/o/${encodeURIComponent(this.organizationId)}${path}`;
469
+ }
404
470
  getWorkflow(workflowId, params) {
405
- return this.get(`/workflows/${workflowId}`, params);
471
+ return this.get(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}`), params);
406
472
  }
407
- /**
408
- * List workflows.
409
- */
410
473
  listWorkflows(params) {
411
- const queryParams = {
412
- page: params?.page,
413
- limit: params?.limit,
414
- includeOnlyNonTerminalState: params?.includeOnlyNonTerminalState,
415
- include: params?.include
416
- };
417
- return this.get("/workflows", queryParams);
474
+ return this.get(this.organizationPath("/workflows"), params);
418
475
  }
419
- /**
420
- * Create a new workflow.
421
- */
422
476
  createWorkflow(data) {
423
- return this.post("/workflows", data);
477
+ return this.post(this.organizationPath("/workflows"), data);
424
478
  }
425
- /**
426
- * Vote on a workflow.
427
- */
428
479
  voteOnWorkflow(workflowId, data) {
429
- return this.post(`/workflows/${workflowId}/vote`, data);
480
+ return this.post(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}/vote`), data);
430
481
  }
431
- /**
432
- * Check if the current entity can vote on a workflow.
433
- */
434
482
  canVoteOnWorkflow(workflowId) {
435
- return this.get(`/workflows/${workflowId}/canVote`);
483
+ return this.get(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}/canVote`));
436
484
  }
437
- /**
438
- * List role templates.
439
- */
440
485
  listRoleTemplates() {
441
- return this.get("/roles");
486
+ return this.get(this.organizationPath("/roles"));
442
487
  }
443
- /**
444
- * Get authenticated entity information.
445
- */
446
488
  getEntityInfo() {
447
- return this.get("/auth/info");
489
+ return this.get(this.organizationPath("/auth/info"));
448
490
  }
449
491
  listVotes(workflowId, params) {
450
- return this.get(`/workflows/${workflowId}/votes`, params);
492
+ return this.get(this.organizationPath(`/workflows/${encodeURIComponent(workflowId)}/votes`), params);
451
493
  }
452
- /**
453
- * Get available authentication providers.
454
- */
455
- getAuthProviders() {
456
- return this.get("/auth/providers");
494
+ generateAgentChallenge(data) {
495
+ return this.post(this.organizationPath("/auth/agents/challenge"), data);
496
+ }
497
+ exchangeAgentToken(data) {
498
+ return this.post(this.organizationPath("/auth/agents/token"), data);
499
+ }
500
+ initiateWebPrivilegeToken() {
501
+ return this.post(this.organizationPath("/auth/web/initiatePrivilegedTokenExchange"));
502
+ }
503
+ exchangeWebPrivilegeToken(data) {
504
+ return this.post(this.organizationPath("/auth/web/exchangePrivilegedToken"), data);
505
+ }
506
+ exchangeCliPrivilegeToken(data) {
507
+ return this.post(this.organizationPath("/auth/cli/exchangePrivilegedToken"), data);
457
508
  }
458
509
  };
459
510
  //#endregion
@@ -461,100 +512,91 @@ var BaseApprovioClient = class {
461
512
  /**
462
513
  * Client for Approvio API (Human/User).
463
514
  */
464
- var ApprovioUserClient = class extends BaseApprovioClient {
515
+ var ApprovioUserClient = class extends OrganizationClient {
465
516
  authenticator;
466
- constructor(config, authenticator) {
467
- super(config, authenticator);
517
+ constructor(config, authenticator, organizationId) {
518
+ super(config, authenticator, organizationId);
468
519
  this.authenticator = authenticator;
469
520
  }
470
521
  listUsers(params) {
471
- return this.get("/users", params);
522
+ return this.get(this.organizationPath("/users"), params);
472
523
  }
473
524
  registerAgent(data) {
474
- return this.post("/agents/register", data);
525
+ return this.post(this.organizationPath("/agents/register"), data);
475
526
  }
476
- assignAgentRoles(agentId, data) {
477
- return this.put(`/agents/${agentId}/roles`, data);
527
+ assignAgentRoles(agentId, data, etag) {
528
+ return this.put(this.organizationPath(`/agents/${encodeURIComponent(agentId)}/roles`), data, { "If-Match": etag });
478
529
  }
479
- removeAgentRoles(agentId, data) {
480
- return this.delete(`/agents/${agentId}/roles`, data);
530
+ removeAgentRoles(agentId, data, etag) {
531
+ return this.delete(this.organizationPath(`/agents/${encodeURIComponent(agentId)}/roles`), data, { "If-Match": etag });
481
532
  }
482
533
  createUser(data) {
483
- return (0, fp_ts_lib_function_js.pipe)(this.postWithLocation("/users", data), fp_ts_lib_TaskEither_js.map(({ location }) => location), fp_ts_lib_TaskEither_js.chain((location) => {
534
+ return (0, fp_ts_lib_function_js.pipe)(this.postWithLocation(this.organizationPath("/users"), data), fp_ts_lib_TaskEither_js.map(({ location }) => location), fp_ts_lib_TaskEither_js.chain((location) => {
484
535
  const id = location.split("/").pop();
485
536
  if (!id) return fp_ts_lib_TaskEither_js.left(/* @__PURE__ */ new Error("Invalid location"));
486
537
  return fp_ts_lib_TaskEither_js.right(id);
487
538
  }));
488
539
  }
489
540
  getUser(userId) {
490
- return this.get(`/users/${userId}`);
541
+ return this.get(this.organizationPath(`/users/${encodeURIComponent(userId)}`));
491
542
  }
492
- assignUserRoles(userId, data) {
493
- return this.put(`/users/${userId}/roles`, data);
543
+ assignUserRoles(userId, data, etag) {
544
+ return this.put(this.organizationPath(`/users/${encodeURIComponent(userId)}/roles`), data, { "If-Match": etag });
494
545
  }
495
- removeUserRoles(userId, data) {
496
- return this.delete(`/users/${userId}/roles`, data);
546
+ removeUserRoles(userId, data, etag) {
547
+ return this.delete(this.organizationPath(`/users/${encodeURIComponent(userId)}/roles`), data, { "If-Match": etag });
497
548
  }
498
549
  createWorkflowTemplate(data) {
499
- return this.post("/workflow-templates", data);
550
+ return this.post(this.organizationPath("/workflow-templates"), data);
500
551
  }
501
552
  listWorkflowTemplates(request) {
502
- return this.get("/workflow-templates", request);
553
+ return this.get(this.organizationPath("/workflow-templates"), request);
503
554
  }
504
555
  getWorkflowTemplate(templateIdentifier) {
505
- return this.get(`/workflow-templates/${templateIdentifier}`);
556
+ return this.get(this.organizationPath(`/workflow-templates/${encodeURIComponent(templateIdentifier)}`));
506
557
  }
507
- updateWorkflowTemplate(templateIdentifier, data) {
508
- return this.put(`/workflow-templates/${templateIdentifier}`, data);
558
+ updateWorkflowTemplate(templateIdentifier, data, etag) {
559
+ return this.put(this.organizationPath(`/workflow-templates/${encodeURIComponent(templateIdentifier)}`), data, { "If-Match": etag });
509
560
  }
510
561
  deprecateWorkflowTemplate(templateName, data) {
511
- return this.post(`/workflow-templates/${templateName}/deprecate`, data);
562
+ return this.post(this.organizationPath(`/workflow-templates/${encodeURIComponent(templateName)}/deprecate`), data);
512
563
  }
513
564
  createGroup(data) {
514
- return (0, fp_ts_lib_function_js.pipe)(this.postWithLocation("/groups", data), fp_ts_lib_TaskEither_js.map(({ location }) => location), fp_ts_lib_TaskEither_js.chain((location) => {
565
+ return (0, fp_ts_lib_function_js.pipe)(this.postWithLocation(this.organizationPath("/groups"), data), fp_ts_lib_TaskEither_js.map(({ location }) => location), fp_ts_lib_TaskEither_js.chain((location) => {
515
566
  const id = location.split("/").pop();
516
567
  if (!id) return fp_ts_lib_TaskEither_js.left(/* @__PURE__ */ new Error("Invalid location"));
517
568
  return fp_ts_lib_TaskEither_js.right(id);
518
569
  }));
519
570
  }
520
571
  listGroups(params) {
521
- return this.get("/groups", params);
572
+ return this.get(this.organizationPath("/groups"), params);
522
573
  }
523
574
  getGroup(groupIdentifier) {
524
- return this.get(`/groups/${groupIdentifier}`);
575
+ return this.get(this.organizationPath(`/groups/${encodeURIComponent(groupIdentifier)}`));
525
576
  }
526
577
  listGroupEntities(groupId, params) {
527
- return this.get(`/groups/${groupId}/entities`, params);
578
+ return this.get(this.organizationPath(`/groups/${encodeURIComponent(groupId)}/entities`), params);
528
579
  }
529
580
  addGroupEntities(groupId, data) {
530
- return this.post(`/groups/${groupId}/entities`, data);
581
+ return this.post(this.organizationPath(`/groups/${encodeURIComponent(groupId)}/entities`), data);
531
582
  }
532
583
  removeGroupEntities(groupId, data) {
533
- return this.delete(`/groups/${groupId}/entities`, data);
584
+ return this.delete(this.organizationPath(`/groups/${encodeURIComponent(groupId)}/entities`), data);
534
585
  }
535
586
  createSpace(data) {
536
- return this.post("/spaces", data);
587
+ return this.post(this.organizationPath("/spaces"), data);
537
588
  }
538
589
  listSpaces(params) {
539
- return this.get("/spaces", params);
590
+ return this.get(this.organizationPath("/spaces"), params);
540
591
  }
541
592
  getSpace(spaceId) {
542
- return this.get(`/spaces/${spaceId}`);
593
+ return this.get(this.organizationPath(`/spaces/${encodeURIComponent(spaceId)}`));
543
594
  }
544
595
  deleteSpace(spaceId) {
545
- return this.delete(`/spaces/${spaceId}`);
546
- }
547
- addOrganizationAdminToOrg(organizationName, data) {
548
- return this.post(`/organization/${organizationName}/admins`, data);
549
- }
550
- listOrganizationAdminsForOrg(organizationName, params) {
551
- return this.get(`/organization/${organizationName}/admins`, params);
552
- }
553
- removeOrganizationAdminFromOrg(organizationName, data) {
554
- return this.delete(`/organization/${organizationName}/admins`, data);
596
+ return this.delete(this.organizationPath(`/spaces/${encodeURIComponent(spaceId)}`));
555
597
  }
556
598
  getAgent(agentId) {
557
- return this.get(`/agents/${agentId}`);
599
+ return this.get(this.organizationPath(`/agents/${encodeURIComponent(agentId)}`));
558
600
  }
559
601
  getEntityInfo() {
560
602
  return (0, fp_ts_lib_function_js.pipe)(super.getEntityInfo(), fp_ts_lib_TaskEither_js.chain((info) => {
@@ -563,13 +605,52 @@ var ApprovioUserClient = class extends BaseApprovioClient {
563
605
  }));
564
606
  }
565
607
  listAuditLogs(params) {
566
- return this.get("/audit-logs", params);
608
+ return this.get(this.organizationPath("/audit-logs"), params);
567
609
  }
568
610
  listMyAuditLogs(params) {
569
- return this.get("/audit-logs/me", params);
611
+ return this.get(this.organizationPath("/audit-logs/me"), params);
570
612
  }
571
613
  resolveResources(params) {
572
- return this.post("/resources/resolve", params);
614
+ return this.post(this.organizationPath("/resources/resolve"), params);
615
+ }
616
+ getOrganizationEntitlements() {
617
+ return this.get(this.organizationPath("/entitlements"));
618
+ }
619
+ getOrganizationUsage(params) {
620
+ return this.get(this.organizationPath("/usage"), params);
621
+ }
622
+ getOrganization() {
623
+ return this.get(this.organizationPath(""));
624
+ }
625
+ updateOrganization(data, etag) {
626
+ return this.patch(this.organizationPath(""), data, { "If-Match": etag });
627
+ }
628
+ deleteOrganization(etag) {
629
+ return this.delete(this.organizationPath(""), void 0, { "If-Match": etag });
630
+ }
631
+ suspendOrganization(etag) {
632
+ return this.post(this.organizationPath("/suspend"), void 0, { "If-Match": etag });
633
+ }
634
+ resumeOrganization(etag) {
635
+ return this.post(this.organizationPath("/resume"), void 0, { "If-Match": etag });
636
+ }
637
+ listMembers(params) {
638
+ return this.get(this.organizationPath("/members"), params);
639
+ }
640
+ changeMemberRole(membershipId, data, etag) {
641
+ return this.patch(this.organizationPath(`/members/${encodeURIComponent(membershipId)}`), data, { "If-Match": etag });
642
+ }
643
+ removeMember(membershipId, etag) {
644
+ return this.delete(this.organizationPath(`/members/${encodeURIComponent(membershipId)}`), void 0, { "If-Match": etag });
645
+ }
646
+ createInvitation(data) {
647
+ return this.post(this.organizationPath("/invitations"), data);
648
+ }
649
+ revokeInvitation(invitationId) {
650
+ return this.delete(this.organizationPath(`/invitations/${encodeURIComponent(invitationId)}`));
651
+ }
652
+ acceptInvitation(invitationId, data) {
653
+ return this.post(this.organizationPath(`/invitations/${encodeURIComponent(invitationId)}/accept`), data);
573
654
  }
574
655
  logout() {
575
656
  return this.post("/auth/web/logout");
@@ -580,10 +661,10 @@ var ApprovioUserClient = class extends BaseApprovioClient {
580
661
  /**
581
662
  * Client for Approvio API (Agent).
582
663
  */
583
- var ApprovioAgentClient = class extends BaseApprovioClient {
664
+ var ApprovioAgentClient = class extends OrganizationClient {
584
665
  authenticator;
585
666
  constructor(config, authenticator) {
586
- super(config, authenticator);
667
+ super(config, authenticator, authenticator.organizationId);
587
668
  this.authenticator = authenticator;
588
669
  }
589
670
  getAuthenticator() {
@@ -597,11 +678,50 @@ var ApprovioAgentClient = class extends BaseApprovioClient {
597
678
  }
598
679
  };
599
680
  //#endregion
681
+ //#region src/client/platform.client.ts
682
+ /** Operations that do not select or access tenant data. */
683
+ var ApprovioPlatformClient = class extends BaseApprovioClient {
684
+ constructor(config, authenticator) {
685
+ super(config, authenticator);
686
+ }
687
+ getAuthProviders() {
688
+ return this.get("/auth/providers");
689
+ }
690
+ getOwnAccount() {
691
+ return this.get("/account");
692
+ }
693
+ listOwnOrganizations(params) {
694
+ return this.get("/o", params);
695
+ }
696
+ createOrganization(data) {
697
+ return this.post("/o", data);
698
+ }
699
+ getWebSession() {
700
+ return this.getVersioned("/auth/web/session");
701
+ }
702
+ switchWebOrganization(data, etag) {
703
+ return this.postVersioned("/auth/web/select-organization", data, { "If-Match": etag });
704
+ }
705
+ selectCliOrganization(data) {
706
+ return this.post("/auth/cli/select-organization", data);
707
+ }
708
+ logout() {
709
+ return this.post("/auth/web/logout");
710
+ }
711
+ };
712
+ //#endregion
600
713
  exports.AgentAuthenticator = AgentAuthenticator;
601
714
  exports.ApprovioAgentClient = ApprovioAgentClient;
715
+ exports.ApprovioPlatformClient = ApprovioPlatformClient;
602
716
  exports.ApprovioUserClient = ApprovioUserClient;
603
717
  exports.AuthHelper = AuthHelper;
604
718
  exports.BaseApprovioClient = BaseApprovioClient;
605
719
  exports.CliUserAuthenticator = CliUserAuthenticator;
720
+ exports.LocationNotFoundError = LocationNotFoundError;
721
+ exports.NetworkError = NetworkError;
722
+ exports.OrganizationClient = OrganizationClient;
723
+ exports.OrganizationContextChangedError = OrganizationContextChangedError;
724
+ exports.OrganizationSuspendedError = OrganizationSuspendedError;
725
+ exports.UnexpectedEntityTypeError = UnexpectedEntityTypeError;
606
726
  exports.WebAuthenticator = WebAuthenticator;
607
727
  exports.isApprovioError = isApprovioError;