@openpkg-ts/extract 0.22.0 → 0.22.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  extract
4
- } from "../shared/chunk-nymjpc96.js";
4
+ } from "../shared/chunk-7fp2zqf8.js";
5
5
 
6
6
  // src/cli/spec.ts
7
7
  import * as fs from "node:fs";
@@ -125,9 +125,15 @@ function buildSchema(type, checker, ctx, _depth = 0) {
125
125
  return buildFunctionSchema(callSignatures, checker, ctx);
126
126
  }
127
127
  const symbol2 = type.getSymbol() || type.aliasSymbol;
128
- if (symbol2) {
128
+ if (symbol2 && !isAnonymous(type)) {
129
129
  return { $ref: `#/types/${symbol2.getName()}` };
130
130
  }
131
+ if (type.flags & ts.TypeFlags.Object) {
132
+ const properties = type.getProperties();
133
+ if (properties.length > 0) {
134
+ return buildObjectSchema(properties, checker, ctx);
135
+ }
136
+ }
131
137
  return { type: checker.typeToString(type) };
132
138
  }
133
139
  if (ctx && type.flags & ts.TypeFlags.Object) {
@@ -1498,6 +1504,169 @@ function serializeTypeAlias(node, ctx) {
1498
1504
  };
1499
1505
  }
1500
1506
 
