@hexclave/shared 1.0.3 → 1.0.6
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/dist/ai/unified-prompts/reminders.js +4 -3
- package/dist/ai/unified-prompts/reminders.js.map +1 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.d.ts +3 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.d.ts.map +1 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.js +216 -29
- package/dist/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.js.map +1 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/docs-index.d.ts.map +1 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/docs-index.js +1 -0
- package/dist/ai/unified-prompts/skill-site-prompt-parts/docs-index.js.map +1 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/docs-json.generated.d.ts +18 -6
- package/dist/ai/unified-prompts/skill-site-prompt-parts/docs-json.generated.js +23 -8
- package/dist/ai/unified-prompts/skill-site-prompt-parts/docs-json.generated.js.map +1 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/openapi-specs.js +1 -1
- package/dist/ai/unified-prompts/skill-site-prompt-parts/openapi-specs.js.map +1 -1
- package/dist/config/schema.d.ts +1 -1
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config/schema.js +5 -5
- package/dist/config/schema.js.map +1 -1
- package/dist/config-authoring.d.ts +4 -4
- package/dist/config-authoring.d.ts.map +1 -1
- package/dist/config-authoring.js +2 -2
- package/dist/config-authoring.js.map +1 -1
- package/dist/config-rendering.d.ts +4 -4
- package/dist/config-rendering.d.ts.map +1 -1
- package/dist/config-rendering.js +19 -18
- package/dist/config-rendering.js.map +1 -1
- package/dist/esm/ai/unified-prompts/reminders.js +4 -3
- package/dist/esm/ai/unified-prompts/reminders.js.map +1 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.d.ts +3 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.d.ts.map +1 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.js +215 -30
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.js.map +1 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/docs-index.d.ts.map +1 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/docs-index.js +1 -0
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/docs-index.js.map +1 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/docs-json.generated.d.ts +18 -6
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/docs-json.generated.js +23 -8
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/docs-json.generated.js.map +1 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/openapi-specs.js +1 -1
- package/dist/esm/ai/unified-prompts/skill-site-prompt-parts/openapi-specs.js.map +1 -1
- package/dist/esm/config/schema.d.ts +1 -1
- package/dist/esm/config/schema.d.ts.map +1 -1
- package/dist/esm/config/schema.js +5 -5
- package/dist/esm/config/schema.js.map +1 -1
- package/dist/esm/config-authoring.d.ts +4 -4
- package/dist/esm/config-authoring.d.ts.map +1 -1
- package/dist/esm/config-authoring.js +2 -2
- package/dist/esm/config-authoring.js.map +1 -1
- package/dist/esm/config-rendering.d.ts +4 -4
- package/dist/esm/config-rendering.d.ts.map +1 -1
- package/dist/esm/config-rendering.js +15 -14
- package/dist/esm/config-rendering.js.map +1 -1
- package/dist/esm/helpers/vault/client-side.d.ts.map +1 -1
- package/dist/esm/helpers/vault/client-side.js.map +1 -1
- package/dist/esm/helpers/vault/server-side.d.ts.map +1 -1
- package/dist/esm/helpers/vault/server-side.js.map +1 -1
- package/dist/esm/hexclave-config-file.d.ts +15 -0
- package/dist/esm/hexclave-config-file.d.ts.map +1 -0
- package/dist/esm/{stack-config-file.js → hexclave-config-file.js} +7 -7
- package/dist/esm/hexclave-config-file.js.map +1 -0
- package/dist/esm/interface/admin-metrics.d.ts +2 -2
- package/dist/esm/interface/admin-metrics.js.map +1 -1
- package/dist/esm/interface/client-interface.js +2 -2
- package/dist/esm/interface/client-interface.js.map +1 -1
- package/dist/esm/interface/conversations.d.ts +32 -32
- package/dist/esm/interface/crud/current-user.d.ts +7 -7
- package/dist/esm/interface/crud/email-outbox.d.ts +42 -42
- package/dist/esm/interface/crud/project-permissions.js +5 -5
- package/dist/esm/interface/crud/project-permissions.js.map +1 -1
- package/dist/esm/interface/crud/team-member-profiles.d.ts +18 -18
- package/dist/esm/interface/crud/team-permissions.js +5 -5
- package/dist/esm/interface/crud/team-permissions.js.map +1 -1
- package/dist/esm/interface/crud/users.d.ts +10 -10
- package/dist/esm/interface/page-component-versions.js +107 -107
- package/dist/esm/interface/page-component-versions.js.map +1 -1
- package/dist/esm/interface/webhooks.d.ts +2 -2
- package/dist/esm/known-errors.d.ts +1 -1
- package/dist/esm/known-errors.d.ts.map +1 -1
- package/dist/esm/known-errors.js +9 -5
- package/dist/esm/known-errors.js.map +1 -1
- package/dist/esm/schema-fields.d.ts +15 -5
- package/dist/esm/schema-fields.d.ts.map +1 -1
- package/dist/esm/schema-fields.js +26 -16
- package/dist/esm/schema-fields.js.map +1 -1
- package/dist/esm/utils/crypto.d.ts.map +1 -1
- package/dist/esm/utils/crypto.js.map +1 -1
- package/dist/esm/utils/currency-constants.d.ts +6 -0
- package/dist/esm/utils/currency-constants.d.ts.map +1 -1
- package/dist/esm/utils/currency-constants.js.map +1 -1
- package/dist/esm/utils/dev-env-state-path.d.ts +3 -3
- package/dist/esm/utils/dev-env-state-path.d.ts.map +1 -1
- package/dist/esm/utils/dev-env-state-path.js +4 -4
- package/dist/esm/utils/dev-env-state-path.js.map +1 -1
- package/dist/esm/utils/errors.js +2 -2
- package/dist/esm/utils/errors.js.map +1 -1
- package/dist/esm/utils/esbuild.d.ts.map +1 -1
- package/dist/esm/utils/esbuild.js +0 -1
- package/dist/esm/utils/esbuild.js.map +1 -1
- package/dist/esm/utils/featurebase.d.ts +3 -3
- package/dist/esm/utils/featurebase.d.ts.map +1 -1
- package/dist/esm/utils/featurebase.js +15 -15
- package/dist/esm/utils/featurebase.js.map +1 -1
- package/dist/esm/utils/fs.js +6 -6
- package/dist/esm/utils/fs.js.map +1 -1
- package/dist/esm/utils/globals.d.ts.map +1 -1
- package/dist/esm/utils/globals.js +10 -10
- package/dist/esm/utils/globals.js.map +1 -1
- package/dist/esm/utils/jwt.d.ts.map +1 -1
- package/dist/esm/utils/jwt.js +3 -3
- package/dist/esm/utils/jwt.js.map +1 -1
- package/dist/esm/utils/redirect-urls.d.ts +3 -3
- package/dist/esm/utils/redirect-urls.d.ts.map +1 -1
- package/dist/esm/utils/redirect-urls.js +5 -5
- package/dist/esm/utils/redirect-urls.js.map +1 -1
- package/dist/esm/utils/urls.js.map +1 -1
- package/dist/helpers/vault/client-side.d.ts.map +1 -1
- package/dist/helpers/vault/client-side.js.map +1 -1
- package/dist/helpers/vault/server-side.d.ts.map +1 -1
- package/dist/helpers/vault/server-side.js.map +1 -1
- package/dist/hexclave-config-file.d.ts +15 -0
- package/dist/hexclave-config-file.d.ts.map +1 -0
- package/dist/{stack-config-file.js → hexclave-config-file.js} +8 -8
- package/dist/hexclave-config-file.js.map +1 -0
- package/dist/interface/admin-metrics.d.ts +12 -12
- package/dist/interface/admin-metrics.js.map +1 -1
- package/dist/interface/client-interface.js +2 -2
- package/dist/interface/client-interface.js.map +1 -1
- package/dist/interface/conversations.d.ts +32 -32
- package/dist/interface/crud/current-user.d.ts +7 -7
- package/dist/interface/crud/email-outbox.d.ts +42 -42
- package/dist/interface/crud/project-permissions.js +5 -5
- package/dist/interface/crud/project-permissions.js.map +1 -1
- package/dist/interface/crud/team-member-profiles.d.ts +18 -18
- package/dist/interface/crud/team-permissions.js +5 -5
- package/dist/interface/crud/team-permissions.js.map +1 -1
- package/dist/interface/crud/users.d.ts +10 -10
- package/dist/interface/page-component-versions.js +107 -107
- package/dist/interface/page-component-versions.js.map +1 -1
- package/dist/interface/webhooks.d.ts +2 -2
- package/dist/known-errors.d.ts +1 -1
- package/dist/known-errors.d.ts.map +1 -1
- package/dist/known-errors.js +9 -5
- package/dist/known-errors.js.map +1 -1
- package/dist/schema-fields.d.ts +15 -5
- package/dist/schema-fields.d.ts.map +1 -1
- package/dist/schema-fields.js +26 -16
- package/dist/schema-fields.js.map +1 -1
- package/dist/utils/crypto.d.ts.map +1 -1
- package/dist/utils/crypto.js.map +1 -1
- package/dist/utils/currency-constants.d.ts +6 -0
- package/dist/utils/currency-constants.d.ts.map +1 -1
- package/dist/utils/currency-constants.js.map +1 -1
- package/dist/utils/dev-env-state-path.d.ts +3 -3
- package/dist/utils/dev-env-state-path.d.ts.map +1 -1
- package/dist/utils/dev-env-state-path.js +5 -5
- package/dist/utils/dev-env-state-path.js.map +1 -1
- package/dist/utils/errors.js +2 -2
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/esbuild.d.ts.map +1 -1
- package/dist/utils/esbuild.js +0 -1
- package/dist/utils/esbuild.js.map +1 -1
- package/dist/utils/featurebase.d.ts +3 -3
- package/dist/utils/featurebase.d.ts.map +1 -1
- package/dist/utils/featurebase.js +15 -15
- package/dist/utils/featurebase.js.map +1 -1
- package/dist/utils/fs.js.map +1 -1
- package/dist/utils/globals.d.ts.map +1 -1
- package/dist/utils/globals.js +10 -10
- package/dist/utils/globals.js.map +1 -1
- package/dist/utils/jwt.d.ts.map +1 -1
- package/dist/utils/jwt.js +3 -3
- package/dist/utils/jwt.js.map +1 -1
- package/dist/utils/redirect-urls.d.ts +3 -3
- package/dist/utils/redirect-urls.d.ts.map +1 -1
- package/dist/utils/redirect-urls.js +5 -5
- package/dist/utils/redirect-urls.js.map +1 -1
- package/dist/utils/urls.js.map +1 -1
- package/package.json +2 -1
- package/src/ai/image-limits.ts +62 -0
- package/src/ai/llms/llms.ts +36 -0
- package/src/ai/unified-prompts/reminders.ts +27 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/ai-setup-prompt.ts +1047 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/brain.ts +5 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/cli-help.ts +5 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/config-docs.ts +5 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/custom-components-instructions.ts +5 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/dashboard-instructions.ts +5 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/docs-index.ts +80 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/docs-json.generated.ts +322 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/openapi-specs.ts +5 -0
- package/src/ai/unified-prompts/skill-site-prompt-parts/sdk-interface-source.ts +5 -0
- package/src/ai/unified-prompts/skill-site-prompt.ts +72 -0
- package/src/apps/apps-config.ts +177 -0
- package/src/apps/apps-ui.tsx +112 -0
- package/src/config/README.md +154 -0
- package/src/config/db-sync-mappings.ts +2293 -0
- package/src/config/format.ts +388 -0
- package/src/config/migrate-catalogs-to-product-lines.ts +475 -0
- package/src/config/schema-fuzzer.test.ts +377 -0
- package/src/config/schema.ts +1507 -0
- package/src/config-authoring.test.ts +52 -0
- package/src/config-authoring.ts +37 -0
- package/src/config-rendering.ts +142 -0
- package/src/crud.tsx +275 -0
- package/src/global.d.ts +1 -0
- package/src/helpers/emails.ts +250 -0
- package/src/helpers/init-prompt.ts +39 -0
- package/src/helpers/password.ts +16 -0
- package/src/helpers/production-mode.ts +58 -0
- package/src/helpers/support-sla.test.ts +124 -0
- package/src/helpers/support-sla.ts +93 -0
- package/src/helpers/vault/client-side.ts +92 -0
- package/src/helpers/vault/server-side.ts +153 -0
- package/src/hexclave-config-file.ts +120 -0
- package/src/hooks/use-async-callback.tsx +43 -0
- package/src/hooks/use-async-external-store.tsx +24 -0
- package/src/hooks/use-hash.tsx +14 -0
- package/src/hooks/use-hover.tsx +88 -0
- package/src/hooks/use-strict-memo.tsx +157 -0
- package/src/index.ts +17 -0
- package/src/interface/admin-interface.ts +1043 -0
- package/src/interface/admin-metrics.ts +214 -0
- package/src/interface/client-interface.test.ts +765 -0
- package/src/interface/client-interface.ts +2586 -0
- package/src/interface/conversations.ts +109 -0
- package/src/interface/crud/analytics.ts +11 -0
- package/src/interface/crud/connected-accounts.ts +52 -0
- package/src/interface/crud/contact-channels.ts +74 -0
- package/src/interface/crud/current-user.ts +72 -0
- package/src/interface/crud/email-outbox.ts +263 -0
- package/src/interface/crud/email-templates.ts +50 -0
- package/src/interface/crud/emails.ts +22 -0
- package/src/interface/crud/internal-api-keys.ts +69 -0
- package/src/interface/crud/invoices.ts +36 -0
- package/src/interface/crud/items.ts +24 -0
- package/src/interface/crud/notification-preferences.ts +33 -0
- package/src/interface/crud/oauth-providers.ts +91 -0
- package/src/interface/crud/products.ts +42 -0
- package/src/interface/crud/project-api-keys.ts +100 -0
- package/src/interface/crud/project-permissions.ts +118 -0
- package/src/interface/crud/projects.ts +229 -0
- package/src/interface/crud/session-replays.ts +85 -0
- package/src/interface/crud/sessions.ts +66 -0
- package/src/interface/crud/sign-up-rules.ts +12 -0
- package/src/interface/crud/svix-token.ts +20 -0
- package/src/interface/crud/team-invitation-details.ts +22 -0
- package/src/interface/crud/team-invitation.ts +35 -0
- package/src/interface/crud/team-member-profiles.ts +61 -0
- package/src/interface/crud/team-memberships.ts +56 -0
- package/src/interface/crud/team-permissions.ts +119 -0
- package/src/interface/crud/teams.ts +142 -0
- package/src/interface/crud/transactions.ts +131 -0
- package/src/interface/crud/users.ts +185 -0
- package/src/interface/handler-urls.ts +74 -0
- package/src/interface/page-component-versions.ts +1702 -0
- package/src/interface/server-interface.ts +1146 -0
- package/src/interface/webhooks.ts +28 -0
- package/src/known-errors.tsx +2034 -0
- package/src/local-emulator.ts +5 -0
- package/src/payments/payment-methods.ts +167 -0
- package/src/payments/stripe-limits.ts +18 -0
- package/src/plans.ts +107 -0
- package/src/schema-fields.ts +944 -0
- package/src/sessions.ts +310 -0
- package/src/utils/api-keys.tsx +122 -0
- package/src/utils/arrays.tsx +248 -0
- package/src/utils/auth-methods.ts +8 -0
- package/src/utils/base64.tsx +24 -0
- package/src/utils/booleans.tsx +34 -0
- package/src/utils/browser-compat.tsx +17 -0
- package/src/utils/bytes.tsx +280 -0
- package/src/utils/caches.tsx +280 -0
- package/src/utils/cel-fields.ts +92 -0
- package/src/utils/cloud-hosts.tsx +92 -0
- package/src/utils/compile-time.tsx +11 -0
- package/src/utils/country-codes.ts +39 -0
- package/src/utils/crypto.tsx +174 -0
- package/src/utils/currencies.tsx +53 -0
- package/src/utils/currency-constants.tsx +51 -0
- package/src/utils/dates.tsx +246 -0
- package/src/utils/dev-env-state-path.ts +14 -0
- package/src/utils/dom.tsx +7 -0
- package/src/utils/env.tsx +117 -0
- package/src/utils/errors.tsx +284 -0
- package/src/utils/esbuild.tsx +235 -0
- package/src/utils/featurebase.tsx +247 -0
- package/src/utils/fs.tsx +33 -0
- package/src/utils/functions.tsx +22 -0
- package/src/utils/geo.tsx +16 -0
- package/src/utils/globals.tsx +48 -0
- package/src/utils/hashes.tsx +52 -0
- package/src/utils/html.tsx +60 -0
- package/src/utils/http.tsx +91 -0
- package/src/utils/ips.tsx +47 -0
- package/src/utils/json.tsx +200 -0
- package/src/utils/jsx-editable-transpiler.test.ts +156 -0
- package/src/utils/jsx-editable-transpiler.tsx +401 -0
- package/src/utils/jwt.tsx +170 -0
- package/src/utils/locks.tsx +71 -0
- package/src/utils/maps.tsx +407 -0
- package/src/utils/math.tsx +18 -0
- package/src/utils/monkey-patch.tsx +29 -0
- package/src/utils/node-http.tsx +47 -0
- package/src/utils/numbers.tsx +82 -0
- package/src/utils/oauth.tsx +9 -0
- package/src/utils/objects.tsx +581 -0
- package/src/utils/paginated-lists.test.ts +1039 -0
- package/src/utils/paginated-lists.tsx +479 -0
- package/src/utils/passkey.tsx +1 -0
- package/src/utils/promises.tsx +540 -0
- package/src/utils/proxies.tsx +212 -0
- package/src/utils/react.tsx +308 -0
- package/src/utils/redirect-urls.tsx +299 -0
- package/src/utils/regex.tsx +10 -0
- package/src/utils/results.tsx +411 -0
- package/src/utils/sentry.tsx +20 -0
- package/src/utils/stores.tsx +238 -0
- package/src/utils/strings.nicify.test.ts +253 -0
- package/src/utils/strings.tsx +723 -0
- package/src/utils/telemetry.tsx +37 -0
- package/src/utils/turnstile-browser.ts +106 -0
- package/src/utils/turnstile-flow.test.ts +101 -0
- package/src/utils/turnstile-flow.ts +266 -0
- package/src/utils/turnstile.ts +34 -0
- package/src/utils/typed-arrays.tsx +17 -0
- package/src/utils/types.tsx +208 -0
- package/src/utils/unicode.tsx +24 -0
- package/src/utils/urls.tsx +411 -0
- package/src/utils/uuids.tsx +47 -0
- package/dist/esm/stack-config-file.d.ts +0 -15
- package/dist/esm/stack-config-file.d.ts.map +0 -1
- package/dist/esm/stack-config-file.js.map +0 -1
- package/dist/stack-config-file.d.ts +0 -15
- package/dist/stack-config-file.d.ts.map +0 -1
- package/dist/stack-config-file.js.map +0 -1
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
// Format spec lives in the eng-handbook (random-thoughts/config-json-format.md).
|
|
2
|
+
// (Previously linked to a public repo that doesn't exist — internal handbook only.)
|
|
3
|
+
|
|
4
|
+
import { HexclaveAssertionError, throwErr } from "../utils/errors";
|
|
5
|
+
import { deleteKey, filterUndefined, get, hasAndNotUndefined, set } from "../utils/objects";
|
|
6
|
+
import { OptionalKeys, RequiredKeys } from "../utils/types";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export type ConfigValue = string | number | boolean | null | ConfigValue[] | Config;
|
|
10
|
+
export type Config = {
|
|
11
|
+
[keyOrDotNotation: string]: ConfigValue | undefined, // must support undefined for optional values
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type NormalizedConfigValue = string | number | boolean | NormalizedConfig | NormalizedConfigValue[];
|
|
15
|
+
export type NormalizedConfig = {
|
|
16
|
+
[key: string]: NormalizedConfigValue | undefined, // must support undefined for optional values
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type _NormalizesTo<N> = N extends readonly any[]
|
|
20
|
+
? { [K in keyof N]: _NormalizesTo<N[K]> }
|
|
21
|
+
: N extends object ? (
|
|
22
|
+
& Config
|
|
23
|
+
& { [K in OptionalKeys<N>]?: _NormalizesTo<N[K]> | null }
|
|
24
|
+
& { [K in RequiredKeys<N>]: undefined extends N[K] ? _NormalizesTo<N[K]> | null : _NormalizesTo<N[K]> }
|
|
25
|
+
& { [K in `${string}.${string}`]: ConfigValue }
|
|
26
|
+
) : N;
|
|
27
|
+
export type NormalizesTo<N extends NormalizedConfig> = _NormalizesTo<N>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Note that a config can both be valid and not normalizable.
|
|
31
|
+
*/
|
|
32
|
+
export function isValidConfig(c: unknown): c is Config {
|
|
33
|
+
return getInvalidConfigReason(c) === undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function getInvalidConfigReason(c: unknown, options: { configName?: string } = {}): string | undefined {
|
|
37
|
+
const configName = options.configName ?? 'config';
|
|
38
|
+
if (c === null || typeof c !== 'object') return `${configName} must be a non-null object`;
|
|
39
|
+
for (const [key, value] of Object.entries(c)) {
|
|
40
|
+
if (value === undefined) continue;
|
|
41
|
+
if (typeof key !== 'string') return `${configName} must have only string keys (found: ${typeof key})`;
|
|
42
|
+
if (!key.match(/^[a-zA-Z0-9_:$][a-zA-Z_:$0-9\-]*(?:\.[a-zA-Z0-9_:$][a-zA-Z_:$0-9\-]*)*$/)) return `All keys of ${configName} must consist of only alphanumeric characters, dots, underscores, colons, dollar signs, or hyphens and start with a character other than a hyphen (found: ${key})`;
|
|
43
|
+
|
|
44
|
+
const entryName = `${configName}.${key}`;
|
|
45
|
+
const reason = getInvalidConfigValueReason(value, { valueName: entryName });
|
|
46
|
+
if (reason) return reason;
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getInvalidConfigValueReason(value: unknown, options: { valueName?: string } = {}): string | undefined {
|
|
52
|
+
const valueName = options.valueName ?? 'value';
|
|
53
|
+
switch (typeof value) {
|
|
54
|
+
case 'string':
|
|
55
|
+
case 'number':
|
|
56
|
+
case 'boolean': {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
case 'object': {
|
|
60
|
+
if (value === null) {
|
|
61
|
+
break;
|
|
62
|
+
} else if (Array.isArray(value)) {
|
|
63
|
+
for (const [index, v] of value.entries()) {
|
|
64
|
+
const elementValueName = `${valueName}[${index}]`;
|
|
65
|
+
if (v === null) return `${elementValueName} is null; tuple elements cannot be null`;
|
|
66
|
+
const reason = getInvalidConfigValueReason(v, { valueName: elementValueName });
|
|
67
|
+
if (reason) return reason;
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
const reason = getInvalidConfigReason(value, { configName: valueName });
|
|
71
|
+
if (reason) return reason;
|
|
72
|
+
}
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
default: {
|
|
76
|
+
return `${valueName} has an invalid value type ${typeof value} (value: ${value})`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function assertValidConfig(c: unknown) {
|
|
83
|
+
const reason = getInvalidConfigReason(c);
|
|
84
|
+
if (reason) throw new HexclaveAssertionError(`Invalid config: ${reason}`, { c });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function override(c1: Config, ...configs: Config[]) {
|
|
88
|
+
if (configs.length === 0) return c1;
|
|
89
|
+
if (configs.length > 1) return override(override(c1, configs[0]), ...configs.slice(1));
|
|
90
|
+
const c2 = configs[0];
|
|
91
|
+
|
|
92
|
+
assertValidConfig(c1);
|
|
93
|
+
assertValidConfig(c2);
|
|
94
|
+
|
|
95
|
+
let result = c1;
|
|
96
|
+
for (const key of Object.keys(filterUndefined(c2))) {
|
|
97
|
+
result = Object.fromEntries(
|
|
98
|
+
Object.entries(result).filter(([k]) => k !== key && !k.startsWith(key + '.'))
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
...result,
|
|
104
|
+
...filterUndefined(c2),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Removes keys from a config override, using the same nested key logic as the `override` function.
|
|
110
|
+
* Resetting key "a.b" also resets "a.b.c" (and any other descendants).
|
|
111
|
+
* Handles both flat dot-notation keys and nested object keys.
|
|
112
|
+
*/
|
|
113
|
+
export function removeKeysFromConfig(config: Config, keysToRemove: string[]): Config {
|
|
114
|
+
if (keysToRemove.length === 0) return config;
|
|
115
|
+
|
|
116
|
+
const result: Config = {};
|
|
117
|
+
for (const [key, value] of Object.entries(config)) {
|
|
118
|
+
if (value === undefined) continue;
|
|
119
|
+
|
|
120
|
+
// Check if this flat key matches or is a child of any key to remove (same logic as override)
|
|
121
|
+
const shouldRemove = keysToRemove.some(k => key === k || key.startsWith(k + '.'));
|
|
122
|
+
if (shouldRemove) continue;
|
|
123
|
+
|
|
124
|
+
// Check if any key to remove is a descendant of this key (meaning it's nested inside this value)
|
|
125
|
+
const childKeysToRemove = keysToRemove
|
|
126
|
+
.filter(k => k.startsWith(key + '.'))
|
|
127
|
+
.map(k => k.slice(key.length + 1));
|
|
128
|
+
|
|
129
|
+
if (childKeysToRemove.length > 0 && typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
130
|
+
const cleaned = removeKeysFromConfig(value as Config, childKeysToRemove);
|
|
131
|
+
if (Object.keys(cleaned).length > 0) {
|
|
132
|
+
result[key] = cleaned;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
result[key] = value;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
import.meta.vitest?.test("removeKeysFromConfig(...)", ({ expect }) => {
|
|
142
|
+
// Basic flat key removal
|
|
143
|
+
expect(removeKeysFromConfig({ a: 1, b: 2 }, ["a"])).toEqual({ b: 2 });
|
|
144
|
+
expect(removeKeysFromConfig({ "a.b": 1, "a.c": 2, d: 3 }, ["a.b"])).toEqual({ "a.c": 2, d: 3 });
|
|
145
|
+
|
|
146
|
+
// Removing a parent removes children (flat keys)
|
|
147
|
+
expect(removeKeysFromConfig({ "a.b": 1, "a.b.c": 2, "a.d": 3 }, ["a.b"])).toEqual({ "a.d": 3 });
|
|
148
|
+
expect(removeKeysFromConfig({ "a.b": 1, "a.c": 2, "a.d": 3 }, ["a"])).toEqual({});
|
|
149
|
+
expect(removeKeysFromConfig({ "teams.allowClientTeamCreation": true, "teams.createPersonalTeamOnSignUp": true }, ["teams"])).toEqual({});
|
|
150
|
+
|
|
151
|
+
// Nested object key removal
|
|
152
|
+
expect(removeKeysFromConfig({ teams: { allowClientTeamCreation: true, createPersonalTeamOnSignUp: true } }, ["teams.allowClientTeamCreation"])).toEqual({ teams: { createPersonalTeamOnSignUp: true } });
|
|
153
|
+
expect(removeKeysFromConfig({ teams: { allowClientTeamCreation: true } }, ["teams.allowClientTeamCreation"])).toEqual({});
|
|
154
|
+
expect(removeKeysFromConfig({ teams: { allowClientTeamCreation: true, createPersonalTeamOnSignUp: true } }, ["teams"])).toEqual({});
|
|
155
|
+
expect(removeKeysFromConfig({ a: { b: { c: 1, d: 2 } } }, ["a.b.c"])).toEqual({ a: { b: { d: 2 } } });
|
|
156
|
+
|
|
157
|
+
// Mixed flat and nested
|
|
158
|
+
expect(removeKeysFromConfig({ "teams.allowClientTeamCreation": true, teams: { createPersonalTeamOnSignUp: true } }, ["teams.allowClientTeamCreation"])).toEqual({ teams: { createPersonalTeamOnSignUp: true } });
|
|
159
|
+
expect(removeKeysFromConfig({ "teams.allowClientTeamCreation": true, teams: { createPersonalTeamOnSignUp: true } }, ["teams"])).toEqual({});
|
|
160
|
+
|
|
161
|
+
// Nested with dot-notation inner keys
|
|
162
|
+
expect(removeKeysFromConfig({ teams: { "a.b": 1 } }, ["teams.a.b"])).toEqual({});
|
|
163
|
+
expect(removeKeysFromConfig({ teams: { "a.b.c": 1 } }, ["teams.a.b"])).toEqual({});
|
|
164
|
+
|
|
165
|
+
// No keys to remove
|
|
166
|
+
expect(removeKeysFromConfig({ a: 1, b: 2 }, [])).toEqual({ a: 1, b: 2 });
|
|
167
|
+
|
|
168
|
+
// Key not present (no-op)
|
|
169
|
+
expect(removeKeysFromConfig({ a: 1, b: 2 }, ["c"])).toEqual({ a: 1, b: 2 });
|
|
170
|
+
expect(removeKeysFromConfig({ a: 1, b: 2 }, ["a.b"])).toEqual({ a: 1, b: 2 });
|
|
171
|
+
|
|
172
|
+
// Multiple keys to remove
|
|
173
|
+
expect(removeKeysFromConfig({ "a.b": 1, "c.d": 2, "e.f": 3 }, ["a.b", "e.f"])).toEqual({ "c.d": 2 });
|
|
174
|
+
expect(removeKeysFromConfig({ a: { b: 1 }, c: { d: 2 } }, ["a.b", "c.d"])).toEqual({});
|
|
175
|
+
|
|
176
|
+
// Removing non-object values with nested key path (no-op for non-objects)
|
|
177
|
+
expect(removeKeysFromConfig({ a: "string" }, ["a.b"])).toEqual({ a: "string" });
|
|
178
|
+
expect(removeKeysFromConfig({ a: 123 }, ["a.b"])).toEqual({ a: 123 });
|
|
179
|
+
expect(removeKeysFromConfig({ a: null }, ["a.b"])).toEqual({ a: null });
|
|
180
|
+
|
|
181
|
+
// Array values are preserved (not recursed into)
|
|
182
|
+
expect(removeKeysFromConfig({ a: [1, 2, 3] }, ["a.0"])).toEqual({ a: [1, 2, 3] });
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
import.meta.vitest?.test("override(...)", ({ expect }) => {
|
|
186
|
+
expect(
|
|
187
|
+
override(
|
|
188
|
+
{
|
|
189
|
+
a: 1,
|
|
190
|
+
b: 2,
|
|
191
|
+
"c.d": 3,
|
|
192
|
+
"c.e.f": 4,
|
|
193
|
+
"c.g": 5,
|
|
194
|
+
h: [6, { i: 7 }, 8],
|
|
195
|
+
k: 123,
|
|
196
|
+
l: undefined,
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
a: 9,
|
|
200
|
+
"c.d": 10,
|
|
201
|
+
"c.e": null,
|
|
202
|
+
"h.0": 11,
|
|
203
|
+
"h.1": {
|
|
204
|
+
j: 12,
|
|
205
|
+
},
|
|
206
|
+
k: undefined,
|
|
207
|
+
},
|
|
208
|
+
)
|
|
209
|
+
).toEqual({
|
|
210
|
+
a: 9,
|
|
211
|
+
b: 2,
|
|
212
|
+
"c.d": 10,
|
|
213
|
+
"c.e": null,
|
|
214
|
+
"c.g": 5,
|
|
215
|
+
h: [6, { i: 7 }, 8],
|
|
216
|
+
"h.0": 11,
|
|
217
|
+
"h.1": {
|
|
218
|
+
j: 12,
|
|
219
|
+
},
|
|
220
|
+
k: 123,
|
|
221
|
+
l: undefined,
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
type NormalizeOptions = {
|
|
226
|
+
/**
|
|
227
|
+
* What to do if a dot notation is used on a value that is not an object.
|
|
228
|
+
*
|
|
229
|
+
* - "throw" (default): Throw an error.
|
|
230
|
+
* - "ignore": Ignore the dot notation field.
|
|
231
|
+
*/
|
|
232
|
+
onDotIntoNonObject?: "throw" | "ignore",
|
|
233
|
+
/**
|
|
234
|
+
* What to do if a dot notation is used on a value that is null.
|
|
235
|
+
*
|
|
236
|
+
* - "like-non-object" (default): Treat it like a non-object. See `onDotIntoNonObject`.
|
|
237
|
+
* - "throw": Throw an error.
|
|
238
|
+
* - "ignore": Ignore the dot notation field.
|
|
239
|
+
* - "empty-object": Set the value to an empty object.
|
|
240
|
+
*/
|
|
241
|
+
onDotIntoNull?: "like-non-object" | "throw" | "ignore" | "empty-object",
|
|
242
|
+
/**
|
|
243
|
+
* If provided, keys that are silently dropped during normalization (when a dot-notation key
|
|
244
|
+
* references a parent that doesn't exist or is not an object and the behavior is "ignore")
|
|
245
|
+
* will be pushed to this array.
|
|
246
|
+
*/
|
|
247
|
+
droppedKeys?: string[],
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export class NormalizationError extends Error {
|
|
251
|
+
constructor(...args: ConstructorParameters<typeof Error>) {
|
|
252
|
+
super(...args);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
NormalizationError.prototype.name = "NormalizationError";
|
|
256
|
+
|
|
257
|
+
export function isNormalized(c: Config): c is NormalizedConfig {
|
|
258
|
+
assertValidConfig(c);
|
|
259
|
+
for (const [key, value] of Object.entries(c)) {
|
|
260
|
+
if (value === undefined) continue;
|
|
261
|
+
if (key.includes('.')) return false;
|
|
262
|
+
if (value === null) return false;
|
|
263
|
+
}
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function assertNormalized(c: Config): asserts c is NormalizedConfig {
|
|
268
|
+
assertValidConfig(c);
|
|
269
|
+
if (!isNormalized(c)) throw new HexclaveAssertionError(`Config is not normalized: ${JSON.stringify(c)}`);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function normalize(c: Config, options: NormalizeOptions = {}): NormalizedConfig {
|
|
273
|
+
assertValidConfig(c);
|
|
274
|
+
const onDotIntoNonObject = options.onDotIntoNonObject ?? "throw";
|
|
275
|
+
const onDotIntoNull = options.onDotIntoNull ?? "like-non-object";
|
|
276
|
+
|
|
277
|
+
const countDots = (s: string) => s.match(/\./g)?.length ?? 0;
|
|
278
|
+
const result: NormalizedConfig = {};
|
|
279
|
+
const keysByDepth = Object.keys(c).sort((a, b) => countDots(a) - countDots(b));
|
|
280
|
+
|
|
281
|
+
outer: for (const key of keysByDepth) {
|
|
282
|
+
const keySegmentsWithoutLast = key.split('.');
|
|
283
|
+
const last = keySegmentsWithoutLast.pop() ?? throwErr('split returns empty array?');
|
|
284
|
+
const value = get(c, key);
|
|
285
|
+
if (value === undefined) continue;
|
|
286
|
+
|
|
287
|
+
let current: NormalizedConfig = result;
|
|
288
|
+
for (const keySegment of keySegmentsWithoutLast) {
|
|
289
|
+
if (!hasAndNotUndefined(current, keySegment)) {
|
|
290
|
+
switch (onDotIntoNull === "like-non-object" ? onDotIntoNonObject : onDotIntoNull) {
|
|
291
|
+
case "throw": {
|
|
292
|
+
throw new NormalizationError(`Tried to use dot notation to access ${JSON.stringify(key)}, but ${JSON.stringify(keySegment)} doesn't exist on the object (or is null).`);
|
|
293
|
+
}
|
|
294
|
+
case "ignore": {
|
|
295
|
+
options.droppedKeys?.push(key);
|
|
296
|
+
continue outer;
|
|
297
|
+
}
|
|
298
|
+
case "empty-object": {
|
|
299
|
+
set(current, keySegment, {});
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const value = get(current, keySegment);
|
|
305
|
+
if (typeof value !== 'object') {
|
|
306
|
+
switch (onDotIntoNonObject) {
|
|
307
|
+
case "throw": {
|
|
308
|
+
throw new NormalizationError(`Tried to use dot notation to access ${JSON.stringify(key)}, but ${JSON.stringify(keySegment)} is not an object.`);
|
|
309
|
+
}
|
|
310
|
+
case "ignore": {
|
|
311
|
+
options.droppedKeys?.push(key);
|
|
312
|
+
continue outer;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
current = value as NormalizedConfig;
|
|
317
|
+
}
|
|
318
|
+
setNormalizedValue(current, last, value, options);
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function normalizeValue(value: ConfigValue, options: NormalizeOptions): NormalizedConfigValue {
|
|
324
|
+
if (value === null) throw new NormalizationError("Tried to normalize a null value");
|
|
325
|
+
if (Array.isArray(value)) return value.map(v => normalizeValue(v, options));
|
|
326
|
+
if (typeof value === 'object') return normalize(value, options);
|
|
327
|
+
return value;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function setNormalizedValue(result: NormalizedConfig, key: string, value: ConfigValue, options: NormalizeOptions) {
|
|
331
|
+
if (value === null) {
|
|
332
|
+
if (hasAndNotUndefined(result, key)) {
|
|
333
|
+
deleteKey(result, key);
|
|
334
|
+
}
|
|
335
|
+
} else {
|
|
336
|
+
set(result, key, normalizeValue(value, options));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
import.meta.vitest?.test("normalize(...)", ({ expect }) => {
|
|
341
|
+
expect(normalize({
|
|
342
|
+
a: 9,
|
|
343
|
+
b: 2,
|
|
344
|
+
c: {},
|
|
345
|
+
"c.d": 10,
|
|
346
|
+
"c.e": null,
|
|
347
|
+
"c.g": 5,
|
|
348
|
+
h: [6, { i: 7 }, 8],
|
|
349
|
+
"h.0": 11,
|
|
350
|
+
"h.1": {
|
|
351
|
+
j: 12,
|
|
352
|
+
},
|
|
353
|
+
k: { l: {} },
|
|
354
|
+
"k.l.m": 13,
|
|
355
|
+
n: undefined,
|
|
356
|
+
}, { onDotIntoNonObject: "ignore" })).toEqual({
|
|
357
|
+
a: 9,
|
|
358
|
+
b: 2,
|
|
359
|
+
c: {
|
|
360
|
+
d: 10,
|
|
361
|
+
g: 5,
|
|
362
|
+
},
|
|
363
|
+
h: [11, { j: 12 }, 8],
|
|
364
|
+
k: { l: { m: 13 } },
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
// dotting into null
|
|
368
|
+
expect(() => normalize({
|
|
369
|
+
"b.c": 2,
|
|
370
|
+
}, { onDotIntoNonObject: "throw" })).toThrow(`Tried to use dot notation to access "b.c", but "b" doesn't exist on the object (or is null)`);
|
|
371
|
+
expect(() => normalize({
|
|
372
|
+
b: null,
|
|
373
|
+
"b.c": 2,
|
|
374
|
+
}, { onDotIntoNonObject: "throw" })).toThrow(`Tried to use dot notation to access "b.c", but "b" doesn't exist on the object (or is null)`);
|
|
375
|
+
expect(normalize({
|
|
376
|
+
"b.c": 2,
|
|
377
|
+
}, { onDotIntoNonObject: "ignore" })).toEqual({});
|
|
378
|
+
|
|
379
|
+
// dotting into non-object
|
|
380
|
+
expect(() => normalize({
|
|
381
|
+
b: 1,
|
|
382
|
+
"b.c": 2,
|
|
383
|
+
}, { onDotIntoNonObject: "throw" })).toThrow(`Tried to use dot notation to access "b.c", but "b" is not an object`);
|
|
384
|
+
expect(normalize({
|
|
385
|
+
b: 1,
|
|
386
|
+
"b.c": 2,
|
|
387
|
+
}, { onDotIntoNonObject: "ignore" })).toEqual({ b: 1 });
|
|
388
|
+
});
|