New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@openpkg-ts/extract

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@openpkg-ts/extract

TypeScript export extraction to OpenPkg spec

latest
Source
npmnpm
Version
0.29.0
Version published
Maintainers
1
Created
Source

@openpkg-ts/extract

TypeScript API extraction library. Generates OpenPkg specs from TypeScript source code with JSON Schema 2020-12 output.

Install

npm install @openpkg-ts/extract

CLI Usage

# Extract API spec from entry point
tspec src/index.ts -o openpkg.json

# With runtime schema extraction (Zod, Valibot, etc.)
tspec src/index.ts --runtime

# With options
tspec src/index.ts --max-depth 4 --verbose

Programmatic Usage

import { extract } from '@openpkg-ts/extract';

const result = await extract({
  entryFile: 'src/index.ts',
  maxTypeDepth: 4,
  resolveExternalTypes: true,
});

console.log(`Extracted ${result.spec.exports.length} exports`);
console.log(`Found ${result.spec.types?.length ?? 0} types`);

// Check for diagnostics
for (const diag of result.diagnostics) {
  console.warn(`${diag.severity}: ${diag.message}`);
}

Options

OptionTypeDefaultDescription
entryFilestringrequiredEntry point file path
baseDirstringcwdBase directory for resolution
maxTypeDepthnumber4Max depth for type traversal
resolveExternalTypesbooleantrueResolve types from node_modules
schemaExtraction'static' | 'hybrid''static'Schema extraction mode
schemaTarget'draft-2020-12' | 'draft-07' | 'openapi-3.0''draft-2020-12'Target JSON Schema dialect
onlystring[]-Only extract these exports (supports * wildcards)
ignorestring[]-Ignore these exports (supports * wildcards)

JSON Schema 2020-12 Output

All schema output is normalized to valid JSON Schema 2020-12. This ensures consistency between static TypeScript analysis and runtime schema extraction from libraries like Zod and Valibot.

Interface/Class Output Format

Interfaces and classes include a schema property containing a JSON Schema object representation:

{
  "kind": "interface",
  "name": "User",
  "schema": {
    "type": "object",
    "properties": {
      "id": { "type": "string" },
      "age": { "type": "number" },
      "email": { "type": "string" }
    },
    "required": ["id", "email"]
  },
  "members": [...]
}

TypeScript Extension Fields (x-ts-*)

TypeScript constructs that don't map directly to JSON Schema are preserved using extension fields:

ExtensionPurposeExample
x-ts-typePreserves original TypeScript type{ "type": "integer", "x-ts-type": "bigint" }
x-ts-functionMarks function types{ "x-ts-function": true, "x-ts-signatures": [...] }
x-ts-signaturesFunction/method signaturesArray of signature objects with parameters and returns
x-ts-type-argumentsGeneric type arguments{ "$ref": "#/types/Promise", "x-ts-type-arguments": [{ "type": "string" }] }
x-ts-accessorGetter/setter markers{ "type": "string", "x-ts-accessor": "getter" }

Type Mappings

TypeScript TypeJSON Schema Output
void{ "type": "null" }
never{ "not": {} }
any{}
unknown{}
undefined{ "type": "null" }
bigint{ "type": "integer", "x-ts-type": "bigint" }
symbol{ "type": "string", "x-ts-type": "symbol" }
[T, U] (tuple){ "type": "array", "prefixedItems": [...], "minItems": 2, "maxItems": 2 }
() => T (function){ "x-ts-function": true, "x-ts-signatures": [...] }
Promise<T>{ "$ref": "#/types/Promise", "x-ts-type-arguments": [<T schema>] }

Example: Function Schema

{
  "kind": "function",
  "name": "fetchUser",
  "signatures": [{
    "parameters": [{
      "name": "id",
      "schema": { "type": "string" },
      "required": true
    }],
    "returns": {
      "schema": {
        "$ref": "#/types/Promise",
        "x-ts-type-arguments": [{ "$ref": "#/types/User" }]
      }
    }
  }]
}

Example: Interface with Methods

{
  "kind": "interface",
  "name": "Repository",
  "schema": {
    "type": "object",
    "properties": {
      "id": { "type": "string" },
      "find": {
        "x-ts-function": true,
        "x-ts-signatures": [{
          "parameters": [{ "name": "query", "schema": { "type": "string" } }],
          "returns": { "schema": { "type": "array", "items": { "$ref": "#/types/Item" } } }
        }]
      }
    },
    "required": ["id", "find"]
  }
}

Exports

Core

  • extract(options) - Main extraction function

AST Utilities

  • getModuleExports - Get exports from a module
  • resolveExportTarget - Resolve re-exports to source

Type Utilities

  • TypeRegistry - Track and dedupe extracted types
  • serializeType - Convert TS types to schema

Schema Normalizer

  • normalizeSchema(schema, options) - Convert SpecSchema to JSON Schema 2020-12
  • normalizeExport(exp, options) - Normalize a SpecExport including nested schemas
  • normalizeType(type, options) - Normalize a SpecType including nested schemas
  • normalizeMembers(members, options) - Convert members array to JSON Schema properties

Schema Adapters

  • ZodAdapter, ValibotAdapter - Runtime schema extraction

How It Works

  • Creates a TypeScript program from the entry file
  • Extracts all exported symbols
  • Serializes each export (functions, classes, types, variables)
  • Resolves type references and builds a type registry
  • Normalizes all schemas to JSON Schema 2020-12
  • Outputs an OpenPkg-compliant JSON spec

License

MIT

Keywords

openpkg

FAQs

Package last updated on 13 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts