Wpis o schematach
Ta funkcja umożliwia uzyskanie opisu komponentów w postaci schematu Zod z bezpieczeństwem typów, zapewniając:
Dowiedz się więcej: https://zod.dev
Aby pobrać swój schemat Zod, wykonaj:
cms-rendererapps/
web/
app/
page.tsx
scripts/
generated-schema.ts # plik skryptu
generated-schema.tsimport { fetchAllCustomSchemaFields, saveZodSchemaCode } from 'cms-renderer/lib/custom-schemas';
import { cmsConfig } from '../lib/cms-config';
async function main() {
const { cmsUrl, websiteId, apiKey } = cmsConfig;
if (!cmsUrl) {
throw new Error(
'[generate-schemas] NEXT_PUBLIC_CMS_API_URL nie jest ustawiona. Ustaw ją w swoim środowisku lub pliku .env.'
);
}
if (!websiteId) {
throw new Error(
'[generate-schemas] CMS_WEBSITE_ID nie jest ustawione. Ustaw je w swoim środowisku lub pliku .env.'
);
}
if (!apiKey) {
throw new Error(
'[generate-schemas] PROFOUND_API_KEY nie jest ustawiony. Ustaw go w swoim środowisku lub pliku .env.'
);
}
const schemas = await fetchAllCustomSchemaFields({ cmsUrl, websiteId, apiKey });
if (schemas.length === 0) {
throw new Error(
'[generate-schemas] API nie zwróciło żadnych komponentów. Sprawdź, czy PROFOUND_API_KEY jest prawidłowy i wskazuje właściwą witrynę.'
);
}
console.log(
`[generate-schemas] Znaleziono ${schemas.length} komponentów: ${schemas.map((s) => s.name).join(', ')}`
);
await saveZodSchemaCode(schemas, './generated/cms-schemas.ts');
console.log('[generate-schemas] Zakończono.');
}
main().catch((err) => {
console.error('[generate-schemas] Niepowodzenie:', err);
process.exit(1);
});
export const cmsConfig = {
cmsUrl: process.env.NEXT_PUBLIC_CMS_API_URL,
apiKey: process.env.CMS_API_KEY,
websiteId: '...',
};
package.jsonUwaga: Jeśli nie masz pliku
tsconfig.json, usuń flagę--tsconfig.
{
"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",
"...": "..."
}
}
bun run generate-schemas
Przykładowe wyjście:
tsx --tsconfig tsconfig.json scripts/generate-schemas.ts
[generate-schemas] Znaleziono 3 komponenty: post, categories, header
[generate-schemas] Zakończono.
apps/
web/
app/
page.tsx
scripts/
generated-schema.ts
generated/
cms-schemas.ts # wygenerowany schemat Zod
page.tsximport type { PetFoodPost, SiteConfig } from '@/generated/cms-schemas';
import { petFoodPostSchema } from '@/generated/cms-schemas';
// Użyj do bezpiecznego parsowania z typami
petFoodPostSchema.parse(obj);
bun run generate-schemas