parse-api-doc — Intelligent API Document Field Parser
Parse any format of API/field documentation into a structured field list + field mapping, used for integrating third-party systems. Pure function implementation (Node ESM, zero dependencies), can be directly imported, and serves as a reference implementation for AI-driven field extraction.
Use Cases
- When you receive API documentation from an ERP / open platform / internal middleware and need to quickly know "what fields exist, their types, and which are required"
- When you need to map fields from an external document to your own collection/storage fields (fuzzy matching aliases for inconsistent field names)
- When extracting fields in batch from Markdown tables, JSON Schema, sample JSON, or plain-text field lists
Supported Input Formats (Auto-Detected)
- Markdown table:
| Field | Type | Required | Description | - JSON Schema: Contains
"properties"+"type" - Sample JSON:
{ "asin": "B0XXX", "title": "Title" } - Plain-text field list:
asin: Product ID/title (string) Product Title
Usage
import { parseErpDocument, matchField, loadVocabulary } from './scripts/doc-parser.mjs';
const doc = `
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| asin | string | Yes | Product ID |
| title | string | No | Product Title |
`;
const result = parseErpDocument(doc);
console.log(result.detectedFormat); // markdown_table
console.log(result.productFields); // [{name,type,required,description}]
console.log(result.fieldMappings); // [{erpField,erpType,erpRequired,erpDescription,collectorField}]
console.log(result.endpoint); // Automatically inferred method+path
Core API
parseErpDocument(content, options?)→DocParseResultoptions.vocabulary: Custom field vocabulary (default includes a common set of e-commerce/API fields; inject your own)- Returns:
{ success, detectedFormat, productFields[], endpoint, requestStructure, responseStructure, fieldMappings[], error? }
matchField(name, vocabulary?)→ Matched standard field key (fuzzy/alias matching, normalized case and_-)loadVocabulary(text)→ Build a vocabulary from text (for ad-hoc extension)
Field Mapping Vocabulary (Injectable)
The default vocabulary covers common fields (asin/title/price/rating/bsr/image/brand...). When integrating with your own system, pass your custom vocabulary:
const myVocab = [
{ key: 'sku', aliases: ['sku', '货号', 'SKU'] },
{ key: 'stock', aliases: ['stock', '库存', 'quantity'] },
];
parseErpDocument(doc, { vocabulary: myVocab });
Notes
- On parse failure,
success=falseand anerror.codeis provided (EMPTY_CONTENT/PARSE_ERROR/NO_FIELDS_FOUND), along witherror.suggestionoffering targeted fix advice. - This is a structure extraction tool; it does not make network calls or send any data—purely local parsing.