@stackframe/stack-shared 2.4.8 → 2.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,163 @@
1
+ # @stackframe/stack-shared
2
+
3
+ ## 2.4.9
4
+
5
+ ### Patch Changes
6
+
7
+ - Bugfixes
8
+ - Updated dependencies
9
+ - @stackframe/stack-sc@1.5.4
10
+
11
+ ## 2.4.8
12
+
13
+ ### Patch Changes
14
+
15
+ - Improved UUID generation
16
+
17
+ ## 2.4.7
18
+
19
+ ### Patch Changes
20
+
21
+ - Bugfixes
22
+ - Updated dependencies
23
+ - @stackframe/stack-sc@1.5.3
24
+
25
+ ## 2.4.6
26
+
27
+ ### Patch Changes
28
+
29
+ - Remove crypto-browserify dependency
30
+
31
+ ## 2.4.5
32
+
33
+ ### Patch Changes
34
+
35
+ - Team selection
36
+
37
+ ## 2.4.4
38
+
39
+ ### Patch Changes
40
+
41
+ - UX improvements
42
+
43
+ ## 2.4.3
44
+
45
+ ### Patch Changes
46
+
47
+ - CRUD schemas
48
+ - Updated dependencies
49
+ - @stackframe/stack-sc@1.5.2
50
+
51
+ ## 2.4.2
52
+
53
+ ### Patch Changes
54
+
55
+ - New projects page
56
+
57
+ ## 2.4.1
58
+
59
+ ### Patch Changes
60
+
61
+ - Teams, permissions and RBAC
62
+ - Updated dependencies
63
+ - @stackframe/stack-sc@1.5.1
64
+
65
+ ## 2.4.0
66
+
67
+ ### Minor Changes
68
+
69
+ - Middleware support
70
+
71
+ ## 2.3.6
72
+
73
+ ### Patch Changes
74
+
75
+ - Bugfixes
76
+
77
+ ## 2.3.5
78
+
79
+ ### Patch Changes
80
+
81
+ - CommonJS support
82
+
83
+ ## 2.3.4
84
+
85
+ ### Patch Changes
86
+
87
+ - Bugfixes
88
+
89
+ ## 2.3.3
90
+
91
+ ### Patch Changes
92
+
93
+ - Partial pre-rendering
94
+
95
+ ## 2.3.2
96
+
97
+ ### Patch Changes
98
+
99
+ - Magic link configuration
100
+
101
+ ## 2.3.1
102
+
103
+ ### Patch Changes
104
+
105
+ - Add README file
106
+
107
+ ## 2.3.0
108
+
109
+ ### Minor Changes
110
+
111
+ - 96c26a7: added magic link
112
+
113
+ ### Patch Changes
114
+
115
+ - Various small improvements
116
+
117
+ ## 2.2.2
118
+
119
+ ### Patch Changes
120
+
121
+ - 5909ecd: fixed bugs
122
+
123
+ ## 2.2.1
124
+
125
+ ### Patch Changes
126
+
127
+ - fixed minor errors
128
+
129
+ ## 2.2.0
130
+
131
+ ### Minor Changes
132
+
133
+ - 2995d96: Added new UserButton component and Account setting page
134
+
135
+ ## 2.1.0
136
+
137
+ ### Minor Changes
138
+
139
+ - 9e9907f: Added new stack UI system
140
+
141
+ ## 2.0.0
142
+
143
+ ### Major Changes
144
+
145
+ - 948252f: removed redirect URL in function options, redirect automatically to default URL
146
+
147
+ ## 1.2.1
148
+
149
+ ### Patch Changes
150
+
151
+ - fixed import bugs
152
+
153
+ ## 1.2.0
154
+
155
+ ### Minor Changes
156
+
157
+ - Fixed infinite reload bug, added dashboard provider update
158
+
159
+ ## 1.1.0
160
+
161
+ ### Minor Changes
162
+
163
+ - fixed bugs
@@ -44,7 +44,8 @@ export type ClientInterfaceOptions = {
44
44
  } & ({
45
45
  publishableClientKey: string;
46
46
  } | {
47
- projectOwnerTokens: ReadonlyTokenStore;
47
+ projectOwnerTokens: TokenStore;
48
+ refreshProjectOwnerTokens: () => Promise<void>;
48
49
  });
