profound-logoProfound CMS
⌘K
Admin
Theme
DocsTutorialBlogPhilosophy
DocsTutorialBlogPhilosophy

Hybrid

Collection of Pages with ComponentsTypes of ComponentsSetup server sent events (SSE) content refetchInstall Profound CMS as a proxyCEL Scripting in Template BuilderProject ScaffoldingBibliotecă media

Fără interfață

Pornire rapidăSplit Screen JSON Component Builder with LLMComponent Zod Pull

API REST

Prezentare generală API RESTgetConnect your websitegetGET /routesgetGET /routegetGET /blocksgetObține blocuri cu cache CELgetGET /blocks/generatedgetGET /componentsgetGET /components/{name}getGET /dataset/{schema_name}getGET /content-changes (SSE)patchPATCH /dataset/{schema_name}postPOST /translationpatchPATCH /translationsgetGET /usagepostPOST /csvpatchPATCH /csv
All Systems Operational
Powered Byprofound-logo
Theme

Component Zod Pull

Extrage descrierea Zod a componentelor tale în timpul compilării folosind un script

Această funcționalitate îți permite să obții descrierea componentei tale sub forma unei descrieri „Zod” type-safe, care oferă validare de schemă orientată pe TypeScript și inferență statică de tip pentru conținutul tău.

https://zod.dev

Pentru a prelua schema noastră Zod, vom încărca câteva dependențe și vom crea un script care folosește câteva utilitare din cms-renderer

Să începem cu structura proiectului nostru

apps/
 web/
 app/
   page.tsx
 scripts/
   generate-schemas.ts // aici se află scriptul nostru

Acum vom edita acest fișier generated-schema.ts

import { fetchAllCustomSchemaFields, saveZodSchemaCode } from 'cms-renderer/lib/custom-schemas';
import { cmsConfig } from '../lib/cms-config'; // importăm configurația mea

async function main() {
  const { cmsUrl, websiteId, apiKey } = cmsConfig;

  if (!cmsUrl) {
    throw new Error(
      '[generate-schemas] NEXT_PUBLIC_CMS_API_URL nu este setată. Seteaz-o în mediul tău sau în fișierul .env.'
    );
  }
  if (!websiteId) {
    throw new Error(
      '[generate-schemas] NEXT_PUBLIC_PROFOUND_WEBSITE_ID nu este setată. Seteaz-o în mediul tău sau în fișierul .env.'
    );
  }
  if (!apiKey) {
    throw new Error(
      '[generate-schemas] PROFOUND_API_KEY nu este setată. Seteaz-o în mediul tău sau în fișierul .env.'
    );
  }

  const schemas = await fetchAllCustomSchemaFields({ cmsUrl, websiteId, apiKey });

  if (schemas.length === 0) {
    throw new Error(
      '[generate-schemas] Nicio componentă nu a fost returnată de API. Verifică faptul că PROFOUND_API_KEY este validă și indică site-ul corect.'
    );
  }

  await saveZodSchemaCode(schemas, './generated/cms-schemas.ts');

  console.log('[generate-schemas] Gata.');
}

main().catch((err) => {
 console.error('[generate-schemas] A eșuat:', err);
  process.exit(1);
});

Configurația ta CmsConfig ar trebui să arate astfel

export const cmsConfig = {
  cmsUrl: process.env.NEXT_PUBLIC_CMS_API_URL,
  apiKey: process.env.CMS_API_KEY,
  websiteId: '...',
};

În interiorul fișierului nostru package.json, definim modul în care rulăm scriptul și câteva dependențe

NOTĂ: Dacă nu ai un tsconfig, atunci poți elimina flagul --tsconfig și valoarea sa

{
  "name": "web",
  "version": "0.1.0",
  "type": "module",
  "private": true,
  "scripts": {
    "generate-schemas": "tsx --tsconfig tsconfig.json scripts/generate-schemas.ts",
    "...": "..."
  },
  "dependencies": {
    "cms-renderer": "0.3.1",
    "zod": "^4.3.6",
    "...": "..."
  },
  "devDependencies": {
    "tsx": "^4.21.0",
 "object-hash": "^3.0.0",
    "...": "..."
  }
}

Acum, după ce rulăm

$ bun run generate-schemas
tsx --tsconfig tsconfig.json scripts/generate-schemas.ts
[generate-schemas] Gata.

Ar trebui să obținem un rezultat similar și putem vedea un fișier nou în structura proiectului nostru

apps/
 web/
 app/
   page.tsx
 scripts/
   generated-schema.ts // aici se află scriptul nostru
 generated/
   cms-schema.ts // schemă zod generată

Poți inspecta fișierul și să vezi cum arată schema ta. Pentru a actualiza acest fișier, trebuie să rulezi din nou comanda bun run generate-schemas, pe care o poți conecta la fluxul tău de lucru dev sau build.

Putem apoi să importăm aceste obiecte Zod în fișierul nostru page.tsx astfel

import type { PetFoodPost, SiteConfig } from '@/generated/cms-schemas';
import { petFoodPostSchema } from '@/generated/cms-schemas';

// apelează petFoodPostSchema.parse(obj) pentru parsarea type-safe a obiectelor

Poți actualiza componenta din panoul de administrare și să preiei din nou schema actualizată pentru un proces de dezvoltare facil

Continue Reading
Previous‹Split Screen JSON Component Builder with LLM