CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-schema-dts

TypeScript definitions and generator for Schema.org vocabulary with strongly-typed JSON-LD support

Pending
Overview
Eval results
Files

schema-generation.mddocs/

Schema Generation

Core functionality for generating TypeScript definitions from RDF ontologies using schema-dts-gen. This package provides both programmatic APIs and a command-line interface for converting Schema.org or custom ontologies into strongly-typed TypeScript definitions.

Capabilities

WriteDeclarations Function

Main function for generating TypeScript declarations from RDF triple data.

/**
 * Generate TypeScript declarations from RDF triple data
 * @param graph - N3 Store containing RDF triples
 * @param includeDeprecated - Whether to include deprecated classes and properties
 * @param context - Context object for JSON-LD context configuration
 * @param write - Callback function to write generated TypeScript content
 */
function WriteDeclarations(
  graph: Store,
  includeDeprecated: boolean,
  context: Context,
  write: (content: string) => Promise<void> | void
): Promise<void>;

Usage Examples:

import { WriteDeclarations, loadTriples } from "schema-dts-gen";
import { Context } from "schema-dts-gen";

// Generate TypeScript from remote ontology
const graph = await loadTriples("https://schema.org/version/latest/schemaorg-all-https.nt");
const context = Context.Parse("https://schema.org");

await WriteDeclarations(graph, false, context, (content) => {
  // Write to file or stdout
  process.stdout.write(content);
});

Triple Loading Functions

Functions for loading RDF triples from various sources.

/**
 * Load RDF triples from a remote HTTPS URL
 * @param url - HTTPS URL to .nt file containing RDF triples
 * @returns Promise resolving to N3 Store with loaded triples
 */
function loadTriples(url: string): Promise<Store>;

/**
 * Load RDF triples from a local file
 * @param path - Local file path to .nt file
 * @returns Promise resolving to N3 Store with loaded triples
 */
function loadFile(path: string): Promise<Store>;

Usage Examples:

import { loadTriples, loadFile } from "schema-dts-gen";

// Load from remote URL
const remoteGraph = await loadTriples("https://schema.org/version/latest/schemaorg-all-https.nt");

// Load from local file
const localGraph = await loadFile("./my-ontology.nt");

Context Management

Configuration system for JSON-LD context handling.

/**
 * Context configuration for JSON-LD generation
 */
class Context {
  /**
   * Parse context specification string into Context object
   * @param contextSpec - Context specification (URL or key:value pairs)
   */
  static Parse(contextSpec: string): Context;
  
  /**
   * Get properly scoped name for a Schema.org entity
   * @param node - RDF NamedNode to get scoped name for
   */
  getScopedName(node: NamedNode): string;
  
  /**
   * Generate @context property for TypeScript interface
   */
  contextProperty(): PropertySignature;
}

Usage Examples:

import { Context } from "schema-dts-gen";

// Simple single-context
const simpleContext = Context.Parse("https://schema.org");

// Multi-namespace context
const multiContext = Context.Parse("rdf:http://www.w3.org/2000/01/rdf-schema,schema:https://schema.org");

Command Line Interface

Schema-dts-gen provides a comprehensive CLI for generating TypeScript definitions.

# Basic usage
npx schema-dts-gen [options]

# Options:
--ontology <url>          # HTTPS URL to .nt file (default: Schema.org latest)
--file <path>             # Local .nt file path
--context <spec>          # JSON-LD context specification
--verbose / --noverbose   # Enable/disable verbose logging
--deprecated / --nodeprecated  # Include/exclude deprecated types

CLI Examples:

# Generate from default Schema.org ontology
npx schema-dts-gen > schema.ts

# Generate from custom ontology
npx schema-dts-gen --ontology=https://example.com/custom.nt > custom.ts

# Generate from local file with verbose output
npx schema-dts-gen --file=./ontology.nt --verbose > local-schema.ts

# Generate with custom context, excluding deprecated types
npx schema-dts-gen --context="custom:https://example.com" --nodeprecated > clean-schema.ts

# Multi-namespace context
npx schema-dts-gen --context="rdf:http://www.w3.org/2000/01/rdf-schema,schema:https://schema.org" > multi-ns-schema.ts

Type System Classes

Core classes for representing Schema.org entities in the TypeScript generation process.

/**
 * Represents a Schema.org class/type
 */
class Class {
  subject: string;
  comment: string;
  deprecated: boolean;
  supersededBy?: string;
  
  /**
   * Generate TypeScript interface declaration
   */
  generateInterface(): string;
}

/**
 * Represents a Schema.org property
 */
class Property {
  subject: string;
  comment: string;
  deprecated: boolean;
  supersededBy?: string;
  
  /**
   * Generate TypeScript property signature
   */
  generateProperty(): string;
}

/**
 * Represents a Schema.org enumeration value
 */
class EnumValue {
  subject: string;
  comment: string;
  
  /**
   * Generate TypeScript enum member
   */
  generateEnumMember(): string;
}

Utility Functions

Helper functions for working with RDF terms and Schema.org concepts.

/**
 * Get name from RDF term using context
 */
function nameFromContext(term: NamedNode, context: string): string | null;

/**
 * Get the named portion of an RDF term
 */
function namedPortion(term: NamedNode): string;

/**
 * Get short string representation of RDF term
 */
function shortStr(term: Term): string;

Logging System

Configurable logging system for debugging and verbose output.

/**
 * Configure logging options
 */
function SetOptions(options: { verbose: boolean }): void;

/**
 * Log a message (respects verbose setting)
 */
function Log(message: string): void;

/**
 * Set custom logger function
 * @returns Function to restore previous logger
 */
function SetLogger(newLogger: (msg: string) => void): () => void;

Usage Examples:

import { SetOptions, Log, SetLogger } from "schema-dts-gen";

// Enable verbose logging
SetOptions({ verbose: true });

// Log a message
Log("Processing ontology...");

// Use custom logger
const restoreLogger = SetLogger((msg) => {
  console.error(`[CUSTOM] ${msg}`);
});

// Restore default logger
restoreLogger();

Install with Tessl CLI

npx tessl i tessl/npm-schema-dts

docs

core-types.md

creative-work.md

events.md

index.md

person-organization.md

place-location.md

products-commerce.md

roles-relationships.md

schema-generation.md

tile.json