@stackframe/stack 2.5.3 → 2.5.5

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 (144) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/components/credential-sign-in-form.js +4 -14
  3. package/dist/components/credential-sign-in-form.js.map +1 -1
  4. package/dist/components/credential-sign-up-form.js +18 -20
  5. package/dist/components/credential-sign-up-form.js.map +1 -1
  6. package/dist/components/elements/maybe-full-page.js.map +1 -1
  7. package/dist/components/elements/ssr-layout-effect.d.mts +1 -0
  8. package/dist/components/elements/ssr-layout-effect.d.ts +1 -0
  9. package/dist/components/elements/ssr-layout-effect.js +8 -1
  10. package/dist/components/elements/ssr-layout-effect.js.map +1 -1
  11. package/dist/components/elements/user-avatar.d.mts +5 -3
  12. package/dist/components/elements/user-avatar.d.ts +5 -3
  13. package/dist/components/elements/user-avatar.js.map +1 -1
  14. package/dist/components/forgot-password-form.js +3 -13
  15. package/dist/components/forgot-password-form.js.map +1 -1
  16. package/dist/components/magic-link-sign-in-form.js +3 -13
  17. package/dist/components/magic-link-sign-in-form.js.map +1 -1
  18. package/dist/components/message-cards/known-error-message-card.js.map +1 -1
  19. package/dist/components/message-cards/message-card.js.map +1 -1
  20. package/dist/components/message-cards/predefined-message-card.js.map +1 -1
  21. package/dist/components/oauth-button-group.d.mts +7 -2
  22. package/dist/components/oauth-button-group.d.ts +7 -2
  23. package/dist/components/oauth-button-group.js +1 -1
  24. package/dist/components/oauth-button-group.js.map +1 -1
  25. package/dist/components/oauth-button.js +28 -26
  26. package/dist/components/oauth-button.js.map +1 -1
  27. package/dist/components/password-reset-form.js +4 -3
  28. package/dist/components/password-reset-form.js.map +1 -1
  29. package/dist/components/selected-team-switcher.d.mts +5 -3
  30. package/dist/components/selected-team-switcher.d.ts +5 -3
  31. package/dist/components/selected-team-switcher.js +12 -1
  32. package/dist/components/selected-team-switcher.js.map +1 -1
  33. package/dist/components/user-button.js.map +1 -1
  34. package/dist/components-page/account-settings.js +42 -7
  35. package/dist/components-page/account-settings.js.map +1 -1
  36. package/dist/components-page/auth-page.d.mts +9 -2
  37. package/dist/components-page/auth-page.d.ts +9 -2
  38. package/dist/components-page/auth-page.js +3 -3
  39. package/dist/components-page/auth-page.js.map +1 -1
  40. package/dist/components-page/email-verification.js +12 -2
  41. package/dist/components-page/email-verification.js.map +1 -1
  42. package/dist/components-page/error-page.js.map +1 -1
  43. package/dist/components-page/magic-link-callback.js +13 -3
  44. package/dist/components-page/magic-link-callback.js.map +1 -1
  45. package/dist/components-page/password-reset.js +2 -2
  46. package/dist/components-page/password-reset.js.map +1 -1
  47. package/dist/components-page/sign-out.js +12 -2
  48. package/dist/components-page/sign-out.js.map +1 -1
  49. package/dist/components-page/stack-handler.d.mts +5 -3
  50. package/dist/components-page/stack-handler.d.ts +5 -3
  51. package/dist/components-page/stack-handler.js +27 -10
  52. package/dist/components-page/stack-handler.js.map +1 -1
  53. package/dist/esm/components/credential-sign-in-form.js +4 -4
  54. package/dist/esm/components/credential-sign-in-form.js.map +1 -1
  55. package/dist/esm/components/credential-sign-up-form.js +18 -20
  56. package/dist/esm/components/credential-sign-up-form.js.map +1 -1
  57. package/dist/esm/components/elements/maybe-full-page.js.map +1 -1
  58. package/dist/esm/components/elements/ssr-layout-effect.js +8 -1
  59. package/dist/esm/components/elements/ssr-layout-effect.js.map +1 -1
  60. package/dist/esm/components/elements/user-avatar.js.map +1 -1
  61. package/dist/esm/components/forgot-password-form.js +3 -3
  62. package/dist/esm/components/forgot-password-form.js.map +1 -1
  63. package/dist/esm/components/magic-link-sign-in-form.js +3 -3
  64. package/dist/esm/components/magic-link-sign-in-form.js.map +1 -1
  65. package/dist/esm/components/message-cards/known-error-message-card.js.map +1 -1
  66. package/dist/esm/components/message-cards/message-card.js.map +1 -1
  67. package/dist/esm/components/message-cards/predefined-message-card.js.map +1 -1
  68. package/dist/esm/components/oauth-button-group.js +1 -1
  69. package/dist/esm/components/oauth-button-group.js.map +1 -1
  70. package/dist/esm/components/oauth-button.js +29 -27
  71. package/dist/esm/components/oauth-button.js.map +1 -1
  72. package/dist/esm/components/password-reset-form.js +4 -3
  73. package/dist/esm/components/password-reset-form.js.map +1 -1
  74. package/dist/esm/components/selected-team-switcher.js +2 -1
  75. package/dist/esm/components/selected-team-switcher.js.map +1 -1
  76. package/dist/esm/components/user-button.js.map +1 -1
  77. package/dist/esm/components-page/account-settings.js +42 -7
  78. package/dist/esm/components-page/account-settings.js.map +1 -1
  79. package/dist/esm/components-page/auth-page.js +3 -3
  80. package/dist/esm/components-page/auth-page.js.map +1 -1
  81. package/dist/esm/components-page/email-verification.js +2 -2
  82. package/dist/esm/components-page/email-verification.js.map +1 -1
  83. package/dist/esm/components-page/error-page.js.map +1 -1
  84. package/dist/esm/components-page/magic-link-callback.js +3 -3
  85. package/dist/esm/components-page/magic-link-callback.js.map +1 -1
  86. package/dist/esm/components-page/password-reset.js +2 -2
  87. package/dist/esm/components-page/password-reset.js.map +1 -1
  88. package/dist/esm/components-page/sign-out.js +2 -2
  89. package/dist/esm/components-page/sign-out.js.map +1 -1
  90. package/dist/esm/components-page/stack-handler.js +27 -10
  91. package/dist/esm/components-page/stack-handler.js.map +1 -1
  92. package/dist/esm/index.js +1 -4
  93. package/dist/esm/index.js.map +1 -1
  94. package/dist/esm/lib/auth.js.map +1 -1
  95. package/dist/esm/lib/cookie.js +6 -1
  96. package/dist/esm/lib/cookie.js.map +1 -1
  97. package/dist/esm/lib/hooks.js.map +1 -1
  98. package/dist/esm/lib/stack-app.js +478 -349
  99. package/dist/esm/lib/stack-app.js.map +1 -1
  100. package/dist/esm/providers/stack-provider-client.js +3 -4
  101. package/dist/esm/providers/stack-provider-client.js.map +1 -1
  102. package/dist/esm/providers/theme-provider.js +16 -7
  103. package/dist/esm/providers/theme-provider.js.map +1 -1
  104. package/dist/esm/utils/browser-script.js +2 -2
  105. package/dist/esm/utils/browser-script.js.map +1 -1
  106. package/dist/esm/utils/email.js +2 -2
  107. package/dist/esm/utils/email.js.map +1 -1
  108. package/dist/index.d.mts +6 -4
  109. package/dist/index.d.ts +6 -4
  110. package/dist/index.js +4 -8
  111. package/dist/index.js.map +1 -1
  112. package/dist/lib/auth.js.map +1 -1
  113. package/dist/lib/cookie.js +6 -1
  114. package/dist/lib/cookie.js.map +1 -1
  115. package/dist/lib/hooks.d.mts +5 -3
  116. package/dist/lib/hooks.d.ts +5 -3
  117. package/dist/lib/hooks.js.map +1 -1
  118. package/dist/lib/stack-app.d.mts +219 -114
  119. package/dist/lib/stack-app.d.ts +219 -114
  120. package/dist/lib/stack-app.js +500 -369
  121. package/dist/lib/stack-app.js.map +1 -1
  122. package/dist/providers/stack-provider-client.d.mts +8 -6
  123. package/dist/providers/stack-provider-client.d.ts +8 -6
  124. package/dist/providers/stack-provider-client.js +4 -5
  125. package/dist/providers/stack-provider-client.js.map +1 -1
  126. package/dist/providers/stack-provider.d.mts +5 -3
  127. package/dist/providers/stack-provider.d.ts +5 -3
  128. package/dist/providers/theme-provider.d.mts +2 -1
  129. package/dist/providers/theme-provider.d.ts +2 -1
  130. package/dist/providers/theme-provider.js +15 -6
  131. package/dist/providers/theme-provider.js.map +1 -1
  132. package/dist/utils/browser-script.d.mts +3 -1
  133. package/dist/utils/browser-script.d.ts +3 -1
  134. package/dist/utils/browser-script.js +2 -2
  135. package/dist/utils/browser-script.js.map +1 -1
  136. package/dist/utils/email.js +2 -12
  137. package/dist/utils/email.js.map +1 -1
  138. package/package.json +4 -5
  139. package/dist/esm/providers/styled-components-registry.js +0 -25
  140. package/dist/esm/providers/styled-components-registry.js.map +0 -1
  141. package/dist/providers/styled-components-registry.d.mts +0 -8
  142. package/dist/providers/styled-components-registry.d.ts +0 -8
  143. package/dist/providers/styled-components-registry.js +0 -45
  144. package/dist/providers/styled-components-registry.js.map +0 -1
@@ -1,31 +1,28 @@
1
1
  // src/lib/stack-app.ts
2
- import React, { use, useCallback, useMemo } from "react";
2
+ import { isReactServer } from "@stackframe/stack-sc";
3
3
  import { KnownError, KnownErrors, StackAdminInterface, StackClientInterface, StackServerInterface } from "@stackframe/stack-shared";
4
- import { deleteCookie, getCookie, setOrDeleteCookie } from "./cookie";
4
+ import { getProductionModeErrors } from "@stackframe/stack-shared/dist/helpers/production-mode";
5
+ import { InternalSession } from "@stackframe/stack-shared/dist/sessions";
6
+ import { AsyncCache } from "@stackframe/stack-shared/dist/utils/caches";
7
+ import { scrambleDuringCompileTime } from "@stackframe/stack-shared/dist/utils/compile-time";
8
+ import { isBrowserLike } from "@stackframe/stack-shared/dist/utils/env";
5
9
  import { StackAssertionError, throwErr } from "@stackframe/stack-shared/dist/utils/errors";
6
- import { generateUuid } from "@stackframe/stack-shared/dist/utils/uuids";
10
+ import { DependenciesMap } from "@stackframe/stack-shared/dist/utils/maps";
11
+ import { deepPlainEquals, filterUndefined, omit } from "@stackframe/stack-shared/dist/utils/objects";
12
+ import { neverResolve, runAsynchronously, wait } from "@stackframe/stack-shared/dist/utils/promises";
13
+ import { suspend, suspendIfSsr } from "@stackframe/stack-shared/dist/utils/react";
7
14
  import { Result } from "@stackframe/stack-shared/dist/utils/results";
8
- import { suspendIfSsr } from "@stackframe/stack-shared/dist/utils/react";
9
15
  import { Store } from "@stackframe/stack-shared/dist/utils/stores";
10
- import { getProductionModeErrors } from "@stackframe/stack-shared/dist/interface/clientInterface";
11
- import { isBrowserLike } from "@stackframe/stack-shared/dist/utils/env";
12
- import { addNewOAuthProviderOrScope, callOAuthCallback, signInWithOAuth } from "./auth";
16
+ import { mergeScopeStrings } from "@stackframe/stack-shared/dist/utils/strings";
17
+ import { generateUuid } from "@stackframe/stack-shared/dist/utils/uuids";
18
+ import * as cookie from "cookie";
13
19
  import * as NextNavigationUnscrambled from "next/navigation";
20
+ import React, { useCallback, useMemo } from "react";
14
21
  import { constructRedirectUrl } from "../utils/url";
15
- import { deepPlainEquals, filterUndefined, omit, pick } from "@stackframe/stack-shared/dist/utils/objects";
16
- import { neverResolve, runAsynchronously, wait } from "@stackframe/stack-shared/dist/utils/promises";
17
- import { AsyncCache } from "@stackframe/stack-shared/dist/utils/caches";
18
- import { suspend } from "@stackframe/stack-shared/dist/utils/react";
19
- import { scrambleDuringCompileTime } from "@stackframe/stack-shared/dist/utils/compile-time";
20
- import { isReactServer } from "@stackframe/stack-sc";
21
- import * as cookie from "cookie";
22
- import { InternalSession } from "@stackframe/stack-shared/dist/sessions";
23
- import { mergeScopeStrings } from "@stackframe/stack-shared/dist/utils/strings";
22
+ import { addNewOAuthProviderOrScope, callOAuthCallback, signInWithOAuth } from "./auth";
23
+ import { deleteCookie, getCookie, setOrDeleteCookie } from "./cookie";
24
24
  var NextNavigation = scrambleDuringCompileTime(NextNavigationUnscrambled);
25
- var clientVersion = "js @stackframe/stack@2.5.3";
26
- function permissionDefinitionScopeToType(scope) {
27
- return { "any-team": "team", "specific-team": "team", "global": "global" }[scope.type];
28
- }
25
+ var clientVersion = "js @stackframe/stack@2.5.5";
29
26
  function getUrls(partial) {
30
27
  const handler = partial.handler ?? "/handler";
31
28
  return {
@@ -74,7 +71,7 @@ function getDefaultSuperSecretAdminKey() {
74
71
  function getDefaultBaseUrl() {
75
72
  return process.env.NEXT_PUBLIC_STACK_URL || defaultBaseUrl;
76
73
  }
77
- var defaultBaseUrl = "https://app.stack-auth.com";
74
+ var defaultBaseUrl = "https://api.stack-auth.com";
78
75
  function createEmptyTokenStore() {
79
76
  return new Store({
80
77
  refreshToken: null,
@@ -103,7 +100,7 @@ function useAsyncCache(cache, dependencies, caller) {
103
100
  getSnapshot,
104
101
  () => throwErr(new Error("getServerSnapshot should never be called in useAsyncCache because we restrict to CSR earlier"))
105
102
  );
106
- return use(promise);
103
+ return React.use(promise);
107
104
  }
108
105
  var stackAppInternalsSymbol = Symbol.for("StackAppInternals");
109
106
  var allClientApps = /* @__PURE__ */ new Map();
@@ -130,12 +127,12 @@ var _StackClientAppImpl = class __StackClientAppImpl {
130
127
  this._options = _options;
131
128
  this._uniqueIdentifier = void 0;
132
129
  this.__DEMO_ENABLE_SLIGHT_FETCH_DELAY = false;
130
+ this._ownedAdminApps = new DependenciesMap();
133
131
  this._currentUserCache = createCacheBySession(async (session) => {
134
132
  if (this.__DEMO_ENABLE_SLIGHT_FETCH_DELAY) {
135
133
  await wait(2e3);
136
134
  }
137
- const user = await this._interface.getClientUserByToken(session);
138
- return Result.or(user, null);
135
+ return await this._interface.getClientUserByToken(session);
139
136
  });
140
137
  this._currentProjectCache = createCache(async () => {
141
138
  return Result.orThrow(await this._interface.getClientProject());
@@ -143,11 +140,11 @@ var _StackClientAppImpl = class __StackClientAppImpl {
143
140
  this._ownedProjectsCache = createCacheBySession(async (session) => {
144
141
  return await this._interface.listProjects(session);
145
142
  });
146
- this._currentUserPermissionsCache = createCacheBySession(async (session, [teamId, type, direct]) => {
147
- return await this._interface.listClientUserTeamPermissions({ teamId, type, direct }, session);
143
+ this._currentUserPermissionsCache = createCacheBySession(async (session, [teamId, recursive]) => {
144
+ return await this._interface.listCurrentUserTeamPermissions({ teamId, recursive }, session);
148
145
  });
149
146
  this._currentUserTeamsCache = createCacheBySession(async (session) => {
150
- return await this._interface.listClientUserTeams(session);
147
+ return await this._interface.listCurrentUserTeams(session);
151
148
  });
152
149
  this._currentUserOAuthConnectionAccessTokensCache = createCacheBySession(
153
150
  async (session, [accountId, scope]) => {
@@ -165,7 +162,7 @@ var _StackClientAppImpl = class __StackClientAppImpl {
165
162
  async (session, [connectionId, scope, redirect]) => {
166
163
  const user = await this._currentUserCache.getOrWait([session], "write-only");
167
164
  let hasConnection = true;
168
- if (!user || !user.oauthProviders.find((p) => p === connectionId)) {
165
+ if (!user || !user.oauth_providers.find((p) => p.id === connectionId)) {
169
166
  hasConnection = false;
170
167
  }
171
168
  const token = await this._currentUserOAuthConnectionAccessTokensCache.getOrWait([session, connectionId, scope || ""], "write-only");
@@ -212,9 +209,9 @@ var _StackClientAppImpl = class __StackClientAppImpl {
212
209
  this._storedCookieTokenStore = null;
213
210
  /**
214
211
  * A map from token stores and session keys to sessions.
215
- *
212
+ *
216
213
  * This isn't just a map from session keys to sessions for two reasons:
217
- *
214
+ *
218
215
  * - So we can garbage-collect Session objects when the token store is garbage-collected
219
216
  * - So different token stores are separated and don't leak information between each other, eg. if the same user sends two requests to the same server they should get a different session object
220
217
  */
@@ -236,9 +233,11 @@ var _StackClientAppImpl = class __StackClientAppImpl {
236
233
  this._uniqueIdentifier = _options.uniqueIdentifier;
237
234
  this._initUniqueIdentifier();
238
235
  }
239
- numberOfAppsCreated++;
240
- if (numberOfAppsCreated > 10) {
241
- (process.env.NODE_ENV === "development" ? console.log : console.warn)(`You have created more than 10 Stack apps (${numberOfAppsCreated}). This is usually a sign of a memory leak, but can sometimes be caused by hot reload of your tech stack. In production, make sure to minimize the number of Stack apps per page (usually, one per project).`);
236
+ if (!_options.noAutomaticPrefetch) {
237
+ numberOfAppsCreated++;
238
+ if (numberOfAppsCreated > 10) {
239
+ (process.env.NODE_ENV === "development" ? console.log : console.warn)(`You have created more than 10 Stack apps with automatic pre-fetch enabled (${numberOfAppsCreated}). This is usually a sign of a memory leak, but can sometimes be caused by hot reload of your tech stack. If you are getting this error and it is not caused by hot reload, make sure to minimize the number of Stack apps per page (usually, one per project). (If it is caused by hot reload and does not occur in production, you can safely ignore it.)`);
240
+ }
242
241
  }
243
242
  }
244
243
  _initUniqueIdentifier() {
@@ -252,7 +251,8 @@ var _StackClientAppImpl = class __StackClientAppImpl {
252
251
  }
253
252
  /**
254
253
  * Cloudflare workers does not allow use of randomness on the global scope (on which the Stack app is probably
255
- * initialized). For that reason, we generate the unique identifier lazily when it is first needed.
254
+ * initialized). For that reason, we generate the unique identifier lazily when it is first needed instead of in the
255
+ * constructor.
256
256
  */
257
257
  _getUniqueIdentifier() {
258
258
  if (!this._uniqueIdentifier) {
@@ -457,38 +457,28 @@ var _StackClientAppImpl = class __StackClientAppImpl {
457
457
  throw new Error("Cannot call this function on a Stack app with a project ID other than 'internal'.");
458
458
  }
459
459
  }
460
- _permissionFromJson(json) {
461
- const type = permissionDefinitionScopeToType(json.scope);
462
- if (type === "team") {
463
- return {
464
- id: json.id,
465
- type,
466
- teamId: json.scope.teamId
467
- };
468
- } else {
469
- return {
470
- id: json.id,
471
- type
472
- };
473
- }
474
- }
475
- _teamFromJson(json) {
460
+ _clientProjectFromCrud(crud) {
476
461
  return {
477
- id: json.id,
478
- displayName: json.displayName,
479
- profileImageUrl: json.profileImageUrl,
480
- createdAt: new Date(json.createdAtMillis),
481
- toJson() {
482
- return json;
462
+ id: crud.id,
463
+ config: {
464
+ credentialEnabled: crud.config.credential_enabled,
465
+ magicLinkEnabled: crud.config.magic_link_enabled,
466
+ oauthProviders: crud.config.enabled_oauth_providers.map((p) => ({
467
+ id: p.id
468
+ }))
483
469
  }
484
470
  };
485
471
  }
486
- _teamMemberFromJson(json) {
487
- if (json === null) return null;
472
+ _clientTeamPermissionFromCrud(crud) {
473
+ return {
474
+ id: crud.id
475
+ };
476
+ }
477
+ _clientTeamFromCrud(crud) {
488
478
  return {
489
- teamId: json.teamId,
490
- userId: json.userId,
491
- displayName: json.displayName
479
+ id: crud.id,
480
+ displayName: crud.display_name,
481
+ profileImageUrl: crud.profile_image_url
492
482
  };
493
483
  }
494
484
  _createAuth(session) {
@@ -518,41 +508,29 @@ var _StackClientAppImpl = class __StackClientAppImpl {
518
508
  }
519
509
  };
520
510
  }
521
- _createBaseUser(json) {
511
+ _createBaseUser(crud) {
512
+ if (!crud) {
513
+ throw new StackAssertionError("User not found");
514
+ }
522
515
  return {
523
- projectId: json.projectId,
524
- id: json.id,
525
- displayName: json.displayName,
526
- primaryEmail: json.primaryEmail,
527
- primaryEmailVerified: json.primaryEmailVerified,
528
- profileImageUrl: json.profileImageUrl,
529
- signedUpAt: new Date(json.signedUpAtMillis),
530
- clientMetadata: json.clientMetadata,
531
- hasPassword: json.hasPassword,
532
- authWithEmail: json.authWithEmail,
533
- oauthProviders: json.oauthProviders,
534
- selectedTeam: json.selectedTeam && this._teamFromJson(json.selectedTeam),
516
+ projectId: crud.project_id,
517
+ id: crud.id,
518
+ displayName: crud.display_name,
519
+ primaryEmail: crud.primary_email,
520
+ primaryEmailVerified: crud.primary_email_verified,
521
+ profileImageUrl: crud.profile_image_url,
522
+ signedUpAt: new Date(crud.signed_up_at_millis),
523
+ clientMetadata: crud.client_metadata,
524
+ hasPassword: crud.has_password,
525
+ emailAuthEnabled: crud.auth_with_email,
526
+ oauthProviders: crud.oauth_providers,
527
+ selectedTeam: crud.selected_team && this._clientTeamFromCrud(crud.selected_team),
535
528
  toClientJson() {
536
- return pick(json, [
537
- "projectId",
538
- "id",
539
- "displayName",
540
- "primaryEmail",
541
- "primaryEmailVerified",
542
- "profileImageUrl",
543
- "signedUpAtMillis",
544
- "clientMetadata",
545
- "hasPassword",
546
- "authMethod",
547
- "authWithEmail",
548
- "selectedTeamId",
549
- "selectedTeam",
550
- "oauthProviders"
551
- ]);
529
+ return crud;
552
530
  }
553
531
  };
554
532
  }
555
- _createUserExtra(json, session) {
533
+ _createCurrentUserExtra(crud, session) {
556
534
  const app = this;
557
535
  async function getConnectedAccount(id, options) {
558
536
  const scopeString = options?.scopes?.join(" ");
@@ -586,24 +564,26 @@ var _StackClientAppImpl = class __StackClientAppImpl {
586
564
  },
587
565
  async listTeams() {
588
566
  const teams = await app._currentUserTeamsCache.getOrWait([session], "write-only");
589
- return teams.map((json2) => app._teamFromJson(json2));
567
+ return teams.map((crud2) => app._clientTeamFromCrud(crud2));
590
568
  },
591
569
  useTeams() {
592
570
  const teams = useAsyncCache(app._currentUserTeamsCache, [session], "user.useTeams()");
593
- return useMemo(() => teams.map((json2) => app._teamFromJson(json2)), [teams]);
571
+ return useMemo(() => teams.map((crud2) => app._clientTeamFromCrud(crud2)), [teams]);
594
572
  },
595
573
  async createTeam(data) {
596
- const teamJson = await app._interface.createTeamForCurrentUser(data, session);
574
+ const crud2 = await app._interface.createTeamForCurrentUser(teamCreateOptionsToCrud(data), session);
597
575
  await app._currentUserTeamsCache.refresh([session]);
598
- return app._teamFromJson(teamJson);
576
+ return app._clientTeamFromCrud(crud2);
599
577
  },
600
578
  async listPermissions(scope, options) {
601
- const permissions = await app._currentUserPermissionsCache.getOrWait([session, scope.id, "team", !!options?.direct], "write-only");
602
- return permissions.map((json2) => app._permissionFromJson(json2));
579
+ const recursive = options?.recursive ?? true;
580
+ const permissions = await app._currentUserPermissionsCache.getOrWait([session, scope.id, recursive], "write-only");
581
+ return permissions.map((crud2) => app._clientTeamPermissionFromCrud(crud2));
603
582
  },
604
583
  usePermissions(scope, options) {
605
- const permissions = useAsyncCache(app._currentUserPermissionsCache, [session, scope.id, "team", !!options?.direct], "user.usePermissions()");
606
- return useMemo(() => permissions.map((json2) => app._permissionFromJson(json2)), [permissions]);
584
+ const recursive = options?.recursive ?? true;
585
+ const permissions = useAsyncCache(app._currentUserPermissionsCache, [session, scope.id, recursive], "user.usePermissions()");
586
+ return useMemo(() => permissions.map((crud2) => app._clientTeamPermissionFromCrud(crud2)), [permissions]);
607
587
  },
608
588
  usePermission(scope, permissionId) {
609
589
  const permissions = this.usePermissions(scope);
@@ -617,7 +597,7 @@ var _StackClientAppImpl = class __StackClientAppImpl {
617
597
  return await this.getPermission(scope, permissionId) !== null;
618
598
  },
619
599
  update(update) {
620
- return app._updateUser(update, session);
600
+ return app._updateClientUser(update, session);
621
601
  },
622
602
  sendVerificationEmail() {
623
603
  return app._sendVerificationEmail(session);
@@ -642,58 +622,27 @@ var _StackClientAppImpl = class __StackClientAppImpl {
642
622
  }
643
623
  };
644
624
  }
645
- _createCurrentUser(json, session) {
625
+ _currentUserFromCrud(crud, session) {
646
626
  const currentUser = {
647
- ...this._createBaseUser(json),
627
+ ...this._createBaseUser(crud),
648
628
  ...this._createAuth(session),
649
- ...this._createUserExtra(json, session),
629
+ ...this._createCurrentUserExtra(crud, session),
650
630
  ...this._isInternalProject() ? this._createInternalUserExtra(session) : {}
651
631
  };
652
632
  Object.freeze(currentUser);
653
633
  return currentUser;
654
634
  }
655
- _projectAdminFromJson(data, adminInterface, onRefresh) {
656
- if (data.id !== adminInterface.projectId) {
657
- throw new Error(`The project ID of the provided project JSON (${data.id}) does not match the project ID of the app (${adminInterface.projectId})! This is a Stack bug.`);
635
+ _getOwnedAdminApp(forProjectId, session) {
636
+ if (!this._ownedAdminApps.has([session, forProjectId])) {
637
+ this._ownedAdminApps.set([session, forProjectId], new _StackAdminAppImpl({
638
+ baseUrl: this._interface.options.baseUrl,
639
+ projectId: forProjectId,
640
+ tokenStore: null,
641
+ projectOwnerSession: session,
642
+ noAutomaticPrefetch: true
643
+ }));
658
644
  }
659
- return {
660
- id: data.id,
661
- displayName: data.displayName,
662
- description: data.description,
663
- createdAt: new Date(data.createdAtMillis),
664
- userCount: data.userCount,
665
- isProductionMode: data.isProductionMode,
666
- evaluatedConfig: {
667
- id: data.evaluatedConfig.id,
668
- credentialEnabled: data.evaluatedConfig.credentialEnabled,
669
- magicLinkEnabled: data.evaluatedConfig.magicLinkEnabled,
670
- allowLocalhost: data.evaluatedConfig.allowLocalhost,
671
- oauthProviders: data.evaluatedConfig.oauthProviders,
672
- emailConfig: data.evaluatedConfig.emailConfig,
673
- domains: data.evaluatedConfig.domains,
674
- createTeamOnSignUp: data.evaluatedConfig.createTeamOnSignUp,
675
- teamCreatorDefaultPermissions: data.evaluatedConfig.teamCreatorDefaultPermissions,
676
- teamMemberDefaultPermissions: data.evaluatedConfig.teamMemberDefaultPermissions
677
- },
678
- async update(update) {
679
- await adminInterface.updateProject(update);
680
- await onRefresh();
681
- },
682
- toJson() {
683
- return data;
684
- },
685
- getProductionModeErrors() {
686
- return getProductionModeErrors(this.toJson());
687
- }
688
- };
689
- }
690
- _createAdminInterface(forProjectId, session) {
691
- return new StackAdminInterface({
692
- baseUrl: this._interface.options.baseUrl,
693
- projectId: forProjectId,
694
- clientVersion,
695
- projectOwnerSession: session
696
- });
645
+ return this._ownedAdminApps.get([session, forProjectId]);
697
646
  }
698
647
  get projectId() {
699
648
  return this._interface.projectId;
@@ -773,8 +722,8 @@ var _StackClientAppImpl = class __StackClientAppImpl {
773
722
  async getUser(options) {
774
723
  this._ensurePersistentTokenStore(options?.tokenStore);
775
724
  const session = this._getSession(options?.tokenStore);
776
- const userJson = await this._currentUserCache.getOrWait([session], "write-only");
777
- if (userJson === null) {
725
+ const crud = await this._currentUserCache.getOrWait([session], "write-only");
726
+ if (crud === null) {
778
727
  switch (options?.or) {
779
728
  case "redirect": {
780
729
  await this.redirectToSignIn();
@@ -788,14 +737,14 @@ var _StackClientAppImpl = class __StackClientAppImpl {
788
737
  }
789
738
  }
790
739
  }
791
- return userJson && this._createCurrentUser(userJson, session);
740
+ return crud && this._currentUserFromCrud(crud, session);
792
741
  }
793
742
  useUser(options) {
794
743
  this._ensurePersistentTokenStore(options?.tokenStore);
795
744
  const router = NextNavigation.useRouter();
796
745
  const session = this._useSession(options?.tokenStore);
797
- const userJson = useAsyncCache(this._currentUserCache, [session], "useUser()");
798
- if (userJson === null) {
746
+ const crud = useAsyncCache(this._currentUserCache, [session], "useUser()");
747
+ if (crud === null) {
799
748
  switch (options?.or) {
800
749
  case "redirect": {
801
750
  setTimeout(() => router.replace(this.urls.signIn), 0);
@@ -811,11 +760,11 @@ var _StackClientAppImpl = class __StackClientAppImpl {
811
760
  }
812
761
  }
813
762
  return useMemo(() => {
814
- return userJson && this._createCurrentUser(userJson, session);
815
- }, [userJson, session, options?.or]);
763
+ return crud && this._currentUserFromCrud(crud, session);
764
+ }, [crud, session, options?.or]);
816
765
  }
817
- async _updateUser(update, session) {
818
- const res = await this._interface.setClientUserCustomizableData(update, session);
766
+ async _updateClientUser(update, session) {
767
+ const res = await this._interface.updateClientUser(userUpdateOptionsToCrud(update), session);
819
768
  await this._refreshUser(session);
820
769
  return res;
821
770
  }
@@ -859,8 +808,7 @@ var _StackClientAppImpl = class __StackClientAppImpl {
859
808
  }
860
809
  async signInWithMagicLink(code) {
861
810
  this._ensurePersistentTokenStore();
862
- const session = this._getSession();
863
- const result = await this._interface.signInWithMagicLink(code, session);
811
+ const result = await this._interface.signInWithMagicLink(code);
864
812
  if (result instanceof KnownError) {
865
813
  return result;
866
814
  }
@@ -907,35 +855,34 @@ var _StackClientAppImpl = class __StackClientAppImpl {
907
855
  }
908
856
  }
909
857
  async getProject() {
910
- return await this._currentProjectCache.getOrWait([], "write-only");
858
+ const crud = await this._currentProjectCache.getOrWait([], "write-only");
859
+ return this._clientProjectFromCrud(crud);
911
860
  }
912
861
  useProject() {
913
- return useAsyncCache(this._currentProjectCache, [], "useProject()");
862
+ const crud = useAsyncCache(this._currentProjectCache, [], "useProject()");
863
+ return useMemo(() => this._clientProjectFromCrud(crud), [crud]);
914
864
  }
915
865
  async _listOwnedProjects(session) {
916
866
  this._ensureInternalProject();
917
- const json = await this._ownedProjectsCache.getOrWait([session], "write-only");
918
- return json.map((j) => this._projectAdminFromJson(
867
+ const crud = await this._ownedProjectsCache.getOrWait([session], "write-only");
868
+ return crud.map((j) => this._getOwnedAdminApp(j.id, session)._adminOwnedProjectFromCrud(
919
869
  j,
920
- this._createAdminInterface(j.id, session),
921
870
  () => this._refreshOwnedProjects(session)
922
871
  ));
923
872
  }
924
873
  _useOwnedProjects(session) {
925
874
  this._ensureInternalProject();
926
- const json = useAsyncCache(this._ownedProjectsCache, [session], "useOwnedProjects()");
927
- return useMemo(() => json.map((j) => this._projectAdminFromJson(
875
+ const projects = useAsyncCache(this._ownedProjectsCache, [session], "useOwnedProjects()");
876
+ return useMemo(() => projects.map((j) => this._getOwnedAdminApp(j.id, session)._adminOwnedProjectFromCrud(
928
877
  j,
929
- this._createAdminInterface(j.id, session),
930
878
  () => this._refreshOwnedProjects(session)
931
- )), [json]);
879
+ )), [projects]);
932
880
  }
933
881
  async _createProject(session, newProject) {
934
882
  this._ensureInternalProject();
935
- const json = await this._interface.createProject(newProject, session);
936
- const res = this._projectAdminFromJson(
937
- json,
938
- this._createAdminInterface(json.id, session),
883
+ const crud = await this._interface.createProject(adminProjectCreateOptionsToCrud(newProject), session);
884
+ const res = this._getOwnedAdminApp(crud.id, session)._adminOwnedProjectFromCrud(
885
+ crud,
939
886
  () => this._refreshOwnedProjects(session)
940
887
  );
941
888
  await this._refreshOwnedProjects(session);
@@ -1019,8 +966,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1019
966
  });
1020
967
  // TODO override the client user cache to use the server user cache, so we save some requests
1021
968
  this._currentServerUserCache = createCacheBySession(async (session) => {
1022
- const user = await this._interface.getServerUserByToken(session);
1023
- return Result.or(user, null);
969
+ return await this._interface.getServerUserByToken(session);
1024
970
  });
1025
971
  this._serverUsersCache = createCache(async () => {
1026
972
  return await this._interface.listServerUsers();
@@ -1032,36 +978,32 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1032
978
  this._serverTeamsCache = createCache(async () => {
1033
979
  return await this._interface.listServerTeams();
1034
980
  });
1035
- this._serverTeamMembersCache = createCache(async ([teamId]) => {
1036
- return await this._interface.listServerTeamMembers(teamId);
981
+ this._serverCurrentUserTeamsCache = createCacheBySession(async (session) => {
982
+ return await this._interface.listServerCurrentUserTeams(session);
1037
983
  });
1038
- this._serverTeamPermissionDefinitionsCache = createCache(async () => {
1039
- return await this._interface.listPermissionDefinitions();
984
+ this._serverTeamUsersCache = createCache(async ([teamId]) => {
985
+ return await this._interface.listServerTeamUsers(teamId);
1040
986
  });
1041
- this._serverTeamUserPermissionsCache = createCache(async ([teamId, userId, type, direct]) => {
1042
- return await this._interface.listServerTeamMemberPermissions({ teamId, userId, type, direct });
1043
- });
1044
- this._serverEmailTemplatesCache = createCache(async () => {
1045
- return await this._interface.listEmailTemplates();
987
+ this._serverTeamUserPermissionsCache = createCache(async ([teamId, userId, recursive]) => {
988
+ return await this._interface.listServerTeamMemberPermissions({ teamId, userId, recursive });
1046
989
  });
1047
990
  }
1048
- _createBaseUser(json) {
991
+ _createBaseUser(crud) {
992
+ if (!crud) {
993
+ throw new StackAssertionError("User not found");
994
+ }
1049
995
  return {
1050
- ...super._createBaseUser(json),
1051
- ..."serverMetadata" in json ? {
1052
- serverMetadata: json.serverMetadata,
1053
- toServerJson() {
1054
- return {
1055
- ...this.toClientJson(),
1056
- ...pick(json, [
1057
- "serverMetadata"
1058
- ])
1059
- };
1060
- }
996
+ ...super._createBaseUser(crud),
997
+ ..."server_metadata" in crud ? {
998
+ // server user
999
+ serverMetadata: crud.server_metadata
1061
1000
  } : {}
1062
1001
  };
1063
1002
  }
1064
- _createUserExtra(json) {
1003
+ _createCurrentUserExtra(crud) {
1004
+ if (!crud) {
1005
+ throw new StackAssertionError("User not found");
1006
+ }
1065
1007
  const app = this;
1066
1008
  return {
1067
1009
  async setDisplayName(displayName) {
@@ -1077,7 +1019,7 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1077
1019
  return await this.update({ selectedTeamId: team?.id ?? null });
1078
1020
  },
1079
1021
  async setPrimaryEmail(email, options) {
1080
- return await this.update({ primaryEmail: email });
1022
+ return await this.update({ primaryEmail: email, primaryEmailVerified: options?.verified });
1081
1023
  },
1082
1024
  getConnectedAccount: async () => {
1083
1025
  return await app._checkFeatureSupport("getConnectedAccount() on ServerUser", {});
@@ -1090,31 +1032,29 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1090
1032
  return teams.find((t) => t.id === teamId) ?? null;
1091
1033
  },
1092
1034
  useTeam(teamId) {
1093
- const teams = this.useTeams();
1094
- return useMemo(() => {
1095
- return teams.find((t) => t.id === teamId) ?? null;
1096
- }, [teams, teamId]);
1035
+ return app._useCheckFeatureSupport("useTeam() on ServerUser", {});
1097
1036
  },
1098
1037
  async listTeams() {
1099
- const teams = await app.listTeams();
1100
- const withMembers = await Promise.all(teams.map(async (t) => [t, await t.listMembers()]));
1101
- return withMembers.filter(([_, members]) => members.find((m) => m.userId === json.id)).map(([t]) => t);
1038
+ const crud2 = await app._serverCurrentUserTeamsCache.getOrWait([app._getSession()], "write-only");
1039
+ return crud2.map((t) => app._serverTeamFromCrud(t));
1102
1040
  },
1103
1041
  useTeams() {
1104
1042
  return app._useCheckFeatureSupport("useTeams() on ServerUser", {});
1105
1043
  },
1106
1044
  createTeam: async (data) => {
1107
- const team = await app._interface.createServerTeamForUser(json.id, data, app._getSession());
1045
+ const team = await app._interface.createServerTeam(serverTeamCreateOptionsToCrud(data), app._getSession());
1108
1046
  await app._serverTeamsCache.refresh([]);
1109
- return app._serverTeamFromJson(team);
1047
+ return app._serverTeamFromCrud(team);
1110
1048
  },
1111
1049
  async listPermissions(scope, options) {
1112
- const permissions = await app._serverTeamUserPermissionsCache.getOrWait([scope.id, json.id, "team", !!options?.direct], "write-only");
1113
- return permissions.map((json2) => app._serverPermissionFromJson(json2));
1050
+ const recursive = options?.recursive ?? true;
1051
+ const permissions = await app._serverTeamUserPermissionsCache.getOrWait([scope.id, crud.id, recursive], "write-only");
1052
+ return permissions.map((crud2) => app._serverPermissionFromCrud(crud2));
1114
1053
  },
1115
1054
  usePermissions(scope, options) {
1116
- const permissions = useAsyncCache(app._serverTeamUserPermissionsCache, [scope.id, json.id, "team", !!options?.direct], "user.usePermissions()");
1117
- return useMemo(() => permissions.map((json2) => app._serverPermissionFromJson(json2)), [permissions]);
1055
+ const recursive = options?.recursive ?? true;
1056
+ const permissions = useAsyncCache(app._serverTeamUserPermissionsCache, [scope.id, crud.id, recursive], "user.usePermissions()");
1057
+ return useMemo(() => permissions.map((crud2) => app._serverPermissionFromCrud(crud2)), [permissions]);
1118
1058
  },
1119
1059
  async getPermission(scope, permissionId) {
1120
1060
  const permissions = await this.listPermissions(scope);
@@ -1128,26 +1068,25 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1128
1068
  return await this.getPermission(scope, permissionId) !== null;
1129
1069
  },
1130
1070
  async grantPermission(scope, permissionId) {
1131
- await app._interface.grantServerTeamUserPermission(scope.id, json.id, permissionId, "team");
1132
- for (const direct of [true, false]) {
1133
- await app._serverTeamUserPermissionsCache.refresh([scope.id, json.id, "team", direct]);
1071
+ await app._interface.grantServerTeamUserPermission(scope.id, crud.id, permissionId);
1072
+ for (const recursive of [true, false]) {
1073
+ await app._serverTeamUserPermissionsCache.refresh([scope.id, crud.id, recursive]);
1134
1074
  }
1135
1075
  },
1136
1076
  async revokePermission(scope, permissionId) {
1137
- await app._interface.revokeServerTeamUserPermission(scope.id, json.id, permissionId, "team");
1138
- for (const direct of [true, false]) {
1139
- await app._serverTeamUserPermissionsCache.refresh([scope.id, json.id, "team", direct]);
1077
+ await app._interface.revokeServerTeamUserPermission(scope.id, crud.id, permissionId);
1078
+ for (const recursive of [true, false]) {
1079
+ await app._serverTeamUserPermissionsCache.refresh([scope.id, crud.id, recursive]);
1140
1080
  }
1141
1081
  },
1142
1082
  async delete() {
1143
- const res = await app._interface.deleteServerServerUser(json.id);
1083
+ const res = await app._interface.deleteServerServerUser(crud.id);
1144
1084
  await app._refreshUsers();
1145
1085
  return res;
1146
1086
  },
1147
1087
  async update(update) {
1148
- const res = await app._interface.setServerUserCustomizableData(json.id, update);
1088
+ const res = await app._interface.updateServerUser(crud.id, serverUserUpdateOptionsToCrud(update));
1149
1089
  await app._refreshUsers();
1150
- return res;
1151
1090
  },
1152
1091
  async sendVerificationEmail() {
1153
1092
  return await app._checkFeatureSupport("sendVerificationEmail() on ServerUser", {});
@@ -1157,76 +1096,65 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1157
1096
  }
1158
1097
  };
1159
1098
  }
1160
- _createUser(json) {
1099
+ _serverUserFromCrud(crud) {
1161
1100
  return {
1162
- ...this._createBaseUser(json),
1163
- ...this._createUserExtra(json)
1101
+ ...this._createBaseUser(crud),
1102
+ ...this._createCurrentUserExtra(crud)
1164
1103
  };
1165
1104
  }
1166
- _createCurrentUser(json, session) {
1105
+ _currentUserFromCrud(crud, session) {
1167
1106
  const app = this;
1168
1107
  const currentUser = {
1169
- ...this._createUser(json),
1108
+ ...this._serverUserFromCrud(crud),
1170
1109
  ...this._createAuth(session),
1171
1110
  ...this._isInternalProject() ? this._createInternalUserExtra(session) : {}
1172
1111
  };
1173
1112
  Object.freeze(currentUser);
1174
1113
  return currentUser;
1175
1114
  }
1176
- _serverTeamMemberFromJson(json) {
1177
- if (json === null) return null;
1178
- const app = this;
1179
- return {
1180
- ...app._teamMemberFromJson(json),
1181
- user: app._createUser(json.user)
1182
- };
1183
- }
1184
- _serverTeamFromJson(json) {
1115
+ _serverTeamFromCrud(crud) {
1185
1116
  const app = this;
1186
1117
  return {
1187
- id: json.id,
1188
- displayName: json.displayName,
1189
- profileImageUrl: json.profileImageUrl,
1190
- createdAt: new Date(json.createdAtMillis),
1191
- async listMembers() {
1192
- return (await app._interface.listServerTeamMembers(json.id)).map((u) => app._serverTeamMemberFromJson(u));
1118
+ id: crud.id,
1119
+ displayName: crud.display_name,
1120
+ profileImageUrl: crud.profile_image_url,
1121
+ createdAt: new Date(crud.created_at_millis),
1122
+ async listUsers() {
1123
+ return (await app._interface.listServerTeamUsers(crud.id)).map((u) => app._serverUserFromCrud(u));
1193
1124
  },
1194
1125
  async update(update) {
1195
- await app._interface.updateServerTeam(json.id, update);
1126
+ await app._interface.updateServerTeam(crud.id, serverTeamUpdateOptionsToCrud(update));
1196
1127
  await app._serverTeamsCache.refresh([]);
1197
1128
  },
1198
1129
  async delete() {
1199
- await app._interface.deleteServerTeam(json.id);
1130
+ await app._interface.deleteServerTeam(crud.id);
1200
1131
  await app._serverTeamsCache.refresh([]);
1201
1132
  },
1202
- useMembers() {
1203
- const result = useAsyncCache(app._serverTeamMembersCache, [json.id], "team.useUsers()");
1204
- return useMemo(() => result.map((u) => app._serverTeamMemberFromJson(u)), [result]);
1133
+ useUsers() {
1134
+ const result = useAsyncCache(app._serverTeamUsersCache, [crud.id], "team.useUsers()");
1135
+ return useMemo(() => result.map((u) => app._serverUserFromCrud(u)), [result]);
1205
1136
  },
1206
1137
  async addUser(userId) {
1207
1138
  await app._interface.addServerUserToTeam({
1208
- teamId: json.id,
1139
+ teamId: crud.id,
1209
1140
  userId
1210
1141
  });
1211
- await app._serverTeamMembersCache.refresh([json.id]);
1142
+ await app._serverTeamUsersCache.refresh([crud.id]);
1212
1143
  },
1213
1144
  async removeUser(userId) {
1214
1145
  await app._interface.removeServerUserFromTeam({
1215
- teamId: json.id,
1146
+ teamId: crud.id,
1216
1147
  userId
1217
1148
  });
1218
- await app._serverTeamMembersCache.refresh([json.id]);
1219
- },
1220
- toJson() {
1221
- return json;
1149
+ await app._serverTeamUsersCache.refresh([crud.id]);
1222
1150
  }
1223
1151
  };
1224
1152
  }
1225
1153
  async getUser(options) {
1226
1154
  this._ensurePersistentTokenStore(options?.tokenStore);
1227
1155
  const session = this._getSession(options?.tokenStore);
1228
- const userJson = await this._currentServerUserCache.getOrWait([session], "write-only");
1229
- if (userJson === null) {
1156
+ const crud = await this._currentServerUserCache.getOrWait([session], "write-only");
1157
+ if (crud === null) {
1230
1158
  switch (options?.or) {
1231
1159
  case "redirect": {
1232
1160
  await this.redirectToSignIn();
@@ -1240,22 +1168,22 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1240
1168
  }
1241
1169
  }
1242
1170
  }
1243
- return userJson && this._createCurrentUser(userJson, session);
1171
+ return crud && this._currentUserFromCrud(crud, session);
1244
1172
  }
1245
1173
  async getServerUser() {
1246
1174
  console.warn("stackServerApp.getServerUser is deprecated; use stackServerApp.getUser instead");
1247
1175
  return await this.getUser();
1248
1176
  }
1249
1177
  async getServerUserById(userId) {
1250
- const json = await this._serverUserCache.getOrWait([userId], "write-only");
1251
- return json && this._createUser(json);
1178
+ const crud = await this._serverUserCache.getOrWait([userId], "write-only");
1179
+ return crud && this._serverUserFromCrud(crud);
1252
1180
  }
1253
1181
  useUser(options) {
1254
1182
  this._ensurePersistentTokenStore(options?.tokenStore);
1255
1183
  const router = NextNavigation.useRouter();
1256
1184
  const session = this._getSession(options?.tokenStore);
1257
- const userJson = useAsyncCache(this._currentServerUserCache, [session], "useUser()");
1258
- if (userJson === null) {
1185
+ const crud = useAsyncCache(this._currentServerUserCache, [session], "useUser()");
1186
+ if (crud === null) {
1259
1187
  switch (options?.or) {
1260
1188
  case "redirect": {
1261
1189
  setTimeout(() => router.replace(this.urls.signIn), 0);
@@ -1271,65 +1199,50 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1271
1199
  }
1272
1200
  }
1273
1201
  return useMemo(() => {
1274
- return userJson && this._createCurrentUser(userJson, session);
1275
- }, [userJson, session, options?.or]);
1202
+ return crud && this._currentUserFromCrud(crud, session);
1203
+ }, [crud, session, options?.or]);
1276
1204
  }
1277
1205
  useUserById(userId) {
1278
- const json = useAsyncCache(this._serverUserCache, [userId], "useUserById()");
1206
+ const crud = useAsyncCache(this._serverUserCache, [userId], "useUserById()");
1279
1207
  return useMemo(() => {
1280
- return json && this._createUser(json);
1281
- }, [json]);
1208
+ return crud && this._serverUserFromCrud(crud);
1209
+ }, [crud]);
1282
1210
  }
1283
1211
  async listUsers() {
1284
- const json = await this._serverUsersCache.getOrWait([], "write-only");
1285
- return json.map((j) => this._createUser(j));
1212
+ const crud = await this._serverUsersCache.getOrWait([], "write-only");
1213
+ return crud.map((j) => this._serverUserFromCrud(j));
1286
1214
  }
1287
1215
  useUsers() {
1288
- const json = useAsyncCache(this._serverUsersCache, [], "useServerUsers()");
1216
+ const crud = useAsyncCache(this._serverUsersCache, [], "useServerUsers()");
1289
1217
  return useMemo(() => {
1290
- return json.map((j) => this._createUser(j));
1291
- }, [json]);
1292
- }
1293
- async listPermissionDefinitions() {
1294
- return await this._serverTeamPermissionDefinitionsCache.getOrWait([], "write-only");
1295
- }
1296
- usePermissionDefinitions() {
1297
- return useAsyncCache(this._serverTeamPermissionDefinitionsCache, [], "usePermissions()");
1218
+ return crud.map((j) => this._serverUserFromCrud(j));
1219
+ }, [crud]);
1298
1220
  }
1299
- _serverPermissionFromJson(json) {
1221
+ _serverPermissionFromCrud(crud) {
1300
1222
  return {
1301
- ...this._permissionFromJson(json),
1302
- __databaseUniqueId: json.__databaseUniqueId,
1303
- description: json.description,
1304
- containPermissionIds: json.containPermissionIds
1223
+ id: crud.id
1305
1224
  };
1306
1225
  }
1307
- async createPermissionDefinition(data) {
1308
- const permission = this._serverPermissionFromJson(await this._interface.createPermissionDefinition(data));
1309
- await this._serverTeamPermissionDefinitionsCache.refresh([]);
1310
- return permission;
1311
- }
1312
- async updatePermissionDefinition(permissionId, data) {
1313
- await this._interface.updatePermissionDefinition(permissionId, data);
1314
- await this._serverTeamPermissionDefinitionsCache.refresh([]);
1315
- }
1316
- async deletePermissionDefinition(permissionId) {
1317
- await this._interface.deletePermissionDefinition(permissionId);
1318
- await this._serverTeamPermissionDefinitionsCache.refresh([]);
1226
+ _serverTeamPermissionDefinitionFromCrud(crud) {
1227
+ return {
1228
+ id: crud.id,
1229
+ description: crud.description,
1230
+ containedPermissionIds: crud.contained_permission_ids
1231
+ };
1319
1232
  }
1320
1233
  async listTeams() {
1321
1234
  const teams = await this._serverTeamsCache.getOrWait([], "write-only");
1322
- return teams.map((t) => this._serverTeamFromJson(t));
1235
+ return teams.map((t) => this._serverTeamFromCrud(t));
1323
1236
  }
1324
1237
  async createTeam(data) {
1325
- const team = await this._interface.createServerTeam(data);
1238
+ const team = await this._interface.createServerTeam(serverTeamCreateOptionsToCrud(data));
1326
1239
  await this._serverTeamsCache.refresh([]);
1327
- return this._serverTeamFromJson(team);
1240
+ return this._serverTeamFromCrud(team);
1328
1241
  }
1329
1242
  useTeams() {
1330
1243
  const teams = useAsyncCache(this._serverTeamsCache, [], "useServerTeams()");
1331
1244
  return useMemo(() => {
1332
- return teams.map((t) => this._serverTeamFromJson(t));
1245
+ return teams.map((t) => this._serverTeamFromCrud(t));
1333
1246
  }, [teams]);
1334
1247
  }
1335
1248
  async getTeam(teamId) {
@@ -1354,20 +1267,6 @@ var _StackServerAppImpl = class extends _StackClientAppImpl {
1354
1267
  this._serverUsersCache.refresh([])
1355
1268
  ]);
1356
1269
  }
1357
- useEmailTemplates() {
1358
- return useAsyncCache(this._serverEmailTemplatesCache, [], "useEmailTemplates()");
1359
- }
1360
- async listEmailTemplates() {
1361
- return await this._serverEmailTemplatesCache.getOrWait([], "write-only");
1362
- }
1363
- async updateEmailTemplate(type, data) {
1364
- await this._interface.updateEmailTemplate(type, data);
1365
- await this._serverEmailTemplatesCache.refresh([]);
1366
- }
1367
- async resetEmailTemplate(type) {
1368
- await this._interface.resetEmailTemplate(type);
1369
- await this._serverEmailTemplatesCache.refresh([]);
1370
- }
1371
1270
  };
1372
1271
  var _StackAdminAppImpl = class extends _StackServerAppImpl {
1373
1272
  constructor(options) {
@@ -1391,18 +1290,114 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
1391
1290
  this._adminProjectCache = createCache(async () => {
1392
1291
  return await this._interface.getProject();
1393
1292
  });
1394
- this._apiKeySetsCache = createCache(async () => {
1395
- return await this._interface.listApiKeySets();
1293
+ this._apiKeysCache = createCache(async () => {
1294
+ return await this._interface.listApiKeys();
1295
+ });
1296
+ this._adminEmailTemplatesCache = createCache(async () => {
1297
+ return await this._interface.listEmailTemplates();
1298
+ });
1299
+ this._adminTeamPermissionDefinitionsCache = createCache(async () => {
1300
+ return await this._interface.listPermissionDefinitions();
1396
1301
  });
1397
1302
  }
1398
- _createApiKeySetBaseFromJson(data) {
1303
+ _adminOwnedProjectFromCrud(data, onRefresh) {
1304
+ if (this._tokenStoreInit !== null) {
1305
+ throw new StackAssertionError("Owned apps must always have tokenStore === null \u2014 did you not create this project with app._createOwnedApp()?");
1306
+ ;
1307
+ }
1308
+ return {
1309
+ ...this._adminProjectFromCrud(data, onRefresh),
1310
+ app: this
1311
+ };
1312
+ }
1313
+ _adminProjectFromCrud(data, onRefresh) {
1314
+ if (data.id !== this.projectId) {
1315
+ throw new StackAssertionError(`The project ID of the provided project JSON (${data.id}) does not match the project ID of the app (${this.projectId})!`);
1316
+ }
1317
+ const app = this;
1318
+ return {
1319
+ id: data.id,
1320
+ displayName: data.display_name,
1321
+ description: data.description,
1322
+ createdAt: new Date(data.created_at_millis),
1323
+ userCount: data.user_count,
1324
+ isProductionMode: data.is_production_mode,
1325
+ config: {
1326
+ id: data.config.id,
1327
+ credentialEnabled: data.config.credential_enabled,
1328
+ magicLinkEnabled: data.config.magic_link_enabled,
1329
+ allowLocalhost: data.config.allow_localhost,
1330
+ oauthProviders: data.config.oauth_providers.map((p) => p.type === "shared" ? {
1331
+ id: p.id,
1332
+ enabled: p.enabled,
1333
+ type: "shared"
1334
+ } : {
1335
+ id: p.id,
1336
+ enabled: p.enabled,
1337
+ type: "standard",
1338
+ clientId: p.client_id ?? throwErr("Client ID is missing"),
1339
+ clientSecret: p.client_secret ?? throwErr("Client secret is missing")
1340
+ }),
1341
+ emailConfig: data.config.email_config.type === "shared" ? {
1342
+ type: "shared"
1343
+ } : {
1344
+ type: "standard",
1345
+ host: data.config.email_config.host ?? throwErr("Email host is missing"),
1346
+ port: data.config.email_config.port ?? throwErr("Email port is missing"),
1347
+ username: data.config.email_config.username ?? throwErr("Email username is missing"),
1348
+ password: data.config.email_config.password ?? throwErr("Email password is missing"),
1349
+ senderName: data.config.email_config.sender_name ?? throwErr("Email sender name is missing"),
1350
+ senderEmail: data.config.email_config.sender_email ?? throwErr("Email sender email is missing")
1351
+ },
1352
+ domains: data.config.domains.map((d) => ({
1353
+ domain: d.domain,
1354
+ handlerPath: d.handler_path
1355
+ })),
1356
+ createTeamOnSignUp: data.config.create_team_on_sign_up,
1357
+ teamCreatorDefaultPermissions: data.config.team_creator_default_permissions,
1358
+ teamMemberDefaultPermissions: data.config.team_member_default_permissions
1359
+ },
1360
+ async update(update) {
1361
+ await app._interface.updateProject(adminProjectUpdateOptionsToCrud(update));
1362
+ await onRefresh();
1363
+ },
1364
+ async getProductionModeErrors() {
1365
+ return getProductionModeErrors(data);
1366
+ },
1367
+ useProductionModeErrors() {
1368
+ return getProductionModeErrors(data);
1369
+ }
1370
+ };
1371
+ }
1372
+ _adminEmailTemplateFromCrud(data) {
1373
+ return {
1374
+ type: data.type,
1375
+ subject: data.subject,
1376
+ content: data.content,
1377
+ isDefault: data.is_default
1378
+ };
1379
+ }
1380
+ async getProject() {
1381
+ return this._adminProjectFromCrud(
1382
+ await this._adminProjectCache.getOrWait([], "write-only"),
1383
+ () => this._refreshProject()
1384
+ );
1385
+ }
1386
+ useProject() {
1387
+ const crud = useAsyncCache(this._adminProjectCache, [], "useProjectAdmin()");
1388
+ return useMemo(() => this._adminProjectFromCrud(
1389
+ crud,
1390
+ () => this._refreshProject()
1391
+ ), [crud]);
1392
+ }
1393
+ _createApiKeyBaseFromCrud(data) {
1399
1394
  const app = this;
1400
1395
  return {
1401
1396
  id: data.id,
1402
1397
  description: data.description,
1403
- expiresAt: new Date(data.expiresAtMillis),
1404
- manuallyRevokedAt: data.manuallyRevokedAtMillis ? new Date(data.manuallyRevokedAtMillis) : null,
1405
- createdAt: new Date(data.createdAtMillis),
1398
+ expiresAt: new Date(data.expires_at_millis),
1399
+ manuallyRevokedAt: data.manually_revoked_at_millis ? new Date(data.manually_revoked_at_millis) : null,
1400
+ createdAt: new Date(data.created_at_millis),
1406
1401
  isValid() {
1407
1402
  return this.whyInvalid() === null;
1408
1403
  },
@@ -1412,57 +1407,83 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
1412
1407
  return null;
1413
1408
  },
1414
1409
  async revoke() {
1415
- const res = await app._interface.revokeApiKeySetById(data.id);
1416
- await app._refreshApiKeySets();
1410
+ const res = await app._interface.revokeApiKeyById(data.id);
1411
+ await app._refreshApiKeys();
1417
1412
  return res;
1418
1413
  }
1419
1414
  };
1420
1415
  }
1421
- _createApiKeySetFromJson(data) {
1416
+ _createApiKeyFromCrud(data) {
1422
1417
  return {
1423
- ...this._createApiKeySetBaseFromJson(data),
1424
- publishableClientKey: data.publishableClientKey ? { lastFour: data.publishableClientKey.lastFour } : null,
1425
- secretServerKey: data.secretServerKey ? { lastFour: data.secretServerKey.lastFour } : null,
1426
- superSecretAdminKey: data.superSecretAdminKey ? { lastFour: data.superSecretAdminKey.lastFour } : null
1418
+ ...this._createApiKeyBaseFromCrud(data),
1419
+ publishableClientKey: data.publishable_client_key ? { lastFour: data.publishable_client_key.last_four } : null,
1420
+ secretServerKey: data.secret_server_key ? { lastFour: data.secret_server_key.last_four } : null,
1421
+ superSecretAdminKey: data.super_secret_admin_key ? { lastFour: data.super_secret_admin_key.last_four } : null
1427
1422
  };
1428
1423
  }
1429
- _createApiKeySetFirstViewFromJson(data) {
1424
+ _createApiKeyFirstViewFromCrud(data) {
1430
1425
  return {
1431
- ...this._createApiKeySetBaseFromJson(data),
1432
- publishableClientKey: data.publishableClientKey,
1433
- secretServerKey: data.secretServerKey,
1434
- superSecretAdminKey: data.superSecretAdminKey
1426
+ ...this._createApiKeyBaseFromCrud(data),
1427
+ publishableClientKey: data.publishable_client_key,
1428
+ secretServerKey: data.secret_server_key,
1429
+ superSecretAdminKey: data.super_secret_admin_key
1435
1430
  };
1436
1431
  }
1437
- async getProjectAdmin() {
1438
- return this._projectAdminFromJson(
1439
- await this._adminProjectCache.getOrWait([], "write-only"),
1440
- this._interface,
1441
- () => this._refreshProject()
1442
- );
1432
+ async listApiKeys() {
1433
+ const crud = await this._apiKeysCache.getOrWait([], "write-only");
1434
+ return crud.map((j) => this._createApiKeyFromCrud(j));
1443
1435
  }
1444
- useProjectAdmin() {
1445
- const json = useAsyncCache(this._adminProjectCache, [], "useProjectAdmin()");
1446
- return useMemo(() => this._projectAdminFromJson(
1447
- json,
1448
- this._interface,
1449
- () => this._refreshProject()
1450
- ), [json]);
1436
+ useApiKeys() {
1437
+ const crud = useAsyncCache(this._apiKeysCache, [], "useApiKeys()");
1438
+ return useMemo(() => {
1439
+ return crud.map((j) => this._createApiKeyFromCrud(j));
1440
+ }, [crud]);
1451
1441
  }
1452
- async listApiKeySets() {
1453
- const json = await this._apiKeySetsCache.getOrWait([], "write-only");
1454
- return json.map((j) => this._createApiKeySetFromJson(j));
1442
+ async createApiKey(options) {
1443
+ const crud = await this._interface.createApiKey(apiKeyCreateOptionsToCrud(options));
1444
+ await this._refreshApiKeys();
1445
+ return this._createApiKeyFirstViewFromCrud(crud);
1455
1446
  }
1456
- useApiKeySets() {
1457
- const json = useAsyncCache(this._apiKeySetsCache, [], "useApiKeySets()");
1447
+ useEmailTemplates() {
1448
+ const crud = useAsyncCache(this._adminEmailTemplatesCache, [], "useEmailTemplates()");
1458
1449
  return useMemo(() => {
1459
- return json.map((j) => this._createApiKeySetFromJson(j));
1460
- }, [json]);
1450
+ return crud.map((j) => this._adminEmailTemplateFromCrud(j));
1451
+ }, [crud]);
1461
1452
  }
1462
- async createApiKeySet(options) {
1463
- const json = await this._interface.createApiKeySet(options);
1464
- await this._refreshApiKeySets();
1465
- return this._createApiKeySetFirstViewFromJson(json);
1453
+ async listEmailTemplates() {
1454
+ const crud = await this._adminEmailTemplatesCache.getOrWait([], "write-only");
1455
+ return crud.map((j) => this._adminEmailTemplateFromCrud(j));
1456
+ }
1457
+ async updateEmailTemplate(type, data) {
1458
+ await this._interface.updateEmailTemplate(type, adminEmailTemplateUpdateOptionsToCrud(data));
1459
+ await this._adminEmailTemplatesCache.refresh([]);
1460
+ }
1461
+ async resetEmailTemplate(type) {
1462
+ await this._interface.resetEmailTemplate(type);
1463
+ await this._adminEmailTemplatesCache.refresh([]);
1464
+ }
1465
+ async createTeamPermissionDefinition(data) {
1466
+ const crud = await this._interface.createPermissionDefinition(serverTeamPermissionDefinitionCreateOptionsToCrud(data));
1467
+ await this._adminTeamPermissionDefinitionsCache.refresh([]);
1468
+ return this._serverTeamPermissionDefinitionFromCrud(crud);
1469
+ }
1470
+ async updateTeamPermissionDefinition(permissionId, data) {
1471
+ await this._interface.updatePermissionDefinition(permissionId, data);
1472
+ await this._adminTeamPermissionDefinitionsCache.refresh([]);
1473
+ }
1474
+ async deleteTeamPermissionDefinition(permissionId) {
1475
+ await this._interface.deletePermissionDefinition(permissionId);
1476
+ await this._adminTeamPermissionDefinitionsCache.refresh([]);
1477
+ }
1478
+ async listTeamPermissionDefinitions() {
1479
+ const crud = await this._adminTeamPermissionDefinitionsCache.getOrWait([], "write-only");
1480
+ return crud.map((p) => this._serverTeamPermissionDefinitionFromCrud(p));
1481
+ }
1482
+ useTeamPermissionDefinitions() {
1483
+ const crud = useAsyncCache(this._adminTeamPermissionDefinitionsCache, [], "usePermissions()");
1484
+ return useMemo(() => {
1485
+ return crud.map((p) => this._serverTeamPermissionDefinitionFromCrud(p));
1486
+ }, [crud]);
1466
1487
  }
1467
1488
  async _refreshProject() {
1468
1489
  await Promise.all([
@@ -1470,17 +1491,125 @@ var _StackAdminAppImpl = class extends _StackServerAppImpl {
1470
1491
  this._adminProjectCache.refresh([])
1471
1492
  ]);
1472
1493
  }
1473
- async _refreshApiKeySets() {
1474
- await this._apiKeySetsCache.refresh([]);
1494
+ async _refreshApiKeys() {
1495
+ await this._apiKeysCache.refresh([]);
1475
1496
  }
1476
1497
  };
1498
+ function userUpdateOptionsToCrud(options) {
1499
+ return {
1500
+ display_name: options.displayName,
1501
+ client_metadata: options.clientMetadata,
1502
+ selected_team_id: options.selectedTeamId
1503
+ };
1504
+ }
1505
+ function serverUserUpdateOptionsToCrud(options) {
1506
+ return {
1507
+ display_name: options.displayName,
1508
+ client_metadata: options.clientMetadata,
1509
+ selected_team_id: options.selectedTeamId,
1510
+ primary_email: options.primaryEmail,
1511
+ primary_email_verified: options.primaryEmailVerified,
1512
+ server_metadata: options.serverMetadata
1513
+ };
1514
+ }
1515
+ function adminProjectUpdateOptionsToCrud(options) {
1516
+ return {
1517
+ display_name: options.displayName,
1518
+ description: options.description,
1519
+ is_production_mode: options.isProductionMode,
1520
+ config: {
1521
+ domains: options.config?.domains?.map((d) => ({
1522
+ domain: d.domain,
1523
+ handler_path: d.handlerPath
1524
+ })),
1525
+ oauth_providers: options.config?.oauthProviders?.map((p) => ({
1526
+ id: p.id,
1527
+ enabled: p.enabled,
1528
+ type: p.type,
1529
+ ...p.type === "standard" && {
1530
+ client_id: p.clientId,
1531
+ client_secret: p.clientSecret
1532
+ }
1533
+ })),
1534
+ email_config: options.config?.emailConfig && (options.config.emailConfig.type === "shared" ? {
1535
+ type: "shared"
1536
+ } : {
1537
+ type: "standard",
1538
+ host: options.config.emailConfig.host,
1539
+ port: options.config.emailConfig.port,
1540
+ username: options.config.emailConfig.username,
1541
+ password: options.config.emailConfig.password,
1542
+ sender_name: options.config.emailConfig.senderName,
1543
+ sender_email: options.config.emailConfig.senderEmail
1544
+ }),
1545
+ credential_enabled: options.config?.credentialEnabled,
1546
+ magic_link_enabled: options.config?.magicLinkEnabled,
1547
+ allow_localhost: options.config?.allowLocalhost,
1548
+ create_team_on_sign_up: options.config?.createTeamOnSignUp,
1549
+ team_creator_default_permissions: options.config?.teamCreatorDefaultPermissions,
1550
+ team_member_default_permissions: options.config?.teamMemberDefaultPermissions
1551
+ }
1552
+ };
1553
+ }
1554
+ function adminProjectCreateOptionsToCrud(options) {
1555
+ return {
1556
+ ...adminProjectUpdateOptionsToCrud(options),
1557
+ display_name: options.displayName
1558
+ };
1559
+ }
1560
+ function apiKeyCreateOptionsToCrud(options) {
1561
+ return {
1562
+ description: options.description,
1563
+ expires_at_millis: options.expiresAt.getTime(),
1564
+ has_publishable_client_key: options.hasPublishableClientKey,
1565
+ has_secret_server_key: options.hasSecretServerKey,
1566
+ has_super_secret_admin_key: options.hasSuperSecretAdminKey
1567
+ };
1568
+ }
1569
+ function teamCreateOptionsToCrud(options) {
1570
+ return {
1571
+ display_name: options.displayName,
1572
+ profile_image_url: options.profileImageUrl
1573
+ };
1574
+ }
1575
+ function serverTeamCreateOptionsToCrud(options) {
1576
+ return teamCreateOptionsToCrud(options);
1577
+ }
1578
+ function serverTeamUpdateOptionsToCrud(options) {
1579
+ return {
1580
+ display_name: options.displayName,
1581
+ profile_image_url: options.profileImageUrl
1582
+ };
1583
+ }
1584
+ function serverTeamPermissionDefinitionCreateOptionsToCrud(options) {
1585
+ return {
1586
+ id: options.id,
1587
+ description: options.description,
1588
+ contained_permission_ids: options.containedPermissionIds
1589
+ };
1590
+ }
1591
+ function serverTeamPermissionDefinitionUpdateOptionsToCrud(options) {
1592
+ return {
1593
+ id: options.id,
1594
+ description: options.description,
1595
+ contained_permission_ids: options.containedPermissionIds
1596
+ };
1597
+ }
1477
1598
  var StackClientApp = _StackClientAppImpl;
1478
1599
  var StackServerApp = _StackServerAppImpl;
1479
1600
  var StackAdminApp = _StackAdminAppImpl;
1601
+ function adminEmailTemplateUpdateOptionsToCrud(options) {
1602
+ return {
1603
+ subject: options.subject,
1604
+ content: options.content
1605
+ };
1606
+ }
1480
1607
  export {
1481
1608
  StackAdminApp,
1482
1609
  StackClientApp,
1483
1610
  StackServerApp,
1611
+ serverTeamPermissionDefinitionCreateOptionsToCrud,
1612
+ serverTeamPermissionDefinitionUpdateOptionsToCrud,
1484
1613
  stackAppInternalsSymbol
1485
1614
  };
1486
1615
  //# sourceMappingURL=stack-app.js.map