tinyzoo 0.0.0-stage → 0.2.1
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/LICENSE +10 -0
- package/README.md +46 -2
- package/app/app.config.ts +35 -0
- package/app/assets/brand/characters/bright/bird.svg +1 -0
- package/app/assets/brand/characters/bright/bunny.svg +1 -0
- package/app/assets/brand/characters/bright/elephant.svg +1 -0
- package/app/assets/brand/characters/bright/fox.svg +1 -0
- package/app/assets/brand/characters/bright/frog.svg +1 -0
- package/app/assets/brand/characters/bright/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/bee.svg +1 -0
- package/app/assets/brand/characters/muted/bird.svg +1 -0
- package/app/assets/brand/characters/muted/butterfly.svg +1 -0
- package/app/assets/brand/characters/muted/chick.svg +1 -0
- package/app/assets/brand/characters/muted/cow.svg +1 -0
- package/app/assets/brand/characters/muted/deer.svg +1 -0
- package/app/assets/brand/characters/muted/duck.svg +1 -0
- package/app/assets/brand/characters/muted/elephant.svg +1 -0
- package/app/assets/brand/characters/muted/fox.svg +1 -0
- package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
- package/app/assets/brand/characters/muted/koala.svg +1 -0
- package/app/assets/brand/characters/muted/ladybug.svg +1 -0
- package/app/assets/brand/characters/muted/lion.svg +1 -0
- package/app/assets/brand/characters/muted/mouse.svg +1 -0
- package/app/assets/brand/characters/muted/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/owl.svg +1 -0
- package/app/assets/brand/characters/muted/panda.svg +1 -0
- package/app/assets/brand/characters/muted/penguin.svg +1 -0
- package/app/assets/brand/characters/muted/pig.svg +1 -0
- package/app/assets/brand/characters/muted/poodle.svg +1 -0
- package/app/assets/brand/characters/muted/raccoon.svg +1 -0
- package/app/assets/brand/characters/muted/seal.svg +1 -0
- package/app/assets/brand/characters/muted/sheep.svg +1 -0
- package/app/assets/brand/characters/muted/snail.svg +1 -0
- package/app/assets/brand/characters/muted/turtle.svg +1 -0
- package/app/assets/brand/characters/muted/whale.svg +1 -0
- package/app/assets/brand/logo/favicon.svg +1 -0
- package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
- package/app/assets/brand/logo/logo-primary.svg +1 -0
- package/app/assets/brand/logo/mascot-reversed.svg +1 -0
- package/app/assets/brand/logo/mascot.svg +1 -0
- package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
- package/app/assets/brand/logo/wordmark.svg +1 -0
- package/app/assets/css/cms-admin.css +607 -0
- package/app/components/CmsBlocks.vue +30 -0
- package/app/components/CmsCell.vue +16 -0
- package/app/components/CmsCellBadge.vue +18 -0
- package/app/components/CmsCellBlocks.vue +9 -0
- package/app/components/CmsCellBoolean.vue +12 -0
- package/app/components/CmsCellCode.vue +7 -0
- package/app/components/CmsCellColor.vue +13 -0
- package/app/components/CmsCellDate.vue +10 -0
- package/app/components/CmsCellExcerpt.vue +21 -0
- package/app/components/CmsCellGallery.vue +10 -0
- package/app/components/CmsCellGroup.vue +23 -0
- package/app/components/CmsCellNumber.vue +7 -0
- package/app/components/CmsCellRelation.vue +8 -0
- package/app/components/CmsCellText.vue +12 -0
- package/app/components/CmsCellThumb.vue +18 -0
- package/app/components/CmsConfirmDialog.vue +55 -0
- package/app/components/CmsContent.vue +103 -0
- package/app/components/CmsDayPicker.vue +44 -0
- package/app/components/CmsEmptyState.vue +35 -0
- package/app/components/CmsField.vue +111 -0
- package/app/components/CmsFieldBlocks.vue +353 -0
- package/app/components/CmsFieldBoolean.vue +8 -0
- package/app/components/CmsFieldColor.vue +42 -0
- package/app/components/CmsFieldDate.vue +131 -0
- package/app/components/CmsFieldGallery.vue +114 -0
- package/app/components/CmsFieldGroup.vue +53 -0
- package/app/components/CmsFieldMedia.vue +219 -0
- package/app/components/CmsFieldNumber.vue +11 -0
- package/app/components/CmsFieldRelationMany.vue +54 -0
- package/app/components/CmsFieldRelationOne.vue +47 -0
- package/app/components/CmsFieldRichText.vue +45 -0
- package/app/components/CmsFieldSelect.vue +41 -0
- package/app/components/CmsFieldSlug.vue +46 -0
- package/app/components/CmsFieldText.vue +75 -0
- package/app/components/CmsFormSections.vue +150 -0
- package/app/components/CmsHighlight.vue +22 -0
- package/app/components/CmsImage.vue +39 -0
- package/app/components/CmsLinkPicker.vue +122 -0
- package/app/components/CmsLinkTarget.vue +35 -0
- package/app/components/CmsListFilters.vue +107 -0
- package/app/components/CmsLiveNotice.vue +64 -0
- package/app/components/CmsLoadError.vue +24 -0
- package/app/components/CmsMascot.vue +10 -0
- package/app/components/CmsMediaLibrary.vue +114 -0
- package/app/components/CmsMediaPreview.vue +34 -0
- package/app/components/CmsMediaThumb.vue +22 -0
- package/app/components/CmsMoment.vue +42 -0
- package/app/components/CmsNav.vue +39 -0
- package/app/components/CmsPage.vue +86 -0
- package/app/components/CmsPasswordInput.vue +22 -0
- package/app/components/CmsPresence.vue +29 -0
- package/app/components/CmsPreviewPanel.vue +131 -0
- package/app/components/CmsPublishReview.vue +102 -0
- package/app/components/CmsRevisions.vue +195 -0
- package/app/components/CmsRichTextEditor.vue +290 -0
- package/app/components/CmsSearch.vue +99 -0
- package/app/components/CmsSection.vue +20 -0
- package/app/components/CmsSingletonForm.vue +283 -0
- package/app/components/CmsStatusBadge.vue +21 -0
- package/app/components/CmsTime.vue +16 -0
- package/app/components/CmsTranslationRef.vue +27 -0
- package/app/components/CmsTreeList.vue +453 -0
- package/app/components/CmsUnsavedText.vue +34 -0
- package/app/components/CmsWordmark.vue +10 -0
- package/app/composables/useCmsAdminHead.ts +18 -0
- package/app/composables/useCmsCollab.ts +165 -0
- package/app/composables/useCmsConfirm.ts +39 -0
- package/app/composables/useCmsEntryActions.ts +75 -0
- package/app/composables/useCmsFocus.ts +13 -0
- package/app/composables/useCmsForm.ts +273 -0
- package/app/composables/useCmsI18n.ts +185 -0
- package/app/composables/useCmsLinkTargets.ts +84 -0
- package/app/composables/useCmsLive.ts +199 -0
- package/app/composables/useCmsSearch.ts +62 -0
- package/app/composables/useCmsSeo.ts +99 -0
- package/app/composables/useCmsSignedIn.ts +24 -0
- package/app/composables/useCmsUiLocale.ts +24 -0
- package/app/i18n/cs.ts +781 -0
- package/app/i18n/de.ts +781 -0
- package/app/i18n/en.ts +782 -0
- package/app/i18n/types.ts +22 -0
- package/app/layouts/admin.vue +224 -0
- package/app/layouts/cms-auth.vue +37 -0
- package/app/middleware/cms-api-not-a-page.global.ts +10 -0
- package/app/middleware/cms-auth.global.ts +16 -0
- package/app/pages/admin/[entity]/[id].vue +1109 -0
- package/app/pages/admin/[entity]/index.vue +733 -0
- package/app/pages/admin/account.vue +117 -0
- package/app/pages/admin/forgot.vue +50 -0
- package/app/pages/admin/index.vue +184 -0
- package/app/pages/admin/invite/[token].vue +73 -0
- package/app/pages/admin/login.vue +84 -0
- package/app/pages/admin/media.vue +494 -0
- package/app/pages/admin/privacy.vue +193 -0
- package/app/pages/admin/reset/[token].vue +52 -0
- package/app/pages/admin/setup.vue +87 -0
- package/app/pages/admin/styleguide.vue +261 -0
- package/app/pages/admin/team.vue +258 -0
- package/app/plugins/cmsLivePreview.client.ts +20 -0
- package/app/plugins/cmsSignedOut.client.ts +31 -0
- package/app/utils/cmsBlockType.ts +11 -0
- package/app/utils/cmsBrand.ts +38 -0
- package/app/utils/cmsDiff.ts +83 -0
- package/app/utils/cmsFetch.ts +7 -0
- package/app/utils/cmsFocusInvalid.ts +22 -0
- package/app/utils/cmsIssues.ts +128 -0
- package/app/utils/cmsJson.ts +28 -0
- package/app/utils/cmsMedia.ts +63 -0
- package/app/utils/cmsSections.ts +154 -0
- package/app/utils/cmsTime.ts +41 -0
- package/cli/backup.ts +713 -0
- package/cli/breaking.ts +174 -0
- package/cli/content-migrate.ts +239 -0
- package/cli/db.ts +86 -0
- package/cli/export.ts +336 -0
- package/cli/generate.ts +146 -0
- package/cli/import.ts +643 -0
- package/cli/index.ts +140 -0
- package/cli/links-convert.ts +291 -0
- package/cli/seed-engine.ts +705 -0
- package/cli/seed.ts +339 -0
- package/cli/status.ts +49 -0
- package/cli/tar.ts +304 -0
- package/cli/transfer.ts +677 -0
- package/cli/up.ts +27 -0
- package/cli/upgrade.ts +60 -0
- package/cli/versions.ts +60 -0
- package/forms/components/CmsForm.vue +186 -0
- package/forms/index.ts +203 -0
- package/forms/messages.ts +104 -0
- package/forms/retention.ts +48 -0
- package/forms/server/form.get.ts +12 -0
- package/forms/server/form.post.ts +109 -0
- package/forms/server/formSettings.ts +12 -0
- package/forms/server/forms.ts +127 -0
- package/module.ts +373 -0
- package/nuxt.config.ts +43 -0
- package/package.json +98 -3
- package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
- package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
- package/server/api/cms/[entity]/[id].delete.ts +52 -0
- package/server/api/cms/[entity]/[id].get.ts +21 -0
- package/server/api/cms/[entity]/[id].patch.ts +116 -0
- package/server/api/cms/[entity]/_export.get.ts +69 -0
- package/server/api/cms/[entity]/_move.post.ts +81 -0
- package/server/api/cms/[entity]/_order.post.ts +49 -0
- package/server/api/cms/[entity]/_tree.get.ts +41 -0
- package/server/api/cms/[entity]/index.get.ts +62 -0
- package/server/api/cms/[entity]/index.post.ts +38 -0
- package/server/api/cms/[entity]/options.get.ts +55 -0
- package/server/api/cms/_auth/accept.post.ts +33 -0
- package/server/api/cms/_auth/forgot.post.ts +39 -0
- package/server/api/cms/_auth/invite/[token].get.ts +18 -0
- package/server/api/cms/_auth/login.post.ts +42 -0
- package/server/api/cms/_auth/logout.post.ts +7 -0
- package/server/api/cms/_auth/me.get.ts +5 -0
- package/server/api/cms/_auth/me.patch.ts +32 -0
- package/server/api/cms/_auth/password.post.ts +29 -0
- package/server/api/cms/_auth/reset.post.ts +34 -0
- package/server/api/cms/_auth/setup.post.ts +46 -0
- package/server/api/cms/_auth/status.get.ts +18 -0
- package/server/api/cms/_collab/session.get.ts +30 -0
- package/server/api/cms/_collab/socket.ts +51 -0
- package/server/api/cms/_collab/unsaved.delete.ts +16 -0
- package/server/api/cms/_graph.get.ts +20 -0
- package/server/api/cms/_links/resolve.post.ts +17 -0
- package/server/api/cms/_links.get.ts +81 -0
- package/server/api/cms/_live.ts +30 -0
- package/server/api/cms/_media/[id]/usage.get.ts +14 -0
- package/server/api/cms/_media/[id].delete.ts +29 -0
- package/server/api/cms/_media/[id].get.ts +13 -0
- package/server/api/cms/_media/[id].patch.ts +64 -0
- package/server/api/cms/_media/exists.post.ts +15 -0
- package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
- package/server/api/cms/_media/index.get.ts +44 -0
- package/server/api/cms/_media/index.post.ts +147 -0
- package/server/api/cms/_media/raw/[...key].get.ts +85 -0
- package/server/api/cms/_media/raw/[...key].head.ts +2 -0
- package/server/api/cms/_overview.get.ts +87 -0
- package/server/api/cms/_preview.post.ts +17 -0
- package/server/api/cms/_privacy/delete.post.ts +14 -0
- package/server/api/cms/_privacy/export.post.ts +25 -0
- package/server/api/cms/_privacy/lookup.post.ts +27 -0
- package/server/api/cms/_search.get.ts +17 -0
- package/server/api/cms/_singleton/[name].get.ts +19 -0
- package/server/api/cms/_singleton/[name].put.ts +76 -0
- package/server/api/cms/_users/[id]/resend.post.ts +25 -0
- package/server/api/cms/_users/[id].delete.ts +21 -0
- package/server/api/cms/_users/[id].patch.ts +45 -0
- package/server/api/cms/_users/index.get.ts +28 -0
- package/server/api/cms/_users/index.post.ts +39 -0
- package/server/api/content/[entity]/[slug].get.ts +74 -0
- package/server/api/content/[entity]/index.get.ts +100 -0
- package/server/api/content/_global/[name].get.ts +23 -0
- package/server/api/content/_search.get.ts +28 -0
- package/server/api/content/_sitemap.get.ts +8 -0
- package/server/api/content/_tree/[entity].get.ts +31 -0
- package/server/api/content-types.ts.get.ts +17 -0
- package/server/api/health/live.get.ts +10 -0
- package/server/api/health/ready.get.ts +21 -0
- package/server/api/openapi.get.ts +48 -0
- package/server/api/openapi.json.get.ts +32 -0
- package/server/db/driver.ts +8 -0
- package/server/mail/mailer.ts +43 -0
- package/server/middleware/cms-0-headers.ts +37 -0
- package/server/middleware/cms-0-migrate-gate.ts +14 -0
- package/server/middleware/cms-csrf.ts +28 -0
- package/server/middleware/cms-limits.ts +20 -0
- package/server/middleware/cms-redirects.ts +21 -0
- package/server/middleware/cms-session.ts +41 -0
- package/server/middleware/cms-z-plugin-access.ts +15 -0
- package/server/plugins/cms-0-config-check.ts +35 -0
- package/server/plugins/cms-changes.ts +53 -0
- package/server/plugins/cms-content-etag.ts +21 -0
- package/server/plugins/cms-errors.ts +45 -0
- package/server/plugins/cms-migrate.ts +5 -0
- package/server/plugins/cms-plugins.ts +17 -0
- package/server/plugins/cms-search.ts +12 -0
- package/server/plugins/cms-setup-code.ts +15 -0
- package/server/routes/sitemap.xml.get.ts +25 -0
- package/server/store/redis.ts +95 -0
- package/server/store/types.ts +21 -0
- package/server/tasks/cms/retention.ts +10 -0
- package/server/tasks/cms/scheduled.ts +10 -0
- package/server/tasks/cms/search-reindex.ts +12 -0
- package/server/utils/cmsAudit.ts +29 -0
- package/server/utils/cmsAuth.ts +143 -0
- package/server/utils/cmsBlocks.ts +47 -0
- package/server/utils/cmsChanges.ts +222 -0
- package/server/utils/cmsCollab.ts +286 -0
- package/server/utils/cmsCollabServer.ts +451 -0
- package/server/utils/cmsCollate.ts +27 -0
- package/server/utils/cmsContent.ts +376 -0
- package/server/utils/cmsCrud.ts +361 -0
- package/server/utils/cmsCsv.ts +24 -0
- package/server/utils/cmsDb.ts +23 -0
- package/server/utils/cmsDemo.ts +36 -0
- package/server/utils/cmsEmails.ts +125 -0
- package/server/utils/cmsErrors.ts +40 -0
- package/server/utils/cmsFold.ts +17 -0
- package/server/utils/cmsImageSizes.ts +51 -0
- package/server/utils/cmsJson.ts +6 -0
- package/server/utils/cmsLinkAddress.ts +50 -0
- package/server/utils/cmsLinks.ts +248 -0
- package/server/utils/cmsListQuery.ts +93 -0
- package/server/utils/cmsLive.ts +393 -0
- package/server/utils/cmsLocales.ts +96 -0
- package/server/utils/cmsMail.ts +28 -0
- package/server/utils/cmsMediaGc.ts +188 -0
- package/server/utils/cmsMediaKeys.ts +62 -0
- package/server/utils/cmsMigrate.ts +86 -0
- package/server/utils/cmsPreviewDraft.ts +72 -0
- package/server/utils/cmsPrivacy.ts +176 -0
- package/server/utils/cmsPublished.ts +57 -0
- package/server/utils/cmsRange.ts +38 -0
- package/server/utils/cmsRateLimit.ts +82 -0
- package/server/utils/cmsRedirects.ts +160 -0
- package/server/utils/cmsRetention.ts +61 -0
- package/server/utils/cmsRevisions.ts +103 -0
- package/server/utils/cmsScheduler.ts +45 -0
- package/server/utils/cmsScrub.ts +107 -0
- package/server/utils/cmsSearch.ts +354 -0
- package/server/utils/cmsSession.ts +102 -0
- package/server/utils/cmsSetupCode.ts +97 -0
- package/server/utils/cmsSitemap.ts +176 -0
- package/server/utils/cmsStorage.ts +580 -0
- package/server/utils/cmsStore.ts +109 -0
- package/server/utils/cmsTiming.ts +14 -0
- package/server/utils/cmsTooMany.ts +9 -0
- package/server/utils/cmsTranslationWrite.ts +186 -0
- package/server/utils/cmsTree.ts +251 -0
- package/server/utils/cmsTreeAddress.ts +65 -0
- package/server/utils/cmsUpload.ts +228 -0
- package/server/utils/cmsWebhooks.ts +104 -0
- package/shared/json.ts +11 -0
- package/shared/richtext-editor.ts +56 -0
- package/shared/richtext-normalize.ts +33 -0
- package/shared/richtext-paste.ts +151 -0
- package/shared/storage-delivery.ts +53 -0
- package/src/blocks.ts +624 -0
- package/src/changes.ts +54 -0
- package/src/content-shape.ts +340 -0
- package/src/deprecate.ts +64 -0
- package/src/descriptor.ts +149 -0
- package/src/docs.ts +27 -0
- package/src/emitter.ts +94 -0
- package/src/entity.ts +624 -0
- package/src/errorReport.ts +41 -0
- package/src/fields.ts +465 -0
- package/src/i18n.ts +26 -0
- package/src/index.ts +54 -0
- package/src/linkref.ts +31 -0
- package/src/links.ts +179 -0
- package/src/locales.ts +39 -0
- package/src/media.ts +48 -0
- package/src/migrate.ts +236 -0
- package/src/openapi.ts +377 -0
- package/src/paths.ts +88 -0
- package/src/plugin.ts +505 -0
- package/src/presets.ts +230 -0
- package/src/richtext.ts +88 -0
- package/src/sdk.ts +181 -0
- package/src/search.ts +312 -0
- package/src/seed.ts +225 -0
- package/src/setupCode.ts +30 -0
- package/src/system.ts +219 -0
- package/src/tree.ts +421 -0
- package/src/upgrades.ts +137 -0
- package/starter/.env.example +10 -0
- package/starter/README.md +65 -0
- package/starter/app/app.config.ts +10 -0
- package/starter/app/app.vue +12 -0
- package/starter/app/assets/css/main.css +15 -0
- package/starter/app/components/global/BlockContact.vue +15 -0
- package/starter/app/components/global/BlockCta.vue +15 -0
- package/starter/app/components/global/BlockHero.vue +23 -0
- package/starter/app/components/global/BlockImage.vue +13 -0
- package/starter/app/components/global/BlockText.vue +12 -0
- package/starter/app/composables/useSite.ts +77 -0
- package/starter/app/error.vue +28 -0
- package/starter/app/layouts/default.vue +106 -0
- package/starter/app/lib/cms-content.ts +152 -0
- package/starter/app/pages/[...slug].vue +66 -0
- package/starter/app/pages/hledat.vue +59 -0
- package/starter/app/pages/index.vue +20 -0
- package/starter/cms.config.ts +137 -0
- package/starter/compose.yaml +15 -0
- package/starter/drizzle.config.ts +8 -0
- package/starter/gitignore.txt +8 -0
- package/starter/nuxt.config.ts +13 -0
- package/starter/package.json +38 -0
- package/starter/scripts/typecheck.mjs +38 -0
- package/starter/seed/index.ts +297 -0
- package/starter/seed/media/about.jpg +0 -0
- package/starter/seed/media/hero.jpg +0 -0
- package/starter/seed/media/logo.png +0 -0
- package/starter/seed/media/news-bread.jpg +0 -0
- package/starter/seed/media/news-morning.jpg +0 -0
- package/starter/server/db/migrations/0000_init.sql +269 -0
- package/starter/server/db/migrations/0001_page_tree.sql +5 -0
- package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
- package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
- package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
- package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
- package/starter/server/db/migrations/meta/_journal.json +27 -0
- package/starter/server/db/schema.ts +253 -0
- package/starter/server/plugins/sitemap.ts +11 -0
- package/starter/server/routes/robots.txt.get.ts +12 -0
- package/starter/tsconfig.json +9 -0
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { and, inArray, or, sql } from 'tinyzoo'
|
|
2
|
+
import type { CmsEntity, CmsRegistry } from 'tinyzoo'
|
|
3
|
+
import { publishedCond } from './cmsPublished'
|
|
4
|
+
|
|
5
|
+
const unique = <T>(arr: T[]): T[] => [...new Set(arr)]
|
|
6
|
+
|
|
7
|
+
/** Length-tolerant, content-constant-time string compare (no early-exit on mismatch). */
|
|
8
|
+
export function constantTimeEqual(a: string, b: string): boolean {
|
|
9
|
+
const max = Math.max(a.length, b.length)
|
|
10
|
+
let diff = a.length ^ b.length
|
|
11
|
+
for (let i = 0; i < max; i++) diff |= (a.charCodeAt(i) || 0) ^ (b.charCodeAt(i) || 0)
|
|
12
|
+
return diff === 0
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A public collection is one with `content.public`. 404 otherwise (never leak its existence as 403). */
|
|
16
|
+
export function getPublicEntity(reg: CmsRegistry, name: string | undefined): CmsEntity {
|
|
17
|
+
const e = name ? reg.byName.get(name) : undefined
|
|
18
|
+
if (!e || !e.content?.public) {
|
|
19
|
+
throw createError({ statusCode: 404, statusMessage: `No public content collection: ${name}` })
|
|
20
|
+
}
|
|
21
|
+
return e
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Is the interactive API reference (/api/openapi) on? `cms.content.docs: true` / `false`
|
|
26
|
+
* (env NUXT_CMS_CONTENT_DOCS) decides; left out, it is on in development and off in production.
|
|
27
|
+
*/
|
|
28
|
+
export function contentDocsEnabled(): boolean {
|
|
29
|
+
const v = (useRuntimeConfig().cms as any)?.content?.docs
|
|
30
|
+
if (v === true || v === 'true' || v === 1 || v === '1') return true
|
|
31
|
+
if (v === false || v === 'false' || v === 0 || v === '0') return false
|
|
32
|
+
return import.meta.dev || process.env.NODE_ENV !== 'production'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Was this request made inside the server, by Nitro itself? The website's pages fetch
|
|
37
|
+
* `/api/content/*` while they render (`useFetch`, `$fetch`, `event.$fetch`): Nitro answers
|
|
38
|
+
* those from the same process, with no network in between, and builds the request from
|
|
39
|
+
* its own stand-in for Node's `IncomingMessage` — the one that carries `__unenv__` (h3
|
|
40
|
+
* relies on the same mark). A request that came over a socket never has it, whatever
|
|
41
|
+
* headers it sends, so an outsider cannot pass for the site.
|
|
42
|
+
*/
|
|
43
|
+
export function isInProcessRequest(event: any): boolean {
|
|
44
|
+
const req = event?.node?.req
|
|
45
|
+
return !!req && typeof req === 'object' && '__unenv__' in req
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Optional API-token gate. No-op unless `cms.content.token` is configured. With a token the
|
|
50
|
+
* API is closed to everyone who comes from outside without it — but the website's own
|
|
51
|
+
* server-side requests (see isInProcessRequest) are the site itself, not a visitor, so
|
|
52
|
+
* its pages render without the token ever being in their code or in a browser. (A
|
|
53
|
+
* *browser* calling the API itself, e.g. on client-side navigation, is outside: it has no
|
|
54
|
+
* token and must not get one.)
|
|
55
|
+
*/
|
|
56
|
+
export function assertContentAuth(event: any): void {
|
|
57
|
+
const token = (useRuntimeConfig().cms as any)?.content?.token
|
|
58
|
+
if (!token) return
|
|
59
|
+
if (isInProcessRequest(event)) return
|
|
60
|
+
const header = getHeader(event, 'authorization') || ''
|
|
61
|
+
const provided = getHeader(event, 'x-api-key') || header.replace(/^Bearer\s+/i, '')
|
|
62
|
+
if (!provided || !constantTimeEqual(provided, token)) {
|
|
63
|
+
noteRefusedContentRequest(event)
|
|
64
|
+
throw createError({ statusCode: 401, statusMessage: 'Invalid content API token' })
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let refusedCount = 0
|
|
69
|
+
let refusedLoggedAt = 0
|
|
70
|
+
/**
|
|
71
|
+
* One log line when requests are refused for the token (at most one a minute, with how many
|
|
72
|
+
* there were): a site that cannot read its own content, or a script with an old token, should
|
|
73
|
+
* not fail in silence — and a scanner must not fill the log.
|
|
74
|
+
*/
|
|
75
|
+
function noteRefusedContentRequest(event: any): void {
|
|
76
|
+
refusedCount++
|
|
77
|
+
const now = Date.now()
|
|
78
|
+
if (refusedLoggedAt && now - refusedLoggedAt < 60_000) return
|
|
79
|
+
refusedLoggedAt = now
|
|
80
|
+
const count = refusedCount
|
|
81
|
+
refusedCount = 0
|
|
82
|
+
const path = String(event?.path ?? '').split('?')[0]
|
|
83
|
+
console.warn('[cms] ' + count + ' content API request' + (count === 1 ? '' : 's') + ' refused: no content token, or a wrong one (' + event?.method + ' ' + path + '). '
|
|
84
|
+
+ "The website's own server-side requests do not need it; a separate site or script sends it as x-api-key (NUXT_CMS_CONTENT_TOKEN). Later refusals are counted and logged at most once a minute.")
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Is this request allowed to see unpublished drafts? Only when `?preview` is present
|
|
89
|
+
* AND the caller is an authenticated CMS user (admin session) OR presents the content
|
|
90
|
+
* token. Never lets an anonymous visitor bypass the published filter.
|
|
91
|
+
*/
|
|
92
|
+
export function isPreviewAuthorized(event: any): boolean {
|
|
93
|
+
if (getQuery(event).preview === undefined) return false
|
|
94
|
+
if (event.context?.cmsUser) return true
|
|
95
|
+
const token = (useRuntimeConfig().cms as any)?.content?.token
|
|
96
|
+
if (!token) return false
|
|
97
|
+
const header = getHeader(event, 'authorization') || ''
|
|
98
|
+
const provided = getHeader(event, 'x-api-key') || header.replace(/^Bearer\s+/i, '') || String(getQuery(event).preview || '')
|
|
99
|
+
return !!provided && constantTimeEqual(provided, token)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** AND the target's published condition into an id filter — so population never serves drafts. */
|
|
103
|
+
function withPublished(target: CmsEntity, idCond: any): any {
|
|
104
|
+
const pub = publishedCond(target)
|
|
105
|
+
return pub ? and(pub, idCond) : idCond
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Cache headers: public+CDN-friendly when open, private+no-store when token-gated or
|
|
110
|
+
* for an editor's preview (drafts and unsaved values must never be cached).
|
|
111
|
+
*/
|
|
112
|
+
export function setContentCache(event: any): void {
|
|
113
|
+
const cfg = (useRuntimeConfig().cms as any)?.content ?? {}
|
|
114
|
+
const locked = cfg.token || isPreviewAuthorized(event)
|
|
115
|
+
// Browsers check back every time (max-age=0) and get a quick 304 when nothing changed
|
|
116
|
+
// (ETag, plugins/cms-content-etag.ts), so a published change shows immediately.
|
|
117
|
+
// A CDN may keep an answer for 5 minutes; publish webhooks tell it what to purge sooner.
|
|
118
|
+
const cc = locked ? 'private, no-store' : cfg.cacheControl || 'public, max-age=0, must-revalidate, s-maxage=300'
|
|
119
|
+
setResponseHeader(event, 'Cache-Control', cc)
|
|
120
|
+
if (!locked) event.context.cmsContentEtag = true
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Media → a lean public shape (never the storage key or internals). */
|
|
124
|
+
/** A file as the website gets it: alt text in the requested language, the focal point. */
|
|
125
|
+
function mediaProjection(m: any, locale: string | null = null) {
|
|
126
|
+
if (!m) return m
|
|
127
|
+
const alt = (locale && m.altLocales?.[locale]) || m.alt
|
|
128
|
+
const focal = m.focalX != null && m.focalY != null ? { x: m.focalX, y: m.focalY } : null
|
|
129
|
+
return { id: m.id, url: m.url, width: m.width, height: m.height, alt, mime: m.mime, focal }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Scalar-only projection of a related target (no deep nesting), respecting its own `hidden`. */
|
|
133
|
+
function projectScalars(target: CmsEntity, row: any) {
|
|
134
|
+
const hidden = new Set(target.content?.hidden ?? [])
|
|
135
|
+
const out: Record<string, any> = { id: row.id }
|
|
136
|
+
for (const [key, fld] of Object.entries(target.fields)) {
|
|
137
|
+
if (hidden.has(key) || fld.relation || fld.type === 'media' || fld.type === 'blocks' || fld.type === 'group' || fld.type === 'gallery') continue
|
|
138
|
+
out[key] = row[key]
|
|
139
|
+
}
|
|
140
|
+
return out
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Safe projection of a populated relation target:
|
|
145
|
+
* - public target → its scalar fields (minus its hidden)
|
|
146
|
+
* - non-public target → a minimal reference {id, title}
|
|
147
|
+
*/
|
|
148
|
+
function publicProjection(target: CmsEntity, row: any) {
|
|
149
|
+
if (target.content?.public) return projectScalars(target, row)
|
|
150
|
+
return { id: row.id, [target.titleField]: row[target.titleField] }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Field keys that can be populated (media + to-one + m2m). */
|
|
154
|
+
function populatableKeys(e: CmsEntity): string[] {
|
|
155
|
+
return Object.entries(e.fields)
|
|
156
|
+
.filter(([k, f]) => k !== e.tree?.parent && (f.type === 'media' || f.relation?.kind === 'many-to-one' || f.relation?.kind === 'many-to-many'))
|
|
157
|
+
.map(([k]) => k)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Resolve media + relations on `rows` IN PLACE (batched, depth 1). Nested targets
|
|
162
|
+
* get a safe projection; non-listed fields are left as raw FK ids for projectItem to drop.
|
|
163
|
+
*/
|
|
164
|
+
export async function populateItems(db: any, reg: CmsRegistry, e: CmsEntity, rows: any[], populate?: string[], locale: string | null = null, opts: { preview?: boolean; user?: any } = {}) {
|
|
165
|
+
if (!rows.length) return
|
|
166
|
+
// Links to entries (`cms:<entity>/<id>`) become their current addresses, here and in
|
|
167
|
+
// every populated target below: nothing leaves with a `cms:` in it.
|
|
168
|
+
// In a preview a link reaches drafts, but only ones the signed-in previewer may read.
|
|
169
|
+
const linkScope = { locale, preview: !!opts.preview, user: opts.user ?? null }
|
|
170
|
+
// Stored JSON of an older shape is brought up to date before anything reads it.
|
|
171
|
+
for (const r of rows) upgradeStoredJson(e, r)
|
|
172
|
+
await resolveContentLinks(db, reg, e, rows, linkScope)
|
|
173
|
+
await populateBlocks(db, reg, e, rows, locale)
|
|
174
|
+
await populateGalleries(db, reg, e, rows, locale)
|
|
175
|
+
const keys = populate ?? populatableKeys(e)
|
|
176
|
+
for (const key of keys) {
|
|
177
|
+
const fld = e.fields[key]
|
|
178
|
+
// A page tree's parent is delivered as the parent's id, not populated.
|
|
179
|
+
if (!fld || key === e.tree?.parent) continue
|
|
180
|
+
|
|
181
|
+
if (fld.type === 'media') {
|
|
182
|
+
const ids = unique(rows.map((r) => r[`${key}Id`]).filter(Boolean))
|
|
183
|
+
const byId = new Map<string, any>()
|
|
184
|
+
if (ids.length) {
|
|
185
|
+
const media: any[] = await db.select().from(reg.cmsMedia).where(inArray(reg.cmsMedia.id, ids))
|
|
186
|
+
for (const m of media) byId.set(m.id, mediaProjection(m, locale))
|
|
187
|
+
}
|
|
188
|
+
for (const r of rows) r[key] = byId.get(r[`${key}Id`]) ?? null
|
|
189
|
+
}
|
|
190
|
+
else if (fld.relation?.kind === 'many-to-one') {
|
|
191
|
+
const target = reg.byName.get(fld.relation.to().name)!
|
|
192
|
+
const ids = unique(rows.map((r) => r[`${key}Id`]).filter(Boolean))
|
|
193
|
+
const byId = new Map<string, any>()
|
|
194
|
+
if (ids.length) {
|
|
195
|
+
const targets: any[] = await db.select().from(target.table).where(withPublished(target, inArray(target.table.id, ids)))
|
|
196
|
+
await overlayTranslations(db, reg, target, targets, locale)
|
|
197
|
+
for (const t of targets) upgradeStoredJson(target, t)
|
|
198
|
+
await resolveContentLinks(db, reg, target, targets, linkScope)
|
|
199
|
+
for (const t of targets) byId.set(t.id, publicProjection(target, t))
|
|
200
|
+
}
|
|
201
|
+
for (const r of rows) r[key] = byId.get(r[`${key}Id`]) ?? null
|
|
202
|
+
}
|
|
203
|
+
else if (fld.relation?.kind === 'many-to-many') {
|
|
204
|
+
const j = reg.junctionByOwnerField.get(`${e.name}.${key}`)!
|
|
205
|
+
const target = reg.byName.get(fld.relation.to().name)!
|
|
206
|
+
const rowIds = rows.map((r) => r.id)
|
|
207
|
+
const links: any[] = await db.select().from(j.table).where(inArray(j.table[j.ownerCol], rowIds))
|
|
208
|
+
const targetIds = unique(links.map((l) => l[j.targetCol]))
|
|
209
|
+
const tById = new Map<string, any>()
|
|
210
|
+
if (targetIds.length) {
|
|
211
|
+
const targets: any[] = await db.select().from(target.table).where(withPublished(target, inArray(target.table.id, targetIds)))
|
|
212
|
+
await overlayTranslations(db, reg, target, targets, locale)
|
|
213
|
+
await resolveContentLinks(db, reg, target, targets, linkScope)
|
|
214
|
+
for (const t of targets) tById.set(t.id, publicProjection(target, t))
|
|
215
|
+
}
|
|
216
|
+
const byOwner = new Map<string, any[]>()
|
|
217
|
+
for (const l of links) {
|
|
218
|
+
const arr = byOwner.get(l[j.ownerCol]) ?? []
|
|
219
|
+
const proj = tById.get(l[j.targetCol])
|
|
220
|
+
if (proj) arr.push(proj)
|
|
221
|
+
byOwner.set(l[j.ownerCol], arr)
|
|
222
|
+
}
|
|
223
|
+
for (const r of rows) r[key] = byOwner.get(r.id) ?? []
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Blocks as the public API returns them (mirrored by content-shape blocksType): only
|
|
230
|
+
* known block types; every block has id, type and each of its fields (null when
|
|
231
|
+
* empty); media ids replaced by the public media object (batched across rows).
|
|
232
|
+
*/
|
|
233
|
+
async function populateBlocks(db: any, reg: CmsRegistry, e: CmsEntity, rows: any[], locale: string | null = null) {
|
|
234
|
+
const jsonFields = Object.entries(e.fields).filter(([, f]) => (f.type === 'blocks' && f.blocks) || (f.type === 'group' && f.fields))
|
|
235
|
+
if (!jsonFields.length) return
|
|
236
|
+
// Media ids from every block, group and the lists inside them, one query for all.
|
|
237
|
+
const ids = new Set<string>()
|
|
238
|
+
for (const [key, fld] of jsonFields) {
|
|
239
|
+
for (const r of rows) collectFieldMedia(fld, r[key], ids)
|
|
240
|
+
}
|
|
241
|
+
const byId = new Map<string, any>()
|
|
242
|
+
if (ids.size) {
|
|
243
|
+
const media: any[] = await db.select().from(reg.cmsMedia).where(inArray(reg.cmsMedia.id, [...ids]))
|
|
244
|
+
for (const m of media) byId.set(m.id, mediaProjection(m, locale))
|
|
245
|
+
}
|
|
246
|
+
for (const [key, fld] of jsonFields) {
|
|
247
|
+
for (const r of rows) r[key] = projectFieldValue(fld, r[key], byId)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const isObject = (v: unknown): v is Record<string, any> => !!v && typeof v === 'object' && !Array.isArray(v)
|
|
252
|
+
|
|
253
|
+
/** Media ids inside one field value: a media field, a list's blocks, a group's fields. */
|
|
254
|
+
function collectFieldMedia(fld: any, value: unknown, ids: Set<string>) {
|
|
255
|
+
if (fld.type === 'media') {
|
|
256
|
+
if (typeof value === 'string' && value) ids.add(value)
|
|
257
|
+
}
|
|
258
|
+
else if (fld.type === 'blocks' && fld.blocks) collectBlockMedia(value, fld.blocks, ids)
|
|
259
|
+
else if (fld.type === 'group' && fld.fields && isObject(value)) {
|
|
260
|
+
for (const [k, inner] of Object.entries(fld.fields)) collectFieldMedia(inner, value[k], ids)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function collectBlockMedia(value: unknown, defs: Record<string, any>, ids: Set<string>) {
|
|
265
|
+
for (const b of Array.isArray(value) ? value : []) {
|
|
266
|
+
if (!b || typeof b.type !== 'string' || !Object.hasOwn(defs, b.type)) continue
|
|
267
|
+
for (const [fk, bf] of Object.entries(defs[b.type]!.fields) as [string, any][]) collectFieldMedia(bf, b[fk], ids)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* One field inside a block or group as the API returns it: a media id becomes the
|
|
273
|
+
* public media object, a list its projected items, a group an object with each of its
|
|
274
|
+
* fields (null when empty), anything else as stored.
|
|
275
|
+
*/
|
|
276
|
+
function projectFieldValue(bf: any, raw: unknown, byId: Map<string, any>): any {
|
|
277
|
+
const v = raw ?? null
|
|
278
|
+
if (bf.type === 'media') return v ? byId.get(v as string) ?? null : null
|
|
279
|
+
if (bf.type === 'blocks' && bf.blocks) return projectBlocks(v, bf.blocks, byId)
|
|
280
|
+
if (bf.type === 'group' && bf.fields) {
|
|
281
|
+
if (!isObject(v)) return null
|
|
282
|
+
const out: Record<string, any> = {}
|
|
283
|
+
for (const [gk, inner] of Object.entries(bf.fields)) out[gk] = projectFieldValue(inner, v[gk], byId)
|
|
284
|
+
return out
|
|
285
|
+
}
|
|
286
|
+
return v
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Known block types only; each field present (null when empty, a list inside a block: []). */
|
|
290
|
+
function projectBlocks(value: unknown, defs: Record<string, any>, byId: Map<string, any>): any[] {
|
|
291
|
+
return (Array.isArray(value) ? value : [])
|
|
292
|
+
.filter((b: any) => b && typeof b === 'object' && typeof b.type === 'string' && Object.hasOwn(defs, b.type))
|
|
293
|
+
.map((b: any) => {
|
|
294
|
+
const out: Record<string, any> = { id: String(b.id), type: b.type }
|
|
295
|
+
for (const [fk, bf] of Object.entries(defs[b.type]!.fields) as [string, any][]) out[fk] = projectFieldValue(bf, b[fk], byId)
|
|
296
|
+
return out
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Final public shape of a row: id + timestamps + non-hidden scalars + populated
|
|
302
|
+
* relations. Raw FK columns (`${key}Id`) and hidden fields are never emitted.
|
|
303
|
+
*/
|
|
304
|
+
export function projectItem(e: CmsEntity, row: any) {
|
|
305
|
+
const hidden = new Set(e.content?.hidden ?? [])
|
|
306
|
+
const out: Record<string, any> = { id: row.id }
|
|
307
|
+
for (const [key, fld] of Object.entries(e.fields)) {
|
|
308
|
+
if (hidden.has(key)) continue
|
|
309
|
+
if (fld.relation?.kind === 'one-to-many') continue // virtual
|
|
310
|
+
// A page tree's parent: the parent's id (the tree and the breadcrumbs say the rest).
|
|
311
|
+
if (key === e.tree?.parent) {
|
|
312
|
+
out[key] = row[`${key}Id`] ?? null
|
|
313
|
+
continue
|
|
314
|
+
}
|
|
315
|
+
if (fld.relation || fld.type === 'media') {
|
|
316
|
+
if (key in row) out[key] = row[key] // populated; otherwise dropped
|
|
317
|
+
continue
|
|
318
|
+
}
|
|
319
|
+
out[key] = row[key] // scalar
|
|
320
|
+
}
|
|
321
|
+
out.createdAt = row.createdAt
|
|
322
|
+
out.updatedAt = row.updatedAt
|
|
323
|
+
return out
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Which language each item is served in, which languages it exists in and (for
|
|
328
|
+
* collections with an address) its address in each — added to items of collections
|
|
329
|
+
* with translatable fields (mirrored by content-shape).
|
|
330
|
+
*/
|
|
331
|
+
export async function attachLocaleInfo(db: any, reg: CmsRegistry, e: CmsEntity, rows: any[], items: any[], locale: string | null) {
|
|
332
|
+
if (!reg.locales || !e.localized.length) return
|
|
333
|
+
const ids = rows.map((r) => r.id)
|
|
334
|
+
const avail = await availableLocales(db, reg, e, ids)
|
|
335
|
+
// The item's address in each language, for collections that live at an address.
|
|
336
|
+
const paths = e.content?.path ? await localePaths(db, reg, e, ids) : null
|
|
337
|
+
items.forEach((item, i) => {
|
|
338
|
+
item.locale = locale ?? reg.locales!.default
|
|
339
|
+
item.locales = avail.get(rows[i].id) ?? [reg.locales!.default]
|
|
340
|
+
if (paths) item.paths = paths.get(rows[i].id) ?? {}
|
|
341
|
+
})
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Match a localized text column in the requested language: the default-language
|
|
346
|
+
* value, or the translation's (so filters and search work in every language).
|
|
347
|
+
*/
|
|
348
|
+
export function localizedMatch(reg: CmsRegistry, e: CmsEntity, key: string, locale: string | null, cond: (col: any) => any): any {
|
|
349
|
+
const base = cond(e.table[key])
|
|
350
|
+
if (!isTranslationLocale(reg, locale) || !e.localized.includes(key) || !e.localesTable) return base
|
|
351
|
+
const t = e.localesTable
|
|
352
|
+
return or(base, sql`exists (select 1 from ${t} where ${t.parentId} = ${e.table.id} and ${t.locale} = ${locale} and ${cond(t[key])})`)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Galleries as the public API returns them: each item the public media object plus
|
|
357
|
+
* its caption, in order; items whose file is gone are left out (batched across rows).
|
|
358
|
+
*/
|
|
359
|
+
async function populateGalleries(db: any, reg: CmsRegistry, e: CmsEntity, rows: any[], locale: string | null = null) {
|
|
360
|
+
const keys = Object.entries(e.fields).filter(([, f]) => f.type === 'gallery').map(([k]) => k)
|
|
361
|
+
if (!keys.length) return
|
|
362
|
+
const ids = new Set<string>()
|
|
363
|
+
for (const k of keys) for (const r of rows) for (const it of Array.isArray(r[k]) ? r[k] : []) if (typeof it?.mediaId === 'string') ids.add(it.mediaId)
|
|
364
|
+
const byId = new Map<string, any>()
|
|
365
|
+
if (ids.size) {
|
|
366
|
+
const media: any[] = await db.select().from(reg.cmsMedia).where(inArray(reg.cmsMedia.id, [...ids]))
|
|
367
|
+
for (const m of media) byId.set(m.id, mediaProjection(m, locale))
|
|
368
|
+
}
|
|
369
|
+
for (const k of keys) {
|
|
370
|
+
for (const r of rows) {
|
|
371
|
+
r[k] = (Array.isArray(r[k]) ? r[k] : [])
|
|
372
|
+
.filter((it: any) => byId.has(it?.mediaId))
|
|
373
|
+
.map((it: any) => ({ ...byId.get(it.mediaId), caption: typeof it.caption === 'string' && it.caption ? it.caption : null }))
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|