์คํค๋ง์ ๊ดํ ๊ฒ์๋ฌผ
์ด ๊ธฐ๋ฅ์ ๊ตฌ์ฑ ์์ ์ค๋ช ์ ํ์ ์์ ํ Zod ์คํค๋ง ํํ๋ก ๊ฐ์ ธ์ฌ ์ ์๊ฒ ํด์ฃผ๋ฉฐ, ๋ค์์ ์ ๊ณตํฉ๋๋ค:
์์ธํ ์์๋ณด๊ธฐ: https://zod.dev
Zod ์คํค๋ง๋ฅผ ๊ฐ์ ธ์ค๋ ค๋ฉด ๋ค์์ ์ํํฉ๋๋ค:
cms-renderer์ ์ ํธ๋ฆฌํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํฉ๋๋คapps/
web/
app/
page.tsx
scripts/
generated-schema.ts # ์คํฌ๋ฆฝํธ ํ์ผ
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์ด ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋๋ .env ํ์ผ์์ ์ค์ ํ์ธ์.'
);
}
if (!websiteId) {
throw new Error(
'[generate-schemas] CMS_WEBSITE_ID๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋๋ .env ํ์ผ์์ ์ค์ ํ์ธ์.'
);
}
if (!apiKey) {
throw new Error(
'[generate-schemas] PROFOUND_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ ๋๋ .env ํ์ผ์์ ์ค์ ํ์ธ์.'
);
}
const schemas = await fetchAllCustomSchemaFields({ cmsUrl, websiteId, apiKey });
if (schemas.length === 0) {
throw new Error(
'[generate-schemas] API์์ ๋ฐํ๋ ๊ตฌ์ฑ ์์๊ฐ ์์ต๋๋ค. PROFOUND_API_KEY๊ฐ ์ ํจํ๋ฉฐ ์ฌ๋ฐ๋ฅธ ์น์ฌ์ดํธ๋ฅผ ๊ฐ๋ฆฌํค๋์ง ํ์ธํ์ธ์.'
);
}
console.log(
`[generate-schemas] ${schemas.length}๊ฐ์ ๊ตฌ์ฑ ์์๋ฅผ ์ฐพ์์ต๋๋ค: ${schemas.map((s) => s.name).join(', ')}`
);
await saveZodSchemaCode(schemas, './generated/cms-schemas.ts');
console.log('[generate-schemas] ์๋ฃ.');
}
main().catch((err) => {
console.error('[generate-schemas] ์คํจ:', err);
process.exit(1);
});
export const cmsConfig = {
cmsUrl: process.env.NEXT_PUBLIC_CMS_API_URL,
apiKey: process.env.CMS_API_KEY,
websiteId: '...',
};
package.json ๊ตฌ์ฑ์ฐธ๊ณ :
tsconfig.json์ด ์๋ค๋ฉด--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
์์ ์ถ๋ ฅ:
tsx --tsconfig tsconfig.json scripts/generate-schemas.ts
[generate-schemas] ๊ตฌ์ฑ ์์ 3๊ฐ๋ฅผ ์ฐพ์์ต๋๋ค: post, categories, header
[generate-schemas] ์๋ฃ.
apps/
web/
app/
page.tsx
scripts/
generated-schema.ts
generated/
cms-schemas.ts # ์์ฑ๋ Zod ์คํค๋ง
page.tsx์์์ ์ฌ์ฉ๋ฒimport type { PetFoodPost, SiteConfig } from '@/generated/cms-schemas';
import { petFoodPostSchema } from '@/generated/cms-schemas';
// ํ์
์์ ํ ํ์ฑ์ ์ฌ์ฉ
petFoodPostSchema.parse(obj);
bun run generate-schemas