icore Node.js
在此页面上

准备输出值

createPresentation() 创建输出值并进行格式化。当所有格式可以使用同一个值时,调用 presentation.render(...)。如果 JSON、表格和 CSV 需要不同的数据,则调用各自的渲染函数。

空输出和文本输出

presentation.empty() 表示命令成功且输出为空。presentation.text(...) 接收已准备好的文本。

TypeScript
import { createPresentation } from 'icore';

const presentation = createPresentation();

const noOutput = presentation.empty();
const readyText = presentation.text('cache cleared\n');

选择 table、json 或 csv 时,这些值仍保留原有形式。

记录

JSON、表格和 CSV 使用相同顶层字段时,可以使用 record(...) 和 records(...)。

TypeScript
const oneJob = presentation.record({
  id: 'job-42',
  status: 'running'
});

const manyJobs = presentation.records([
  {
    id: 'job-41',
    status: 'done'
  },
  {
    id: 'job-42',
    status: 'running'
  }
]);

presentation.render(oneJob, 'table');
presentation.render(manyJobs, 'json');

PresentationRecord 支持任意字符串键。将普通 TypeScript 接口中所需的字段映射到对象字面量,可以直接传入,无需类型断言。

TypeScript
interface JobReportRow {
  id: string;
  status: string;
  internalTraceId: string;
}

function presentJobs(rows: readonly JobReportRow[]) {
  return presentation.records(rows.map((row) => ({
    id: row.id,
    status: row.status
  })));
}

在表格格式下,record(row) 生成包含 field 和 value 两列的竖向表格。使用 records([row]) 可以生成以字段名为表头的横向表格。对于 records([]),JSON 输出 [],表格和 CSV 输出空文本。若希望在无数据行时显示表头,请将表头显式传给表格或 CSV 渲染函数。

已准备好的表格和 CSV 单元格

将已准备好的文本单元格传给 table(...),将标量值的行传给 csv(...)。列顺序和单元格值由应用决定。

TypeScript
const table = presentation.table([
  ['id', 'status'],
  ['job-41', 'done'],
  ['job-42', 'running']
]);

const csv = presentation.csv([
  ['id', 'status'],
  ['job-41', 'done'],
  ['job-42', 'running']
]);

文本表格根据 JavaScript 字符串长度对齐。ANSI 序列、表情符号、组合字符、制表符和全角字符可能显示错位。列对齐仅用于文本表格。

验证请求的格式

可以使用 presentation.formats、presentationFormats 和 isPresentationFormat(...) 获取并验证支持的格式。

TypeScript
import { isPresentationFormat, presentationFormats } from 'icore';

function normalizeFormat(value: unknown) {
  return isPresentationFormat(value) ? value : 'table';
}

presentation.formats;
presentationFormats;

渲染输出值

presentation.render(...) 调用 renderPresentationResult(...)。独立函数适合已取得输出值的测试或适配器。

TypeScript
import { renderPresentationResult } from 'icore';

const view = presentation.records([
  {
    id: 'job-42',
    status: 'running'
  }
]);

const fromFacade = presentation.render(view, 'csv');
const fromPrimitive = renderPresentationResult(view, 'csv');

检查输出值

在渲染未知结果前,使用 isPresentationResult(...) 进行检查。

TypeScript
import { isPresentationResult } from 'icore';

function renderUnknown(value: unknown): string {
  if (!isPresentationResult(value)) {
    throw new Error('Expected presentation result');
  }

  return presentation.render(value, 'table');
}

直接调用渲染函数

createPresentation() 提供针对已按格式准备的数据的渲染函数。

TypeScript
const jsonText = presentation.renderers.json.render({
  ok: true
});

const tableText = presentation.renderers.table.render([
  ['field', 'value'],
  ['ok', 'true']
]);

const csvRow = presentation.renderers.csv.renderRow([
  'job-42',
  'running'
]);

const csvText = presentation.renderers.csv.render([
  ['id', 'status'],
  ['job-42', 'running']
]);

同样的渲染函数也可以独立导入。

TypeScript
import { renderCsv, renderCsvRow, renderJson, renderTextTable } from 'icore';

renderJson({ ok: true });
renderTextTable([
  ['field', 'value'],
  ['ok', 'true']
]);
renderCsvRow(['job-42', 'running']);
renderCsv([
  ['id', 'status'],
  ['job-42', 'running']
]);

直接调用时,JSON 可以保留完整的嵌套报告,表格和 CSV 则可以选择自己的列并格式化单元格。应用读取选项后选择对应的渲染函数。

TypeScript
const jsonText = renderJson(report);
const tableText = renderTextTable([
  ['id', 'status'],
  ...report.orders.map((order) => [
    order.id,
    order.status
  ])
]);