@stackframe/stack-shared 2.5.2 → 2.5.4

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 (81) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/crud.d.ts +10 -3
  3. package/dist/helpers/production-mode.d.ts +6 -0
  4. package/dist/helpers/production-mode.js +43 -0
  5. package/dist/index.d.ts +4 -4
  6. package/dist/index.js +4 -4
  7. package/dist/interface/adminInterface.d.ts +28 -67
  8. package/dist/interface/adminInterface.js +63 -22
  9. package/dist/interface/clientInterface.d.ts +21 -133
  10. package/dist/interface/clientInterface.js +92 -119
  11. package/dist/interface/crud/api-keys.d.ts +134 -0
  12. package/dist/interface/crud/api-keys.js +61 -0
  13. package/dist/interface/crud/current-user.d.ts +47 -11
  14. package/dist/interface/crud/current-user.js +7 -3
  15. package/dist/interface/crud/email-templates.d.ts +53 -34
  16. package/dist/interface/crud/email-templates.js +37 -24
  17. package/dist/interface/crud/oauth.d.ts +8 -9
  18. package/dist/interface/crud/oauth.js +5 -5
  19. package/dist/interface/crud/projects.d.ts +446 -0
  20. package/dist/interface/crud/projects.js +110 -0
  21. package/dist/interface/crud/team-memberships.d.ts +22 -0
  22. package/dist/interface/crud/team-memberships.js +22 -0
  23. package/dist/interface/crud/team-permissions.d.ts +129 -0
  24. package/dist/interface/crud/team-permissions.js +83 -0
  25. package/dist/interface/crud/teams.d.ts +148 -0
  26. package/dist/interface/crud/teams.js +80 -0
  27. package/dist/interface/crud/users.d.ts +88 -33
  28. package/dist/interface/crud/users.js +22 -14
  29. package/dist/interface/crud-deprecated/api-keys.d.ts +134 -0
  30. package/dist/interface/crud-deprecated/api-keys.js +61 -0
  31. package/dist/interface/crud-deprecated/current-user.d.ts +127 -0
  32. package/dist/interface/crud-deprecated/current-user.js +49 -0
  33. package/dist/interface/crud-deprecated/email-templates.d.ts +75 -0
  34. package/dist/interface/crud-deprecated/email-templates.js +41 -0
  35. package/dist/interface/crud-deprecated/oauth.d.ts +24 -0
  36. package/dist/interface/crud-deprecated/oauth.js +12 -0
  37. package/dist/interface/crud-deprecated/projects.d.ts +440 -0
  38. package/dist/interface/crud-deprecated/projects.js +109 -0
  39. package/dist/interface/crud-deprecated/team-memberships.d.ts +22 -0
  40. package/dist/interface/crud-deprecated/team-memberships.js +22 -0
  41. package/dist/interface/crud-deprecated/team-permissions.d.ts +129 -0
  42. package/dist/interface/crud-deprecated/team-permissions.js +83 -0
  43. package/dist/interface/crud-deprecated/teams.d.ts +126 -0
  44. package/dist/interface/crud-deprecated/teams.js +78 -0
  45. package/dist/interface/crud-deprecated/users.d.ts +201 -0
  46. package/dist/interface/crud-deprecated/users.js +75 -0
  47. package/dist/interface/serverInterface.d.ts +33 -60
  48. package/dist/interface/serverInterface.js +74 -102
  49. package/dist/known-errors.d.ts +43 -26
  50. package/dist/known-errors.js +135 -92
  51. package/dist/schema-fields.d.ts +53 -4
  52. package/dist/schema-fields.js +156 -26
  53. package/dist/sessions.d.ts +1 -0
  54. package/dist/sessions.js +20 -26
  55. package/dist/utils/arrays.d.ts +4 -0
  56. package/dist/utils/arrays.js +10 -0
  57. package/dist/utils/caches.js +11 -18
  58. package/dist/utils/compile-time.d.ts +3 -1
  59. package/dist/utils/compile-time.js +3 -1
  60. package/dist/utils/errors.d.ts +8 -1
  61. package/dist/utils/errors.js +58 -47
  62. package/dist/utils/globals.js +3 -0
  63. package/dist/utils/maps.js +8 -5
  64. package/dist/utils/numbers.js +5 -5
  65. package/dist/utils/objects.d.ts +4 -1
  66. package/dist/utils/objects.js +16 -8
  67. package/dist/utils/promises.js +6 -2
  68. package/dist/utils/proxies.d.ts +1 -0
  69. package/dist/utils/proxies.js +65 -0
  70. package/dist/utils/react.d.ts +1 -1
  71. package/dist/utils/react.js +2 -2
  72. package/dist/utils/results.js +0 -1
  73. package/dist/utils/stores.js +7 -10
  74. package/dist/utils/strings.js +7 -2
  75. package/dist/utils/urls.d.ts +1 -0
  76. package/dist/utils/urls.js +8 -0
  77. package/dist/utils/uuids.d.ts +1 -1
  78. package/dist/utils/uuids.js +2 -1
  79. package/package.json +2 -2
  80. package/dist/utils/yup.d.ts +0 -3
  81. package/dist/utils/yup.js +0 -13
@@ -1,23 +1,16 @@
1
- import { StatusError, throwErr } from "./utils/errors";
1
+ import { StackAssertionError, StatusError, throwErr } from "./utils/errors";
2
2
  import { identityArgs } from "./utils/functions";
3
3
  import { deindent } from "./utils/strings";
4
4
  export class KnownError extends StatusError {
5
- statusCode;
6
- humanReadableMessage;
7
- details;
8
- name = "KnownError";
9
5
  constructor(statusCode, humanReadableMessage, details) {
10
6
  super(statusCode, humanReadableMessage);
11
7
  this.statusCode = statusCode;
12
8
  this.humanReadableMessage = humanReadableMessage;
13
9
  this.details = details;
10
+ this.name = "KnownError";
14
11
  }
15
12
  getBody() {
16
- return new TextEncoder().encode(JSON.stringify({
17
- code: this.errorCode,
18
- details: this.details,
19
- error: this.humanReadableMessage,
20
- }, undefined, 2));
13
+ return new TextEncoder().encode(JSON.stringify(this.toDescriptiveJson(), undefined, 2));
21
14
  }
22
15
  getHeaders() {
23
16
  return {
@@ -25,6 +18,13 @@ export class KnownError extends StatusError {
25
18
  "X-Stack-Known-Error": [this.errorCode],
26
19
  };
27
20
  }
21
+ toDescriptiveJson() {
22
+ return {
23
+ code: this.errorCode,
24
+ ...this.details ? { details: this.details } : {},
25
+ error: this.humanReadableMessage,
26
+ };
27
+ }
28
28
  get errorCode() {
29
29
  return this.constructor.errorCode ?? throwErr(`Can't find error code for this KnownError. Is its constructor a KnownErrorConstructor? ${this}`);
30
30
  }
@@ -32,18 +32,19 @@ export class KnownError extends StatusError {
32
32
  return [
33
33
  400,
34
34
  json.message,
35
- json.details,
35
+ json,
36
36
  ];
37
37
  }
38
38
  static fromJson(json) {
39
39
  for (const [_, KnownErrorType] of Object.entries(KnownErrors)) {
40
40
  if (json.code === KnownErrorType.prototype.errorCode) {
41
+ const constructorArgs = KnownErrorType.constructorArgsFromJson(json);
41
42
  return new KnownErrorType(
42
43
  // @ts-expect-error
43
- ...KnownErrorType.constructorArgsFromJson(json));
44
+ ...constructorArgs);
44
45
  }
45
46
  }
46
- throw new Error(`Unknown KnownError code: ${json.code}`);
47
+ throw new Error(`Unknown KnownError code. You may need to update your version of Stack to see more detailed information. ${json.code}: ${json.message}`);
47
48
  }
48
49
  }
49
50
  const knownErrorConstructorErrorCodeSentinel = Symbol("knownErrorConstructorErrorCodeSentinel");
@@ -52,30 +53,29 @@ function createKnownErrorConstructor(SuperClass, errorCode, create, constructorA
52
53
  const constructorArgsFromJsonFn = constructorArgsFromJson === "inherit" ? SuperClass.constructorArgsFromJson : constructorArgsFromJson;
53
54
  // @ts-expect-error this is not a mixin, but TS detects it as one
54
55
  class KnownErrorImpl extends SuperClass {
55
- static errorCode = errorCode;
56
- name = `KnownError<${errorCode}>`;
57
- constructorArgs;
58
56
  constructor(...args) {
59
57
  // @ts-expect-error
60
58
  super(...createFn(...args));
59
+ this.name = `KnownError<${errorCode}>`;
61
60
  this.constructorArgs = args;
62
61
  }
63
62
  static constructorArgsFromJson(json) {
64
- return constructorArgsFromJsonFn(json);
63
+ return constructorArgsFromJsonFn(json.details);
65
64
  }
66
65
  }
66
+ KnownErrorImpl.errorCode = errorCode;
67
67
  ;
68
68
  // @ts-expect-error
69
69
  return KnownErrorImpl;
70
70
  }
71
71
  const UnsupportedError = createKnownErrorConstructor(KnownError, "UNSUPPORTED_ERROR", (originalErrorCode) => [
72
72
  500,
73
- `An error occured that is not currently supported (possibly because it was added in a version of Stack that is newer than this client). The original unsupported error code was: ${originalErrorCode}`,
73
+ `An error occurred that is not currently supported (possibly because it was added in a version of Stack that is newer than this client). The original unsupported error code was: ${originalErrorCode}`,
74
74
  {
75
75
  originalErrorCode,
76
76
  },
77
77
  ], (json) => [
78
- json.details?.originalErrorCode ?? throwErr("originalErrorCode not found in UnsupportedError details"),
78
+ json?.originalErrorCode ?? throwErr("originalErrorCode not found in UnsupportedError details"),
79
79
  ]);
80
80
  const BodyParsingError = createKnownErrorConstructor(KnownError, "BODY_PARSING_ERROR", (message) => [
81
81
  400,
@@ -83,7 +83,10 @@ const BodyParsingError = createKnownErrorConstructor(KnownError, "BODY_PARSING_E
83
83
  ], (json) => [json.message]);
84
84
  const SchemaError = createKnownErrorConstructor(KnownError, "SCHEMA_ERROR", (message) => [
85
85
  400,
86
- message,
86
+ message || throwErr("SchemaError requires a message"),
87
+ {
88
+ message,
89
+ },
87
90
  ], (json) => [json.message]);
88
91
  const AllOverloadsFailed = createKnownErrorConstructor(KnownError, "ALL_OVERLOADS_FAILED", (overloadErrors) => [
89
92
  400,
@@ -98,58 +101,61 @@ const AllOverloadsFailed = createKnownErrorConstructor(KnownError, "ALL_OVERLOAD
98
101
  overload_errors: overloadErrors,
99
102
  },
100
103
  ], (json) => [
101
- json.details?.overload_errors ?? throwErr("overload_errors not found in AllOverloadsFailed details"),
104
+ json?.overload_errors ?? throwErr("overload_errors not found in AllOverloadsFailed details"),
102
105
  ]);
103
106
  const ProjectAuthenticationError = createKnownErrorConstructor(KnownError, "PROJECT_AUTHENTICATION_ERROR", "inherit", "inherit");
104
- const InvalidProjectAccess = createKnownErrorConstructor(ProjectAuthenticationError, "INVALID_PROJECT_AUTHENTICATION", "inherit", "inherit");
107
+ const InvalidProjectAuthentication = createKnownErrorConstructor(ProjectAuthenticationError, "INVALID_PROJECT_AUTHENTICATION", "inherit", "inherit");
108
+ // TODO next-release: delete deprecated error type
105
109
  /**
106
110
  * @deprecated Use ProjectKeyWithoutAccessType instead
107
111
  */
108
- const ProjectKeyWithoutRequestType = createKnownErrorConstructor(InvalidProjectAccess, "PROJECT_KEY_WITHOUT_REQUEST_TYPE", () => [
112
+ const ProjectKeyWithoutRequestType = createKnownErrorConstructor(InvalidProjectAuthentication, "PROJECT_KEY_WITHOUT_REQUEST_TYPE", () => [
109
113
  400,
110
- "Either an API key or an admin access token was provided, but the x-stack-request-type header is missing. Set it to 'client', 'server', or 'admin' as appropriate.",
114
+ "Either an API key or an admin access token was provided, but the x-stack-access-type header is missing. Set it to 'client', 'server', or 'admin' as appropriate.",
111
115
  ], () => []);
116
+ // TODO next-release: delete deprecated error type
112
117
  /**
113
118
  * @deprecated Use InvalidAccessType instead
114
119
  */
115
- const InvalidRequestType = createKnownErrorConstructor(InvalidProjectAccess, "INVALID_REQUEST_TYPE", (requestType) => [
120
+ const InvalidRequestType = createKnownErrorConstructor(InvalidProjectAuthentication, "INVALID_REQUEST_TYPE", (requestType) => [
116
121
  400,
117
- `The x-stack-request-type header must be 'client', 'server', or 'admin', but was '${requestType}'.`,
122
+ `The x-stack-access-type header must be 'client', 'server', or 'admin', but was '${requestType}'.`,
118
123
  ], (json) => [
119
- json.details?.requestType ?? throwErr("requestType not found in InvalidRequestType details"),
124
+ json?.requestType ?? throwErr("requestType not found in InvalidRequestType details"),
120
125
  ]);
126
+ // TODO next-release: delete deprecated error type
121
127
  /**
122
128
  * @deprecated Use AccessTypeWithoutProjectId instead
123
129
  */
124
- const RequestTypeWithoutProjectId = createKnownErrorConstructor(InvalidProjectAccess, "REQUEST_TYPE_WITHOUT_PROJECT_ID", (requestType) => [
130
+ const RequestTypeWithoutProjectId = createKnownErrorConstructor(InvalidProjectAuthentication, "REQUEST_TYPE_WITHOUT_PROJECT_ID", (requestType) => [
125
131
  400,
126
- `The x-stack-request-type header was '${requestType}', but the x-stack-project-id header was not provided.`,
132
+ `The x-stack-access-type header was '${requestType}', but the x-stack-project-id header was not provided.`,
127
133
  {
128
134
  request_type: requestType,
129
135
  },
130
136
  ], (json) => [json.request_type]);
131
- const ProjectKeyWithoutAccessType = createKnownErrorConstructor(InvalidProjectAccess, "PROJECT_KEY_WITHOUT_ACCESS_TYPE", () => [
137
+ const ProjectKeyWithoutAccessType = createKnownErrorConstructor(InvalidProjectAuthentication, "PROJECT_KEY_WITHOUT_ACCESS_TYPE", () => [
132
138
  400,
133
139
  "Either an API key or an admin access token was provided, but the x-stack-access-type header is missing. Set it to 'client', 'server', or 'admin' as appropriate.",
134
140
  ], () => []);
135
- const InvalidAccessType = createKnownErrorConstructor(InvalidProjectAccess, "INVALID_ACCESS_TYPE", (requestType) => [
141
+ const InvalidAccessType = createKnownErrorConstructor(InvalidProjectAuthentication, "INVALID_ACCESS_TYPE", (accessType) => [
136
142
  400,
137
- `The x-stack-access-type header must be 'client', 'server', or 'admin', but was '${requestType}'.`,
143
+ `The x-stack-access-type header must be 'client', 'server', or 'admin', but was '${accessType}'.`,
138
144
  ], (json) => [
139
- json.details?.requestType ?? throwErr("requestType not found in InvalidRequestType details"),
145
+ json?.accessType ?? throwErr("accessType not found in InvalidAccessType details"),
140
146
  ]);
141
- const AccessTypeWithoutProjectId = createKnownErrorConstructor(InvalidProjectAccess, "ACCESS_TYPE_WITHOUT_PROJECT_ID", (requestType) => [
147
+ const AccessTypeWithoutProjectId = createKnownErrorConstructor(InvalidProjectAuthentication, "ACCESS_TYPE_WITHOUT_PROJECT_ID", (accessType) => [
142
148
  400,
143
- `The x-stack-access-type header was '${requestType}', but the x-stack-project-id header was not provided.`,
149
+ `The x-stack-access-type header was '${accessType}', but the x-stack-project-id header was not provided.`,
144
150
  {
145
- request_type: requestType,
151
+ request_type: accessType,
146
152
  },
147
153
  ], (json) => [json.request_type]);
148
- const AccessTypeRequired = createKnownErrorConstructor(InvalidProjectAccess, "ACCESS_TYPE_REQUIRED", () => [
154
+ const AccessTypeRequired = createKnownErrorConstructor(InvalidProjectAuthentication, "ACCESS_TYPE_REQUIRED", () => [
149
155
  400,
150
156
  `You must specify an access level for this Stack project. Make sure project API keys are provided (eg. x-stack-publishable-client-key) and you set the x-stack-access-type header to 'client', 'server', or 'admin'.`,
151
157
  ], () => []);
152
- const InsufficientAccessType = createKnownErrorConstructor(InvalidProjectAccess, "INSUFFICIENT_ACCESS_TYPE", (actualAccessType, allowedAccessTypes) => [
158
+ const InsufficientAccessType = createKnownErrorConstructor(InvalidProjectAuthentication, "INSUFFICIENT_ACCESS_TYPE", (actualAccessType, allowedAccessTypes) => [
153
159
  401,
154
160
  `The x-stack-access-type header must be ${allowedAccessTypes.map(s => `'${s}'`).join(" or ")}, but was '${actualAccessType}'.`,
155
161
  {
@@ -157,31 +163,31 @@ const InsufficientAccessType = createKnownErrorConstructor(InvalidProjectAccess,
157
163
  allowed_access_types: allowedAccessTypes,
158
164
  },
159
165
  ], (json) => [
160
- json.details.actual_access_type,
161
- json.details.allowed_access_types,
166
+ json.actual_access_type,
167
+ json.allowed_access_types,
162
168
  ]);
163
- const InvalidPublishableClientKey = createKnownErrorConstructor(InvalidProjectAccess, "INVALID_PUBLISHABLE_CLIENT_KEY", (projectId) => [
169
+ const InvalidPublishableClientKey = createKnownErrorConstructor(InvalidProjectAuthentication, "INVALID_PUBLISHABLE_CLIENT_KEY", (projectId) => [
164
170
  401,
165
171
  `The publishable key is not valid for the project ${JSON.stringify(projectId)}. Does the project and/or the key exist?`,
166
172
  {
167
173
  project_id: projectId,
168
174
  },
169
175
  ], (json) => [json.project_id]);
170
- const InvalidSecretServerKey = createKnownErrorConstructor(InvalidProjectAccess, "INVALID_SECRET_SERVER_KEY", (projectId) => [
176
+ const InvalidSecretServerKey = createKnownErrorConstructor(InvalidProjectAuthentication, "INVALID_SECRET_SERVER_KEY", (projectId) => [
171
177
  401,
172
178
  `The secret server key is not valid for the project ${JSON.stringify(projectId)}. Does the project and/or the key exist?`,
173
179
  {
174
180
  project_id: projectId,
175
181
  },
176
182
  ], (json) => [json.project_id]);
177
- const InvalidSuperSecretAdminKey = createKnownErrorConstructor(InvalidProjectAccess, "INVALID_SUPER_SECRET_ADMIN_KEY", (projectId) => [
183
+ const InvalidSuperSecretAdminKey = createKnownErrorConstructor(InvalidProjectAuthentication, "INVALID_SUPER_SECRET_ADMIN_KEY", (projectId) => [
178
184
  401,
179
185
  `The super secret admin key is not valid for the project ${JSON.stringify(projectId)}. Does the project and/or the key exist?`,
180
186
  {
181
187
  project_id: projectId,
182
188
  },
183
189
  ], (json) => [json.project_id]);
184
- const InvalidAdminAccessToken = createKnownErrorConstructor(InvalidProjectAccess, "INVALID_ADMIN_ACCESS_TOKEN", "inherit", "inherit");
190
+ const InvalidAdminAccessToken = createKnownErrorConstructor(InvalidProjectAuthentication, "INVALID_ADMIN_ACCESS_TOKEN", "inherit", "inherit");
185
191
  const UnparsableAdminAccessToken = createKnownErrorConstructor(InvalidAdminAccessToken, "UNPARSABLE_ADMIN_ACCESS_TOKEN", () => [
186
192
  401,
187
193
  "Admin access token is not parsable.",
@@ -263,22 +269,14 @@ const InvalidProjectForAccessToken = createKnownErrorConstructor(InvalidAccessTo
263
269
  401,
264
270
  "Access token not valid for this project.",
265
271
  ], () => []);
266
- const SessionUserEmailNotVerified = createKnownErrorConstructor(InvalidSessionAuthentication, "SESSION_USER_EMAIL_NOT_VERIFIED", () => [
267
- 401,
268
- "User e-mail not verified, but is required by the project.",
269
- ], () => []);
270
- const SessionAuthenticationRequired = createKnownErrorConstructor(SessionAuthenticationError, "SESSION_AUTHENTICATION_REQUIRED", () => [
272
+ const RefreshTokenError = createKnownErrorConstructor(KnownError, "REFRESH_TOKEN_ERROR", "inherit", "inherit");
273
+ const RefreshTokenNotFoundOrExpired = createKnownErrorConstructor(RefreshTokenError, "REFRESH_TOKEN_NOT_FOUND_OR_EXPIRED", () => [
271
274
  401,
272
- "Session required for this request.",
275
+ "Refresh token not found for this project, or the session has expired/been revoked.",
273
276
  ], () => []);
274
- const RefreshTokenError = createKnownErrorConstructor(KnownError, "INVALID_REFRESH_TOKEN", "inherit", "inherit");
275
277
  const ProviderRejected = createKnownErrorConstructor(RefreshTokenError, "PROVIDER_REJECTED", () => [
276
278
  401,
277
- "The provider refused to refresh their token.",
278
- ], () => []);
279
- const InvalidRefreshToken = createKnownErrorConstructor(RefreshTokenError, "REFRESH_TOKEN_EXPIRED", () => [
280
- 401,
281
- "Refresh token has expired. A new refresh token requires reauthentication.",
279
+ "The provider refused to refresh their token. This usually means that the provider used to authenticate the user no longer regards this session as valid, and the user must re-authenticate.",
282
280
  ], () => []);
283
281
  const UserEmailAlreadyExists = createKnownErrorConstructor(KnownError, "USER_EMAIL_ALREADY_EXISTS", () => [
284
282
  400,
@@ -296,9 +294,20 @@ const ApiKeyNotFound = createKnownErrorConstructor(KnownError, "API_KEY_NOT_FOUN
296
294
  404,
297
295
  "API key not found.",
298
296
  ], () => []);
299
- const ProjectNotFound = createKnownErrorConstructor(KnownError, "PROJECT_NOT_FOUND", () => [
300
- 404,
301
- "Project not found or is not accessible with the current user.",
297
+ const ProjectNotFound = createKnownErrorConstructor(KnownError, "PROJECT_NOT_FOUND", (projectId) => {
298
+ if (typeof projectId !== "string")
299
+ throw new StackAssertionError("projectId of KnownErrors.ProjectNotFound must be a string");
300
+ return [
301
+ 404,
302
+ `Project ${projectId} not found or is not accessible with the current user.`,
303
+ {
304
+ project_id: projectId,
305
+ },
306
+ ];
307
+ }, (json) => [json.project_id]);
308
+ const PasswordAuthenticationNotEnabled = createKnownErrorConstructor(KnownError, "PASSWORD_AUTHENTICATION_NOT_ENABLED", () => [
309
+ 400,
310
+ "Password authentication is not enabled for this project.",
302
311
  ], () => []);
303
312
  const EmailPasswordMismatch = createKnownErrorConstructor(KnownError, "EMAIL_PASSWORD_MISMATCH", () => [
304
313
  400,
@@ -313,10 +322,10 @@ const PasswordTooShort = createKnownErrorConstructor(PasswordRequirementsNotMet,
313
322
  400,
314
323
  `Password too short. Minimum length is ${minLength}.`,
315
324
  {
316
- minLength,
325
+ min_length: minLength,
317
326
  },
318
327
  ], (json) => [
319
- json.details?.minLength ?? throwErr("minLength not found in PasswordTooShort details"),
328
+ json?.min_length ?? throwErr("min_length not found in PasswordTooShort details"),
320
329
  ]);
321
330
  const PasswordTooLong = createKnownErrorConstructor(PasswordRequirementsNotMet, "PASSWORD_TOO_LONG", (maxLength) => [
322
331
  400,
@@ -325,8 +334,12 @@ const PasswordTooLong = createKnownErrorConstructor(PasswordRequirementsNotMet,
325
334
  maxLength,
326
335
  },
327
336
  ], (json) => [
328
- json.details?.maxLength ?? throwErr("maxLength not found in PasswordTooLong details"),
337
+ json?.maxLength ?? throwErr("maxLength not found in PasswordTooLong details"),
329
338
  ]);
339
+ const UserDoesNotHavePassword = createKnownErrorConstructor(KnownError, "USER_DOES_NOT_HAVE_PASSWORD", () => [
340
+ 400,
341
+ "This user does not have password authentication enabled.",
342
+ ], () => []);
330
343
  const VerificationCodeError = createKnownErrorConstructor(KnownError, "VERIFICATION_ERROR", "inherit", "inherit");
331
344
  const VerificationCodeNotFound = createKnownErrorConstructor(VerificationCodeError, "VERIFICATION_CODE_NOT_FOUND", () => [
332
345
  404,
@@ -340,7 +353,7 @@ const VerificationCodeAlreadyUsed = createKnownErrorConstructor(VerificationCode
340
353
  400,
341
354
  "The verification link has already been used.",
342
355
  ], () => []);
343
- const PasswordMismatch = createKnownErrorConstructor(KnownError, "PASSWORD_MISMATCH", () => [
356
+ const PasswordConfirmationMismatch = createKnownErrorConstructor(KnownError, "PASSWORD_CONFIRMATION_MISMATCH", () => [
344
357
  400,
345
358
  "Passwords do not match.",
346
359
  ], () => []);
@@ -348,43 +361,47 @@ const EmailAlreadyVerified = createKnownErrorConstructor(KnownError, "EMAIL_ALRE
348
361
  400,
349
362
  "The e-mail is already verified.",
350
363
  ], () => []);
364
+ const EmailNotAssociatedWithUser = createKnownErrorConstructor(KnownError, "EMAIL_NOT_ASSOCIATED_WITH_USER", () => [
365
+ 400,
366
+ "The e-mail is not associated with a user that could log in with that e-mail.",
367
+ ], () => []);
368
+ const EmailIsNotPrimaryEmail = createKnownErrorConstructor(KnownError, "EMAIL_IS_NOT_PRIMARY_EMAIL", (email, primaryEmail) => [
369
+ 400,
370
+ `The given e-mail (${email}) must equal the user's primary e-mail (${primaryEmail}).`,
371
+ {
372
+ email,
373
+ primary_email: primaryEmail,
374
+ },
375
+ ], (json) => [json.email, json.primary_email]);
351
376
  const PermissionNotFound = createKnownErrorConstructor(KnownError, "PERMISSION_NOT_FOUND", (permissionId) => [
352
377
  404,
353
- `Permission ${permissionId} not found. Make sure you created it on the dashboard.`,
378
+ `Permission "${permissionId}" not found. Make sure you created it on the dashboard.`,
354
379
  {
355
380
  permission_id: permissionId,
356
381
  },
357
- ], (json) => [json.details.permission_id]);
358
- const PermissionScopeMismatch = createKnownErrorConstructor(KnownError, "PERMISSION_SCOPE_MISMATCH", (permissionId, permissionScope, testScope) => {
359
- return [
360
- 400,
361
- `The scope of the permission with ID ${permissionId} is \`${permissionScope.type}\` but you tested against permissions of scope \`${testScope.type}\`. ${{
362
- "global": `Please don't specify any teams when using global permissions. For example: \`user.hasPermission(${JSON.stringify(permissionId)})\`.`,
363
- "any-team": `Please specify the team. For example: \`user.hasPermission(team, ${JSON.stringify(permissionId)})\`.`,
364
- "specific-team": `Please specify the team. For example: \`user.hasPermission(team, ${JSON.stringify(permissionId)})\`.`,
365
- }[permissionScope.type]}`,
366
- {
367
- permission_id: permissionId,
368
- permission_scope: permissionScope,
369
- test_scope: testScope,
370
- },
371
- ];
372
- }, (json) => [json.details.permission_id, json.details.permission_scope, json.details.test_scope]);
373
- const UserNotInTeam = createKnownErrorConstructor(KnownError, "USER_NOT_IN_TEAM", (userId, teamId) => [
382
+ ], (json) => [json.permission_id]);
383
+ const ContainedPermissionNotFound = createKnownErrorConstructor(KnownError, "CONTAINED_PERMISSION_NOT_FOUND", (permissionId) => [
374
384
  400,
375
- `User ${userId} is not in team ${teamId}.`,
385
+ `Contained permission with ID "${permissionId}" not found. Make sure you created it on the dashboard.`,
376
386
  {
377
- user_id: userId,
378
- team_id: teamId,
387
+ permission_id: permissionId,
379
388
  },
380
- ], (json) => [json.details.user_id, json.details.team_id]);
389
+ ], (json) => [json.permission_id]);
381
390
  const TeamNotFound = createKnownErrorConstructor(KnownError, "TEAM_NOT_FOUND", (teamId) => [
382
391
  404,
383
392
  `Team ${teamId} not found.`,
384
393
  {
385
394
  team_id: teamId,
386
395
  },
387
- ], (json) => [json.details.team_id]);
396
+ ], (json) => [json.team_id]);
397
+ const TeamMembershipNotFound = createKnownErrorConstructor(KnownError, "TEAM_MEMBERSHIP_NOT_FOUND", (teamId, userId) => [
398
+ 404,
399
+ `User ${userId} is not found in team ${teamId}.`,
400
+ {
401
+ team_id: teamId,
402
+ user_id: userId,
403
+ },
404
+ ], (json) => [json.team_id, json.user_id]);
388
405
  const EmailTemplateAlreadyExists = createKnownErrorConstructor(KnownError, "EMAIL_TEMPLATE_ALREADY_EXISTS", () => [
389
406
  400,
390
407
  "Email template already exists.",
@@ -409,6 +426,17 @@ const OAuthAccessTokenNotAvailableWithSharedOAuthKeys = createKnownErrorConstruc
409
426
  400,
410
427
  "Access tokens are not available with shared OAuth keys. Please add your own OAuth keys on the Stack dashboard to use access tokens.",
411
428
  ], () => []);
429
+ const InvalidOAuthClientIdOrSecret = createKnownErrorConstructor(KnownError, "INVALID_OAUTH_CLIENT_ID_OR_SECRET", (clientId) => [
430
+ 400,
431
+ "The OAuth client ID or secret is invalid. The client ID must be equal to the project ID, and the client secret must be a publishable client key.",
432
+ {
433
+ client_id: clientId ?? null,
434
+ },
435
+ ], (json) => [json.client_id ?? undefined]);
436
+ const InvalidScope = createKnownErrorConstructor(KnownError, "INVALID_SCOPE", (scope) => [
437
+ 400,
438
+ `The scope "${scope}" is not a valid OAuth scope for Stack.`,
439
+ ], (json) => [json.scope]);
412
440
  const UserAlreadyConnectedToAnotherOAuthConnection = createKnownErrorConstructor(KnownError, "USER_ALREADY_CONNECTED_TO_ANOTHER_OAUTH_CONNECTION", () => [
413
441
  400,
414
442
  "The user is already connected to another OAuth account. Did you maybe selected the wrong account?",
@@ -417,13 +445,21 @@ const OuterOAuthTimeout = createKnownErrorConstructor(KnownError, "OUTER_OAUTH_T
417
445
  408,
418
446
  "The OAuth flow has timed out. Please sign in again.",
419
447
  ], () => []);
448
+ const OAuthProviderNotFoundOrNotEnabled = createKnownErrorConstructor(KnownError, "OAUTH_PROVIDER_NOT_FOUND_OR_NOT_ENABLED", () => [
449
+ 400,
450
+ "The OAuth provider is not found or not enabled.",
451
+ ], () => []);
452
+ const UserAuthenticationRequired = createKnownErrorConstructor(KnownError, "USER_AUTHENTICATION_REQUIRED", () => [
453
+ 401,
454
+ "User authentication required for this endpoint.",
455
+ ], () => []);
420
456
  export const KnownErrors = {
421
457
  UnsupportedError,
422
458
  BodyParsingError,
423
459
  SchemaError,
424
460
  AllOverloadsFailed,
425
461
  ProjectAuthenticationError,
426
- InvalidProjectAuthentication: InvalidProjectAccess,
462
+ InvalidProjectAuthentication,
427
463
  ProjectKeyWithoutRequestType,
428
464
  InvalidRequestType,
429
465
  RequestTypeWithoutProjectId,
@@ -455,37 +491,44 @@ export const KnownErrors = {
455
491
  UnparsableAccessToken,
456
492
  AccessTokenExpired,
457
493
  InvalidProjectForAccessToken,
458
- SessionUserEmailNotVerified,
459
- SessionAuthenticationRequired,
460
494
  RefreshTokenError,
461
495
  ProviderRejected,
462
- InvalidRefreshToken,
496
+ RefreshTokenNotFoundOrExpired,
463
497
  UserEmailAlreadyExists,
464
498
  UserNotFound,
465
499
  ApiKeyNotFound,
466
500
  ProjectNotFound,
501
+ PasswordAuthenticationNotEnabled,
467
502
  EmailPasswordMismatch,
468
503
  RedirectUrlNotWhitelisted,
469
504
  PasswordRequirementsNotMet,
470
505
  PasswordTooShort,
471
506
  PasswordTooLong,
507
+ UserDoesNotHavePassword,
472
508
  VerificationCodeError,
473
509
  VerificationCodeNotFound,
474
510
  VerificationCodeExpired,
475
511
  VerificationCodeAlreadyUsed,
476
- PasswordMismatch,
512
+ PasswordConfirmationMismatch,
477
513
  EmailAlreadyVerified,
514
+ EmailNotAssociatedWithUser,
515
+ EmailIsNotPrimaryEmail,
478
516
  PermissionNotFound,
479
- PermissionScopeMismatch,
517
+ ContainedPermissionNotFound,
480
518
  TeamNotFound,
519
+ TeamMembershipNotFound,
481
520
  EmailTemplateAlreadyExists,
482
521
  OAuthConnectionNotConnectedToUser,
483
522
  OAuthConnectionAlreadyConnectedToAnotherUser,
484
523
  OAuthConnectionDoesNotHaveRequiredScope,
485
524
  OAuthExtraScopeNotAvailableWithSharedOAuthKeys,
486
525
  OAuthAccessTokenNotAvailableWithSharedOAuthKeys,
526
+ InvalidOAuthClientIdOrSecret,
527
+ InvalidScope,
487
528
  UserAlreadyConnectedToAnotherOAuthConnection,
488
529
  OuterOAuthTimeout,
530
+ OAuthProviderNotFoundOrNotEnabled,
531
+ UserAuthenticationRequired,
489
532
  };
490
533
  // ensure that all known error codes are unique
491
534
  const knownErrorCodes = new Set();
@@ -1,22 +1,59 @@
1
1
  import * as yup from "yup";
2
2
  declare const StackAdaptSentinel: unique symbol;
3
3
  export type StackAdaptSentinel = typeof StackAdaptSentinel;
4
+ export declare function yupString<A extends string, B extends yup.Maybe<yup.AnyObject> = yup.AnyObject>(...args: Parameters<typeof yup.string<A, B>>): yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
5
+ export declare function yupNumber<A extends number, B extends yup.Maybe<yup.AnyObject> = yup.AnyObject>(...args: Parameters<typeof yup.number<A, B>>): yup.NumberSchema<number | undefined, yup.AnyObject, undefined, "">;
6
+ export declare function yupBoolean<A extends boolean, B extends yup.Maybe<yup.AnyObject> = yup.AnyObject>(...args: Parameters<typeof yup.boolean<A, B>>): yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
7
+ /**
8
+ * @deprecated, use number of milliseconds since epoch instead
9
+ */
10
+ export declare function yupDate<A extends Date, B extends yup.Maybe<yup.AnyObject> = yup.AnyObject>(...args: Parameters<typeof yup.date<A, B>>): yup.DateSchema<Date | undefined, yup.AnyObject, undefined, "">;
11
+ export declare function yupMixed<A extends {}>(...args: Parameters<typeof yup.mixed<A>>): yup.MixedSchema<A | undefined, yup.AnyObject, undefined, "">;
12
+ export declare function yupArray<A extends yup.Maybe<yup.AnyObject> = yup.AnyObject, B = any>(...args: Parameters<typeof yup.array<A, B>>): yup.ArraySchema<B[] | undefined, A, undefined, "">;
13
+ export declare function yupTuple<T extends [unknown, ...unknown[]]>(...args: Parameters<typeof yup.tuple<T>>): yup.TupleSchema<T | undefined, yup.AnyObject, undefined, "">;
14
+ export declare function yupObject<A extends yup.Maybe<yup.AnyObject>, B extends yup.ObjectShape>(...args: Parameters<typeof yup.object<A, B>>): yup.ObjectSchema<yup.TypeFromShape<B, yup.AnyObject> extends infer T ? T extends yup.TypeFromShape<B, yup.AnyObject> ? T extends {} ? { [k in keyof T]: T[k]; } : T : never : never, yup.AnyObject, yup.DefaultFromShape<B> extends infer T_1 ? T_1 extends yup.DefaultFromShape<B> ? T_1 extends {} ? { [k_1 in keyof T_1]: T_1[k_1]; } : T_1 : never : never, "">;
4
15
  export declare const adaptSchema: yup.MixedSchema<typeof StackAdaptSentinel | undefined, yup.AnyObject, undefined, "">;
5
16
  /**
6
17
  * Yup's URL schema does not recognize some URLs (including `http://localhost`) as a valid URL. This schema is a workaround for that.
7
18
  */
8
19
  export declare const urlSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
20
+ export declare const jsonSchema: yup.MixedSchema<{} | null, yup.AnyObject, undefined, "">;
21
+ export declare const jsonStringSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
22
+ export declare const emailSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
9
23
  export declare const clientOrHigherAuthTypeSchema: yup.StringSchema<"client" | "server" | "admin" | undefined, yup.AnyObject, undefined, "">;
10
24
  export declare const serverOrHigherAuthTypeSchema: yup.StringSchema<"server" | "admin" | undefined, yup.AnyObject, undefined, "">;
11
25
  export declare const adminAuthTypeSchema: yup.StringSchema<"admin" | undefined, yup.AnyObject, undefined, "">;
12
26
  export declare const projectIdSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
13
- export declare const userIdRequestSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
27
+ export declare const projectDisplayNameSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
28
+ export declare const projectDescriptionSchema: yup.StringSchema<string | null | undefined, yup.AnyObject, undefined, "">;
29
+ export declare const projectCreatedAtMillisSchema: yup.NumberSchema<number | undefined, yup.AnyObject, undefined, "">;
30
+ export declare const projectUserCountSchema: yup.NumberSchema<number | undefined, yup.AnyObject, undefined, "">;
31
+ export declare const projectIsProductionModeSchema: yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
32
+ export declare const projectConfigIdSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
33
+ export declare const projectAllowLocalhostSchema: yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
34
+ export declare const projectCreateTeamOnSignUpSchema: yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
35
+ export declare const projectMagicLinkEnabledSchema: yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
36
+ export declare const projectCredentialEnabledSchema: yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
37
+ export declare const oauthIdSchema: yup.StringSchema<"google" | "github" | "facebook" | "microsoft" | "spotify" | undefined, yup.AnyObject, undefined, "">;
38
+ export declare const oauthEnabledSchema: yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
39
+ export declare const oauthTypeSchema: yup.StringSchema<"shared" | "standard" | undefined, yup.AnyObject, undefined, "">;
40
+ export declare const oauthClientIdSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
41
+ export declare const oauthClientSecretSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
42
+ export declare const emailTypeSchema: yup.StringSchema<"shared" | "standard" | undefined, yup.AnyObject, undefined, "">;
43
+ export declare const emailSenderNameSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
44
+ export declare const emailHostSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
45
+ export declare const emailPortSchema: yup.NumberSchema<number | undefined, yup.AnyObject, undefined, "">;
46
+ export declare const emailUsernameSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
47
+ export declare const emailSenderEmailSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
48
+ export declare const emailPasswordSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
49
+ export declare const domainSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
50
+ export declare const handlerPathSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
14
51
  export declare class ReplaceFieldWithOwnUserId extends Error {
15
52
  readonly path: string;
16
53
  constructor(path: string);
17
54
  }
18
- export declare const userIdOrMeRequestSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
19
- export declare const userIdResponseSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
55
+ export declare const userIdOrMeSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
56
+ export declare const userIdSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
20
57
  export declare const primaryEmailSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
21
58
  export declare const primaryEmailVerifiedSchema: yup.BooleanSchema<boolean | undefined, yup.AnyObject, undefined, "">;
22
59
  export declare const userDisplayNameSchema: yup.StringSchema<string | null | undefined, yup.AnyObject, undefined, "">;
@@ -26,7 +63,8 @@ export declare const signedUpAtMillisSchema: yup.NumberSchema<number | undefined
26
63
  export declare const userClientMetadataSchema: yup.MixedSchema<{} | null, yup.AnyObject, undefined, "">;
27
64
  export declare const userServerMetadataSchema: yup.MixedSchema<{} | null, yup.AnyObject, undefined, "">;
28
65
  export declare const signInEmailSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
29
- export declare const verificationLinkRedirectUrlSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
66
+ export declare const emailOtpSignInCallbackUrlSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
67
+ export declare const emailVerificationCallbackUrlSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
30
68
  export declare const accessTokenResponseSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
31
69
  export declare const refreshTokenResponseSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
32
70
  export declare const signInResponseSchema: yup.ObjectSchema<{
@@ -40,4 +78,15 @@ export declare const signInResponseSchema: yup.ObjectSchema<{
40
78
  is_new_user: undefined;
41
79
  user_id: undefined;
42
80
  }, "">;
81
+ export declare const teamSystemPermissions: readonly ["$update_team", "$delete_team", "$read_members", "$remove_members", "$invite_members"];
82
+ export declare const teamPermissionIdSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
83
+ export declare const customTeamPermissionIdSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
84
+ export declare const teamPermissionDescriptionSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
85
+ export declare const containedPermissionIdsSchema: yup.ArraySchema<string[] | undefined, yup.AnyObject, undefined, "">;
86
+ export declare const teamIdSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
87
+ export declare const teamDisplayNameSchema: yup.StringSchema<string | undefined, yup.AnyObject, undefined, "">;
88
+ export declare const teamClientMetadataSchema: yup.MixedSchema<{} | null, yup.AnyObject, undefined, "">;
89
+ export declare const teamServerMetadataSchema: yup.MixedSchema<{} | null, yup.AnyObject, undefined, "">;
90
+ export declare const teamCreatedAtMillisSchema: yup.NumberSchema<number | undefined, yup.AnyObject, undefined, "">;
91
+ export declare function yupRequiredWhen<S extends yup.AnyObject>(schema: S, triggerName: string, isValue: any): S;
43
92
  export {};