@async/db 0.2.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.
- package/CHANGELOG.md +167 -0
- package/README.md +431 -0
- package/SPEC.md +1429 -0
- package/db.config.example.mjs +128 -0
- package/dist/cli/args.d.ts +8 -0
- package/dist/cli/args.js +16 -0
- package/dist/cli/commands/create.d.ts +3 -0
- package/dist/cli/commands/create.js +13 -0
- package/dist/cli/commands/doctor.d.ts +3 -0
- package/dist/cli/commands/doctor.js +31 -0
- package/dist/cli/commands/generate.d.ts +6 -0
- package/dist/cli/commands/generate.js +24 -0
- package/dist/cli/commands/operations.d.ts +12 -0
- package/dist/cli/commands/operations.js +61 -0
- package/dist/cli/commands/schema.d.ts +11 -0
- package/dist/cli/commands/schema.js +1086 -0
- package/dist/cli/commands/serve.d.ts +9 -0
- package/dist/cli/commands/serve.js +18 -0
- package/dist/cli/commands/sync.d.ts +3 -0
- package/dist/cli/commands/sync.js +11 -0
- package/dist/cli/commands/types.d.ts +7 -0
- package/dist/cli/commands/types.js +37 -0
- package/dist/cli/commands/viewer.d.ts +6 -0
- package/dist/cli/commands/viewer.js +29 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +108 -0
- package/dist/cli/output.d.ts +25 -0
- package/dist/cli/output.js +149 -0
- package/dist/cli/schema-prompt.d.ts +20 -0
- package/dist/cli/schema-prompt.js +66 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +3 -0
- package/dist/client-cache.d.ts +105 -0
- package/dist/client-cache.js +916 -0
- package/dist/client.d.ts +64 -0
- package/dist/client.js +405 -0
- package/dist/config-public.d.ts +1 -0
- package/dist/config-public.js +1 -0
- package/dist/config.d.ts +54 -0
- package/dist/config.js +2 -0
- package/dist/csv.d.ts +1 -0
- package/dist/csv.js +1 -0
- package/dist/db.d.ts +3 -0
- package/dist/db.js +3 -0
- package/dist/doctor.d.ts +1 -0
- package/dist/doctor.js +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +1 -0
- package/dist/features/config/defaults.d.ts +98 -0
- package/dist/features/config/defaults.js +95 -0
- package/dist/features/config/load.d.ts +11 -0
- package/dist/features/config/load.js +265 -0
- package/dist/features/config/public.d.ts +17 -0
- package/dist/features/config/public.js +75 -0
- package/dist/features/doctor/duplicate-ids.d.ts +18 -0
- package/dist/features/doctor/duplicate-ids.js +79 -0
- package/dist/features/doctor/field-consistency.d.ts +17 -0
- package/dist/features/doctor/field-consistency.js +48 -0
- package/dist/features/doctor/index.d.ts +39 -0
- package/dist/features/doctor/index.js +177 -0
- package/dist/features/doctor/relations.d.ts +22 -0
- package/dist/features/doctor/relations.js +90 -0
- package/dist/features/doctor/schema-guidance.d.ts +35 -0
- package/dist/features/doctor/schema-guidance.js +184 -0
- package/dist/features/generate/registry.d.ts +14 -0
- package/dist/features/generate/registry.js +37 -0
- package/dist/features/http/registry.d.ts +46 -0
- package/dist/features/http/registry.js +86 -0
- package/dist/features/operations/index.d.ts +49 -0
- package/dist/features/operations/index.js +199 -0
- package/dist/features/operations/maps.d.ts +1 -0
- package/dist/features/operations/maps.js +10 -0
- package/dist/features/operations/readiness.d.ts +30 -0
- package/dist/features/operations/readiness.js +228 -0
- package/dist/features/operations/runtime.d.ts +57 -0
- package/dist/features/operations/runtime.js +288 -0
- package/dist/features/runtime/collection.d.ts +51 -0
- package/dist/features/runtime/collection.js +198 -0
- package/dist/features/runtime/db.d.ts +152 -0
- package/dist/features/runtime/db.js +824 -0
- package/dist/features/runtime/document.d.ts +43 -0
- package/dist/features/runtime/document.js +111 -0
- package/dist/features/runtime/fanout.d.ts +24 -0
- package/dist/features/runtime/fanout.js +77 -0
- package/dist/features/runtime/json-pointer.d.ts +5 -0
- package/dist/features/runtime/json-pointer.js +49 -0
- package/dist/features/runtime/scope-state.d.ts +44 -0
- package/dist/features/runtime/scope-state.js +185 -0
- package/dist/features/runtime/state.d.ts +1 -0
- package/dist/features/runtime/state.js +1 -0
- package/dist/features/schema/api.d.ts +107 -0
- package/dist/features/schema/api.js +460 -0
- package/dist/features/schema/builders.d.ts +86 -0
- package/dist/features/schema/builders.js +110 -0
- package/dist/features/schema/fields.d.ts +38 -0
- package/dist/features/schema/fields.js +296 -0
- package/dist/features/schema/generated.d.ts +29 -0
- package/dist/features/schema/generated.js +32 -0
- package/dist/features/schema/locator.d.ts +16 -0
- package/dist/features/schema/locator.js +135 -0
- package/dist/features/schema/manifest.d.ts +91 -0
- package/dist/features/schema/manifest.js +384 -0
- package/dist/features/schema/metadata.d.ts +30 -0
- package/dist/features/schema/metadata.js +75 -0
- package/dist/features/schema/project.d.ts +46 -0
- package/dist/features/schema/project.js +442 -0
- package/dist/features/schema/relations.d.ts +38 -0
- package/dist/features/schema/relations.js +109 -0
- package/dist/features/schema/resolvers.d.ts +36 -0
- package/dist/features/schema/resolvers.js +111 -0
- package/dist/features/schema/resource.d.ts +75 -0
- package/dist/features/schema/resource.js +253 -0
- package/dist/features/schema/source-definitions.d.ts +21 -0
- package/dist/features/schema/source-definitions.js +29 -0
- package/dist/features/schema/sources.d.ts +83 -0
- package/dist/features/schema/sources.js +689 -0
- package/dist/features/schema/standard-schema.d.ts +57 -0
- package/dist/features/schema/standard-schema.js +232 -0
- package/dist/features/schema/validation.d.ts +69 -0
- package/dist/features/schema/validation.js +434 -0
- package/dist/features/storage/events.d.ts +12 -0
- package/dist/features/storage/events.js +30 -0
- package/dist/features/storage/json.d.ts +112 -0
- package/dist/features/storage/json.js +239 -0
- package/dist/features/storage/memory.d.ts +30 -0
- package/dist/features/storage/memory.js +44 -0
- package/dist/features/storage/resource-json.d.ts +31 -0
- package/dist/features/storage/resource-json.js +76 -0
- package/dist/features/storage/runtime.d.ts +37 -0
- package/dist/features/storage/runtime.js +184 -0
- package/dist/features/storage/source-metadata.d.ts +20 -0
- package/dist/features/storage/source-metadata.js +25 -0
- package/dist/features/storage/source.d.ts +37 -0
- package/dist/features/storage/source.js +60 -0
- package/dist/features/storage/static.d.ts +29 -0
- package/dist/features/storage/static.js +42 -0
- package/dist/features/sync/defaults.d.ts +21 -0
- package/dist/features/sync/defaults.js +21 -0
- package/dist/features/sync/index.d.ts +35 -0
- package/dist/features/sync/index.js +85 -0
- package/dist/features/sync/mirror-state.d.ts +14 -0
- package/dist/features/sync/mirror-state.js +4 -0
- package/dist/features/sync/runtime-dirs.d.ts +5 -0
- package/dist/features/sync/runtime-dirs.js +9 -0
- package/dist/features/sync/source-writes.d.ts +15 -0
- package/dist/features/sync/source-writes.js +27 -0
- package/dist/features/sync/synthetic-seed.d.ts +26 -0
- package/dist/features/sync/synthetic-seed.js +83 -0
- package/dist/features/viewer/manifest.d.ts +148 -0
- package/dist/features/viewer/manifest.js +165 -0
- package/dist/fs-utils.d.ts +1 -0
- package/dist/fs-utils.js +1 -0
- package/dist/generate/hono/app.d.ts +6 -0
- package/dist/generate/hono/app.js +51 -0
- package/dist/generate/hono/graphql.d.ts +7 -0
- package/dist/generate/hono/graphql.js +53 -0
- package/dist/generate/hono/index.d.ts +55 -0
- package/dist/generate/hono/index.js +140 -0
- package/dist/generate/hono/package.d.ts +6 -0
- package/dist/generate/hono/package.js +44 -0
- package/dist/generate/hono/readme.d.ts +13 -0
- package/dist/generate/hono/readme.js +28 -0
- package/dist/generate/hono/repository.d.ts +1 -0
- package/dist/generate/hono/repository.js +27 -0
- package/dist/generate/hono/rest.d.ts +1 -0
- package/dist/generate/hono/rest.js +38 -0
- package/dist/generate/hono/schema.d.ts +13 -0
- package/dist/generate/hono/schema.js +18 -0
- package/dist/generate/hono/sqlite.d.ts +20 -0
- package/dist/generate/hono/sqlite.js +266 -0
- package/dist/generate/hono/validators.d.ts +1 -0
- package/dist/generate/hono/validators.js +141 -0
- package/dist/generate/hono.d.ts +1 -0
- package/dist/generate/hono.js +1 -0
- package/dist/graphql/execute.d.ts +14 -0
- package/dist/graphql/execute.js +719 -0
- package/dist/graphql/http.d.ts +15 -0
- package/dist/graphql/http.js +29 -0
- package/dist/graphql/index.d.ts +3 -0
- package/dist/graphql/index.js +3 -0
- package/dist/graphql/parser.d.ts +54 -0
- package/dist/graphql/parser.js +433 -0
- package/dist/hono.d.ts +77 -0
- package/dist/hono.js +1 -0
- package/dist/index.d.ts +1065 -0
- package/dist/index.js +14 -0
- package/dist/integrations/hono.d.ts +136 -0
- package/dist/integrations/hono.js +508 -0
- package/dist/integrations/kv.d.ts +69 -0
- package/dist/integrations/kv.js +69 -0
- package/dist/integrations/postgres.d.ts +52 -0
- package/dist/integrations/postgres.js +113 -0
- package/dist/integrations/sqlite.d.ts +112 -0
- package/dist/integrations/sqlite.js +489 -0
- package/dist/integrations/vite.d.ts +45 -0
- package/dist/integrations/vite.js +111 -0
- package/dist/json.d.ts +48 -0
- package/dist/json.js +1 -0
- package/dist/jsonc.d.ts +1 -0
- package/dist/jsonc.js +1 -0
- package/dist/kv.d.ts +24 -0
- package/dist/kv.js +1 -0
- package/dist/mock.d.ts +1 -0
- package/dist/mock.js +1 -0
- package/dist/names.d.ts +1 -0
- package/dist/names.js +1 -0
- package/dist/operations.d.ts +3 -0
- package/dist/operations.js +3 -0
- package/dist/postgres.d.ts +24 -0
- package/dist/postgres.js +1 -0
- package/dist/redis.d.ts +14 -0
- package/dist/redis.js +1 -0
- package/dist/rest/formats.d.ts +80 -0
- package/dist/rest/formats.js +318 -0
- package/dist/rest/handler.d.ts +111 -0
- package/dist/rest/handler.js +833 -0
- package/dist/rest/shape.d.ts +33 -0
- package/dist/rest/shape.js +218 -0
- package/dist/schema-builders.d.ts +1 -0
- package/dist/schema-builders.js +1 -0
- package/dist/schema-manifest.d.ts +1 -0
- package/dist/schema-manifest.js +1 -0
- package/dist/schema.d.ts +193 -0
- package/dist/schema.js +6 -0
- package/dist/server.d.ts +116 -0
- package/dist/server.js +601 -0
- package/dist/shared/csv.d.ts +8 -0
- package/dist/shared/csv.js +149 -0
- package/dist/shared/errors.d.ts +40 -0
- package/dist/shared/errors.js +55 -0
- package/dist/shared/fs-utils.d.ts +4 -0
- package/dist/shared/fs-utils.js +30 -0
- package/dist/shared/jsonc.d.ts +2 -0
- package/dist/shared/jsonc.js +99 -0
- package/dist/shared/mock.d.ts +40 -0
- package/dist/shared/mock.js +83 -0
- package/dist/shared/names.d.ts +28 -0
- package/dist/shared/names.js +127 -0
- package/dist/shared/operations.d.ts +32 -0
- package/dist/shared/operations.js +302 -0
- package/dist/sqlite.d.ts +24 -0
- package/dist/sqlite.js +1 -0
- package/dist/state.d.ts +1 -0
- package/dist/state.js +1 -0
- package/dist/sync.d.ts +1 -0
- package/dist/sync.js +1 -0
- package/dist/tracing.d.ts +95 -0
- package/dist/tracing.js +260 -0
- package/dist/types.d.ts +51 -0
- package/dist/types.js +285 -0
- package/dist/viewer-manifest.d.ts +1 -0
- package/dist/viewer-manifest.js +1 -0
- package/dist/vite.d.ts +59 -0
- package/dist/vite.js +1 -0
- package/dist/web/json-viewer.d.ts +5 -0
- package/dist/web/json-viewer.js +176 -0
- package/dist/web/viewer.d.ts +12 -0
- package/dist/web/viewer.js +1015 -0
- package/docs/README.md +42 -0
- package/docs/architecture.md +112 -0
- package/docs/ci-and-release.md +177 -0
- package/docs/cms-storage-patterns.md +108 -0
- package/docs/concepts.md +141 -0
- package/docs/configuration.md +552 -0
- package/docs/fixtures-and-schemas.md +527 -0
- package/docs/fork-branch-workflows.md +108 -0
- package/docs/generated-files.md +174 -0
- package/docs/getting-started.md +165 -0
- package/docs/integrations.md +206 -0
- package/docs/json-production.md +120 -0
- package/docs/package-api.md +418 -0
- package/docs/prototype-to-production.md +378 -0
- package/docs/server-and-viewer.md +466 -0
- package/docs/store-graduation.md +120 -0
- package/docs/typescript-schema-sources.md +79 -0
- package/examples/advanced/README.md +55 -0
- package/examples/advanced/db/projects.schema.jsonc +44 -0
- package/examples/advanced/db/settings.jsonc +9 -0
- package/examples/advanced/db/users.json +23 -0
- package/examples/advanced/db/users.schema.mjs +31 -0
- package/examples/advanced/db.config.mjs +18 -0
- package/examples/advanced/example.json +5 -0
- package/examples/advanced/src/generated/db.types.d.ts +64 -0
- package/examples/basic/README.md +95 -0
- package/examples/basic/db/operations/get-user.jsonc +8 -0
- package/examples/basic/db/settings.json +7 -0
- package/examples/basic/db/users.schema.jsonc +36 -0
- package/examples/basic/db.config.mjs +68 -0
- package/examples/basic/example.json +5 -0
- package/examples/basic/src/generated/db.types.d.ts +39 -0
- package/examples/cms-json-publish/README.md +21 -0
- package/examples/cms-json-publish/db/navigation.json +7 -0
- package/examples/cms-json-publish/db/pages.json +18 -0
- package/examples/cms-json-publish/example.json +5 -0
- package/examples/cms-json-publish/src/cms.mjs +104 -0
- package/examples/computed-fields/README.md +93 -0
- package/examples/computed-fields/db/orders.schema.mjs +62 -0
- package/examples/computed-fields/db/posts.schema.mjs +59 -0
- package/examples/computed-fields/db/products.schema.mjs +39 -0
- package/examples/computed-fields/db/users.schema.mjs +43 -0
- package/examples/computed-fields/db.config.mjs +15 -0
- package/examples/computed-fields/example.json +5 -0
- package/examples/computed-fields/src/generated/db.types.d.ts +81 -0
- package/examples/content-collections/README.md +91 -0
- package/examples/content-collections/db/authors.json +12 -0
- package/examples/content-collections/db/authors.schema.mjs +20 -0
- package/examples/content-collections/db/blog/draft-roadmap.mdx +12 -0
- package/examples/content-collections/db/blog/index.schema.mjs +61 -0
- package/examples/content-collections/db/blog/launch-notes.mdx +15 -0
- package/examples/content-collections/db/docs/index.schema.mjs +32 -0
- package/examples/content-collections/db/docs/intro.mdx +11 -0
- package/examples/content-collections/db/docs/schema-workflow.mdx +10 -0
- package/examples/content-collections/db/site.schema.jsonc +21 -0
- package/examples/content-collections/db.config.mjs +26 -0
- package/examples/content-collections/example.json +5 -0
- package/examples/content-collections/src/content-preview.mjs +66 -0
- package/examples/content-collections/src/generated/db.types.d.ts +81 -0
- package/examples/csv/README.md +52 -0
- package/examples/csv/db/customers.csv +4 -0
- package/examples/csv/db.config.mjs +13 -0
- package/examples/csv/example.json +5 -0
- package/examples/data-first/README.md +54 -0
- package/examples/data-first/db/posts.json +16 -0
- package/examples/data-first/db/settings.json +8 -0
- package/examples/data-first/db/users.json +14 -0
- package/examples/data-first/db.config.mjs +13 -0
- package/examples/data-first/example.json +5 -0
- package/examples/diagnostics/README.md +55 -0
- package/examples/diagnostics/db/projects.schema.jsonc +27 -0
- package/examples/diagnostics/db/users.json +9 -0
- package/examples/diagnostics/db/users.schema.jsonc +23 -0
- package/examples/diagnostics/db.config.mjs +16 -0
- package/examples/diagnostics/example.json +5 -0
- package/examples/free-plan-upgrade/README.md +22 -0
- package/examples/free-plan-upgrade/db/appSettings.json +4 -0
- package/examples/free-plan-upgrade/db/projects.json +7 -0
- package/examples/free-plan-upgrade/example.json +5 -0
- package/examples/free-plan-upgrade/src/upgrade-tenant-to-paid.mjs +105 -0
- package/examples/hono-auth/README.md +74 -0
- package/examples/hono-auth/db/pages.schema.jsonc +44 -0
- package/examples/hono-auth/db/users.schema.jsonc +42 -0
- package/examples/hono-auth/db.config.mjs +17 -0
- package/examples/hono-auth/example.json +5 -0
- package/examples/hono-auth/package.json +14 -0
- package/examples/hono-auth/src/app.mjs +79 -0
- package/examples/hono-auth/src/server.mjs +13 -0
- package/examples/production-json/README.md +102 -0
- package/examples/production-json/db/appSettings.schema.jsonc +41 -0
- package/examples/production-json/db/featureFlags.schema.jsonc +84 -0
- package/examples/production-json/db/operations/get-control-plane.jsonc +6 -0
- package/examples/production-json/db/operations/get-feature-flag.jsonc +9 -0
- package/examples/production-json/db/operations/list-feature-flags.jsonc +8 -0
- package/examples/production-json/db/operations/read-public-settings.jsonc +8 -0
- package/examples/production-json/db.config.mjs +33 -0
- package/examples/production-json/example.json +5 -0
- package/examples/production-json/src/client-demo.mjs +28 -0
- package/examples/production-json/src/generated/db.types.d.ts +60 -0
- package/examples/relations/README.md +56 -0
- package/examples/relations/db/posts.schema.jsonc +46 -0
- package/examples/relations/db/users.schema.jsonc +34 -0
- package/examples/relations/db.config.mjs +13 -0
- package/examples/relations/example.json +5 -0
- package/examples/rest-client/README.md +54 -0
- package/examples/rest-client/db/settings.json +5 -0
- package/examples/rest-client/db/users.schema.jsonc +42 -0
- package/examples/rest-client/db.config.mjs +13 -0
- package/examples/rest-client/example.json +5 -0
- package/examples/rest-client/src/client-demo.mjs +24 -0
- package/examples/schema-first/README.md +55 -0
- package/examples/schema-first/db/auditEvents.schema.jsonc +24 -0
- package/examples/schema-first/db/settings.schema.jsonc +29 -0
- package/examples/schema-first/db/users.schema.jsonc +36 -0
- package/examples/schema-first/db.config.mjs +15 -0
- package/examples/schema-first/example.json +5 -0
- package/examples/schema-first/src/generated/db.types.d.ts +47 -0
- package/examples/schema-manifest/README.md +50 -0
- package/examples/schema-manifest/db/projects.schema.jsonc +48 -0
- package/examples/schema-manifest/db/users.schema.jsonc +35 -0
- package/examples/schema-manifest/db.config.mjs +41 -0
- package/examples/schema-manifest/example.json +5 -0
- package/examples/schema-manifest/src/generated/db.schema.json +130 -0
- package/examples/schema-manifest/src/generated/db.types.d.ts +50 -0
- package/examples/schema-ui/README.md +103 -0
- package/examples/schema-ui/db/pages.schema.jsonc +53 -0
- package/examples/schema-ui/db/users.schema.jsonc +30 -0
- package/examples/schema-ui/db.config.mjs +55 -0
- package/examples/schema-ui/example.json +5 -0
- package/examples/schema-ui/src/cms-ssr.mjs +276 -0
- package/examples/schema-ui/src/generated/db.schema.json +133 -0
- package/examples/schema-ui/src/generated/db.types.d.ts +46 -0
- package/examples/schema-ui/src/render-admin.mjs +175 -0
- package/examples/schema-ui/src/schema-ui-ssr-handler.mjs +149 -0
- package/examples/schema-ui/src/start-schema-ui-server.mjs +140 -0
- package/examples/standard-schema/README.md +55 -0
- package/examples/standard-schema/db/settings.schema.mjs +22 -0
- package/examples/standard-schema/db/users.schema.mjs +72 -0
- package/examples/standard-schema/example.json +5 -0
- package/package.json +108 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
type GeneratedTypesConfig = {
|
|
2
|
+
cwd: string;
|
|
3
|
+
types?: {
|
|
4
|
+
enabled?: boolean;
|
|
5
|
+
emitComments?: boolean;
|
|
6
|
+
useReadonly?: boolean;
|
|
7
|
+
exportRuntimeHelpers?: boolean;
|
|
8
|
+
outFile?: string | null;
|
|
9
|
+
commitOutFile?: string | null;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
};
|
|
14
|
+
type SchemaField = {
|
|
15
|
+
type?: string;
|
|
16
|
+
required?: boolean;
|
|
17
|
+
nullable?: boolean;
|
|
18
|
+
description?: string;
|
|
19
|
+
values?: unknown[];
|
|
20
|
+
items?: SchemaField;
|
|
21
|
+
fields?: Record<string, SchemaField>;
|
|
22
|
+
variants?: Record<string, SchemaVariant>;
|
|
23
|
+
additionalProperties?: boolean;
|
|
24
|
+
[key: string]: unknown;
|
|
25
|
+
};
|
|
26
|
+
type SchemaVariant = {
|
|
27
|
+
fields?: Record<string, SchemaField>;
|
|
28
|
+
additionalProperties?: boolean;
|
|
29
|
+
[key: string]: unknown;
|
|
30
|
+
};
|
|
31
|
+
type SchemaResource = {
|
|
32
|
+
name: string;
|
|
33
|
+
kind: string;
|
|
34
|
+
typeName: string;
|
|
35
|
+
fields: Record<string, SchemaField>;
|
|
36
|
+
description?: string;
|
|
37
|
+
typeFallback?: string;
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
};
|
|
40
|
+
type GenerateTypesOptions = {
|
|
41
|
+
project?: unknown;
|
|
42
|
+
outFile?: string;
|
|
43
|
+
};
|
|
44
|
+
type GenerateTypesResult = {
|
|
45
|
+
content: string;
|
|
46
|
+
outFiles: string[];
|
|
47
|
+
diagnostics: unknown[];
|
|
48
|
+
};
|
|
49
|
+
export declare function generateTypes(config: GeneratedTypesConfig, options?: GenerateTypesOptions): Promise<GenerateTypesResult>;
|
|
50
|
+
export declare function renderTypes(resources: SchemaResource[], config: GeneratedTypesConfig): string;
|
|
51
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { writeText } from './fs-utils.js';
|
|
3
|
+
import { pascalCase } from './names.js';
|
|
4
|
+
import { loadProjectSchema } from './schema.js';
|
|
5
|
+
export async function generateTypes(config, options = {}) {
|
|
6
|
+
const project = (options.project ?? await loadProjectSchema(config));
|
|
7
|
+
const content = renderTypes(project.resources, config);
|
|
8
|
+
const outFiles = outputFiles(config, options);
|
|
9
|
+
for (const outFile of outFiles) {
|
|
10
|
+
await writeText(outFile, content);
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
content,
|
|
14
|
+
outFiles,
|
|
15
|
+
diagnostics: project.diagnostics,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export function renderTypes(resources, config) {
|
|
19
|
+
const lines = [
|
|
20
|
+
'/* eslint-disable */',
|
|
21
|
+
'// This file is generated by db. Do not edit by hand.',
|
|
22
|
+
'',
|
|
23
|
+
];
|
|
24
|
+
const enumAliases = collectEnumAliases(resources);
|
|
25
|
+
for (const alias of enumAliases) {
|
|
26
|
+
lines.push(renderEnumAlias(alias), '');
|
|
27
|
+
}
|
|
28
|
+
const variantAliases = collectVariantAliases(resources);
|
|
29
|
+
for (const alias of variantAliases) {
|
|
30
|
+
lines.push(...renderVariantAlias(alias, config), '');
|
|
31
|
+
}
|
|
32
|
+
for (const resource of resources) {
|
|
33
|
+
if (resource.description && config.types?.emitComments !== false) {
|
|
34
|
+
lines.push(jsDoc(resource.description));
|
|
35
|
+
}
|
|
36
|
+
lines.push(`export type ${resource.typeName} = {`);
|
|
37
|
+
const fieldEntries = Object.entries(resource.fields ?? {});
|
|
38
|
+
if (fieldEntries.length === 0 && resource.typeFallback === 'record') {
|
|
39
|
+
lines.push(' [key: string]: unknown;');
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
for (const [fieldName, field] of fieldEntries) {
|
|
43
|
+
const fieldLines = renderField(fieldName, field, resource, config, 1);
|
|
44
|
+
lines.push(...fieldLines);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
lines.push('};', '');
|
|
48
|
+
}
|
|
49
|
+
const collectionResources = resources.filter((resource) => resource.kind === 'collection');
|
|
50
|
+
const documentResources = resources.filter((resource) => resource.kind === 'document');
|
|
51
|
+
lines.push('export type DbCollections = {');
|
|
52
|
+
for (const resource of collectionResources) {
|
|
53
|
+
lines.push(` ${propertyName(resource.name)}: ${resource.typeName};`);
|
|
54
|
+
}
|
|
55
|
+
lines.push('};', '');
|
|
56
|
+
lines.push('export type DbDocuments = {');
|
|
57
|
+
for (const resource of documentResources) {
|
|
58
|
+
lines.push(` ${propertyName(resource.name)}: ${resource.typeName};`);
|
|
59
|
+
}
|
|
60
|
+
lines.push('};', '');
|
|
61
|
+
lines.push('export type DbTypes = {');
|
|
62
|
+
lines.push(' collections: DbCollections;');
|
|
63
|
+
lines.push(' documents: DbDocuments;');
|
|
64
|
+
lines.push('};');
|
|
65
|
+
if (config.types?.exportRuntimeHelpers !== false) {
|
|
66
|
+
lines.push('', 'export type DbCollectionName = keyof DbCollections;', 'export type DbDocumentName = keyof DbDocuments;');
|
|
67
|
+
}
|
|
68
|
+
return `${lines.join('\n')}\n`;
|
|
69
|
+
}
|
|
70
|
+
function outputFiles(config, options) {
|
|
71
|
+
const outFiles = [];
|
|
72
|
+
if (options.outFile) {
|
|
73
|
+
outFiles.push(path.resolve(config.cwd, options.outFile));
|
|
74
|
+
return unique(outFiles);
|
|
75
|
+
}
|
|
76
|
+
if (config.types?.outFile) {
|
|
77
|
+
outFiles.push(config.types.outFile);
|
|
78
|
+
}
|
|
79
|
+
if (config.types?.commitOutFile) {
|
|
80
|
+
outFiles.push(config.types.commitOutFile);
|
|
81
|
+
}
|
|
82
|
+
return unique(outFiles);
|
|
83
|
+
}
|
|
84
|
+
function renderField(fieldName, field, resource, config, depth, fieldPath = fieldName) {
|
|
85
|
+
const lines = [];
|
|
86
|
+
const indent = ' '.repeat(depth);
|
|
87
|
+
if (field.description && config.types?.emitComments !== false) {
|
|
88
|
+
lines.push(`${indent}${jsDoc(field.description, depth)}`);
|
|
89
|
+
}
|
|
90
|
+
const readonly = config.types?.useReadonly ? 'readonly ' : '';
|
|
91
|
+
const optional = field.required ? '' : '?';
|
|
92
|
+
lines.push(`${indent}${readonly}${propertyName(fieldName)}${optional}: ${typeForField(field, fieldPath, resource, config, depth)};`);
|
|
93
|
+
return lines;
|
|
94
|
+
}
|
|
95
|
+
function typeForField(field, fieldPath, resource, config, depth) {
|
|
96
|
+
let type;
|
|
97
|
+
switch (field.type) {
|
|
98
|
+
case 'string':
|
|
99
|
+
case 'datetime':
|
|
100
|
+
type = 'string';
|
|
101
|
+
break;
|
|
102
|
+
case 'number':
|
|
103
|
+
type = 'number';
|
|
104
|
+
break;
|
|
105
|
+
case 'boolean':
|
|
106
|
+
type = 'boolean';
|
|
107
|
+
break;
|
|
108
|
+
case 'enum':
|
|
109
|
+
type = enumAliasName(resource, fieldPath);
|
|
110
|
+
break;
|
|
111
|
+
case 'array':
|
|
112
|
+
type = `Array<${typeForField(field.items ?? { type: 'unknown' }, `${fieldPath}Item`, resource, config, depth)}>`;
|
|
113
|
+
break;
|
|
114
|
+
case 'object':
|
|
115
|
+
type = field.variants
|
|
116
|
+
? variantAliasName(resource, fieldPath)
|
|
117
|
+
: objectType(field, resource, config, depth, fieldPath);
|
|
118
|
+
break;
|
|
119
|
+
case 'unknown':
|
|
120
|
+
default:
|
|
121
|
+
type = 'unknown';
|
|
122
|
+
}
|
|
123
|
+
return field.nullable ? `${type} | null` : type;
|
|
124
|
+
}
|
|
125
|
+
function objectType(field, resource, config, depth, fieldPath) {
|
|
126
|
+
const fields = field.fields ?? {};
|
|
127
|
+
const entries = Object.entries(fields);
|
|
128
|
+
if (entries.length === 0) {
|
|
129
|
+
return 'Record<string, unknown>';
|
|
130
|
+
}
|
|
131
|
+
const indent = ' '.repeat(depth);
|
|
132
|
+
const childIndent = ' '.repeat(depth + 1);
|
|
133
|
+
const readonly = config.types?.useReadonly ? 'readonly ' : '';
|
|
134
|
+
const lines = ['{'];
|
|
135
|
+
for (const [fieldName, childField] of entries) {
|
|
136
|
+
if (childField.description && config.types?.emitComments !== false) {
|
|
137
|
+
lines.push(`${childIndent}${jsDoc(childField.description, depth + 1)}`);
|
|
138
|
+
}
|
|
139
|
+
const optional = childField.required ? '' : '?';
|
|
140
|
+
lines.push(`${childIndent}${readonly}${propertyName(fieldName)}${optional}: ${typeForField(childField, `${fieldPath}${pascalCase(fieldName)}`, resource, config, depth + 1)};`);
|
|
141
|
+
}
|
|
142
|
+
if (field.additionalProperties === true) {
|
|
143
|
+
lines.push(`${childIndent}[key: string]: unknown;`);
|
|
144
|
+
}
|
|
145
|
+
lines.push(`${indent}}`);
|
|
146
|
+
return lines.join('\n');
|
|
147
|
+
}
|
|
148
|
+
function renderVariantAlias(alias, config) {
|
|
149
|
+
const lines = [`export type ${alias.name} =`];
|
|
150
|
+
const entries = Object.entries(alias.variants);
|
|
151
|
+
for (const [index, [variantName, variant]] of entries.entries()) {
|
|
152
|
+
const prefix = index === 0 ? ' |' : ' |';
|
|
153
|
+
lines.push(`${prefix} ${variantObjectType(variantName, variant, alias.resource, config, alias.fieldPath, 2)}`);
|
|
154
|
+
}
|
|
155
|
+
return [`${lines.join('\n')};`];
|
|
156
|
+
}
|
|
157
|
+
function renderEnumAlias(alias) {
|
|
158
|
+
const values = alias.values.map((value) => literalType(value));
|
|
159
|
+
if (values.length <= 3) {
|
|
160
|
+
return `export type ${alias.name} = ${values.join(' | ')};`;
|
|
161
|
+
}
|
|
162
|
+
return [
|
|
163
|
+
`export type ${alias.name} =`,
|
|
164
|
+
...values.map((value) => ` | ${value}`),
|
|
165
|
+
].join('\n') + ';';
|
|
166
|
+
}
|
|
167
|
+
function variantObjectType(variantName, variant, resource, config, fieldPath, depth) {
|
|
168
|
+
const fields = variant.fields ?? {};
|
|
169
|
+
const indent = ' '.repeat(depth);
|
|
170
|
+
const childIndent = ' '.repeat(depth + 1);
|
|
171
|
+
const readonly = config.types?.useReadonly ? 'readonly ' : '';
|
|
172
|
+
const lines = ['{'];
|
|
173
|
+
for (const [fieldName, childField] of Object.entries(fields)) {
|
|
174
|
+
if (childField.description && config.types?.emitComments !== false) {
|
|
175
|
+
lines.push(`${childIndent}${jsDoc(childField.description, depth + 1)}`);
|
|
176
|
+
}
|
|
177
|
+
const optional = childField.required ? '' : '?';
|
|
178
|
+
const childPath = `${fieldPath}${pascalCase(fieldName)}`;
|
|
179
|
+
lines.push(`${childIndent}${readonly}${propertyName(fieldName)}${optional}: ${variantFieldType(childField, fieldName, variantName, resource, config, depth + 1, childPath)};`);
|
|
180
|
+
}
|
|
181
|
+
if (variant.additionalProperties === true) {
|
|
182
|
+
lines.push(`${childIndent}[key: string]: unknown;`);
|
|
183
|
+
}
|
|
184
|
+
lines.push(`${indent}}`);
|
|
185
|
+
return lines.join('\n');
|
|
186
|
+
}
|
|
187
|
+
function variantFieldType(field, fieldName, variantName, resource, config, depth, fieldPath) {
|
|
188
|
+
if (field.type === 'enum' && field.values?.length === 1 && field.values[0] === variantName) {
|
|
189
|
+
return literalType(variantName);
|
|
190
|
+
}
|
|
191
|
+
return typeForField(field, fieldPath, resource, config, depth);
|
|
192
|
+
}
|
|
193
|
+
function collectEnumAliases(resources) {
|
|
194
|
+
const aliases = new Map();
|
|
195
|
+
for (const resource of resources) {
|
|
196
|
+
for (const [fieldName, field] of Object.entries(resource.fields)) {
|
|
197
|
+
collectFieldEnumAliases(aliases, resource, fieldName, field);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return [...aliases.values()];
|
|
201
|
+
}
|
|
202
|
+
function collectFieldEnumAliases(aliases, resource, fieldName, field) {
|
|
203
|
+
if (field.type === 'enum') {
|
|
204
|
+
addEnumAlias(aliases, enumAliasName(resource, fieldName), field.values ?? []);
|
|
205
|
+
}
|
|
206
|
+
if (field.type === 'object') {
|
|
207
|
+
for (const [childName, childField] of Object.entries(field.fields ?? {})) {
|
|
208
|
+
collectFieldEnumAliases(aliases, resource, `${fieldName}${pascalCase(childName)}`, childField);
|
|
209
|
+
}
|
|
210
|
+
for (const variant of Object.values(field.variants ?? {})) {
|
|
211
|
+
for (const [childName, childField] of Object.entries(variant.fields ?? {})) {
|
|
212
|
+
if (childField.type === 'enum' && childField.values?.length === 1) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
collectFieldEnumAliases(aliases, resource, `${fieldName}${pascalCase(childName)}`, childField);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (field.type === 'array' && field.items) {
|
|
220
|
+
collectFieldEnumAliases(aliases, resource, `${fieldName}Item`, field.items);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function addEnumAlias(aliases, name, values) {
|
|
224
|
+
const existing = aliases.get(name);
|
|
225
|
+
if (!existing) {
|
|
226
|
+
aliases.set(name, {
|
|
227
|
+
name,
|
|
228
|
+
values: [...values],
|
|
229
|
+
});
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
existing.values = [...new Set([...existing.values, ...values])];
|
|
233
|
+
}
|
|
234
|
+
function collectVariantAliases(resources) {
|
|
235
|
+
const aliases = [];
|
|
236
|
+
for (const resource of resources) {
|
|
237
|
+
for (const [fieldName, field] of Object.entries(resource.fields)) {
|
|
238
|
+
collectFieldVariantAliases(aliases, resource, fieldName, field);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return aliases;
|
|
242
|
+
}
|
|
243
|
+
function collectFieldVariantAliases(aliases, resource, fieldName, field) {
|
|
244
|
+
if (field.type === 'object' && field.variants) {
|
|
245
|
+
aliases.push({
|
|
246
|
+
name: variantAliasName(resource, fieldName),
|
|
247
|
+
resource,
|
|
248
|
+
fieldPath: fieldName,
|
|
249
|
+
variants: field.variants,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (field.type === 'object') {
|
|
253
|
+
for (const [childName, childField] of Object.entries(field.fields ?? {})) {
|
|
254
|
+
collectFieldVariantAliases(aliases, resource, `${fieldName}${pascalCase(childName)}`, childField);
|
|
255
|
+
}
|
|
256
|
+
for (const variant of Object.values(field.variants ?? {})) {
|
|
257
|
+
for (const [childName, childField] of Object.entries(variant.fields ?? {})) {
|
|
258
|
+
collectFieldVariantAliases(aliases, resource, `${fieldName}${pascalCase(childName)}`, childField);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (field.type === 'array' && field.items) {
|
|
263
|
+
collectFieldVariantAliases(aliases, resource, `${fieldName}Item`, field.items);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function enumAliasName(resource, fieldName) {
|
|
267
|
+
return `${resource.typeName}${pascalCase(fieldName)}`;
|
|
268
|
+
}
|
|
269
|
+
function variantAliasName(resource, fieldName) {
|
|
270
|
+
return `${resource.typeName}${pascalCase(fieldName)}`;
|
|
271
|
+
}
|
|
272
|
+
function literalType(value) {
|
|
273
|
+
return JSON.stringify(value);
|
|
274
|
+
}
|
|
275
|
+
function propertyName(value) {
|
|
276
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value) ? value : JSON.stringify(value);
|
|
277
|
+
}
|
|
278
|
+
function jsDoc(text, depth = 0) {
|
|
279
|
+
const indent = ' '.repeat(depth);
|
|
280
|
+
const escaped = String(text).replaceAll('*/', '* /');
|
|
281
|
+
return `${indent}/** ${escaped} */`.trimStart();
|
|
282
|
+
}
|
|
283
|
+
function unique(values) {
|
|
284
|
+
return [...new Set(values.filter(Boolean))];
|
|
285
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generateViewerManifest, renderViewerManifest } from './features/viewer/manifest.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generateViewerManifest, renderViewerManifest } from './features/viewer/manifest.js';
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { DbCacheEventPolicy, DbCacheReadPolicy, DbCacheWritePolicy, DbClient, DbOptions, DbTraceOptions } from './index.d.ts';
|
|
2
|
+
|
|
3
|
+
export type DbVirtualClient = DbClient & {
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type DbViteClientCacheOptions = boolean | {
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
readPolicy?: DbCacheReadPolicy;
|
|
9
|
+
writePolicy?: DbCacheWritePolicy;
|
|
10
|
+
eventPolicy?: DbCacheEventPolicy;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type DbVitePluginOptions = Pick<DbOptions, 'cwd' | 'configPath' | 'dbDir' | 'sourceDir' | 'stateDir' | 'outputs' | 'schemaOutFile' | 'viewerManifestOutFile' | 'schemaManifest' | 'types' | 'schema' | 'defaults' | 'seed' | 'collections' | 'server' | 'rest' | 'graphql' | 'operations' | 'mock'> & {
|
|
14
|
+
/** Scoped base for db dev tools. Defaults to "/__db". */
|
|
15
|
+
apiBase?: string;
|
|
16
|
+
/** App-facing REST data route alias. Defaults to "/db"; set false to disable. */
|
|
17
|
+
dataPath?: string | false;
|
|
18
|
+
/** Serve root REST routes such as "/users" during Vite dev. Defaults to false. */
|
|
19
|
+
rootRoutes?: boolean;
|
|
20
|
+
/** Scoped REST resource base. Defaults to "<apiBase>/rest". */
|
|
21
|
+
restBasePath?: string;
|
|
22
|
+
/** Scoped GraphQL endpoint. Defaults to "<apiBase>/graphql". */
|
|
23
|
+
graphqlPath?: string;
|
|
24
|
+
/** Explicit request trace option. Wins over db.config.mjs server.trace. */
|
|
25
|
+
trace?: DbTraceOptions;
|
|
26
|
+
/** Virtual module id for the browser-safe client. Defaults to "virtual:db/client"; false disables it. */
|
|
27
|
+
clientVirtualModule?: string | false;
|
|
28
|
+
/** Import specifier used inside the virtual client. Defaults to "@async/db/client". */
|
|
29
|
+
clientImport?: string;
|
|
30
|
+
/** Opt the virtual browser client into memory cache behavior during Vite dev. */
|
|
31
|
+
clientCache?: DbViteClientCacheOptions;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type ViteLikePlugin = {
|
|
35
|
+
name: string;
|
|
36
|
+
apply: 'serve';
|
|
37
|
+
configureServer(server: {
|
|
38
|
+
middlewares: {
|
|
39
|
+
use(middleware: (request: unknown, response: unknown, next: () => void) => void): void;
|
|
40
|
+
};
|
|
41
|
+
httpServer?: {
|
|
42
|
+
once(event: 'close', callback: () => void): void;
|
|
43
|
+
};
|
|
44
|
+
config?: {
|
|
45
|
+
logger?: {
|
|
46
|
+
warn(message: string): void;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
}): void | Promise<void>;
|
|
50
|
+
resolveId(id: string): string | null | Promise<string | null>;
|
|
51
|
+
load(id: string): string | null | Promise<string | null>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export function dbPlugin(options?: DbVitePluginOptions): ViteLikePlugin;
|
|
55
|
+
|
|
56
|
+
declare module 'virtual:db/client' {
|
|
57
|
+
export const client: DbVirtualClient;
|
|
58
|
+
export default client;
|
|
59
|
+
}
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { dbPlugin } from './integrations/vite.js';
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
export function renderJsonViewer(value, options = {}) {
|
|
2
|
+
const title = options.title ?? 'db JSON';
|
|
3
|
+
const json = normalizeJsonText(value);
|
|
4
|
+
const formatted = formatJsonText(json);
|
|
5
|
+
const compact = compactJsonText(json);
|
|
6
|
+
return `<!doctype html>
|
|
7
|
+
<html lang="en" data-theme-mode="dark">
|
|
8
|
+
<head>
|
|
9
|
+
<meta charset="utf-8">
|
|
10
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
11
|
+
<title>${escapeHtml(title)}</title>
|
|
12
|
+
<script src="https://cdn.tailwindcss.com"></script>
|
|
13
|
+
</head>
|
|
14
|
+
<body class="min-h-screen bg-slate-950 text-slate-50 antialiased">
|
|
15
|
+
<header class="flex flex-wrap items-center justify-between gap-3 border-b border-slate-700 bg-slate-900 px-5 py-4 text-slate-50 sm:flex-nowrap" data-theme-surface="header">
|
|
16
|
+
<h1 class="m-0 min-w-0 truncate text-base font-bold leading-tight">${escapeHtml(title)}</h1>
|
|
17
|
+
<div class="flex flex-wrap items-center justify-end gap-2">
|
|
18
|
+
<div class="inline-flex overflow-hidden rounded-md border border-slate-700 bg-slate-800" aria-label="Theme" data-theme-surface="control">
|
|
19
|
+
<button class="min-h-9 bg-teal-700 px-3 py-2 text-[13px] font-bold text-white hover:bg-teal-600" type="button" data-theme-choice="dark" aria-pressed="true">Dark</button>
|
|
20
|
+
<button class="min-h-9 px-3 py-2 text-[13px] font-bold text-slate-50 hover:bg-slate-700" type="button" data-theme-choice="light" aria-pressed="false">Light</button>
|
|
21
|
+
<button class="min-h-9 px-3 py-2 text-[13px] font-bold text-slate-50 hover:bg-slate-700" type="button" data-theme-choice="system" aria-pressed="false">System</button>
|
|
22
|
+
</div>
|
|
23
|
+
<div class="inline-flex overflow-hidden rounded-md border border-slate-700 bg-slate-800" aria-label="Formatting" data-theme-surface="control">
|
|
24
|
+
<button class="min-h-9 bg-teal-700 px-3 py-2 text-[13px] font-bold text-white hover:bg-teal-600" type="button" data-format-choice="pretty" aria-pressed="true">Pretty</button>
|
|
25
|
+
<button class="min-h-9 px-3 py-2 text-[13px] font-bold text-slate-50 hover:bg-slate-700" type="button" data-format-choice="raw" aria-pressed="false">Raw</button>
|
|
26
|
+
</div>
|
|
27
|
+
<button class="min-h-9 rounded-md border border-slate-700 bg-slate-800 px-3 py-2 text-[13px] font-bold text-slate-50 hover:border-teal-400 hover:bg-slate-700" type="button" id="copy-json">Copy</button>
|
|
28
|
+
<span class="min-w-16 text-right text-[13px] text-slate-400" id="copy-status"></span>
|
|
29
|
+
</div>
|
|
30
|
+
</header>
|
|
31
|
+
<main class="p-5">
|
|
32
|
+
<pre class="m-0 min-h-[calc(100vh-112px)] overflow-auto rounded-lg border border-slate-700 bg-slate-900 p-4 font-mono text-[13px] leading-[1.55] text-slate-50 [tab-size:2]" id="json-output">${escapeHtml(formatted)}</pre>
|
|
33
|
+
</main>
|
|
34
|
+
<script>
|
|
35
|
+
const root = document.documentElement;
|
|
36
|
+
const copyButton = document.getElementById('copy-json');
|
|
37
|
+
const status = document.getElementById('copy-status');
|
|
38
|
+
const output = document.getElementById('json-output');
|
|
39
|
+
const themeButtons = [...document.querySelectorAll('[data-theme-choice]')];
|
|
40
|
+
const formatButtons = [...document.querySelectorAll('[data-format-choice]')];
|
|
41
|
+
const formattedJson = ${JSON.stringify(formatted)};
|
|
42
|
+
const compactJson = ${JSON.stringify(compact)};
|
|
43
|
+
const systemTheme = window.matchMedia('(prefers-color-scheme: light)');
|
|
44
|
+
let currentTheme = 'dark';
|
|
45
|
+
const themedSurfaces = [
|
|
46
|
+
{
|
|
47
|
+
element: document.body,
|
|
48
|
+
dark: ['bg-slate-950', 'text-slate-50'],
|
|
49
|
+
light: ['bg-slate-50', 'text-slate-950'],
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
element: document.querySelector('[data-theme-surface="header"]'),
|
|
53
|
+
dark: ['border-slate-700', 'bg-slate-900', 'text-slate-50'],
|
|
54
|
+
light: ['border-slate-200', 'bg-white', 'text-slate-950'],
|
|
55
|
+
},
|
|
56
|
+
...[...document.querySelectorAll('[data-theme-surface="control"]')].map((element) => ({
|
|
57
|
+
element,
|
|
58
|
+
dark: ['border-slate-700', 'bg-slate-800'],
|
|
59
|
+
light: ['border-slate-300', 'bg-slate-100'],
|
|
60
|
+
})),
|
|
61
|
+
{
|
|
62
|
+
element: document.getElementById('copy-json'),
|
|
63
|
+
dark: ['border-slate-700', 'bg-slate-800', 'text-slate-50', 'hover:border-teal-400', 'hover:bg-slate-700'],
|
|
64
|
+
light: ['border-slate-300', 'bg-slate-100', 'text-slate-700', 'hover:border-teal-500', 'hover:bg-slate-200'],
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
element: document.getElementById('copy-status'),
|
|
68
|
+
dark: ['text-slate-400'],
|
|
69
|
+
light: ['text-slate-500'],
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
element: output,
|
|
73
|
+
dark: ['border-slate-700', 'bg-slate-900', 'text-slate-50'],
|
|
74
|
+
light: ['border-slate-200', 'bg-white', 'text-slate-950'],
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
const activeButtonClasses = ['bg-teal-700', 'text-white', 'hover:bg-teal-600'];
|
|
78
|
+
const inactiveButtonClasses = ['text-slate-50', 'hover:bg-slate-700', 'text-slate-700', 'hover:bg-slate-200'];
|
|
79
|
+
|
|
80
|
+
function applyTheme(mode) {
|
|
81
|
+
root.dataset.themeMode = mode;
|
|
82
|
+
currentTheme = mode === 'system'
|
|
83
|
+
? (systemTheme.matches ? 'light' : 'dark')
|
|
84
|
+
: mode;
|
|
85
|
+
for (const surface of themedSurfaces) {
|
|
86
|
+
if (!surface.element) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
surface.element.classList.remove(...surface.dark, ...surface.light);
|
|
90
|
+
surface.element.classList.add(...surface[currentTheme]);
|
|
91
|
+
}
|
|
92
|
+
for (const button of themeButtons) {
|
|
93
|
+
setPressed(button, button.dataset.themeChoice === mode);
|
|
94
|
+
}
|
|
95
|
+
for (const button of formatButtons) {
|
|
96
|
+
setPressed(button, button.getAttribute('aria-pressed') === 'true');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (const button of themeButtons) {
|
|
101
|
+
button.addEventListener('click', () => {
|
|
102
|
+
applyTheme(button.dataset.themeChoice);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
systemTheme.addEventListener('change', () => {
|
|
107
|
+
if (root.dataset.themeMode === 'system') {
|
|
108
|
+
applyTheme('system');
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
function setFormat(mode) {
|
|
113
|
+
output.textContent = mode === 'raw' ? compactJson : formattedJson;
|
|
114
|
+
for (const button of formatButtons) {
|
|
115
|
+
setPressed(button, button.dataset.formatChoice === mode);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function setPressed(button, pressed) {
|
|
120
|
+
button.setAttribute('aria-pressed', String(pressed));
|
|
121
|
+
button.classList.remove(...activeButtonClasses, ...inactiveButtonClasses);
|
|
122
|
+
button.classList.add(...(pressed
|
|
123
|
+
? activeButtonClasses
|
|
124
|
+
: currentTheme === 'dark'
|
|
125
|
+
? ['text-slate-50', 'hover:bg-slate-700']
|
|
126
|
+
: ['text-slate-700', 'hover:bg-slate-200']));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for (const button of formatButtons) {
|
|
130
|
+
button.addEventListener('click', () => {
|
|
131
|
+
setFormat(button.dataset.formatChoice);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
copyButton.addEventListener('click', async () => {
|
|
136
|
+
try {
|
|
137
|
+
await navigator.clipboard.writeText(output.textContent);
|
|
138
|
+
status.textContent = 'Copied';
|
|
139
|
+
} catch {
|
|
140
|
+
status.textContent = 'Copy failed';
|
|
141
|
+
}
|
|
142
|
+
setTimeout(() => {
|
|
143
|
+
status.textContent = '';
|
|
144
|
+
}, 1200);
|
|
145
|
+
});
|
|
146
|
+
</script>
|
|
147
|
+
</body>
|
|
148
|
+
</html>`;
|
|
149
|
+
}
|
|
150
|
+
function normalizeJsonText(value) {
|
|
151
|
+
return typeof value === 'string' ? value : JSON.stringify(value);
|
|
152
|
+
}
|
|
153
|
+
function formatJsonText(value) {
|
|
154
|
+
try {
|
|
155
|
+
return JSON.stringify(JSON.parse(value), null, 2);
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return String(value);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function compactJsonText(value) {
|
|
162
|
+
try {
|
|
163
|
+
return JSON.stringify(JSON.parse(value));
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return String(value);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function escapeHtml(value) {
|
|
170
|
+
return String(value)
|
|
171
|
+
.replaceAll('&', '&')
|
|
172
|
+
.replaceAll('<', '<')
|
|
173
|
+
.replaceAll('>', '>')
|
|
174
|
+
.replaceAll('"', '"')
|
|
175
|
+
.replaceAll("'", ''');
|
|
176
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type DbViewerOptions = {
|
|
2
|
+
graphqlPath?: string;
|
|
3
|
+
schemaPath?: string;
|
|
4
|
+
manifestPath?: string;
|
|
5
|
+
eventsPath?: string;
|
|
6
|
+
importPath?: string;
|
|
7
|
+
restBatchPath?: string;
|
|
8
|
+
restBasePath?: string;
|
|
9
|
+
sourceDirLabel?: string;
|
|
10
|
+
};
|
|
11
|
+
export declare function renderDbViewer(options?: DbViewerOptions): string;
|
|
12
|
+
export {};
|