@openpkg-ts/extract 0.13.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin/tspec.js +3 -3
- package/dist/shared/{chunk-ksf9k654.js → chunk-khwn5myc.js} +343 -215
- package/dist/src/index.d.ts +201 -57
- package/dist/src/index.js +361 -43
- package/package.json +1 -1
package/dist/src/index.d.ts
CHANGED
|
@@ -8,24 +8,17 @@ declare class TypeRegistry {
|
|
|
8
8
|
has(id: string): boolean;
|
|
9
9
|
getAll(): SpecType[];
|
|
10
10
|
/**
|
|
11
|
-
* Register a type from a ts.Type
|
|
11
|
+
* Register a type from a ts.Type (lightweight stub).
|
|
12
12
|
* Returns the type ID if registered, undefined if skipped.
|
|
13
13
|
*/
|
|
14
14
|
registerType(type: ts.Type, checker: ts.TypeChecker, exportedIds: Set<string>): string | undefined;
|
|
15
|
-
private buildSpecType;
|
|
16
15
|
/**
|
|
17
|
-
* Build a
|
|
18
|
-
* Only captures top-level structure with $refs.
|
|
16
|
+
* Build a lightweight stub type (no deep schema extraction).
|
|
19
17
|
*/
|
|
20
|
-
private
|
|
21
|
-
/**
|
|
22
|
-
* Extract shallow members for classes/interfaces.
|
|
23
|
-
* Only captures property names and simple type info.
|
|
24
|
-
*/
|
|
25
|
-
private extractShallowMembers;
|
|
18
|
+
private buildStubType;
|
|
26
19
|
registerFromSymbol(symbol: ts.Symbol, checker: ts.TypeChecker): SpecType | undefined;
|
|
27
20
|
}
|
|
28
|
-
import { SpecExample, SpecSource, SpecTag } from "@openpkg-ts/spec";
|
|
21
|
+
import { SpecExample, SpecSource, SpecTag, SpecTypeParameter } from "@openpkg-ts/spec";
|
|
29
22
|
import ts2 from "typescript";
|
|
30
23
|
declare function getJSDocComment(node: ts2.Node): {
|
|
31
24
|
description?: string;
|
|
@@ -33,6 +26,23 @@ declare function getJSDocComment(node: ts2.Node): {
|
|
|
33
26
|
examples: SpecExample[];
|
|
34
27
|
};
|
|
35
28
|
declare function getSourceLocation(node: ts2.Node, sourceFile: ts2.SourceFile): SpecSource;
|
|
29
|
+
/**
|
|
30
|
+
* Get description for a destructured parameter property from JSDoc @param tags.
|
|
31
|
+
* Matches patterns like:
|
|
32
|
+
* - @param paramName - exact match
|
|
33
|
+
* - @param opts.paramName - dotted notation with alias
|
|
34
|
+
* - @param {type} paramName - type annotation format
|
|
35
|
+
*/
|
|
36
|
+
declare function getParamDescription(propertyName: string, jsdocTags: readonly ts2.JSDocTag[], inferredAlias?: string): string | undefined;
|
|
37
|
+
type DeclarationWithTypeParams = ts2.FunctionDeclaration | ts2.ClassDeclaration | ts2.InterfaceDeclaration | ts2.TypeAliasDeclaration | ts2.MethodDeclaration | ts2.ArrowFunction;
|
|
38
|
+
/**
|
|
39
|
+
* Extract type parameters from declarations like `<T extends Base, K = Default>`
|
|
40
|
+
*/
|
|
41
|
+
declare function extractTypeParameters(node: DeclarationWithTypeParams, checker: ts2.TypeChecker): SpecTypeParameter[] | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Check if a symbol is marked as deprecated via @deprecated JSDoc tag.
|
|
44
|
+
*/
|
|
45
|
+
declare function isSymbolDeprecated(symbol: ts2.Symbol | undefined): boolean;
|
|
36
46
|
import { OpenPkg } from "@openpkg-ts/spec";
|
|
37
47
|
interface ExtractOptions {
|
|
38
48
|
entryFile: string;
|
|
@@ -42,6 +52,8 @@ interface ExtractOptions {
|
|
|
42
52
|
maxExternalTypeDepth?: number;
|
|
43
53
|
resolveExternalTypes?: boolean;
|
|
44
54
|
schemaExtraction?: "static" | "hybrid";
|
|
55
|
+
/** Include $schema URL in output */
|
|
56
|
+
includeSchema?: boolean;
|
|
45
57
|
}
|
|
46
58
|
interface ExtractResult {
|
|
47
59
|
spec: OpenPkg;
|
|
@@ -50,6 +62,8 @@ interface ExtractResult {
|
|
|
50
62
|
interface Diagnostic {
|
|
51
63
|
message: string;
|
|
52
64
|
severity: "error" | "warning" | "info";
|
|
65
|
+
code?: string;
|
|
66
|
+
suggestion?: string;
|
|
53
67
|
location?: {
|
|
54
68
|
file?: string;
|
|
55
69
|
line?: number;
|
|
@@ -71,35 +85,135 @@ interface ProgramResult {
|
|
|
71
85
|
configPath?: string;
|
|
72
86
|
}
|
|
73
87
|
declare function createProgram({ entryFile, baseDir, content }: ProgramOptions): ProgramResult;
|
|
74
|
-
import
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
88
|
+
import * as TS from "typescript";
|
|
89
|
+
/**
|
|
90
|
+
* A schema adapter can detect and extract output types from a specific
|
|
91
|
+
* schema validation library.
|
|
92
|
+
*/
|
|
93
|
+
interface SchemaAdapter {
|
|
94
|
+
/** Unique identifier for this adapter */
|
|
95
|
+
readonly id: string;
|
|
96
|
+
/** npm package name(s) this adapter handles */
|
|
97
|
+
readonly packages: readonly string[];
|
|
98
|
+
/**
|
|
99
|
+
* Check if a type matches this adapter's schema library.
|
|
100
|
+
* Should be fast - called for every export.
|
|
101
|
+
*/
|
|
102
|
+
matches(type: TS.Type, checker: TS.TypeChecker): boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Extract the output type from a schema type.
|
|
105
|
+
* Returns null if extraction fails.
|
|
106
|
+
*/
|
|
107
|
+
extractOutputType(type: TS.Type, checker: TS.TypeChecker): TS.Type | null;
|
|
108
|
+
/**
|
|
109
|
+
* Extract the input type from a schema type (optional).
|
|
110
|
+
* Useful for transforms where input differs from output.
|
|
111
|
+
*/
|
|
112
|
+
extractInputType?(type: TS.Type, checker: TS.TypeChecker): TS.Type | null;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Result of schema type extraction
|
|
116
|
+
*/
|
|
117
|
+
interface SchemaExtractionResult {
|
|
118
|
+
/** The adapter that matched */
|
|
119
|
+
adapter: SchemaAdapter;
|
|
120
|
+
/** The extracted output type */
|
|
121
|
+
outputType: TS.Type;
|
|
122
|
+
/** The extracted input type (if different from output) */
|
|
123
|
+
inputType?: TS.Type;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Utility: Check if type is an object type reference (has type arguments)
|
|
127
|
+
*/
|
|
128
|
+
declare function isTypeReference(type: TS.Type): type is TS.TypeReference;
|
|
129
|
+
/**
|
|
130
|
+
* Utility: Remove undefined/null from a union type
|
|
131
|
+
*/
|
|
132
|
+
declare function getNonNullableType(type: TS.Type): TS.Type;
|
|
81
133
|
declare function registerAdapter(adapter: SchemaAdapter): void;
|
|
82
|
-
declare function findAdapter(
|
|
83
|
-
declare function isSchemaType(
|
|
84
|
-
declare function extractSchemaType(
|
|
134
|
+
declare function findAdapter(type: TS.Type, checker: TS.TypeChecker): SchemaAdapter | undefined;
|
|
135
|
+
declare function isSchemaType(type: TS.Type, checker: TS.TypeChecker): boolean;
|
|
136
|
+
declare function extractSchemaType(type: TS.Type, checker: TS.TypeChecker): SchemaExtractionResult | null;
|
|
85
137
|
declare const arktypeAdapter: SchemaAdapter;
|
|
86
138
|
declare const typeboxAdapter: SchemaAdapter;
|
|
87
139
|
declare const valibotAdapter: SchemaAdapter;
|
|
88
140
|
declare const zodAdapter: SchemaAdapter;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Standard JSON Schema v1 interface (minimal for detection).
|
|
143
|
+
*/
|
|
144
|
+
interface StandardJSONSchemaV1 {
|
|
145
|
+
"~standard": {
|
|
146
|
+
version: number;
|
|
147
|
+
vendor: string;
|
|
148
|
+
jsonSchema?: {
|
|
149
|
+
output: (target?: string) => Record<string, unknown>;
|
|
150
|
+
input?: (target?: string) => Record<string, unknown>;
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Result of extracting Standard Schema from an export.
|
|
156
|
+
*/
|
|
157
|
+
interface StandardSchemaExtractionResult {
|
|
158
|
+
exportName: string;
|
|
93
159
|
vendor: string;
|
|
160
|
+
outputSchema: Record<string, unknown>;
|
|
161
|
+
inputSchema?: Record<string, unknown>;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Options for runtime Standard Schema extraction.
|
|
165
|
+
*/
|
|
166
|
+
interface ExtractStandardSchemasOptions {
|
|
167
|
+
/** Timeout in milliseconds (default: 10000) */
|
|
168
|
+
timeout?: number;
|
|
169
|
+
/** JSON Schema target version (default: 'draft-2020-12') */
|
|
170
|
+
target?: "draft-2020-12" | "draft-07" | "openapi-3.0";
|
|
94
171
|
}
|
|
95
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Result of Standard Schema extraction.
|
|
174
|
+
*/
|
|
175
|
+
interface StandardSchemaExtractionOutput {
|
|
176
|
+
schemas: Map<string, StandardSchemaExtractionResult>;
|
|
177
|
+
errors: string[];
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Check if an object implements StandardJSONSchemaV1.
|
|
181
|
+
* This is a static type guard - doesn't require runtime.
|
|
182
|
+
*/
|
|
183
|
+
declare function isStandardJSONSchema(obj: unknown): obj is StandardJSONSchemaV1;
|
|
184
|
+
/**
|
|
185
|
+
* Resolve compiled JS path from TypeScript source.
|
|
186
|
+
* Tries common output locations: dist/, build/, lib/, same dir.
|
|
187
|
+
*/
|
|
188
|
+
declare function resolveCompiledPath(tsPath: string, baseDir: string): string | null;
|
|
189
|
+
/**
|
|
190
|
+
* Extract Standard Schema JSON Schemas from a compiled JS module.
|
|
191
|
+
*
|
|
192
|
+
* **Security Note**: This executes the module in a subprocess.
|
|
193
|
+
* Only use with trusted code (user's own packages).
|
|
194
|
+
*
|
|
195
|
+
* @param compiledJsPath - Path to compiled .js file
|
|
196
|
+
* @param options - Extraction options
|
|
197
|
+
* @returns Extraction results with schemas and any errors
|
|
198
|
+
*/
|
|
199
|
+
declare function extractStandardSchemas(compiledJsPath: string, options?: ExtractStandardSchemasOptions): Promise<StandardSchemaExtractionOutput>;
|
|
200
|
+
/**
|
|
201
|
+
* Extract Standard Schema from a TypeScript project.
|
|
202
|
+
*
|
|
203
|
+
* Convenience function that resolves compiled JS and extracts schemas.
|
|
204
|
+
*
|
|
205
|
+
* @param entryFile - TypeScript entry file path
|
|
206
|
+
* @param baseDir - Project base directory
|
|
207
|
+
* @param options - Extraction options
|
|
208
|
+
*/
|
|
209
|
+
declare function extractStandardSchemasFromProject(entryFile: string, baseDir: string, options?: ExtractStandardSchemasOptions): Promise<StandardSchemaExtractionOutput>;
|
|
96
210
|
import { SpecExport } from "@openpkg-ts/spec";
|
|
97
|
-
import
|
|
98
|
-
import
|
|
211
|
+
import ts5 from "typescript";
|
|
212
|
+
import ts4 from "typescript";
|
|
99
213
|
interface SerializerContext {
|
|
100
|
-
typeChecker:
|
|
101
|
-
program:
|
|
102
|
-
sourceFile:
|
|
214
|
+
typeChecker: ts4.TypeChecker;
|
|
215
|
+
program: ts4.Program;
|
|
216
|
+
sourceFile: ts4.SourceFile;
|
|
103
217
|
maxTypeDepth: number;
|
|
104
218
|
maxExternalTypeDepth: number;
|
|
105
219
|
currentDepth: number;
|
|
@@ -107,37 +221,39 @@ interface SerializerContext {
|
|
|
107
221
|
typeRegistry: TypeRegistry;
|
|
108
222
|
exportedIds: Set<string>;
|
|
109
223
|
/** Track visited types to prevent infinite recursion */
|
|
110
|
-
visitedTypes: Set<
|
|
224
|
+
visitedTypes: Set<ts4.Type>;
|
|
111
225
|
}
|
|
112
|
-
declare function serializeClass(node:
|
|
226
|
+
declare function serializeClass(node: ts5.ClassDeclaration, ctx: SerializerContext): SpecExport | null;
|
|
113
227
|
import { SpecExport as SpecExport2 } from "@openpkg-ts/spec";
|
|
114
|
-
import
|
|
115
|
-
declare function serializeEnum(node:
|
|
228
|
+
import ts6 from "typescript";
|
|
229
|
+
declare function serializeEnum(node: ts6.EnumDeclaration, ctx: SerializerContext): SpecExport2 | null;
|
|
116
230
|
import { SpecExport as SpecExport3 } from "@openpkg-ts/spec";
|
|
117
|
-
import
|
|
118
|
-
declare function serializeFunctionExport(node:
|
|
231
|
+
import ts7 from "typescript";
|
|
232
|
+
declare function serializeFunctionExport(node: ts7.FunctionDeclaration | ts7.ArrowFunction, ctx: SerializerContext): SpecExport3 | null;
|
|
119
233
|
import { SpecExport as SpecExport4 } from "@openpkg-ts/spec";
|
|
120
|
-
import
|
|
121
|
-
declare function serializeInterface(node:
|
|
234
|
+
import ts8 from "typescript";
|
|
235
|
+
declare function serializeInterface(node: ts8.InterfaceDeclaration, ctx: SerializerContext): SpecExport4 | null;
|
|
122
236
|
import { SpecExport as SpecExport5 } from "@openpkg-ts/spec";
|
|
123
|
-
import
|
|
124
|
-
declare function serializeTypeAlias(node:
|
|
237
|
+
import ts9 from "typescript";
|
|
238
|
+
declare function serializeTypeAlias(node: ts9.TypeAliasDeclaration, ctx: SerializerContext): SpecExport5 | null;
|
|
125
239
|
import { SpecExport as SpecExport6 } from "@openpkg-ts/spec";
|
|
126
|
-
import
|
|
127
|
-
declare function serializeVariable(node:
|
|
128
|
-
import ts13 from "typescript";
|
|
129
|
-
declare function formatTypeReference(type: ts13.Type, checker: ts13.TypeChecker): string;
|
|
130
|
-
declare function collectReferencedTypes(type: ts13.Type, checker: ts13.TypeChecker, visited?: Set<string>): string[];
|
|
240
|
+
import ts10 from "typescript";
|
|
241
|
+
declare function serializeVariable(node: ts10.VariableDeclaration, statement: ts10.VariableStatement, ctx: SerializerContext): SpecExport6 | null;
|
|
131
242
|
import { SpecSignatureParameter } from "@openpkg-ts/spec";
|
|
132
|
-
import
|
|
133
|
-
declare function extractParameters(signature:
|
|
243
|
+
import ts11 from "typescript";
|
|
244
|
+
declare function extractParameters(signature: ts11.Signature, ctx: SerializerContext): SpecSignatureParameter[];
|
|
134
245
|
/**
|
|
135
246
|
* Recursively register types referenced by a ts.Type.
|
|
136
247
|
* Uses ctx.visitedTypes to prevent infinite recursion on circular types.
|
|
137
248
|
*/
|
|
138
|
-
declare function registerReferencedTypes(type:
|
|
139
|
-
import { SpecSchema
|
|
140
|
-
import
|
|
249
|
+
declare function registerReferencedTypes(type: ts11.Type, ctx: SerializerContext, depth?: number): void;
|
|
250
|
+
import { SpecSchema } from "@openpkg-ts/spec";
|
|
251
|
+
import ts12 from "typescript";
|
|
252
|
+
/**
|
|
253
|
+
* Built-in type schemas with JSON Schema format hints.
|
|
254
|
+
* Used for types that have specific serialization formats.
|
|
255
|
+
*/
|
|
256
|
+
declare const BUILTIN_TYPE_SCHEMAS: Record<string, SpecSchema>;
|
|
141
257
|
/**
|
|
142
258
|
* Check if a name is a primitive type
|
|
143
259
|
*/
|
|
@@ -149,13 +265,41 @@ declare function isBuiltinGeneric(name: string): boolean;
|
|
|
149
265
|
/**
|
|
150
266
|
* Check if a type is anonymous (no meaningful symbol name)
|
|
151
267
|
*/
|
|
152
|
-
declare function isAnonymous(type:
|
|
268
|
+
declare function isAnonymous(type: ts12.Type): boolean;
|
|
153
269
|
/**
|
|
154
270
|
* Build a structured SpecSchema from a TypeScript type.
|
|
155
271
|
* Uses $ref for named types and typeArguments for generics.
|
|
156
272
|
*/
|
|
157
|
-
declare function buildSchema(type:
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
273
|
+
declare function buildSchema(type: ts12.Type, checker: ts12.TypeChecker, ctx?: SerializerContext, _depth?: number): SpecSchema;
|
|
274
|
+
/**
|
|
275
|
+
* Check if a schema is a pure $ref (only has $ref property)
|
|
276
|
+
*/
|
|
277
|
+
declare function isPureRefSchema(schema: SpecSchema): schema is {
|
|
278
|
+
$ref: string;
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Add description to a schema, handling $ref properly.
|
|
282
|
+
* For pure $ref schemas, wraps in allOf to preserve the reference.
|
|
283
|
+
*/
|
|
284
|
+
declare function withDescription(schema: SpecSchema, description: string): SpecSchema;
|
|
285
|
+
/**
|
|
286
|
+
* Check if a schema represents the 'any' type
|
|
287
|
+
*/
|
|
288
|
+
declare function schemaIsAny(schema: SpecSchema): boolean;
|
|
289
|
+
/**
|
|
290
|
+
* Deep equality comparison for schemas
|
|
291
|
+
*/
|
|
292
|
+
declare function schemasAreEqual(left: SpecSchema, right: SpecSchema): boolean;
|
|
293
|
+
/**
|
|
294
|
+
* Remove duplicate schemas from an array while preserving order.
|
|
295
|
+
*/
|
|
296
|
+
declare function deduplicateSchemas(schemas: SpecSchema[]): SpecSchema[];
|
|
297
|
+
/**
|
|
298
|
+
* Find a discriminator property in a union of object types (tagged union pattern).
|
|
299
|
+
* A valid discriminator has a unique literal value in each union member.
|
|
300
|
+
*/
|
|
301
|
+
declare function findDiscriminatorProperty(unionTypes: ts12.Type[], checker: ts12.TypeChecker): string | undefined;
|
|
302
|
+
import ts13 from "typescript";
|
|
303
|
+
declare function isExported(node: ts13.Node): boolean;
|
|
304
|
+
declare function getNodeName(node: ts13.Node): string | undefined;
|
|
305
|
+
export { zodAdapter, withDescription, valibotAdapter, typeboxAdapter, serializeVariable, serializeTypeAlias, serializeInterface, serializeFunctionExport, serializeEnum, serializeClass, schemasAreEqual, schemaIsAny, resolveCompiledPath, registerReferencedTypes, registerAdapter, isTypeReference, isSymbolDeprecated, isStandardJSONSchema, isSchemaType, isPureRefSchema, isPrimitiveName, isExported, isBuiltinGeneric, isAnonymous, getSourceLocation, getParamDescription, getNonNullableType, getNodeName, getJSDocComment, findDiscriminatorProperty, findAdapter, extractTypeParameters, extractStandardSchemasFromProject, extractStandardSchemas, extractSchemaType, extractParameters, extract, deduplicateSchemas, createProgram, buildSchema, arktypeAdapter, TypeRegistry, StandardSchemaExtractionResult, StandardSchemaExtractionOutput, StandardJSONSchemaV1, SerializerContext, SchemaExtractionResult, SchemaAdapter, ProgramResult, ProgramOptions, ExtractStandardSchemasOptions, ExtractResult, ExtractOptions, Diagnostic, BUILTIN_TYPE_SCHEMAS };
|