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