@stackframe/stack-shared 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/LICENSE +7 -0
  2. package/dist/helpers/fetch-token.d.ts +1 -0
  3. package/dist/helpers/fetch-token.d.ts.map +1 -0
  4. package/dist/helpers/fetch-token.js +1 -0
  5. package/dist/helpers/password.d.ts +1 -0
  6. package/dist/helpers/password.d.ts.map +1 -0
  7. package/dist/helpers/password.js +33 -0
  8. package/dist/hooks/use-async-external-store.d.ts +4 -0
  9. package/dist/hooks/use-async-external-store.d.ts.map +1 -0
  10. package/dist/hooks/use-async-external-store.js +20 -0
  11. package/dist/hooks/use-strict-memo.d.ts +6 -0
  12. package/dist/hooks/use-strict-memo.d.ts.map +1 -0
  13. package/dist/hooks/use-strict-memo.js +64 -0
  14. package/dist/index.d.ts +4 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +4 -0
  17. package/dist/interface/adminInterface.d.ts +88 -0
  18. package/dist/interface/adminInterface.d.ts.map +1 -0
  19. package/dist/interface/adminInterface.js +109 -0
  20. package/dist/interface/clientInterface.d.ts +134 -0
  21. package/dist/interface/clientInterface.d.ts.map +1 -0
  22. package/dist/interface/clientInterface.js +444 -0
  23. package/dist/interface/serverInterface.d.ts +33 -0
  24. package/dist/interface/serverInterface.d.ts.map +1 -0
  25. package/dist/interface/serverInterface.js +70 -0
  26. package/dist/utils/arrays.d.ts +10 -0
  27. package/dist/utils/arrays.d.ts.map +1 -0
  28. package/dist/utils/arrays.js +54 -0
  29. package/dist/utils/caches.d.ts +76 -0
  30. package/dist/utils/caches.d.ts.map +1 -0
  31. package/dist/utils/caches.js +100 -0
  32. package/dist/utils/crypto.d.ts +1 -0
  33. package/dist/utils/crypto.d.ts.map +1 -0
  34. package/dist/utils/crypto.js +5 -0
  35. package/dist/utils/dates.d.ts +12 -0
  36. package/dist/utils/dates.d.ts.map +1 -0
  37. package/dist/utils/dates.js +57 -0
  38. package/dist/utils/dom.d.ts +4 -0
  39. package/dist/utils/dom.d.ts.map +1 -0
  40. package/dist/utils/dom.js +11 -0
  41. package/dist/utils/env.d.ts +4 -0
  42. package/dist/utils/env.d.ts.map +1 -0
  43. package/dist/utils/env.js +7 -0
  44. package/dist/utils/errors.d.ts +184 -0
  45. package/dist/utils/errors.d.ts.map +1 -0
  46. package/dist/utils/errors.js +72 -0
  47. package/dist/utils/html.d.ts +2 -0
  48. package/dist/utils/html.d.ts.map +1 -0
  49. package/dist/utils/html.js +12 -0
  50. package/dist/utils/json.d.ts +10 -0
  51. package/dist/utils/json.d.ts.map +1 -0
  52. package/dist/utils/json.js +26 -0
  53. package/dist/utils/jwt.d.ts +3 -0
  54. package/dist/utils/jwt.d.ts.map +1 -0
  55. package/dist/utils/jwt.js +16 -0
  56. package/dist/utils/math.d.ts +4 -0
  57. package/dist/utils/math.d.ts.map +1 -0
  58. package/dist/utils/math.js +6 -0
  59. package/dist/utils/numbers.d.ts +2 -0
  60. package/dist/utils/numbers.d.ts.map +1 -0
  61. package/dist/utils/numbers.js +26 -0
  62. package/dist/utils/objects.d.ts +18 -0
  63. package/dist/utils/objects.d.ts.map +1 -0
  64. package/dist/utils/objects.js +63 -0
  65. package/dist/utils/password.d.ts +2 -0
  66. package/dist/utils/password.d.ts.map +1 -0
  67. package/dist/utils/password.js +8 -0
  68. package/dist/utils/promises.d.ts +49 -0
  69. package/dist/utils/promises.d.ts.map +1 -0
  70. package/dist/utils/promises.js +145 -0
  71. package/dist/utils/react.d.ts +12 -0
  72. package/dist/utils/react.d.ts.map +1 -0
  73. package/dist/utils/react.js +44 -0
  74. package/dist/utils/results.d.ts +73 -0
  75. package/dist/utils/results.d.ts.map +1 -0
  76. package/dist/utils/results.js +112 -0
  77. package/dist/utils/stores.d.ts +57 -0
  78. package/dist/utils/stores.d.ts.map +1 -0
  79. package/dist/utils/stores.js +122 -0
  80. package/dist/utils/strings.d.ts +40 -0
  81. package/dist/utils/strings.d.ts.map +1 -0
  82. package/dist/utils/strings.js +91 -0
  83. package/dist/utils/types.d.ts +33 -0
  84. package/dist/utils/types.d.ts.map +1 -0
  85. package/dist/utils/types.js +61 -0
  86. package/dist/utils/uuids.d.ts +1 -0
  87. package/dist/utils/uuids.d.ts.map +1 -0
  88. package/dist/utils/uuids.js +4 -0
  89. package/package.json +40 -0
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2024 Stackframe
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ export declare const fetchTokenPrefix = "#stack-auth--if-you-see-this-you-probably-forgot-to-add-an-auth-provider----";
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-token.d.ts","sourceRoot":"","sources":["../../src/helpers/fetch-token.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,iFAAiF,CAAC"}
@@ -0,0 +1 @@
1
+ export const fetchTokenPrefix = "#stack-auth--if-you-see-this-you-probably-forgot-to-add-an-auth-provider----";
@@ -0,0 +1 @@
1
+ export declare function getPasswordError(password: string): string | null;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"password.d.ts","sourceRoot":"","sources":["../../src/helpers/password.ts"],"names":[],"mappings":"AAoBA,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAsBhE"}
@@ -0,0 +1,33 @@
1
+ function checkPasswordTooShort(password) {
2
+ return password.length >= 8;
3
+ }
4
+ function checkPasswordTooLong(password) {
5
+ return password.length <= 64;
6
+ }
7
+ function checkPasswordCharacters(password) {
8
+ return /^[a-zA-Z0-9!@#$%^&*()_+?]+$/.test(password);
9
+ }
10
+ function checkPasswordContainsLetter(password) {
11
+ return /[a-zA-Z]/.test(password);
12
+ }
13
+ function checkPasswordContainsNumber(password) {
14
+ return /[0-9]/.test(password);
15
+ }
16
+ export function getPasswordError(password) {
17
+ if (!checkPasswordTooShort(password)) {
18
+ return "password must be at least 8 characters long";
19
+ }
20
+ if (!checkPasswordTooLong(password)) {
21
+ return "password must be at most 64 characters long";
22
+ }
23
+ if (!checkPasswordCharacters(password)) {
24
+ return "password must contain only letters, numbers, and the following special characters: !@#$%^&*()_+?";
25
+ }
26
+ if (!checkPasswordContainsLetter(password)) {
27
+ return "password must contain at least one letter";
28
+ }
29
+ if (!checkPasswordContainsNumber(password)) {
30
+ return "password must contain at least one number";
31
+ }
32
+ return null;
33
+ }
@@ -0,0 +1,4 @@
1
+ import { AsyncResult } from "../utils/results";
2
+ export declare function useAsyncExternalStore<T>(subscribe: (callback: (t: T) => void) => (() => void)): AsyncResult<T, never> & {
3
+ status: "ok" | "pending";
4
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-async-external-store.d.ts","sourceRoot":"","sources":["../../src/hooks/use-async-external-store.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,wBAAgB,qBAAqB,CAAC,CAAC,EACrC,SAAS,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,KAAK,CAAC,MAAM,IAAI,CAAC,GACpD,WAAW,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG;IAAE,MAAM,EAAE,IAAI,GAAG,SAAS,CAAA;CAAE,CAkBtD"}
@@ -0,0 +1,20 @@
1
+ import { useEffect, useState } from "react";
2
+ import { AsyncResult } from "../utils/results";
3
+ export function useAsyncExternalStore(subscribe) {
4
+ // sure, the "sync" in useSyncExternalStore refers to "synchronize a store" and not "sync/async", but it's too good of a name to pass up on
5
+ const [isAvailable, setIsAvailable] = useState(false);
6
+ const [value, setValue] = useState();
7
+ useEffect(() => {
8
+ const unsubscribe = subscribe((value) => {
9
+ setValue(() => value);
10
+ setIsAvailable(() => true);
11
+ });
12
+ return unsubscribe;
13
+ }, [subscribe]);
14
+ if (isAvailable) {
15
+ return AsyncResult.ok(value);
16
+ }
17
+ else {
18
+ return AsyncResult.pending();
19
+ }
20
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Like memo, but minimizes recomputation of the value at all costs (instead of useMemo which recomputes whenever the renderer feels like it).
3
+ *
4
+ * The most recent value will be kept from garbage collection until one of the dependencies becomes unreachable. This may be true even after the component no longer renders. Be wary of memory leaks.
5
+ */
6
+ export declare function useStrictMemo<T>(callback: () => T, dependencies: any[]): T;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-strict-memo.d.ts","sourceRoot":"","sources":["../../src/hooks/use-strict-memo.tsx"],"names":[],"mappings":"AA8CA;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,YAAY,EAAE,GAAG,EAAE,GAAG,CAAC,CAkB1E"}
@@ -0,0 +1,64 @@
1
+ import { useId, useInsertionEffect } from "react";
2
+ import { Result } from "../utils/results";
3
+ const cached = new Map();
4
+ function unwrapFromInner(dependencies, inner) {
5
+ if ((dependencies.length === 0) !== ("isNotNestedMap" in inner)) {
6
+ return Result.error(undefined);
7
+ }
8
+ if ("isNotNestedMap" in inner) {
9
+ if (dependencies.length === 0) {
10
+ return Result.ok(inner.value);
11
+ }
12
+ else {
13
+ return Result.error(undefined);
14
+ }
15
+ }
16
+ else {
17
+ if (dependencies.length === 0) {
18
+ return Result.error(undefined);
19
+ }
20
+ else {
21
+ const [key, ...rest] = dependencies;
22
+ const newInner = inner.get(key);
23
+ if (!newInner) {
24
+ return Result.error(undefined);
25
+ }
26
+ return unwrapFromInner(rest, newInner);
27
+ }
28
+ }
29
+ }
30
+ function wrapToInner(dependencies, value) {
31
+ if (dependencies.length === 0) {
32
+ return { isNotNestedMap: true, value };
33
+ }
34
+ const [key, ...rest] = dependencies;
35
+ const inner = wrapToInner(rest, value);
36
+ const isObject = (typeof key === "object" && key !== null);
37
+ const isUnregisteredSymbol = (typeof key === "symbol" && Symbol.keyFor(key) === undefined);
38
+ const isWeak = isObject || isUnregisteredSymbol;
39
+ const mapType = isWeak ? WeakMap : Map;
40
+ return new mapType([[key, inner]]);
41
+ }
42
+ /**
43
+ * Like memo, but minimizes recomputation of the value at all costs (instead of useMemo which recomputes whenever the renderer feels like it).
44
+ *
45
+ * The most recent value will be kept from garbage collection until one of the dependencies becomes unreachable. This may be true even after the component no longer renders. Be wary of memory leaks.
46
+ */
47
+ export function useStrictMemo(callback, dependencies) {
48
+ const id = useId();
49
+ useInsertionEffect(() => {
50
+ return () => {
51
+ cached.delete(id);
52
+ };
53
+ }, [id]);
54
+ const c = cached.get(id);
55
+ if (c) {
56
+ const unwrapped = unwrapFromInner(dependencies, c);
57
+ if (unwrapped.status === "ok") {
58
+ return unwrapped.data;
59
+ }
60
+ }
61
+ const value = callback();
62
+ cached.set(id, wrapToInner(dependencies, value));
63
+ return value;
64
+ }
@@ -0,0 +1,4 @@
1
+ export { StackClientInterface, UserJson as UserJson, UserCustomizableJson, ClientProjectJson, ProjectJson, OauthProviderConfigJson, getProductionModeErrors, } from "./interface/clientInterface";
2
+ export { StackServerInterface, ServerUserJson, ServerUserCustomizableJson, } from "./interface/serverInterface";
3
+ export { StackAdminInterface, ApiKeySetBase, ApiKeySetBaseJson, ApiKeySetFirstView, ApiKeySetFirstViewJson, ApiKeySetSummary, ApiKeySetSummaryJson, } from "./interface/adminInterface";
4
+ export { fetchTokenPrefix } from "./helpers/fetch-token";
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,QAAQ,IAAI,QAAQ,EACpB,oBAAoB,EACpB,iBAAiB,EACjB,WAAW,EACX,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,oBAAoB,EACpB,cAAc,EACd,0BAA0B,GAC3B,MAAM,6BAA6B,CAAC;AACrC,OAAO,EACL,mBAAmB,EACnB,aAAa,EACb,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,EACtB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { StackClientInterface, getProductionModeErrors, } from "./interface/clientInterface";
2
+ export { StackServerInterface, } from "./interface/serverInterface";
3
+ export { StackAdminInterface, } from "./interface/adminInterface";
4
+ export { fetchTokenPrefix } from "./helpers/fetch-token";
@@ -0,0 +1,88 @@
1
+ import { ServerAuthApplicationOptions, StackServerInterface } from "./serverInterface";
2
+ import { AsyncValueCache } from "../utils/caches";
3
+ import { ProjectJson, TokenStore } from "./clientInterface";
4
+ export type AdminAuthApplicationOptions = Readonly<ServerAuthApplicationOptions & ({
5
+ superSecretAdminKey: string;
6
+ } | {
7
+ internalAdminAccessToken: string;
8
+ })>;
9
+ export type ApiKeySetBase = Readonly<{
10
+ id: string;
11
+ description: string;
12
+ expiresAt: Date;
13
+ manuallyRevokedAt: Date | null;
14
+ createdAt: Date;
15
+ isValid(): boolean;
16
+ whyInvalid(): "expired" | "manually-revoked" | null;
17
+ }>;
18
+ export type ApiKeySetBaseJson = Readonly<{
19
+ id: string;
20
+ description: string;
21
+ expiresAtMillis: number;
22
+ manuallyRevokedAtMillis: number | null;
23
+ createdAtMillis: number;
24
+ }>;
25
+ export type ApiKeySetFirstView = Readonly<ApiKeySetBase & {
26
+ publishableClientKey?: string;
27
+ secretServerKey?: string;
28
+ superSecretAdminKey?: string;
29
+ }>;
30
+ export type ApiKeySetFirstViewJson = Readonly<ApiKeySetBaseJson & {
31
+ publishableClientKey?: string;
32
+ secretServerKey?: string;
33
+ superSecretAdminKey?: string;
34
+ }>;
35
+ export type ApiKeySetSummary = Readonly<ApiKeySetBase & {
36
+ publishableClientKey: null | {
37
+ lastFour: string;
38
+ };
39
+ secretServerKey: null | {
40
+ lastFour: string;
41
+ };
42
+ superSecretAdminKey: null | {
43
+ lastFour: string;
44
+ };
45
+ }>;
46
+ export type ApiKeySetSummaryJson = Readonly<ApiKeySetBaseJson & {
47
+ publishableClientKey: null | {
48
+ lastFour: string;
49
+ };
50
+ secretServerKey: null | {
51
+ lastFour: string;
52
+ };
53
+ superSecretAdminKey: null | {
54
+ lastFour: string;
55
+ };
56
+ }>;
57
+ export declare class StackAdminInterface extends StackServerInterface {
58
+ readonly options: AdminAuthApplicationOptions;
59
+ readonly projectCache: AsyncValueCache<ProjectJson>;
60
+ constructor(options: AdminAuthApplicationOptions);
61
+ protected sendAdminRequest(path: string, options: RequestInit, tokenStore: TokenStore | null): Promise<Response & {
62
+ usedTokens: Readonly<{
63
+ refreshToken: string | null;
64
+ accessToken: string | null;
65
+ }>;
66
+ }>;
67
+ refreshProject(): Promise<void>;
68
+ getProject(): Promise<ProjectJson>;
69
+ updateProject(update: {
70
+ isProductionMode?: boolean;
71
+ config?: {
72
+ domains?: {
73
+ domain: string;
74
+ handlerPath: string;
75
+ }[];
76
+ };
77
+ }): Promise<ProjectJson>;
78
+ createApiKeySet(options: {
79
+ hasPublishableClientKey: boolean;
80
+ hasSecretServerKey: boolean;
81
+ hasSuperSecretAdminKey: boolean;
82
+ expiresAt: Date;
83
+ description: string;
84
+ }): Promise<ApiKeySetFirstView>;
85
+ listApiKeySets(): Promise<ApiKeySetSummary[]>;
86
+ revokeApiKeySetById(id: string): Promise<void>;
87
+ getApiKeySet(id: string, tokenStore: TokenStore): Promise<ApiKeySetSummary>;
88
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adminInterface.d.ts","sourceRoot":"","sources":["../../src/interface/adminInterface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACvF,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG5D,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAChD,4BAA4B,GAC5B,CACI;IACA,mBAAmB,EAAE,MAAM,CAAC;CAC7B,GACC;IACA,wBAAwB,EAAE,MAAM,CAAC;CAClC,CACF,CACF,CAAA;AAED,MAAM,MAAM,aAAa,GAAG,QAAQ,CAAC;IACnC,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,IAAI,CAAC;IAChB,iBAAiB,EAAE,IAAI,GAAG,IAAI,CAAC;IAC/B,SAAS,EAAE,IAAI,CAAC;IAChB,OAAO,IAAI,OAAO,CAAC;IACnB,UAAU,IAAI,SAAS,GAAG,kBAAkB,GAAG,IAAI,CAAC;CACrD,CAAC,CAAA;AAEF,MAAM,MAAM,iBAAiB,GAAG,QAAQ,CAAC;IACvC,EAAE,EAAE,MAAM,CAAC;IACX,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;IACxB,uBAAuB,EAAE,MAAM,GAAG,IAAI,CAAC;IACvC,eAAe,EAAE,MAAM,CAAC;CACzB,CAAC,CAAA;AAEF,MAAM,MAAM,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,UAAU,IAAI,OAAO,CAAC,WAAW,CAAC;IASlC,aAAa,CACjB,MAAM,EAAE;QACN,gBAAgB,CAAC,EAAE,OAAO,CAAC;QAC3B,MAAM,CAAC,EAAE;YACP,OAAO,CAAC,EAAE;gBACR,MAAM,EAAE,MAAM,CAAC;gBACf,WAAW,EAAE,MAAM,CAAC;aACrB,EAAE,CAAC;SACL,CAAC;KACH,GACA,OAAO,CAAC,WAAW,CAAC;IAgBjB,eAAe,CACnB,OAAO,EAAE;QACP,uBAAuB,EAAE,OAAO,CAAC;QACjC,kBAAkB,EAAE,OAAO,CAAC;QAC5B,sBAAsB,EAAE,OAAO,CAAC;QAChC,SAAS,EAAE,IAAI,CAAC;QAChB,WAAW,EAAE,MAAM,CAAC;KACrB,GACA,OAAO,CAAC,kBAAkB,CAAC;IAexB,cAAc,IAAI,OAAO,CAAC,gBAAgB,EAAE,CAAC;IAM7C,mBAAmB,CAAC,EAAE,EAAE,MAAM;IAe9B,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAIlF"}
@@ -0,0 +1,109 @@
1
+ import { StackServerInterface } from "./serverInterface";
2
+ import { AsyncValueCache } from "../utils/caches";
3
+ import { throwErr } from "../utils/errors";
4
+ export class StackAdminInterface extends StackServerInterface {
5
+ options;
6
+ projectCache;
7
+ constructor(options) {
8
+ super(options);
9
+ this.options = options;
10
+ this.projectCache = new AsyncValueCache(async () => {
11
+ return await this.getProject() ?? throwErr("Can't fetch project because it was not found");
12
+ });
13
+ }
14
+ async sendAdminRequest(path, options, tokenStore) {
15
+ return await this.sendServerRequest(path, {
16
+ ...options,
17
+ headers: {
18
+ "x-stack-super-secret-admin-key": "superSecretAdminKey" in this.options ? this.options.superSecretAdminKey : "",
19
+ ...options.headers,
20
+ },
21
+ }, tokenStore);
22
+ }
23
+ async refreshProject() {
24
+ await Promise.all([
25
+ super.refreshProject(),
26
+ this.projectCache.refresh(),
27
+ ]);
28
+ }
29
+ async getProject() {
30
+ const response = await this.sendAdminRequest("/projects/" + encodeURIComponent(this.projectId), {}, null);
31
+ return await response.json();
32
+ }
33
+ async updateProject(update) {
34
+ const response = await this.sendAdminRequest("/projects/" + encodeURIComponent(this.projectId), {
35
+ method: "PUT",
36
+ headers: {
37
+ "content-type": "application/json",
38
+ },
39
+ body: JSON.stringify(update),
40
+ }, null);
41
+ await this.refreshProject();
42
+ return await response.json();
43
+ }
44
+ async createApiKeySet(options) {
45
+ const response = await this.sendServerRequest("/api-keys", {
46
+ method: "POST",
47
+ headers: {
48
+ "content-type": "application/json",
49
+ },
50
+ body: JSON.stringify(options),
51
+ }, null);
52
+ return createApiKeySetFirstViewFromJson(await response.json());
53
+ }
54
+ async listApiKeySets() {
55
+ const response = await this.sendAdminRequest("/api-keys", {}, null);
56
+ const json = await response.json();
57
+ return json.map((k) => createApiKeySetSummaryFromJson(k));
58
+ }
59
+ async revokeApiKeySetById(id) {
60
+ await this.sendAdminRequest(`/api-keys/${id}`, {
61
+ method: "PUT",
62
+ headers: {
63
+ "content-type": "application/json",
64
+ },
65
+ body: JSON.stringify({
66
+ revoke: true,
67
+ }),
68
+ }, null);
69
+ }
70
+ async getApiKeySet(id, tokenStore) {
71
+ const response = await this.sendAdminRequest(`/api-keys/${id}`, {}, tokenStore);
72
+ return await response.json();
73
+ }
74
+ }
75
+ function createApiKeySetBaseFromJson(data) {
76
+ return {
77
+ id: data.id,
78
+ description: data.description,
79
+ expiresAt: new Date(data.expiresAtMillis),
80
+ manuallyRevokedAt: data.manuallyRevokedAtMillis ? new Date(data.manuallyRevokedAtMillis) : null,
81
+ createdAt: new Date(data.createdAtMillis),
82
+ isValid() {
83
+ return this.whyInvalid() === null;
84
+ },
85
+ whyInvalid() {
86
+ if (this.expiresAt.getTime() < Date.now())
87
+ return "expired";
88
+ if (this.manuallyRevokedAt)
89
+ return "manually-revoked";
90
+ return null;
91
+ },
92
+ };
93
+ }
94
+ function createApiKeySetSummaryFromJson(data) {
95
+ return {
96
+ ...createApiKeySetBaseFromJson(data),
97
+ publishableClientKey: data.publishableClientKey ? { lastFour: data.publishableClientKey.lastFour } : null,
98
+ secretServerKey: data.secretServerKey ? { lastFour: data.secretServerKey.lastFour } : null,
99
+ superSecretAdminKey: data.superSecretAdminKey ? { lastFour: data.superSecretAdminKey.lastFour } : null,
100
+ };
101
+ }
102
+ function createApiKeySetFirstViewFromJson(data) {
103
+ return {
104
+ ...createApiKeySetBaseFromJson(data),
105
+ publishableClientKey: data.publishableClientKey,
106
+ secretServerKey: data.secretServerKey,
107
+ superSecretAdminKey: data.superSecretAdminKey,
108
+ };
109
+ }
@@ -0,0 +1,134 @@
1
+ import { ReadonlyJson, SignInErrorCode, SignUpErrorCode, EmailVerificationLinkErrorCode, PasswordResetLinkErrorCode } from "../utils/types";
2
+ import { Result } from "../utils/results";
3
+ import { AsyncCache, AsyncValueCache } from '../utils/caches';
4
+ import { AsyncStore } from '../utils/stores';
5
+ export type UserCustomizableJson = {
6
+ readonly projectId: string;
7
+ readonly displayName: string | null;
8
+ readonly clientMetadata: ReadonlyJson;
9
+ };
10
+ export type UserJson = UserCustomizableJson & {
11
+ readonly id: string;
12
+ readonly primaryEmail: string | null;
13
+ readonly primaryEmailVerified: boolean;
14
+ readonly displayName: string | null;
15
+ readonly clientMetadata: ReadonlyJson;
16
+ readonly profileImageUrl: string | null;
17
+ readonly signedUpAtMillis: number;
18
+ };
19
+ export type ClientProjectJson = {
20
+ readonly id: string;
21
+ readonly oauthProviders: readonly {
22
+ id: string;
23
+ }[];
24
+ };
25
+ export type ClientInterfaceOptions = {
26
+ readonly baseUrl: string;
27
+ readonly projectId: string;
28
+ } & ({
29
+ readonly publishableClientKey: string;
30
+ } | {
31
+ readonly internalAdminAccessToken: string;
32
+ });
33
+ export type TokenStore = AsyncStore<TokenObject>;
34
+ export type TokenObject = Readonly<{
35
+ refreshToken: string | null;
36
+ accessToken: string | null;
37
+ }>;
38
+ export type ProjectJson = Readonly<{
39
+ id: string;
40
+ displayName: string;
41
+ description?: string;
42
+ createdAtMillis: number;
43
+ userCount: number;
44
+ isProductionMode: boolean;
45
+ evaluatedConfig: {
46
+ id: string;
47
+ allowLocalhost: boolean;
48
+ oauthProviders: OauthProviderConfigJson[];
49
+ emailConfig?: EmailConfigJson;
50
+ domains: DomainConfigJson[];
51
+ };
52
+ }>;
53
+ export type OauthProviderConfigJson = {
54
+ id: string;
55
+ } & ({
56
+ type: "shared-github" | "shared-google" | "shared-facebook" | "shared-slack" | "shared-twitter" | "shared-linkedin" | "shared-microsoft";
57
+ } | {
58
+ type: "github" | "facebook" | "slack" | "twitter" | "linkedin" | "google" | "microsoft";
59
+ clientId: string;
60
+ clientSecret: string;
61
+ tenantId?: string;
62
+ });
63
+ export type EmailConfigJson = ({
64
+ type: "standard";
65
+ senderName: string;
66
+ senderEmail: string;
67
+ host: string;
68
+ port: number;
69
+ username: string;
70
+ password: string;
71
+ } | {
72
+ type: "shared";
73
+ senderName: string;
74
+ });
75
+ export type DomainConfigJson = {
76
+ domain: string;
77
+ handlerPath: string;
78
+ };
79
+ export declare class StackClientInterface {
80
+ readonly options: ClientInterfaceOptions;
81
+ readonly currentUserCache: AsyncCache<TokenStore, UserJson | null>;
82
+ readonly clientProjectCache: AsyncValueCache<ClientProjectJson>;
83
+ constructor(options: ClientInterfaceOptions);
84
+ get projectId(): string;
85
+ getSessionCookieName(): string;
86
+ getApiUrl(): string;
87
+ refreshUser(tokenStore: TokenStore): Promise<void>;
88
+ refreshProject(): Promise<void>;
89
+ protected refreshAccessToken(tokenStore: TokenStore): Promise<void>;
90
+ protected sendClientRequest(path: string, requestOptions: RequestInit, tokenStoreOrNull: TokenStore | null): Promise<Response & {
91
+ usedTokens: Readonly<{
92
+ refreshToken: string | null;
93
+ accessToken: string | null;
94
+ }>;
95
+ }>;
96
+ protected sendClientRequestAndCatchKnownError<E>(path: string, requestOptions: RequestInit, tokenStoreOrNull: TokenStore | null, errorCodes: string[]): Promise<({
97
+ status: "ok";
98
+ data: Response & {
99
+ usedTokens: Readonly<{
100
+ refreshToken: string | null;
101
+ accessToken: string | null;
102
+ }>;
103
+ };
104
+ } & {
105
+ status: "ok";
106
+ }) | ({
107
+ status: "error";
108
+ error: E;
109
+ } & {
110
+ status: "error";
111
+ })>;
112
+ private sendClientRequestInner;
113
+ sendForgotPasswordEmail(email: string, redirectUrl: string): Promise<PasswordResetLinkErrorCode | undefined>;
114
+ resetPassword(options: {
115
+ password: string;
116
+ code: string;
117
+ }): Promise<PasswordResetLinkErrorCode | undefined>;
118
+ verifyPasswordResetCode(code: string): Promise<PasswordResetLinkErrorCode | undefined>;
119
+ verifyEmail(code: string): Promise<EmailVerificationLinkErrorCode | undefined>;
120
+ signInWithCredential(email: string, password: string, tokenStore: TokenStore): Promise<SignInErrorCode | undefined>;
121
+ signUpWithCredential(email: string, password: string, emailVerificationRedirectUrl: string, tokenStore: TokenStore): Promise<SignUpErrorCode | undefined>;
122
+ getOauthUrl(provider: string, redirectUrl: string, codeChallenge: string, state: string): Promise<string>;
123
+ callOauthCallback(oauthParams: URLSearchParams, redirectUri: string, codeVerifier: string, state: string, tokenStore: TokenStore): Promise<void>;
124
+ signOut(tokenStore: TokenStore): Promise<void>;
125
+ getClientUserByToken(tokenStore: TokenStore): Promise<Result<UserJson>>;
126
+ getClientProject(): Promise<Result<ClientProjectJson>>;
127
+ setClientUserCustomizableData(update: Partial<UserCustomizableJson>, tokenStore: TokenStore): Promise<void>;
128
+ listProjects(tokenStore: TokenStore): Promise<ProjectJson[]>;
129
+ createProject(project: Pick<ProjectJson, "displayName" | "description">, tokenStore: TokenStore): Promise<ProjectJson>;
130
+ }
131
+ export declare function getProductionModeErrors(project: ProjectJson): {
132
+ errorMessage: string;
133
+ fixUrlRelative: string;
134
+ }[];
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clientInterface.d.ts","sourceRoot":"","sources":["../../src/interface/clientInterface.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,YA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