1507
+ // src/schema/registry.ts
1508
+ function isTypeReference(type) {
1509
+ return !!(type.flags & 524288 && type.objectFlags && type.objectFlags & 4);
1510
+ }
1511
+ function getNonNullableType(type) {
1512
+ if (type.isUnion()) {
1513
+ const nonNullable = type.types.filter((t) => !(t.flags & 32768) && !(t.flags & 65536));
1514
+ if (nonNullable.length === 1) {
1515
+ return nonNullable[0];
1516
+ }
1517
+ }
1518
+ return type;
1519
+ }
1520
+ var adapters = [];
1521
+ function registerAdapter(adapter) {
1522
+ adapters.push(adapter);
1523
+ }
1524
+ function findAdapter(type, checker) {
1525
+ return adapters.find((a) => a.matches(type, checker));
1526
+ }
1527
+ function isSchemaType(type, checker) {
1528
+ return adapters.some((a) => a.matches(type, checker));
1529
+ }
1530
+ function extractSchemaType(type, checker) {
1531
+ const adapter = findAdapter(type, checker);
1532
+ if (!adapter)
1533
+ return null;
1534
+ const outputType = adapter.extractOutputType(type, checker);
1535
+ if (!outputType)
1536
+ return null;
1537
+ const inputType = adapter.extractInputType?.(type, checker) ?? undefined;
1538
+ return {
1539
+ adapter,
1540
+ outputType,
1541
+ inputType
1542
+ };
1543
+ }
1544
+
1545
+ // src/schema/adapters/arktype.ts
1546
+ var ARKTYPE_TYPE_PATTERN = /^Type</;
1547
+ var arktypeAdapter = {
1548
+ id: "arktype",
1549
+ packages: ["arktype"],
1550
+ matches(type, checker) {
1551
+ const typeName = checker.typeToString(type);
1552
+ return ARKTYPE_TYPE_PATTERN.test(typeName);
1553
+ },
1554
+ extractOutputType(type, checker) {
1555
+ if (!isTypeReference(type)) {
1556
+ return null;
1557
+ }
1558
+ const args = checker.getTypeArguments(type);
1559
+ if (args.length < 1) {
1560
+ return null;
1561
+ }
1562
+ return args[0];
1563
+ },
1564
+ extractInputType(type, checker) {
1565
+ if (!isTypeReference(type)) {
1566
+ return null;
1567
+ }
1568
+ const args = checker.getTypeArguments(type);
1569
+ if (args.length < 2) {
1570
+ return null;
1571
+ }
1572
+ return args[1];
1573
+ }
1574
+ };
1575
+
1576
+ // src/schema/adapters/typebox.ts
1577
+ var TYPEBOX_TYPE_PATTERN = /^T[A-Z]/;
1578
+ var typeboxAdapter = {
1579
+ id: "typebox",
1580
+ packages: ["@sinclair/typebox"],
1581
+ matches(type, checker) {
1582
+ const typeName = checker.typeToString(type);
1583
+ if (!TYPEBOX_TYPE_PATTERN.test(typeName)) {
1584
+ return false;
1585
+ }
1586
+ const typeProperty = type.getProperty("type");
1587
+ return typeProperty !== undefined;
1588
+ },
1589
+ extractOutputType(type, checker) {
1590
+ const staticSymbol = type.getProperty("static");
1591
+ if (staticSymbol) {
1592
+ return checker.getTypeOfSymbol(staticSymbol);
1593
+ }
1594
+ return null;
1595
+ }
1596
+ };
1597
+
1598
+ // src/schema/adapters/valibot.ts
1599
+ var VALIBOT_TYPE_PATTERN = /Schema(<|$)/;
1600
+ var valibotAdapter = {
1601
+ id: "valibot",
1602
+ packages: ["valibot"],
1603
+ matches(type, checker) {
1604
+ const typeName = checker.typeToString(type);
1605
+ return VALIBOT_TYPE_PATTERN.test(typeName) && !typeName.includes("Zod");
1606
+ },
1607
+ extractOutputType(type, checker) {
1608
+ const typesSymbol = type.getProperty("~types");
1609
+ if (!typesSymbol) {
1610
+ return null;
1611
+ }
1612
+ let typesType = checker.getTypeOfSymbol(typesSymbol);
1613
+ typesType = getNonNullableType(typesType);
1614
+ const outputSymbol = typesType.getProperty("output");
1615
+ if (!outputSymbol) {
1616
+ return null;
1617
+ }
1618
+ return checker.getTypeOfSymbol(outputSymbol);
1619
+ },
1620
+ extractInputType(type, checker) {
1621
+ const typesSymbol = type.getProperty("~types");
1622
+ if (!typesSymbol) {
1623
+ return null;
1624
+ }
1625
+ let typesType = checker.getTypeOfSymbol(typesSymbol);
1626
+ typesType = getNonNullableType(typesType);
1627
+ const inputSymbol = typesType.getProperty("input");
1628
+ if (!inputSymbol) {
1629
+ return null;
1630
+ }
1631
+ return checker.getTypeOfSymbol(inputSymbol);
1632
+ }
1633
+ };
1634
+
1635
+ // src/schema/adapters/zod.ts
1636
+ var ZOD_TYPE_PATTERN = /^Zod[A-Z]/;
1637
+ var zodAdapter = {
1638
+ id: "zod",
1639
+ packages: ["zod"],
1640
+ matches(type, checker) {
1641
+ const typeName = checker.typeToString(type);
1642
+ return ZOD_TYPE_PATTERN.test(typeName);
1643
+ },
1644
+ extractOutputType(type, checker) {
1645
+ const outputSymbol = type.getProperty("_output");
1646
+ if (outputSymbol) {
1647
+ return checker.getTypeOfSymbol(outputSymbol);
1648
+ }
1649
+ const typeSymbol = type.getProperty("_type");
1650
+ if (typeSymbol) {
1651
+ return checker.getTypeOfSymbol(typeSymbol);
1652
+ }
1653
+ return null;
1654
+ },
1655
+ extractInputType(type, checker) {
1656
+ const inputSymbol = type.getProperty("_input");
1657
+ if (inputSymbol) {
1658
+ return checker.getTypeOfSymbol(inputSymbol);
1659
+ }
1660
+ return null;
1661
+ }
1662
+ };
1663
+
1664
+ // src/schema/adapters/index.ts
1665
+ registerAdapter(zodAdapter);
1666
+ registerAdapter(valibotAdapter);
1667
+ registerAdapter(arktypeAdapter);
1668
+ registerAdapter(typeboxAdapter);
1669
+
1501
1670
  // src/serializers/variables.ts
1502
1671
  function serializeVariable(node, statement, ctx) {
1503
1672
  const symbol = ctx.typeChecker.getSymbolAtLocation(node.name);
@@ -1508,8 +1677,14 @@ function serializeVariable(node, statement, ctx) {
1508
1677
  const { description, tags, examples } = getJSDocComment(statement);
1509
1678
  const source = getSourceLocation(node, declSourceFile);
1510
1679
  const type = ctx.typeChecker.getTypeAtLocation(node);
1511
- registerReferencedTypes(type, ctx);
1512
- const schema = buildSchema(type, ctx.typeChecker, ctx);
1680
+ const schemaExtraction = extractSchemaType(type, ctx.typeChecker);
1681
+ const typeToSerialize = schemaExtraction?.outputType ?? type;
1682
+ registerReferencedTypes(typeToSerialize, ctx);
1683
+ const schema = buildSchema(typeToSerialize, ctx.typeChecker, ctx);
1684
+ const flags = schemaExtraction ? {
1685
+ schemaLibrary: schemaExtraction.adapter.id,
1686
+ ...schemaExtraction.inputType && schemaExtraction.inputType !== schemaExtraction.outputType ? { hasTransform: true } : {}
1687
+ } : undefined;
1513
1688
  return {
1514
1689
  id: name,
1515
1690
  name,
@@ -1518,6 +1693,7 @@ function serializeVariable(node, statement, ctx) {
1518
1693
  tags,
1519
1694
  source,
1520
1695
  schema,
1696
+ ...flags ? { flags } : {},
1521
1697
  ...examples.length > 0 ? { examples } : {}
1522
1698
  };
1523
1699
  }
@@ -2577,4 +2753,4 @@ async function getPackageMeta(entryFile, baseDir) {
2577
2753
  } catch {}
2578
2754
  return { name: path3.basename(searchDir) };
2579
2755
  }
2580
- export { BUILTIN_TYPE_SCHEMAS, isPrimitiveName, isBuiltinGeneric, isAnonymous, buildSchema, isPureRefSchema, withDescription, schemaIsAny, schemasAreEqual, deduplicateSchemas, findDiscriminatorProperty, TypeRegistry, getJSDocComment, getSourceLocation, getParamDescription, extractTypeParameters, isSymbolDeprecated, createProgram, extractParameters, registerReferencedTypes, serializeClass, serializeEnum, serializeFunctionExport, serializeInterface, serializeTypeAlias, serializeVariable, isStandardJSONSchema, detectTsRuntime, extractStandardSchemasFromTs, resolveCompiledPath, extractStandardSchemas, extractStandardSchemasFromProject, extract };
2756
+ export { BUILTIN_TYPE_SCHEMAS, isPrimitiveName, isBuiltinGeneric, isAnonymous, buildSchema, isPureRefSchema, withDescription, schemaIsAny, schemasAreEqual, deduplicateSchemas, findDiscriminatorProperty, TypeRegistry, getJSDocComment, getSourceLocation, getParamDescription, extractTypeParameters, isSymbolDeprecated, createProgram, extractParameters, registerReferencedTypes, serializeClass, serializeEnum, serializeFunctionExport, serializeInterface, serializeTypeAlias, isTypeReference, getNonNullableType, registerAdapter, findAdapter, isSchemaType, extractSchemaType, arktypeAdapter, typeboxAdapter, valibotAdapter, zodAdapter, serializeVariable, isStandardJSONSchema, detectTsRuntime, extractStandardSchemasFromTs, resolveCompiledPath, extractStandardSchemas, extractStandardSchemasFromProject, extract };
package/dist/src/index.js CHANGED
@@ -1,26 +1,33 @@
1
1
  import {
2
2
  BUILTIN_TYPE_SCHEMAS,
3
3
  TypeRegistry,
4
+ arktypeAdapter,
4
5
  buildSchema,
5
6
  createProgram,
6
7
  deduplicateSchemas,
7
8
  detectTsRuntime,
8
9
  extract,
9
10
  extractParameters,
11
+ extractSchemaType,
10
12
  extractStandardSchemas,
11
13
  extractStandardSchemasFromProject,
12
14
  extractStandardSchemasFromTs,
13
15
  extractTypeParameters,
16
+ findAdapter,
14
17
  findDiscriminatorProperty,
15
18
  getJSDocComment,
19
+ getNonNullableType,
16
20
  getParamDescription,
17
21
  getSourceLocation,
18
22
  isAnonymous,
19
23
  isBuiltinGeneric,
20
24
  isPrimitiveName,
21
25
  isPureRefSchema,
26
+ isSchemaType,
22
27
  isStandardJSONSchema,
23
28
  isSymbolDeprecated,
29
+ isTypeReference,
30
+ registerAdapter,
24
31
  registerReferencedTypes,
25
32
  resolveCompiledPath,
26
33
  schemaIsAny,
@@ -31,170 +38,11 @@ import {
31
38
  serializeInterface,
32
39
  serializeTypeAlias,
33
40
  serializeVariable,
34
- withDescription
35
- } from "../shared/chunk-nymjpc96.js";
36
- // src/schema/registry.ts
37
- function isTypeReference(type) {
38
- return !!(type.flags & 524288 && type.objectFlags && type.objectFlags & 4);
39
- }
40
- function getNonNullableType(type) {
41
- if (type.isUnion()) {
42
- const nonNullable = type.types.filter((t) => !(t.flags & 32768) && !(t.flags & 65536));
43
- if (nonNullable.length === 1) {
44
- return nonNullable[0];
45
- }
46
- }
47
- return type;
48
- }
49
- var adapters = [];
50
- function registerAdapter(adapter) {
51
- adapters.push(adapter);
52
- }
53
- function findAdapter(type, checker) {
54
- return adapters.find((a) => a.matches(type, checker));
55
- }
56
- function isSchemaType(type, checker) {
57
- return adapters.some((a) => a.matches(type, checker));
58
- }
59
- function extractSchemaType(type, checker) {
60
- const adapter = findAdapter(type, checker);
61
- if (!adapter)
62
- return null;
63
- const outputType = adapter.extractOutputType(type, checker);
64
- if (!outputType)
65
- return null;
66
- const inputType = adapter.extractInputType?.(type, checker) ?? undefined;
67
- return {
68
- adapter,
69
- outputType,
70
- inputType
71
- };
72
- }
73
-
74
- // src/schema/adapters/arktype.ts
75
- var ARKTYPE_TYPE_PATTERN = /^Type</;
76
- var arktypeAdapter = {
77
- id: "arktype",
78
- packages: ["arktype"],
79
- matches(type, checker) {
80
- const typeName = checker.typeToString(type);
81
- return ARKTYPE_TYPE_PATTERN.test(typeName);
82
- },
83
- extractOutputType(type, checker) {
84
- if (!isTypeReference(type)) {
85
- return null;
86
- }
87
- const args = checker.getTypeArguments(type);
88
- if (args.length < 1) {
89
- return null;
90
- }
91
- return args[0];
92
- },
93
- extractInputType(type, checker) {
94
- if (!isTypeReference(type)) {
95
- return null;
96
- }
97
- const args = checker.getTypeArguments(type);
98
- if (args.length < 2) {
99
- return null;
100
- }
101
- return args[1];
102
- }
103
- };
104
-
105
- // src/schema/adapters/typebox.ts
106
- var TYPEBOX_TYPE_PATTERN = /^T[A-Z]/;
107
- var typeboxAdapter = {
108
- id: "typebox",
109
- packages: ["@sinclair/typebox"],
110
- matches(type, checker) {
111
- const typeName = checker.typeToString(type);
112
- if (!TYPEBOX_TYPE_PATTERN.test(typeName)) {
113
- return false;
114
- }
115
- const typeProperty = type.getProperty("type");
116
- return typeProperty !== undefined;
117
- },
118
- extractOutputType(type, checker) {
119
- const staticSymbol = type.getProperty("static");
120
- if (staticSymbol) {
121
- return checker.getTypeOfSymbol(staticSymbol);
122
- }
123
- return null;
124
- }
125
- };
126
-
127
- // src/schema/adapters/valibot.ts
128
- var VALIBOT_TYPE_PATTERN = /Schema(<|$)/;
129
- var valibotAdapter = {
130
- id: "valibot",
131
- packages: ["valibot"],
132
- matches(type, checker) {
133
- const typeName = checker.typeToString(type);
134
- return VALIBOT_TYPE_PATTERN.test(typeName) && !typeName.includes("Zod");
135
- },
136
- extractOutputType(type, checker) {
137
- const typesSymbol = type.getProperty("~types");
138
- if (!typesSymbol) {
139
- return null;
140
- }
141
- let typesType = checker.getTypeOfSymbol(typesSymbol);
142
- typesType = getNonNullableType(typesType);
143
- const outputSymbol = typesType.getProperty("output");
144
- if (!outputSymbol) {
145
- return null;
146
- }
147
- return checker.getTypeOfSymbol(outputSymbol);
148
- },
149
- extractInputType(type, checker) {
150
- const typesSymbol = type.getProperty("~types");
151
- if (!typesSymbol) {
152
- return null;
153
- }
154
- let typesType = checker.getTypeOfSymbol(typesSymbol);
155
- typesType = getNonNullableType(typesType);
156
- const inputSymbol = typesType.getProperty("input");
157
- if (!inputSymbol) {
158
- return null;
159
- }
160
- return checker.getTypeOfSymbol(inputSymbol);
161
- }
162
- };
163
-
164
- // src/schema/adapters/zod.ts
165
- var ZOD_TYPE_PATTERN = /^Zod[A-Z]/;
166
- var zodAdapter = {
167
- id: "zod",
168
- packages: ["zod"],
169
- matches(type, checker) {
170
- const typeName = checker.typeToString(type);
171
- return ZOD_TYPE_PATTERN.test(typeName);
172
- },
173
- extractOutputType(type, checker) {
174
- const outputSymbol = type.getProperty("_output");
175
- if (outputSymbol) {
176
- return checker.getTypeOfSymbol(outputSymbol);
177
- }
178
- const typeSymbol = type.getProperty("_type");
179
- if (typeSymbol) {
180
- return checker.getTypeOfSymbol(typeSymbol);
181
- }
182
- return null;
183
- },
184
- extractInputType(type, checker) {
185
- const inputSymbol = type.getProperty("_input");
186
- if (inputSymbol) {
187
- return checker.getTypeOfSymbol(inputSymbol);
188
- }
189
- return null;
190
- }
191
- };
192
-
193
- // src/schema/adapters/index.ts
194
- registerAdapter(zodAdapter);
195
- registerAdapter(valibotAdapter);
196
- registerAdapter(arktypeAdapter);
197
- registerAdapter(typeboxAdapter);
41
+ typeboxAdapter,
42
+ valibotAdapter,
43
+ withDescription,
44
+ zodAdapter
45
+ } from "../shared/chunk-7fp2zqf8.js";
198
46
  // src/types/utils.ts
199
47
  function isExported(node) {
200
48
  const modifiers = node.modifiers;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpkg-ts/extract",
3
- "version": "0.22.0",
3
+ "version": "0.22.1",
4
4
  "description": "TypeScript export extraction to OpenPkg spec",
5
5
  "keywords": [
6
6
  "openpkg",