49
50
  export type SharedProvider = "shared-github" | "shared-google" | "shared-facebook" | "shared-microsoft";
50
51
  export declare const sharedProviders: readonly ["shared-github", "shared-google", "shared-facebook", "shared-microsoft"];
@@ -135,7 +136,7 @@ export declare class StackClientInterface {
135
136
  constructor(options: ClientInterfaceOptions);
136
137
  get projectId(): string;
137
138
  getApiUrl(): string;
138
- protected refreshAccessToken(tokenStore: TokenStore): Promise<void>;
139
+ refreshAccessToken(tokenStore: TokenStore): Promise<void>;
139
140
  protected sendClientRequest(path: string, requestOptions: RequestInit, tokenStoreOrNull: TokenStore | null, requestType?: "client" | "server" | "admin"): Promise<Response & {
140
141
  usedTokens: Readonly<{
141
142
  refreshToken: string | null;
@@ -119,6 +119,16 @@ export class StackClientInterface {
119
119
  await this.refreshAccessToken(tokenStore);
120
120
  tokenObj = await tokenStore.getOrWait();
121
121
  }
122
+ let adminTokenStore = null;
123
+ let adminTokenObj = null;
124
+ if ("projectOwnerTokens" in this.options) {
125
+ adminTokenStore = this.options.projectOwnerTokens;
126
+ adminTokenObj = await adminTokenStore.getOrWait();
127
+ if (!adminTokenObj.accessToken) {
128
+ await this.options.refreshProjectOwnerTokens();
129
+ adminTokenObj = await adminTokenStore.getOrWait();
130
+ }
131
+ }
122
132
  // all requests should be dynamic to prevent Next.js caching
123
133
  cookies?.();
124
134
  const url = this.getApiUrl() + path;
@@ -129,8 +139,13 @@ export class StackClientInterface {
129
139
  *
130
140
  * To help debugging, also omit cookies on same-origin, so we don't accidentally
131
141
  * implement reliance on cookies anywhere.
142
+ *
143
+ * However, Cloudflare Workers don't actually support `credentials`, so we only set it
144
+ * if Cloudflare-exclusive globals are not detected. https://github.com/cloudflare/workers-sdk/issues/2514
132
145
  */
133
- credentials: "omit",
146
+ ..."WebSocketPair" in globalThis ? {} : {
147
+ credentials: "omit",
148
+ },
134
149
  ...options,
135
150
  headers: {
136
151
  "X-Stack-Override-Error-Status": "true",
@@ -147,13 +162,26 @@ export class StackClientInterface {
147
162
  ...'publishableClientKey' in this.options ? {
148
163
  "X-Stack-Publishable-Client-Key": this.options.publishableClientKey,
149
164
  } : {},
150
- ...'projectOwnerTokens' in this.options ? {
151
- "X-Stack-Admin-Access-Token": (await this.options.projectOwnerTokens?.getOrWait())?.accessToken ?? "",
165
+ ...adminTokenObj ? {
166
+ "X-Stack-Admin-Access-Token": adminTokenObj.accessToken ?? "",
152
167
  } : {},
168
+ /**
169
+ * Next.js until v15 would cache fetch requests by default, and forcefully disabling it was nearly impossible.
170
+ *
171
+ * This header is used to change the cache key and hence always disable it, because we do our own caching.
172
+ *
173
+ * When we drop support for Next.js <15, we may be able to remove this header, but please make sure that this is
174
+ * the case (I haven't actually tested.)
175
+ */
153
176
  "X-Stack-Random-Nonce": generateSecureRandomString(),
154
177
  ...options.headers,
155
178
  },
156
- cache: "no-store",
179
+ /**
180
+ * Cloudflare Workers does not support cache, so don't pass it there
181
+ */
182
+ ..."WebSocketPair" in globalThis ? {} : {
183
+ cache: "no-store",
184
+ },
157
185
  };
158
186
  const rawRes = await fetch(url, params);
159
187
  const processedRes = await this._processResponse(rawRes);
@@ -166,6 +194,15 @@ export class StackClientInterface {
166
194
  });
167
195
  return Result.error(new Error("Access token expired"));
168
196
  }
197
+ // Same for the admin access token
198
+ // TODO HACK: Some of the backend hasn't been ported to use the new error codes, so if we have project owner tokens we need to check for ApiKeyNotFound too. Once the migration to smartRouteHandlers is complete, we can check for AdminAccessTokenExpired only.
199
+ if (adminTokenStore && (processedRes.error instanceof KnownErrors.AdminAccessTokenExpired || processedRes.error instanceof KnownErrors.ApiKeyNotFound)) {
200
+ adminTokenStore.set({
201
+ accessToken: null,
202
+ refreshToken: adminTokenObj.refreshToken,
203
+ });
204
+ return Result.error(new Error("Admin access token expired"));
205
+ }
169
206
  // Known errors are client side errors, and should hence not be retried (except for access token expired above).
170
207
  // Hence, throw instead of returning an error
171
208
  throw processedRes.error;
@@ -1,4 +1,4 @@
1
- import { StatusError, throwErr, throwStackErr } from "./utils/errors";
1
+ import { 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 {
@@ -26,7 +26,7 @@ export class KnownError extends StatusError {
26
26
  };
27
27
  }
28
28
  get errorCode() {
29
- return this.constructor.errorCode ?? throwStackErr(`Can't find error code for this KnownError. Is its constructor a KnownErrorConstructor? ${this}`);
29
+ return this.constructor.errorCode ?? throwErr(`Can't find error code for this KnownError. Is its constructor a KnownErrorConstructor? ${this}`);
30
30
  }
31
31
  static constructorArgsFromJson(json) {
32
32
  return [
@@ -1,12 +1,11 @@
1
1
  import { Json } from "./json";
2
- export declare function throwErr(errorMessage: string): never;
2
+ export declare function throwErr(errorMessage: string, extraData?: any): never;
3
3
  export declare function throwErr(error: Error): never;
4
4
  export declare function throwErr(...args: StatusErrorConstructorParameters): never;
5
5
  export declare class StackAssertionError extends Error {
6
6
  readonly extraData?: Record<string, any> | undefined;
7
7
  constructor(message: string, extraData?: Record<string, any> | undefined, options?: ErrorOptions);
8
8
  }
9
- export declare function throwStackErr(message: string, extraData?: any): never;
10
9
  export declare function registerErrorSink(sink: (location: string, error: unknown) => void): void;
11
10
  export declare function captureError(location: string, error: unknown): void;
12
11
  type Status = {
@@ -1,6 +1,6 @@
1
1
  export function throwErr(...args) {
2
2
  if (typeof args[0] === "string") {
3
- throw new StackAssertionError(args[0]);
3
+ throw new StackAssertionError(args[0], args[1]);
4
4
  }
5
5
  else if (args[0] instanceof Error) {
6
6
  throw args[0];
@@ -19,9 +19,6 @@ export class StackAssertionError extends Error {
19
19
  }
20
20
  }
21
21
  StackAssertionError.prototype.name = "StackAssertionError";
22
- export function throwStackErr(message, extraData) {
23
- throw new StackAssertionError(message, extraData);
24
- }
25
22
  const errorSinks = new Set();
26
23
  export function registerErrorSink(sink) {
27
24
  if (errorSinks.has(sink)) {
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@stackframe/stack-shared",
3
- "version": "2.4.8",
3
+ "version": "2.4.9",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "files": [
7
7
  "README.md",
8
- "dist"
8
+ "dist",
9
+ "CHANGELOG.md",
10
+ "LICENSE"
9
11
  ],
10
12
  "peerDependencies": {
11
13
  "react": "^18.2",
@@ -24,7 +26,7 @@
24
26
  "jose": "^5.2.2",
25
27
  "oauth4webapi": "^2.10.3",
26
28
  "uuid": "^9.0.1",
27
- "@stackframe/stack-sc": "1.5.3"
29
+ "@stackframe/stack-sc": "1.5.4"
28
30
  },
29
31
  "devDependencies": {
30
32
  "@types/bcrypt": "^5.0.2",