fix: avoid exporting loader in index
This commit is contained in:
parent
740bb503b1
commit
0954dcf594
|
|
@ -1,5 +1,5 @@
|
||||||
import { parse } from 'csv-parse/sync';
|
import { parse } from 'csv-parse/sync';
|
||||||
import { parseSchema, createValidator, parseValue } from '../index.js';
|
import { parseSchema, createValidator, parseValue, schemaToTypeString } from '../index.js';
|
||||||
import type { Schema, ReferenceSchema } from '../types.js';
|
import type { Schema, ReferenceSchema } from '../types.js';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
|
@ -403,7 +403,7 @@ function parseReferenceValue(
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parser for reference values (extracts IDs from value string)
|
* Generate TypeScript interface for the CSV data
|
||||||
*/
|
*/
|
||||||
class ReferenceValueParser {
|
class ReferenceValueParser {
|
||||||
private input: string;
|
private input: string;
|
||||||
|
|
@ -490,54 +490,6 @@ class ReferenceValueParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a schema to TypeScript type string
|
|
||||||
*/
|
|
||||||
export function schemaToTypeString(schema: Schema, resourceNames?: Map<string, string>): string {
|
|
||||||
switch (schema.type) {
|
|
||||||
case 'string':
|
|
||||||
return 'string';
|
|
||||||
case 'number':
|
|
||||||
case 'int':
|
|
||||||
case 'float':
|
|
||||||
return 'number';
|
|
||||||
case 'boolean':
|
|
||||||
return 'boolean';
|
|
||||||
case 'stringLiteral':
|
|
||||||
return `"${schema.value}"`;
|
|
||||||
case 'union':
|
|
||||||
return schema.members.map(m => schemaToTypeString(m, resourceNames)).join(' | ');
|
|
||||||
case 'reference': {
|
|
||||||
const typeName = resourceNames?.get(schema.tableName) ||
|
|
||||||
schema.tableName.charAt(0).toUpperCase() + schema.tableName.slice(1);
|
|
||||||
const baseType = schema.isArray ? `${typeName}[]` : typeName;
|
|
||||||
return schema.isOptional ? `${baseType} | null` : baseType;
|
|
||||||
}
|
|
||||||
case 'array':
|
|
||||||
if (schema.element.type === 'tuple') {
|
|
||||||
const tupleElements = schema.element.elements.map((el) => {
|
|
||||||
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
|
||||||
return el.name ? `${el.name}: ${typeStr}` : typeStr;
|
|
||||||
});
|
|
||||||
return `[${tupleElements.join(', ')}][]`;
|
|
||||||
}
|
|
||||||
// Wrap union types in parentheses to maintain correct precedence
|
|
||||||
const elementType = schemaToTypeString(schema.element, resourceNames);
|
|
||||||
if (schema.element.type === 'union') {
|
|
||||||
return `(${elementType})[]`;
|
|
||||||
}
|
|
||||||
return `${elementType}[]`;
|
|
||||||
case 'tuple':
|
|
||||||
const tupleElements = schema.elements.map((el) => {
|
|
||||||
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
|
||||||
return el.name ? `${el.name}: ${typeStr}` : typeStr;
|
|
||||||
});
|
|
||||||
return `[${tupleElements.join(', ')}]`;
|
|
||||||
default:
|
|
||||||
return 'unknown';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate TypeScript interface for the CSV data
|
* Generate TypeScript interface for the CSV data
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { parseSchema } from './parser';
|
import { parseSchema } from './parser';
|
||||||
import { parseValue, createValidator } from './validator';
|
import { parseValue, createValidator, schemaToTypeString } from './validator';
|
||||||
import type { Schema, PrimitiveSchema, TupleSchema, ArraySchema, ReferenceSchema, StringLiteralSchema, UnionSchema, ParsedSchema } from './types';
|
import type { Schema, PrimitiveSchema, TupleSchema, ArraySchema, ReferenceSchema, StringLiteralSchema, UnionSchema, ParsedSchema } from './types';
|
||||||
import { ParseError } from './parser';
|
import { ParseError } from './parser';
|
||||||
|
|
||||||
|
|
@ -14,6 +14,5 @@ export function defineSchema(schemaString: string): ParsedSchema {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export { parseSchema, parseValue, createValidator, ParseError };
|
export { parseSchema, parseValue, createValidator, ParseError, schemaToTypeString };
|
||||||
export { schemaToTypeString } from './csv-loader/loader.js';
|
|
||||||
export type { Schema, PrimitiveSchema, TupleSchema, ArraySchema, ReferenceSchema, StringLiteralSchema, UnionSchema, ParsedSchema };
|
export type { Schema, PrimitiveSchema, TupleSchema, ArraySchema, ReferenceSchema, StringLiteralSchema, UnionSchema, ParsedSchema };
|
||||||
|
|
|
||||||
|
|
@ -393,6 +393,50 @@ export function parseValue(schema: Schema, valueString: string): unknown {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function schemaToTypeString(schema: Schema, resourceNames?: Map<string, string>): string {
|
||||||
|
switch (schema.type) {
|
||||||
|
case 'string':
|
||||||
|
return 'string';
|
||||||
|
case 'number':
|
||||||
|
case 'int':
|
||||||
|
case 'float':
|
||||||
|
return 'number';
|
||||||
|
case 'boolean':
|
||||||
|
return 'boolean';
|
||||||
|
case 'stringLiteral':
|
||||||
|
return `"${schema.value}"`;
|
||||||
|
case 'union':
|
||||||
|
return schema.members.map(m => schemaToTypeString(m, resourceNames)).join(' | ');
|
||||||
|
case 'reference': {
|
||||||
|
const typeName = resourceNames?.get(schema.tableName) ||
|
||||||
|
schema.tableName.charAt(0).toUpperCase() + schema.tableName.slice(1);
|
||||||
|
const baseType = schema.isArray ? `${typeName}[]` : typeName;
|
||||||
|
return schema.isOptional ? `${baseType} | null` : baseType;
|
||||||
|
}
|
||||||
|
case 'array':
|
||||||
|
if (schema.element.type === 'tuple') {
|
||||||
|
const tupleElements = schema.element.elements.map((el) => {
|
||||||
|
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
||||||
|
return el.name ? `${el.name}: ${typeStr}` : typeStr;
|
||||||
|
});
|
||||||
|
return `[${tupleElements.join(', ')}][]`;
|
||||||
|
}
|
||||||
|
const elementType = schemaToTypeString(schema.element, resourceNames);
|
||||||
|
if (schema.element.type === 'union') {
|
||||||
|
return `(${elementType})[]`;
|
||||||
|
}
|
||||||
|
return `${elementType}[]`;
|
||||||
|
case 'tuple':
|
||||||
|
const tupleElements = schema.elements.map((el) => {
|
||||||
|
const typeStr = schemaToTypeString(el.schema, resourceNames);
|
||||||
|
return el.name ? `${el.name}: ${typeStr}` : typeStr;
|
||||||
|
});
|
||||||
|
return `[${tupleElements.join(', ')}]`;
|
||||||
|
default:
|
||||||
|
return 'unknown';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createValidator(schema: Schema): (value: unknown) => boolean {
|
export function createValidator(schema: Schema): (value: unknown) => boolean {
|
||||||
return function validate(value: unknown): boolean {
|
return function validate(value: unknown): boolean {
|
||||||
switch (schema.type) {
|
switch (schema.type) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue