@sylphx/sdk 0.3.6 → 0.4.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
@@ -103,6 +103,13 @@ var SylphxError = class _SylphxError extends Error {
103
103
  static isRateLimited(err) {
104
104
  return err instanceof _SylphxError && err.code === "TOO_MANY_REQUESTS";
105
105
  }
106
+ /**
107
+ * Check if error is an account lockout error (too many failed login attempts).
108
+ * When true, `error.data?.lockoutUntil` contains the ISO 8601 timestamp when the lockout expires.
109
+ */
110
+ static isAccountLocked(err) {
111
+ return err instanceof _SylphxError && err.code === "TOO_MANY_REQUESTS" && err.data?.code === "ACCOUNT_LOCKED";
112
+ }
106
113
  /**
107
114
  * Check if error is a quota exceeded error (plan limit reached)
108
115
  */
@@ -1314,11 +1321,14 @@ async function verifyTwoFactor(config, userId, code) {
1314
1321
  async function introspectToken(config, token, tokenTypeHint) {
1315
1322
  const response = await fetch(buildApiUrl(config, "/auth/introspect"), {
1316
1323
  method: "POST",
1317
- headers: { "Content-Type": "application/json" },
1324
+ headers: {
1325
+ "Content-Type": "application/json",
1326
+ // RFC 7662 §2: server-to-server call — authenticate with secret key
1327
+ "x-app-secret": config.secretKey ?? ""
1328
+ },
1318
1329
  body: JSON.stringify({
1319
1330
  token,
1320
- token_type_hint: tokenTypeHint,
1321
- client_secret: config.secretKey
1331
+ token_type_hint: tokenTypeHint
1322
1332
  })
1323
1333
  });
1324
1334
  if (!response.ok) {
@@ -1353,6 +1363,12 @@ async function inviteUser(config, input) {
1353
1363
  body: input
1354
1364
  });
1355
1365
  }
1366
+ async function switchOrg(config, orgId) {
1367
+ return callApi(config, "/auth/switch-org", {
1368
+ method: "POST",
1369
+ body: JSON.stringify({ orgId })
1370
+ });
1371
+ }
1356
1372
 
1357
1373
  // src/admin.ts
1358
1374
  async function listUsers(config, opts) {
@@ -2676,6 +2692,86 @@ function canDeleteOrganization(membership) {
2676
2692
  return hasRole(membership, "super_admin");
2677
2693
  }
2678
2694
 
2695
+ // src/permissions.ts
2696
+ async function listPermissions(config) {
2697
+ return callApi(config, "/permissions");
2698
+ }
2699
+ async function createPermission(config, input) {
2700
+ return callApi(config, "/permissions", {
2701
+ method: "POST",
2702
+ body: input
2703
+ });
2704
+ }
2705
+ async function deletePermission(config, permissionKey) {
2706
+ return callApi(config, `/permissions/${permissionKey}`, {
2707
+ method: "DELETE"
2708
+ });
2709
+ }
2710
+ async function getMemberPermissions(config, orgIdOrSlug, memberId) {
2711
+ return callApi(
2712
+ config,
2713
+ `/orgs/${orgIdOrSlug}/members/${memberId}/permissions`
2714
+ );
2715
+ }
2716
+ function hasPermission(permissions, required) {
2717
+ return permissions.includes(required);
2718
+ }
2719
+ function hasAnyPermission(permissions, required) {
2720
+ return required.some((perm) => permissions.includes(perm));
2721
+ }
2722
+ function hasAllPermissions(permissions, required) {
2723
+ return required.every((perm) => permissions.includes(perm));
2724
+ }
2725
+
2726
+ // src/roles.ts
2727
+ async function listRoles(config) {
2728
+ return callApi(config, "/roles");
2729
+ }
2730
+ async function getRole(config, roleKey) {
2731
+ return callApi(config, `/roles/${roleKey}`);
2732
+ }
2733
+ async function createRole(config, input) {
2734
+ const body = {
2735
+ key: input.key,
2736
+ name: input.name
2737
+ };
2738
+ if (input.description !== void 0) body.description = input.description;
2739
+ if (input.permissions !== void 0) body.permissionKeys = input.permissions;
2740
+ if (input.isDefault !== void 0) body.isDefault = input.isDefault;
2741
+ if (input.sortOrder !== void 0) body.sortOrder = input.sortOrder;
2742
+ return callApi(config, "/roles", {
2743
+ method: "POST",
2744
+ body
2745
+ });
2746
+ }
2747
+ async function updateRole(config, roleKey, input) {
2748
+ const body = {};
2749
+ if (input.name !== void 0) body.name = input.name;
2750
+ if (input.description !== void 0) body.description = input.description;
2751
+ if (input.permissions !== void 0) body.permissionKeys = input.permissions;
2752
+ if (input.isDefault !== void 0) body.isDefault = input.isDefault;
2753
+ if (input.sortOrder !== void 0) body.sortOrder = input.sortOrder;
2754
+ return callApi(config, `/roles/${roleKey}`, {
2755
+ method: "PUT",
2756
+ body
2757
+ });
2758
+ }
2759
+ async function deleteRole(config, roleKey) {
2760
+ return callApi(config, `/roles/${roleKey}`, {
2761
+ method: "DELETE"
2762
+ });
2763
+ }
2764
+ async function assignMemberRole(config, orgIdOrSlug, memberId, roleKey) {
2765
+ return callApi(
2766
+ config,
2767
+ `/orgs/${orgIdOrSlug}/members/${memberId}/assign-role`,
2768
+ {
2769
+ method: "PUT",
2770
+ body: { roleKey }
2771
+ }
2772
+ );
2773
+ }
2774
+
2679
2775
  // src/secrets.ts
2680
2776
  async function getSecret(config, input) {
2681
2777
  return callApi(config, "/secrets/get", {
@@ -3523,6 +3619,7 @@ export {
3523
3619
  WorkersClient,
3524
3620
  acceptAllConsents,
3525
3621
  acceptOrganizationInvitation,
3622
+ assignMemberRole,
3526
3623
  batchIndex,
3527
3624
  canDeleteOrganization,
3528
3625
  canManageMembers,
@@ -3541,8 +3638,10 @@ export {
3541
3638
  createCron,
3542
3639
  createDynamicRestClient,
3543
3640
  createOrganization,
3641
+ createPermission,
3544
3642
  createPortalSession,
3545
3643
  createRestClient,
3644
+ createRole,
3546
3645
  createServiceWorkerScript,
3547
3646
  createStepContext,
3548
3647
  createTasksHandler,
@@ -3557,6 +3656,8 @@ export {
3557
3656
  deleteEnvVar,
3558
3657
  deleteFile,
3559
3658
  deleteOrganization,
3659
+ deletePermission,
3660
+ deleteRole,
3560
3661
  deleteUser,
3561
3662
  disableDebug,
3562
3663
  embed,
@@ -3590,6 +3691,7 @@ export {
3590
3691
  getFlagPayload,
3591
3692
  getFlags,
3592
3693
  getLeaderboard,
3694
+ getMemberPermissions,
3593
3695
  getMyReferralCode,
3594
3696
  getOrganization,
3595
3697
  getOrganizationInvitations,
@@ -3601,6 +3703,7 @@ export {
3601
3703
  getReferralLeaderboard,
3602
3704
  getReferralStats,
3603
3705
  getRestErrorMessage,
3706
+ getRole,
3604
3707
  getScheduledEmail,
3605
3708
  getScheduledEmailStats,
3606
3709
  getSearchStats,
@@ -3620,8 +3723,11 @@ export {
3620
3723
  getWebhookDeliveries,
3621
3724
  getWebhookDelivery,
3622
3725
  getWebhookStats,
3726
+ hasAllPermissions,
3727
+ hasAnyPermission,
3623
3728
  hasConsent,
3624
3729
  hasError,
3730
+ hasPermission,
3625
3731
  hasRole,
3626
3732
  hasSecret,
3627
3733
  identify,
@@ -3658,6 +3764,8 @@ export {
3658
3764
  leaveOrganization,
3659
3765
  linkAnonymousConsents,
3660
3766
  listEnvVars,
3767
+ listPermissions,
3768
+ listRoles,
3661
3769
  listScheduledEmails,
3662
3770
  listSecretKeys,
3663
3771
  listTasks,
@@ -3699,6 +3807,7 @@ export {
3699
3807
  streamToString,
3700
3808
  submitScore,
3701
3809
  suspendUser,
3810
+ switchOrg,
3702
3811
  toSylphxError,
3703
3812
  track,
3704
3813
  trackBatch,
@@ -3709,6 +3818,7 @@ export {
3709
3818
  updateOrganization,
3710
3819
  updateOrganizationMemberRole,
3711
3820
  updatePushPreferences,
3821
+ updateRole,
3712
3822
  updateUser,
3713
3823
  updateUserMetadata,
3714
3824
  updateWebhookConfig,