Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): give component() the CLI's detail and lang validation (#6576)

Joey7ae45e8c3f43
diff --git a/.changeset/cli-spec-component-api-control-values.md b/.changeset/cli-spec-component-api-control-values.md
new file mode 100644
index 0000000000..ace7594883
--- /dev/null
+++ b/.changeset/cli-spec-component-api-control-values.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] The programmatic `component()` API now rejects `detail` and `lang` values that the `astryx component` command rejects, with the same codes (`ERR_INVALID_DETAIL`, `ERR_INVALID_LANG`). It used to fall back silently: an unknown `detail` returned the name list, and an unknown `lang` returned English.
+
+@josephfarina
diff --git a/packages/cli/api/component/component.doc.mjs b/packages/cli/api/component/component.doc.mjs
index 91d9feccc5..7f0e70fc13 100644
--- a/packages/cli/api/component/component.doc.mjs
+++ b/packages/cli/api/component/component.doc.mjs
@@ -91,7 +91,8 @@ export const doc = {
{
name: 'options.lang',
type: 'string',
- description: 'Language code for localized doc content.',
+ description:
+ "Language code for localized doc content: 'en', 'zh', or 'dense'.",
},
{
name: 'options.zh',
@@ -135,6 +136,14 @@ export const doc = {
},
],
throws: [
+ {
+ code: 'ERR_INVALID_DETAIL',
+ when: "options.detail is not 'full', 'compact', or 'brief'",
+ },
+ {
+ code: 'ERR_INVALID_LANG',
+ when: "options.lang is set to anything other than 'en', 'zh', or 'dense'",
+ },
{
code: 'ERR_CORE_NOT_FOUND',
when: '@astryxdesign/core cannot be resolved from cwd',
diff --git a/packages/cli/api/component/component.mjs b/packages/cli/api/component/component.mjs
index 3f946d47e8..8e142c8c12 100644
--- a/packages/cli/api/component/component.mjs
+++ b/packages/cli/api/component/component.mjs
@@ -34,6 +34,11 @@ import {componentDetailSource} from './detail/source/source.mjs';
import {componentDetailShowcase} from './detail/showcase/showcase.mjs';
import {componentDetailBlocks} from './detail/blocks/blocks.mjs';
+/** @type {ReadonlyArray<string>} */
+const DETAIL_LEVELS = ['full', 'compact', 'brief'];
+/** @type {ReadonlyArray<string>} */
+const LANGS = ['en', 'zh', 'dense'];
+
/**
* @param {string} [name]
* @param {object} [options]
@@ -81,6 +86,23 @@ export async function component(name, options = {}) {
const isListView = list || category != null || !name;
const detail = detailOption ?? (isListView ? 'brief' : 'full');
+ // Same accepted values and codes as the CLI's --detail and --lang, checked
+ // first as the CLI parser does.
+ if (!DETAIL_LEVELS.includes(detail)) {
+ throw new AstryxError(
+ `Invalid detail "${String(detail)}". Valid levels: ${DETAIL_LEVELS.join(', ')}`,
+ undefined,
+ ERROR_CODES.ERR_INVALID_DETAIL,
+ );
+ }
+ if (lang != null && !LANGS.includes(lang)) {
+ throw new AstryxError(
+ `Invalid lang "${String(lang)}". Valid values: ${LANGS.join(', ')}`,
+ undefined,
+ ERROR_CODES.ERR_INVALID_LANG,
+ );
+ }
+
const coreDir = requireCoreDir(cwd);
// A public API caller could pass a non-string category; the list leaf does
diff --git a/packages/cli/api/component/component.test.mjs b/packages/cli/api/component/component.test.mjs
index e2e3c27ed0..cec562c1d3 100644
--- a/packages/cli/api/component/component.test.mjs
+++ b/packages/cli/api/component/component.test.mjs
@@ -14,6 +14,7 @@ import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
import {component} from './component.mjs';
import {AstryxError} from '../error.mjs';
+import {runCli} from '../../test-utils/run-cli.mjs';
// api/component/ -> up 4 = repo root (has packages/core, which findCoreDir walks to).
const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../..');
@@ -161,3 +162,40 @@ describe('component dispatcher — category guard', () => {
}
}, SLOW);
});
+
+describe('component dispatcher — detail and lang guards', () => {
+ // `detail` and `lang` are the same controls as the CLI's --detail and --lang:
+ // same accepted values, same error codes.
+ it('rejects a detail level the CLI rejects, with the same code', async () => {
+ const cliRun = await runCli(['--json', 'component', '--list', '--detail', 'bogus'], cwd);
+ expect(cliRun.code).toBe(1);
+ expect(JSON.parse(cliRun.stdout).code).toBe('ERR_INVALID_DETAIL');
+
+ const bogus = /** @type {any} */ ('bogus');
+ for (const call of [
+ () => component(undefined, {cwd, list: true, detail: bogus}),
+ () => component('Button', {cwd, detail: bogus}),
+ ]) {
+ const err = await call().catch(e => e);
+ expect(err).toBeInstanceOf(AstryxError);
+ expect(err.code).toBe('ERR_INVALID_DETAIL');
+ }
+ }, SLOW);
+
+ it('rejects a lang the CLI rejects, with the same code', async () => {
+ const cliRun = await runCli(['--json', 'component', 'Button', '--lang', 'fr'], cwd);
+ expect(cliRun.code).toBe(1);
+ expect(JSON.parse(cliRun.stdout).code).toBe('ERR_INVALID_LANG');
+
+ for (const call of [
+ () => component('Button', {cwd, lang: 'fr'}),
+ () => component(undefined, {cwd, list: true, detail: 'compact', lang: 'fr'}),
+ ]) {
+ const err = await call().catch(e => e);
+ expect(err).toBeInstanceOf(AstryxError);
+ expect(err.code).toBe('ERR_INVALID_LANG');
+ }
+ const zh = await component('Button', {cwd, lang: 'zh'});
+ expect(zh.type).toBe('component.detail');
+ }, SLOW);
+});