@stackframe/stack-shared 2.7.14 → 2.7.16
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 +14 -0
- package/dist/helpers/password.js +1 -1
- package/dist/interface/clientInterface.d.ts +1 -1
- package/dist/interface/clientInterface.js +8 -5
- package/dist/interface/crud/teams.d.ts +2 -2
- package/dist/interface/crud/teams.js +1 -1
- package/dist/known-errors.js +9 -9
- package/dist/utils/env.d.ts +24 -0
- package/dist/utils/env.js +79 -0
- package/dist/utils/strings.d.ts +1 -1
- package/dist/utils/strings.js +31 -1
- package/dist/utils/strings.test.d.ts +1 -0
- package/dist/utils/strings.test.js +26 -0
- package/dist/utils/urls.d.ts +1 -0
- package/dist/utils/urls.js +7 -1
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @stackframe/stack-shared
|
|
2
2
|
|
|
3
|
+
## 2.7.16
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Various changes
|
|
8
|
+
- @stackframe/stack-sc@2.7.16
|
|
9
|
+
|
|
10
|
+
## 2.7.15
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Various changes
|
|
15
|
+
- @stackframe/stack-sc@2.7.15
|
|
16
|
+
|
|
3
17
|
## 2.7.14
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/helpers/password.js
CHANGED
|
@@ -13,7 +13,7 @@ import { TeamPermissionsCrud } from './crud/team-permissions';
|
|
|
13
13
|
import { TeamsCrud } from './crud/teams';
|
|
14
14
|
export type ClientInterfaceOptions = {
|
|
15
15
|
clientVersion: string;
|
|
16
|
-
|
|
16
|
+
getBaseUrl: () => string;
|
|
17
17
|
projectId: string;
|
|
18
18
|
} & ({
|
|
19
19
|
publishableClientKey: string;
|
|
@@ -19,7 +19,7 @@ export class StackClientInterface {
|
|
|
19
19
|
return this.options.projectId;
|
|
20
20
|
}
|
|
21
21
|
getApiUrl() {
|
|
22
|
-
return this.options.
|
|
22
|
+
return this.options.getBaseUrl() + "/api/v1";
|
|
23
23
|
}
|
|
24
24
|
async runNetworkDiagnostics(session, requestType) {
|
|
25
25
|
const tryRequest = async (cb) => {
|
|
@@ -101,7 +101,7 @@ export class StackClientInterface {
|
|
|
101
101
|
throw new Error("Admin session token is currently not supported for fetching new access token. Did you try to log in on a StackApp initiated with the admin session?");
|
|
102
102
|
}
|
|
103
103
|
const as = {
|
|
104
|
-
issuer: this.options.
|
|
104
|
+
issuer: this.options.getBaseUrl(),
|
|
105
105
|
algorithm: 'oauth2',
|
|
106
106
|
token_endpoint: this.getApiUrl() + '/auth/oauth/token',
|
|
107
107
|
};
|
|
@@ -168,7 +168,10 @@ export class StackClientInterface {
|
|
|
168
168
|
let adminTokenObj = adminSession ? await adminSession.getOrFetchLikelyValidTokens(20000) : null;
|
|
169
169
|
// all requests should be dynamic to prevent Next.js caching
|
|
170
170
|
await cookies?.();
|
|
171
|
-
|
|
171
|
+
let url = this.getApiUrl() + path;
|
|
172
|
+
if (url.endsWith("/")) {
|
|
173
|
+
url = url.slice(0, -1);
|
|
174
|
+
}
|
|
172
175
|
const params = {
|
|
173
176
|
/**
|
|
174
177
|
* This fetch may be cross-origin, in which case we don't want to send cookies of the
|
|
@@ -277,7 +280,7 @@ export class StackClientInterface {
|
|
|
277
280
|
}
|
|
278
281
|
else {
|
|
279
282
|
const error = await res.text();
|
|
280
|
-
const errorObj = new StackAssertionError(`Failed to send request to ${url}: ${res.status} ${error}`, { request: params, res });
|
|
283
|
+
const errorObj = new StackAssertionError(`Failed to send request to ${url}: ${res.status} ${error}`, { request: params, res, path });
|
|
281
284
|
if (res.status === 508 && error.includes("INFINITE_LOOP_DETECTED")) {
|
|
282
285
|
// Some Vercel deployments seem to have an odd infinite loop bug. In that case, retry.
|
|
283
286
|
// See: https://github.com/stack-auth/stack/issues/319
|
|
@@ -652,7 +655,7 @@ export class StackClientInterface {
|
|
|
652
655
|
throw new Error("Admin session token is currently not supported for OAuth");
|
|
653
656
|
}
|
|
654
657
|
const as = {
|
|
655
|
-
issuer: this.options.
|
|
658
|
+
issuer: this.options.getBaseUrl(),
|
|
656
659
|
algorithm: 'oauth2',
|
|
657
660
|
token_endpoint: this.getApiUrl() + '/auth/oauth/token',
|
|
658
661
|
};
|
|
@@ -59,7 +59,7 @@ export declare const teamsCrudClientCreateSchema: import("yup").ObjectSchema<{
|
|
|
59
59
|
client_metadata: {} | null | undefined;
|
|
60
60
|
} & {
|
|
61
61
|
display_name: string;
|
|
62
|
-
creator_user_id: string
|
|
62
|
+
creator_user_id: string;
|
|
63
63
|
}, import("yup").AnyObject, {
|
|
64
64
|
display_name: undefined;
|
|
65
65
|
profile_image_url: undefined;
|
|
@@ -114,7 +114,7 @@ export declare const teamsCrud: import("../../crud").CrudSchemaFromOptions<{
|
|
|
114
114
|
client_metadata: {} | null | undefined;
|
|
115
115
|
} & {
|
|
116
116
|
display_name: string;
|
|
117
|
-
creator_user_id: string
|
|
117
|
+
creator_user_id: string;
|
|
118
118
|
}, import("yup").AnyObject, {
|
|
119
119
|
display_name: undefined;
|
|
120
120
|
profile_image_url: undefined;
|
|
@@ -26,7 +26,7 @@ export const teamsCrudServerUpdateSchema = teamsCrudClientUpdateSchema.concat(yu
|
|
|
26
26
|
// Create
|
|
27
27
|
export const teamsCrudClientCreateSchema = teamsCrudClientUpdateSchema.concat(yupObject({
|
|
28
28
|
display_name: fieldSchema.teamDisplayNameSchema.defined(),
|
|
29
|
-
creator_user_id: fieldSchema.teamCreatorUserIdSchema.
|
|
29
|
+
creator_user_id: fieldSchema.teamCreatorUserIdSchema.defined(),
|
|
30
30
|
}).defined());
|
|
31
31
|
export const teamsCrudServerCreateSchema = teamsCrudServerUpdateSchema.concat(yupObject({
|
|
32
32
|
display_name: fieldSchema.teamDisplayNameSchema.defined(),
|
package/dist/known-errors.js
CHANGED
|
@@ -260,7 +260,7 @@ const ProviderRejected = createKnownErrorConstructor(RefreshTokenError, "PROVIDE
|
|
|
260
260
|
"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.",
|
|
261
261
|
], () => []);
|
|
262
262
|
const UserEmailAlreadyExists = createKnownErrorConstructor(KnownError, "USER_EMAIL_ALREADY_EXISTS", () => [
|
|
263
|
-
|
|
263
|
+
409,
|
|
264
264
|
"User email already exists.",
|
|
265
265
|
], () => []);
|
|
266
266
|
const CannotGetOwnUserWithoutUser = createKnownErrorConstructor(KnownError, "CANNOT_GET_OWN_USER_WITHOUT_USER", () => [
|
|
@@ -346,7 +346,7 @@ const VerificationCodeExpired = createKnownErrorConstructor(VerificationCodeErro
|
|
|
346
346
|
"The verification code has expired.",
|
|
347
347
|
], () => []);
|
|
348
348
|
const VerificationCodeAlreadyUsed = createKnownErrorConstructor(VerificationCodeError, "VERIFICATION_CODE_ALREADY_USED", () => [
|
|
349
|
-
|
|
349
|
+
409,
|
|
350
350
|
"The verification link has already been used.",
|
|
351
351
|
], () => []);
|
|
352
352
|
const VerificationCodeMaxAttemptsReached = createKnownErrorConstructor(VerificationCodeError, "VERIFICATION_CODE_MAX_ATTEMPTS_REACHED", () => [
|
|
@@ -358,7 +358,7 @@ const PasswordConfirmationMismatch = createKnownErrorConstructor(KnownError, "PA
|
|
|
358
358
|
"Passwords do not match.",
|
|
359
359
|
], () => []);
|
|
360
360
|
const EmailAlreadyVerified = createKnownErrorConstructor(KnownError, "EMAIL_ALREADY_VERIFIED", () => [
|
|
361
|
-
|
|
361
|
+
409,
|
|
362
362
|
"The e-mail is already verified.",
|
|
363
363
|
], () => []);
|
|
364
364
|
const EmailNotAssociatedWithUser = createKnownErrorConstructor(KnownError, "EMAIL_NOT_ASSOCIATED_WITH_USER", () => [
|
|
@@ -411,7 +411,7 @@ const TeamNotFound = createKnownErrorConstructor(KnownError, "TEAM_NOT_FOUND", (
|
|
|
411
411
|
},
|
|
412
412
|
], (json) => [json.team_id]);
|
|
413
413
|
const TeamAlreadyExists = createKnownErrorConstructor(KnownError, "TEAM_ALREADY_EXISTS", (teamId) => [
|
|
414
|
-
|
|
414
|
+
409,
|
|
415
415
|
`Team ${teamId} already exists.`,
|
|
416
416
|
{
|
|
417
417
|
team_id: teamId,
|
|
@@ -426,7 +426,7 @@ const TeamMembershipNotFound = createKnownErrorConstructor(KnownError, "TEAM_MEM
|
|
|
426
426
|
},
|
|
427
427
|
], (json) => [json.team_id, json.user_id]);
|
|
428
428
|
const EmailTemplateAlreadyExists = createKnownErrorConstructor(KnownError, "EMAIL_TEMPLATE_ALREADY_EXISTS", () => [
|
|
429
|
-
|
|
429
|
+
409,
|
|
430
430
|
"Email template already exists.",
|
|
431
431
|
], () => []);
|
|
432
432
|
const OAuthConnectionNotConnectedToUser = createKnownErrorConstructor(KnownError, "OAUTH_CONNECTION_NOT_CONNECTED_TO_USER", () => [
|
|
@@ -434,7 +434,7 @@ const OAuthConnectionNotConnectedToUser = createKnownErrorConstructor(KnownError
|
|
|
434
434
|
"The OAuth connection is not connected to any user.",
|
|
435
435
|
], () => []);
|
|
436
436
|
const OAuthConnectionAlreadyConnectedToAnotherUser = createKnownErrorConstructor(KnownError, "OAUTH_CONNECTION_ALREADY_CONNECTED_TO_ANOTHER_USER", () => [
|
|
437
|
-
|
|
437
|
+
409,
|
|
438
438
|
"The OAuth connection is already connected to another user.",
|
|
439
439
|
], () => []);
|
|
440
440
|
const OAuthConnectionDoesNotHaveRequiredScope = createKnownErrorConstructor(KnownError, "OAUTH_CONNECTION_DOES_NOT_HAVE_REQUIRED_SCOPE", () => [
|
|
@@ -461,7 +461,7 @@ const InvalidScope = createKnownErrorConstructor(KnownError, "INVALID_SCOPE", (s
|
|
|
461
461
|
`The scope "${scope}" is not a valid OAuth scope for Stack.`,
|
|
462
462
|
], (json) => [json.scope]);
|
|
463
463
|
const UserAlreadyConnectedToAnotherOAuthConnection = createKnownErrorConstructor(KnownError, "USER_ALREADY_CONNECTED_TO_ANOTHER_OAUTH_CONNECTION", () => [
|
|
464
|
-
|
|
464
|
+
409,
|
|
465
465
|
"The user is already connected to another OAuth account. Did you maybe selected the wrong account?",
|
|
466
466
|
], () => []);
|
|
467
467
|
const OuterOAuthTimeout = createKnownErrorConstructor(KnownError, "OUTER_OAUTH_TIMEOUT", () => [
|
|
@@ -488,7 +488,7 @@ const UserAuthenticationRequired = createKnownErrorConstructor(KnownError, "USER
|
|
|
488
488
|
"User authentication required for this endpoint.",
|
|
489
489
|
], () => []);
|
|
490
490
|
const TeamMembershipAlreadyExists = createKnownErrorConstructor(KnownError, "TEAM_MEMBERSHIP_ALREADY_EXISTS", () => [
|
|
491
|
-
|
|
491
|
+
409,
|
|
492
492
|
"Team membership already exists.",
|
|
493
493
|
], () => []);
|
|
494
494
|
const TeamPermissionRequired = createKnownErrorConstructor(KnownError, "TEAM_PERMISSION_REQUIRED", (teamId, userId, permissionId) => [
|
|
@@ -532,7 +532,7 @@ const OAuthProviderAccessDenied = createKnownErrorConstructor(KnownError, "OAUTH
|
|
|
532
532
|
"The OAuth provider denied access to the user.",
|
|
533
533
|
], () => []);
|
|
534
534
|
const ContactChannelAlreadyUsedForAuthBySomeoneElse = createKnownErrorConstructor(KnownError, "CONTACT_CHANNEL_ALREADY_USED_FOR_AUTH_BY_SOMEONE_ELSE", (type) => [
|
|
535
|
-
|
|
535
|
+
409,
|
|
536
536
|
`This ${type} is already used for authentication by another account.`,
|
|
537
537
|
{ type },
|
|
538
538
|
], (json) => [json.type]);
|
package/dist/utils/env.d.ts
CHANGED
|
@@ -5,3 +5,27 @@ export declare function isBrowserLike(): boolean;
|
|
|
5
5
|
export declare function getEnvVariable(name: string, defaultValue?: string | undefined): string;
|
|
6
6
|
export declare function getNextRuntime(): string;
|
|
7
7
|
export declare function getNodeEnvironment(): string;
|
|
8
|
+
declare const _inlineEnvVars: {
|
|
9
|
+
readonly NEXT_PUBLIC_STACK_API_URL: {
|
|
10
|
+
readonly default: string | undefined;
|
|
11
|
+
readonly client: string | undefined;
|
|
12
|
+
readonly server: string | undefined;
|
|
13
|
+
};
|
|
14
|
+
readonly NEXT_PUBLIC_STACK_DASHBOARD_URL: {
|
|
15
|
+
readonly default: string | undefined;
|
|
16
|
+
readonly client: string | undefined;
|
|
17
|
+
readonly server: string | undefined;
|
|
18
|
+
};
|
|
19
|
+
readonly NEXT_PUBLIC_POSTHOG_KEY: string | undefined;
|
|
20
|
+
readonly NEXT_PUBLIC_STACK_SVIX_SERVER_URL: string | undefined;
|
|
21
|
+
readonly NEXT_PUBLIC_SENTRY_DSN: string | undefined;
|
|
22
|
+
readonly NEXT_PUBLIC_VERSION_ALERTER_SEVERE_ONLY: string | undefined;
|
|
23
|
+
readonly NEXT_PUBLIC_STACK_EMULATOR_ENABLED: string | undefined;
|
|
24
|
+
readonly NEXT_PUBLIC_STACK_EMULATOR_PROJECT_ID: string | undefined;
|
|
25
|
+
readonly NEXT_PUBLIC_STACK_PROJECT_ID: string | undefined;
|
|
26
|
+
readonly NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY: string | undefined;
|
|
27
|
+
readonly NEXT_PUBLIC_STACK_URL: string | undefined;
|
|
28
|
+
readonly NEXT_PUBLIC_STACK_INBUCKET_WEB_URL: string | undefined;
|
|
29
|
+
};
|
|
30
|
+
export declare function getPublicEnvVar(name: keyof typeof _inlineEnvVars): string | undefined;
|
|
31
|
+
export {};
|
package/dist/utils/env.js
CHANGED
|
@@ -57,3 +57,82 @@ export function getNextRuntime() {
|
|
|
57
57
|
export function getNodeEnvironment() {
|
|
58
58
|
return getEnvVariable("NODE_ENV", "");
|
|
59
59
|
}
|
|
60
|
+
// ===================== Hack to use dynamic env vars in docker build =====================
|
|
61
|
+
const _inlineEnvVars = {
|
|
62
|
+
NEXT_PUBLIC_STACK_API_URL: {
|
|
63
|
+
'default': process.env.NEXT_PUBLIC_STACK_API_URL,
|
|
64
|
+
'client': process.env.NEXT_PUBLIC_CLIENT_STACK_API_URL,
|
|
65
|
+
'server': process.env.NEXT_PUBLIC_SERVER_STACK_API_URL,
|
|
66
|
+
},
|
|
67
|
+
NEXT_PUBLIC_STACK_DASHBOARD_URL: {
|
|
68
|
+
'default': process.env.NEXT_PUBLIC_STACK_DASHBOARD_URL,
|
|
69
|
+
'client': process.env.NEXT_PUBLIC_CLIENT_STACK_DASHBOARD_URL,
|
|
70
|
+
'server': process.env.NEXT_PUBLIC_SERVER_STACK_DASHBOARD_URL,
|
|
71
|
+
},
|
|
72
|
+
NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
|
|
73
|
+
NEXT_PUBLIC_STACK_SVIX_SERVER_URL: process.env.NEXT_PUBLIC_STACK_SVIX_SERVER_URL,
|
|
74
|
+
NEXT_PUBLIC_SENTRY_DSN: process.env.NEXT_PUBLIC_SENTRY_DSN,
|
|
75
|
+
NEXT_PUBLIC_VERSION_ALERTER_SEVERE_ONLY: process.env.NEXT_PUBLIC_VERSION_ALERTER_SEVERE_ONLY,
|
|
76
|
+
NEXT_PUBLIC_STACK_EMULATOR_ENABLED: process.env.NEXT_PUBLIC_STACK_EMULATOR_ENABLED,
|
|
77
|
+
NEXT_PUBLIC_STACK_EMULATOR_PROJECT_ID: process.env.NEXT_PUBLIC_STACK_EMULATOR_PROJECT_ID,
|
|
78
|
+
NEXT_PUBLIC_STACK_PROJECT_ID: process.env.NEXT_PUBLIC_STACK_PROJECT_ID,
|
|
79
|
+
NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY: process.env.NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY,
|
|
80
|
+
NEXT_PUBLIC_STACK_URL: process.env.NEXT_PUBLIC_STACK_URL,
|
|
81
|
+
NEXT_PUBLIC_STACK_INBUCKET_WEB_URL: process.env.NEXT_PUBLIC_STACK_INBUCKET_WEB_URL,
|
|
82
|
+
};
|
|
83
|
+
// This will be replaced with the actual env vars after a docker build
|
|
84
|
+
const _postBuildEnvVars = {
|
|
85
|
+
NEXT_PUBLIC_STACK_API_URL: {
|
|
86
|
+
'default': 'STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_API_URL',
|
|
87
|
+
'client': 'STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_CLIENT_STACK_API_URL',
|
|
88
|
+
'server': 'STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_SERVER_STACK_API_URL',
|
|
89
|
+
},
|
|
90
|
+
NEXT_PUBLIC_STACK_DASHBOARD_URL: {
|
|
91
|
+
'default': 'STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_DASHBOARD_URL',
|
|
92
|
+
'client': 'STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_CLIENT_STACK_DASHBOARD_URL',
|
|
93
|
+
'server': 'STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_SERVER_STACK_DASHBOARD_URL',
|
|
94
|
+
},
|
|
95
|
+
NEXT_PUBLIC_STACK_PROJECT_ID: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_PROJECT_ID",
|
|
96
|
+
NEXT_PUBLIC_POSTHOG_KEY: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_POSTHOG_KEY",
|
|
97
|
+
NEXT_PUBLIC_STACK_SVIX_SERVER_URL: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_SVIX_SERVER_URL",
|
|
98
|
+
NEXT_PUBLIC_SENTRY_DSN: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_SENTRY_DSN",
|
|
99
|
+
NEXT_PUBLIC_VERSION_ALERTER_SEVERE_ONLY: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_VERSION_ALERTER_SEVERE_ONLY",
|
|
100
|
+
NEXT_PUBLIC_STACK_EMULATOR_ENABLED: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_EMULATOR_ENABLED",
|
|
101
|
+
NEXT_PUBLIC_STACK_EMULATOR_PROJECT_ID: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_EMULATOR_PROJECT_ID",
|
|
102
|
+
NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY",
|
|
103
|
+
NEXT_PUBLIC_STACK_URL: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_URL",
|
|
104
|
+
NEXT_PUBLIC_STACK_INBUCKET_WEB_URL: "STACK_ENV_VAR_SENTINEL_NEXT_PUBLIC_STACK_INBUCKET_WEB_URL",
|
|
105
|
+
};
|
|
106
|
+
// If this is not replaced with "true", then we will not use inline env vars
|
|
107
|
+
const _usePostBuildEnvVars = 'STACK_ENV_VAR_SENTINEL_USE_INLINE_ENV_VARS';
|
|
108
|
+
export function getPublicEnvVar(name) {
|
|
109
|
+
// This is a hack to force the compiler not to do any smart optimizations
|
|
110
|
+
const _ = _usePostBuildEnvVars.toString() + _inlineEnvVars.toString(); // Force runtime evaluation
|
|
111
|
+
const value = _usePostBuildEnvVars.slice(0) === 'true' ? _postBuildEnvVars[name] : _inlineEnvVars[name];
|
|
112
|
+
// Helper function to check if a value is a sentinel
|
|
113
|
+
const isSentinel = (str) => {
|
|
114
|
+
return _usePostBuildEnvVars.slice(0) === 'true' && str && str.startsWith('STACK_ENV_VAR_SENTINEL');
|
|
115
|
+
};
|
|
116
|
+
// If it's a dictionary with client/server values
|
|
117
|
+
if (typeof value === 'object') {
|
|
118
|
+
const preferredValue = isBrowserLike() ? value.client : value.server;
|
|
119
|
+
// Check for sentinel values
|
|
120
|
+
if (isSentinel(preferredValue)) {
|
|
121
|
+
return isSentinel(value.default) ? undefined : value.default;
|
|
122
|
+
}
|
|
123
|
+
if (isSentinel(value.default)) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return preferredValue || value.default;
|
|
127
|
+
}
|
|
128
|
+
else if (typeof value === 'string') {
|
|
129
|
+
if (isSentinel(value)) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// ======================================================================
|
package/dist/utils/strings.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export declare function trimLines(s: string): string;
|
|
|
40
40
|
*
|
|
41
41
|
* Useful for implementing your own template literal tags.
|
|
42
42
|
*/
|
|
43
|
-
export declare function templateIdentity(strings: TemplateStringsArray | readonly string[], ...values:
|
|
43
|
+
export declare function templateIdentity(strings: TemplateStringsArray | readonly string[], ...values: string[]): string;
|
|
44
44
|
export declare function deindent(code: string): string;
|
|
45
45
|
export declare function deindent(strings: TemplateStringsArray | readonly string[], ...values: any[]): string;
|
|
46
46
|
export declare function extractScopes(scope: string, removeDuplicates?: boolean): string[];
|
package/dist/utils/strings.js
CHANGED
|
@@ -61,21 +61,51 @@ export function trimEmptyLinesEnd(s) {
|
|
|
61
61
|
export function trimLines(s) {
|
|
62
62
|
return trimEmptyLinesEnd(trimEmptyLinesStart(s));
|
|
63
63
|
}
|
|
64
|
+
import.meta.vitest?.test("trimLines", ({ expect }) => {
|
|
65
|
+
expect(trimLines("")).toBe("");
|
|
66
|
+
expect(trimLines(" ")).toBe("");
|
|
67
|
+
expect(trimLines(" \n ")).toBe("");
|
|
68
|
+
expect(trimLines(" abc ")).toBe(" abc ");
|
|
69
|
+
expect(trimLines("\n \nLine1\nLine2\n \n")).toBe("Line1\nLine2");
|
|
70
|
+
expect(trimLines("Line1\n \nLine2")).toBe("Line1\n \nLine2");
|
|
71
|
+
expect(trimLines(" \n \n\t")).toBe("");
|
|
72
|
+
expect(trimLines(" Hello World")).toBe(" Hello World");
|
|
73
|
+
expect(trimLines("\n")).toBe("");
|
|
74
|
+
expect(trimLines("\t \n\t\tLine1 \n \nLine2\t\t\n\t ")).toBe("\t\tLine1 \n \nLine2\t\t");
|
|
75
|
+
});
|
|
64
76
|
/**
|
|
65
77
|
* A template literal tag that returns the same string as the template literal without a tag.
|
|
66
78
|
*
|
|
67
79
|
* Useful for implementing your own template literal tags.
|
|
68
80
|
*/
|
|
69
81
|
export function templateIdentity(strings, ...values) {
|
|
82
|
+
if (values.length !== strings.length - 1)
|
|
83
|
+
throw new StackAssertionError("Invalid number of values; must be one less than strings", { strings, values });
|
|
70
84
|
return strings.reduce((result, str, i) => result + str + (values[i] ?? ''), '');
|
|
71
85
|
}
|
|
86
|
+
import.meta.vitest?.test("templateIdentity", ({ expect }) => {
|
|
87
|
+
expect(templateIdentity `Hello World`).toBe("Hello World");
|
|
88
|
+
expect(templateIdentity `${"Hello"}`).toBe("Hello");
|
|
89
|
+
const greeting = "Hello";
|
|
90
|
+
const subject = "World";
|
|
91
|
+
expect(templateIdentity `${greeting}, ${subject}!`).toBe("Hello, World!");
|
|
92
|
+
expect(templateIdentity `${"A"}${"B"}${"C"}`).toBe("ABC");
|
|
93
|
+
expect(templateIdentity `Start${""}Middle${""}End`).toBe("StartMiddleEnd");
|
|
94
|
+
expect(templateIdentity ``).toBe("");
|
|
95
|
+
expect(templateIdentity `Line1
|
|
96
|
+
Line2`).toBe("Line1\nLine2");
|
|
97
|
+
expect(templateIdentity(["a ", " scientific ", "gun"], "certain", "rail")).toBe("a certain scientific railgun");
|
|
98
|
+
expect(templateIdentity(["only one part"])).toBe("only one part");
|
|
99
|
+
expect(() => templateIdentity(["a ", "b", "c"], "only one")).toThrow("Invalid number of values");
|
|
100
|
+
expect(() => templateIdentity(["a", "b"], "x", "y")).toThrow("Invalid number of values");
|
|
101
|
+
});
|
|
72
102
|
export function deindent(strings, ...values) {
|
|
73
103
|
if (typeof strings === "string")
|
|
74
104
|
return deindent([strings]);
|
|
75
105
|
if (strings.length === 0)
|
|
76
106
|
return "";
|
|
77
107
|
if (values.length !== strings.length - 1)
|
|
78
|
-
throw new
|
|
108
|
+
throw new StackAssertionError("Invalid number of values; must be one less than strings", { strings, values });
|
|
79
109
|
const trimmedStrings = [...strings];
|
|
80
110
|
trimmedStrings[0] = trimEmptyLinesStart(trimmedStrings[0]);
|
|
81
111
|
trimmedStrings[trimmedStrings.length - 1] = trimEmptyLinesEnd(trimmedStrings[trimmedStrings.length - 1]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { templateIdentity } from "./strings";
|
|
3
|
+
describe("templateIdentity", () => {
|
|
4
|
+
it("should be equivalent to a regular template string", () => {
|
|
5
|
+
const adjective = "scientific";
|
|
6
|
+
const noun = "railgun";
|
|
7
|
+
expect(templateIdentity `a certain scientific railgun`).toBe("a certain scientific railgun");
|
|
8
|
+
expect(templateIdentity `a certain ${adjective} railgun`).toBe(`a certain scientific railgun`);
|
|
9
|
+
expect(templateIdentity `a certain ${adjective} ${noun}`).toBe(`a certain scientific railgun`);
|
|
10
|
+
expect(templateIdentity `${adjective}${noun}`).toBe(`scientificrailgun`);
|
|
11
|
+
});
|
|
12
|
+
it("should work with empty strings", () => {
|
|
13
|
+
expect(templateIdentity ``).toBe("");
|
|
14
|
+
expect(templateIdentity `${""}`).toBe("");
|
|
15
|
+
expect(templateIdentity `${""}${""}`).toBe("");
|
|
16
|
+
});
|
|
17
|
+
it("should work with normal arrays", () => {
|
|
18
|
+
expect(templateIdentity(["a ", " scientific ", "gun"], "certain", "rail")).toBe("a certain scientific railgun");
|
|
19
|
+
expect(templateIdentity(["a"])).toBe("a");
|
|
20
|
+
});
|
|
21
|
+
it("should throw an error with wrong number of value arguments", () => {
|
|
22
|
+
expect(() => templateIdentity([])).toThrow();
|
|
23
|
+
expect(() => templateIdentity(["a", "b"])).toThrow();
|
|
24
|
+
expect(() => templateIdentity(["a", "b", "c"], "a", "b", "c")).toThrow();
|
|
25
|
+
});
|
|
26
|
+
});
|
package/dist/utils/urls.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export declare function createUrlIfValid(...args: ConstructorParameters<typeof URL>): URL | null;
|
|
2
2
|
export declare function isValidUrl(url: string): boolean;
|
|
3
|
+
export declare function isValidHostname(hostname: string): boolean;
|
|
3
4
|
export declare function isLocalhost(urlOrString: string | URL): boolean;
|
|
4
5
|
export declare function isRelative(url: string): boolean;
|
|
5
6
|
export declare function getRelativePart(url: URL): string;
|
package/dist/utils/urls.js
CHANGED
|
@@ -11,6 +11,12 @@ export function createUrlIfValid(...args) {
|
|
|
11
11
|
export function isValidUrl(url) {
|
|
12
12
|
return !!createUrlIfValid(url);
|
|
13
13
|
}
|
|
14
|
+
export function isValidHostname(hostname) {
|
|
15
|
+
const url = createUrlIfValid(`https://${hostname}`);
|
|
16
|
+
if (!url)
|
|
17
|
+
return false;
|
|
18
|
+
return url.hostname === hostname;
|
|
19
|
+
}
|
|
14
20
|
export function isLocalhost(urlOrString) {
|
|
15
21
|
const url = createUrlIfValid(urlOrString);
|
|
16
22
|
if (!url)
|
|
@@ -49,5 +55,5 @@ export function url(strings, ...values) {
|
|
|
49
55
|
* Any values passed are encoded.
|
|
50
56
|
*/
|
|
51
57
|
export function urlString(strings, ...values) {
|
|
52
|
-
return templateIdentity(strings, values.map(encodeURIComponent));
|
|
58
|
+
return templateIdentity(strings, ...values.map(encodeURIComponent));
|
|
53
59
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackframe/stack-shared",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.16",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"oauth4webapi": "^2.10.3",
|
|
53
53
|
"semver": "^7.6.3",
|
|
54
54
|
"uuid": "^9.0.1",
|
|
55
|
-
"@stackframe/stack-sc": "2.7.
|
|
55
|
+
"@stackframe/stack-sc": "2.7.16"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@sentry/nextjs": "^8.40.0",
|
|
@@ -69,6 +69,7 @@
|
|
|
69
69
|
"scripts": {
|
|
70
70
|
"build": "tsc",
|
|
71
71
|
"typecheck": "tsc --noEmit",
|
|
72
|
+
"test": "vitest run",
|
|
72
73
|
"clean": "rimraf dist && rimraf node_modules",
|
|
73
74
|
"dev": "tsc -w --preserveWatchOutput --declarationMap",
|
|
74
75
|
"lint": "eslint --ext .tsx,.ts ."
|