@openpkg-ts/sdk 0.54.11 → 0.55.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/index.js CHANGED
@@ -18,6 +18,11 @@ import {
18
18
  groupByVisibility,
19
19
  isMethod,
20
20
  isProperty,
21
+ isRequiredOnlyAnyOf,
22
+ normalizeExport,
23
+ normalizeMembers,
24
+ normalizeSchema,
25
+ normalizeType,
21
26
  query,
22
27
  resolveTypeRef,
23
28
  sortByName,
@@ -25,7 +30,7 @@ import {
25
30
  toPagefindRecords,
26
31
  toSearchIndex,
27
32
  toSearchIndexJSON
28
- } from "./shared/chunk-7287tqkx.js";
33
+ } from "./shared/chunk-jfjmy1r0.js";
29
34
 
30
35
  // src/primitives/diff.ts
31
36
  import {
@@ -2111,17 +2116,41 @@ function getSourceLocation(node, sourceFile) {
2111
2116
  line: line + 1
2112
2117
  };
2113
2118
  }
2119
+ function bindingPatternKind(decl) {
2120
+ if (!decl)
2121
+ return;
2122
+ if (ts2.isObjectBindingPattern(decl.name))
2123
+ return "object";
2124
+ if (ts2.isArrayBindingPattern(decl.name))
2125
+ return "array";
2126
+ return;
2127
+ }
2128
+ function destructuredParamName(kind, taken, jsdocName) {
2129
+ if (jsdocName && !taken.has(jsdocName))
2130
+ return jsdocName;
2131
+ const base = kind === "object" ? "options" : "args";
2132
+ if (!taken.has(base))
2133
+ return base;
2134
+ let i = 2;
2135
+ while (taken.has(`${base}${i}`))
2136
+ i++;
2137
+ return `${base}${i}`;
2138
+ }
2139
+ function jsdocParamTagName(tag) {
2140
+ if (tag.tagName.text !== "param")
2141
+ return "";
2142
+ const paramTag = tag;
2143
+ try {
2144
+ return paramTag.name?.getText() ?? "";
2145
+ } catch {
2146
+ return paramTag.name?.text ?? "";
2147
+ }
2148
+ }
2114
2149
  function getParamDescription(propertyName, jsdocTags, inferredAlias) {
2115
2150
  for (const tag of jsdocTags) {
2116
2151
  if (tag.tagName.text !== "param")
2117
2152
  continue;
2118
- const paramTag = tag;
2119
- let tagParamName = "";
2120
- try {
2121
- tagParamName = paramTag.name?.getText() ?? "";
2122
- } catch {
2123
- tagParamName = paramTag.name?.text ?? "";
2124
- }
2153
+ const tagParamName = jsdocParamTagName(tag);
2125
2154
  const isMatch = tagParamName === propertyName || inferredAlias && tagParamName === `${inferredAlias}.${propertyName}` || tagParamName.endsWith(`.${propertyName}`);
2126
2155
  if (isMatch) {
2127
2156
  const comment = typeof tag.comment === "string" ? tag.comment : ts2.getTextOfJSDocComment(tag.comment);
@@ -2873,6 +2902,23 @@ function declaredTypeNode(decl) {
2873
2902
  const withType = decl;
2874
2903
  return withType?.type;
2875
2904
  }
2905
+ function propertyTypeNode(prop) {
2906
+ const decls = prop.getDeclarations() ?? [];
2907
+ if (decls.length <= 1)
2908
+ return declaredTypeNode(prop.valueDeclaration ?? decls[0]);
2909
+ const nodes = decls.map(declaredTypeNode).filter((n) => n !== undefined);
2910
+ const informative = nodes.filter((n) => n.kind !== ts5.SyntaxKind.NeverKeyword && n.kind !== ts5.SyntaxKind.UndefinedKeyword);
2911
+ const [first, ...rest] = informative.length > 0 ? informative : nodes;
2912
+ if (!first)
2913
+ return;
2914
+ try {
2915
+ const text = (n) => n.getText().replace(/\s+/g, " ");
2916
+ const firstText = text(first);
2917
+ return rest.every((n) => text(n) === firstText) ? first : undefined;
2918
+ } catch {
2919
+ return;
2920
+ }
2921
+ }
2876
2922
  function typeNodeOfSignature(sig) {
2877
2923
  const decl = sig.getDeclaration();
2878
2924
  if (!decl || !ts5.isFunctionLike(decl))
@@ -3014,7 +3060,7 @@ function decoratePropertySchema(schema, prop, propType, checker) {
3014
3060
  let result = obj;
3015
3061
  if (!derivable && !("x-ts-type" in obj)) {
3016
3062
  result = { ...result, "x-ts-type": text };
3017
- const declared = writtenTypeText(declaredTypeNode(decl));
3063
+ const declared = writtenTypeText(propertyTypeNode(prop));
3018
3064
  if (declared && declared !== text && !PRIMITIVES.has(declared) && !("x-ts-declared" in obj)) {
3019
3065
  result = { ...result, "x-ts-declared": declared };
3020
3066
  }
@@ -3893,6 +3939,7 @@ function buildSchemaInternal(type, checker, ctx, typeNode) {
3893
3939
  function buildFunctionSchema(callSignatures, checker, ctx) {
3894
3940
  const buildSignatures = () => {
3895
3941
  const signatures = callSignatures.map((sig) => {
3942
+ const taken = new Set(sig.getParameters().filter((p) => !bindingPatternKind(p.valueDeclaration)).map((p) => p.getName()));
3896
3943
  const params = sig.getParameters().flatMap((param) => {
3897
3944
  const decl = param.valueDeclaration;
3898
3945
  if (!decl)
@@ -3900,11 +3947,18 @@ function buildFunctionSchema(callSignatures, checker, ctx) {
3900
3947
  const paramType = checker.getTypeOfSymbolAtLocation(param, decl);
3901
3948
  const isOptional = !!decl?.questionToken || !!decl?.initializer;
3902
3949
  const effectiveType = isOptional ? stripUndefinedFromType(paramType, checker) : paramType;
3950
+ const pattern = bindingPatternKind(decl);
3951
+ let name = param.getName();
3952
+ if (pattern) {
3953
+ name = destructuredParamName(pattern, taken);
3954
+ taken.add(name);
3955
+ }
3903
3956
  return {
3904
- name: param.getName(),
3957
+ name,
3905
3958
  schema: buildSchema(effectiveType, checker, ctx, decl.type),
3906
3959
  required: !isOptional && !decl.dotDotDotToken,
3907
- ...decl.dotDotDotToken ? { rest: true } : {}
3960
+ ...decl.dotDotDotToken ? { rest: true } : {},
3961
+ ...pattern ? { "x-ts-destructured": true } : {}
3908
3962
  };
3909
3963
  });
3910
3964
  const returnType = checker.getReturnTypeOfSignature(sig);
@@ -3945,8 +3999,7 @@ function buildObjectSchema(properties, checker, ctx, originalType) {
3945
3999
  const isOptionalProp = !!(prop.flags & ts5.SymbolFlags.Optional);
3946
4000
  const rawPropType = checker.getTypeOfSymbol(prop);
3947
4001
  const propType = isOptionalProp ? stripUndefinedFromType(rawPropType, checker) : rawPropType;
3948
- const decl = prop.valueDeclaration ?? prop.getDeclarations()?.[0];
3949
- let propSchema = buildSchema(propType, checker, ctx, declaredTypeNode(decl));
4002
+ let propSchema = buildSchema(propType, checker, ctx, propertyTypeNode(prop));
3950
4003
  const docComment = prop.getDocumentationComment(checker);
3951
4004
  if (docComment.length > 0) {
3952
4005
  const description = docComment.map((c) => c.text).join(`
@@ -4505,121 +4558,206 @@ function extractParameters(signature, ctx) {
4505
4558
  const result = [];
4506
4559
  const signatureDecl = signature.getDeclaration();
4507
4560
  const jsdocTags = signatureDecl ? ts7.getJSDocTags(signatureDecl) : [];
4561
+ const names = destructuredNames(signature.getParameters(), jsdocTags, checker);
4508
4562
  for (const param of signature.getParameters()) {
4509
4563
  const decl = param.valueDeclaration;
4510
4564
  if (!decl)
4511
4565
  continue;
4512
4566
  const defer = typeNodeDefersExpansion(decl.type, checker, ctx.program);
4513
4567
  const type = defer ? undefined : checker.getTypeOfSymbolAtLocation(param, decl);
4514
- if (decl && ts7.isObjectBindingPattern(decl.name)) {
4515
- const expandedParams = expandBindingPattern(decl, type ?? checker.getTypeOfSymbolAtLocation(param, decl), jsdocTags, ctx);
4516
- result.push(...expandedParams);
4517
- } else {
4518
- const isOptional = !!decl?.questionToken || !!decl?.initializer;
4519
- const isRest = !!decl.dotDotDotToken;
4520
- const paramName = param.getName();
4521
- const description = getParamDescription(paramName, jsdocTags);
4522
- const schema = defer ? buildSchemaFromTypeNode(decl.type, checker, ctx) : buildSchema(isOptional ? stripUndefinedFromType(type, checker) : type, checker, ctx, decl.type);
4523
- if (!defer && type) {
4524
- registerReferencedTypes(isOptional ? stripUndefinedFromType(type, checker) : type, ctx);
4525
- }
4526
- const paramResult = {
4527
- name: paramName,
4528
- schema,
4529
- required: !isOptional && !isRest,
4530
- ...isRest ? { rest: true } : {}
4531
- };
4532
- if (description) {
4533
- paramResult.description = description;
4534
- const inlineTags = parseInlineTags(description);
4535
- if (inlineTags)
4536
- paramResult.inlineTags = inlineTags;
4537
- }
4538
- if (decl.initializer) {
4539
- applyDefault(paramResult, decl.initializer);
4540
- }
4541
- result.push(paramResult);
4542
- }
4543
- }
4544
- return result;
4545
- }
4546
- function expandBindingPattern(paramDecl, paramType, jsdocTags, ctx) {
4547
- const { typeChecker: checker } = ctx;
4548
- const result = [];
4549
- const bindingPattern = paramDecl.name;
4550
- const allProperties = getEffectiveProperties(paramType, checker);
4551
- const inferredAlias = inferParamAlias(jsdocTags);
4552
- for (const element of bindingPattern.elements) {
4553
- if (!ts7.isBindingElement(element))
4554
- continue;
4555
- const propertyName = element.propertyName ? ts7.isIdentifier(element.propertyName) ? element.propertyName.text : element.propertyName.getText() : ts7.isIdentifier(element.name) ? element.name.text : element.name.getText();
4556
- const propSymbol = allProperties.get(propertyName);
4557
- if (!propSymbol)
4558
- continue;
4559
- const isOptional = !!(propSymbol.flags & ts7.SymbolFlags.Optional) || element.initializer !== undefined;
4560
- const propType = checker.getTypeOfSymbol(propSymbol);
4561
- const effectiveType = isOptional ? stripUndefinedFromType(propType, checker) : propType;
4562
- registerReferencedTypes(effectiveType, ctx);
4563
- const description = getParamDescription(propertyName, jsdocTags, inferredAlias);
4564
- const param = {
4565
- name: propertyName,
4566
- schema: buildSchema(effectiveType, checker, ctx, declaredTypeNode(propSymbol.valueDeclaration)),
4567
- required: !isOptional
4568
+ const isOptional = !!decl.questionToken || !!decl.initializer;
4569
+ const isRest = !!decl.dotDotDotToken;
4570
+ const pattern = bindingPatternKind(decl);
4571
+ const paramName = pattern ? names.get(param) ?? param.getName() : param.getName();
4572
+ const description = getParamDescription(paramName, jsdocTags);
4573
+ let schema = defer ? buildSchemaFromTypeNode(decl.type, checker, ctx) : buildSchema(isOptional ? stripUndefinedFromType(type, checker) : type, checker, ctx, decl.type);
4574
+ if (!defer && type) {
4575
+ registerReferencedTypes(isOptional ? stripUndefinedFromType(type, checker) : type, ctx);
4576
+ }
4577
+ if (pattern === "object") {
4578
+ schema = resolvedObjectSchema(schema, param, decl, isOptional, ctx);
4579
+ }
4580
+ const paramResult = {
4581
+ name: paramName,
4582
+ schema,
4583
+ required: !isOptional && !isRest,
4584
+ ...isRest ? { rest: true } : {},
4585
+ ...pattern ? { "x-ts-destructured": true } : {}
4568
4586
  };
4569
4587
  if (description) {
4570
- param.description = description;
4588
+ paramResult.description = description;
4571
4589
  const inlineTags = parseInlineTags(description);
4572
4590
  if (inlineTags)
4573
- param.inlineTags = inlineTags;
4591
+ paramResult.inlineTags = inlineTags;
4574
4592
  }
4575
- if (element.initializer) {
4576
- applyDefault(param, element.initializer);
4593
+ if (decl.initializer) {
4594
+ applyDefault(paramResult, decl.initializer);
4577
4595
  }
4578
- result.push(param);
4596
+ if (pattern === "object") {
4597
+ annotateBindingElements(decl.name, paramResult, jsdocTags);
4598
+ }
4599
+ result.push(paramResult);
4579
4600
  }
4580
4601
  return result;
4581
4602
  }
4582
- function getEffectiveProperties(type, _checker) {
4583
- const properties = new Map;
4584
- if (type.isIntersection()) {
4585
- for (const subType of type.types) {
4586
- for (const prop of subType.getProperties()) {
4587
- properties.set(prop.getName(), prop);
4588
- }
4603
+ function destructuredNames(params, jsdocTags, checker) {
4604
+ const names = new Map;
4605
+ const taken = new Set;
4606
+ const patterns = [];
4607
+ for (const param of params) {
4608
+ const decl = param.valueDeclaration;
4609
+ const kind = bindingPatternKind(decl);
4610
+ if (!kind || !decl) {
4611
+ taken.add(param.getName());
4612
+ continue;
4589
4613
  }
4590
- } else {
4591
- for (const prop of type.getProperties()) {
4592
- properties.set(prop.getName(), prop);
4614
+ patterns.push({ symbol: param, decl, kind });
4615
+ }
4616
+ if (patterns.length === 0)
4617
+ return names;
4618
+ const tagNames = jsdocTags.map(jsdocParamTagName).filter((n) => n && !n.startsWith("__"));
4619
+ if (tagNames.length === 0) {
4620
+ for (const { symbol, kind } of patterns) {
4621
+ const name = destructuredParamName(kind, taken);
4622
+ taken.add(name);
4623
+ names.set(symbol, name);
4593
4624
  }
4625
+ return names;
4594
4626
  }
4595
- return properties;
4596
- }
4597
- function inferParamAlias(jsdocTags) {
4598
- const prefixes = [];
4599
- for (const tag of jsdocTags) {
4600
- if (tag.tagName.text !== "param")
4627
+ const keys = new Set;
4628
+ for (const { symbol, decl } of patterns) {
4629
+ for (const element of decl.name.elements) {
4630
+ const key = bindingElementKey(element);
4631
+ if (key)
4632
+ keys.add(key);
4633
+ }
4634
+ const type = checker.getTypeOfSymbolAtLocation(symbol, decl);
4635
+ for (const prop of resolvedProperties(type, checker))
4636
+ keys.add(prop.getName());
4637
+ }
4638
+ const candidates = [];
4639
+ for (const tagName of tagNames) {
4640
+ const [head, ...rest] = tagName.split(".");
4641
+ if (taken.has(head))
4601
4642
  continue;
4602
- const tagText = typeof tag.comment === "string" ? tag.comment : ts7.getTextOfJSDocComment(tag.comment) ?? "";
4603
- const paramTag = tag;
4604
- const paramName = paramTag.name?.getText() ?? "";
4605
- if (paramName.includes(".")) {
4606
- const prefix = paramName.split(".")[0];
4607
- if (prefix && !prefix.startsWith("__")) {
4608
- prefixes.push(prefix);
4609
- }
4610
- } else if (tagText.includes(".")) {
4611
- const match = tagText.match(/^(\w+)\./);
4612
- if (match && !match[1].startsWith("__")) {
4613
- prefixes.push(match[1]);
4643
+ if (rest.length === 0 && keys.has(head))
4644
+ continue;
4645
+ if (!candidates.includes(head))
4646
+ candidates.push(head);
4647
+ }
4648
+ patterns.forEach(({ symbol, kind }, i) => {
4649
+ const name = destructuredParamName(kind, taken, candidates[i]);
4650
+ taken.add(name);
4651
+ names.set(symbol, name);
4652
+ });
4653
+ return names;
4654
+ }
4655
+ function bindingElementKey(element) {
4656
+ if (!ts7.isBindingElement(element) || element.dotDotDotToken)
4657
+ return;
4658
+ const key = element.propertyName ?? element.name;
4659
+ if (ts7.isIdentifier(key))
4660
+ return key.text;
4661
+ if (ts7.isStringLiteral(key) || ts7.isNumericLiteral(key))
4662
+ return key.text;
4663
+ return;
4664
+ }
4665
+ function resolvedProperties(type, checker) {
4666
+ const seen = new Map;
4667
+ for (const arm of objectArms(type, checker)) {
4668
+ for (const prop of armProperties(arm, checker)) {
4669
+ const name = prop.getName();
4670
+ const current = seen.get(name);
4671
+ if (!current || isAbsentMarker(current, checker) && !isAbsentMarker(prop, checker)) {
4672
+ seen.set(name, prop);
4614
4673
  }
4615
4674
  }
4616
4675
  }
4617
- if (prefixes.length === 0)
4676
+ return [...seen.values()];
4677
+ }
4678
+ function isAbsentMarker(prop, checker) {
4679
+ const type = stripUndefinedFromType(checker.getTypeOfSymbol(prop), checker);
4680
+ return !!(type.flags & (ts7.TypeFlags.Never | ts7.TypeFlags.Undefined));
4681
+ }
4682
+ function armRequiredNames(arm, checker) {
4683
+ return new Set(armProperties(arm, checker).filter((p) => !(p.flags & ts7.SymbolFlags.Optional)).map((p) => p.getName()));
4684
+ }
4685
+ var PRIMITIVE_LIKE = ts7.TypeFlags.StringLike | ts7.TypeFlags.NumberLike | ts7.TypeFlags.BigIntLike | ts7.TypeFlags.BooleanLike | ts7.TypeFlags.ESSymbolLike | ts7.TypeFlags.Void | ts7.TypeFlags.Undefined | ts7.TypeFlags.Null;
4686
+ function objectArms(type, checker) {
4687
+ const arms = type.isUnion() ? type.types : [type];
4688
+ return arms.filter((arm) => !(arm.flags & PRIMITIVE_LIKE) && armProperties(arm, checker).length > 0);
4689
+ }
4690
+ function armProperties(arm, checker) {
4691
+ const own = checker.getPropertiesOfType(arm);
4692
+ return own.length > 0 ? own : checker.getPropertiesOfType(checker.getApparentType(arm));
4693
+ }
4694
+ function resolvedObjectSchema(schema, param, decl, isOptional, ctx) {
4695
+ if (typeof schema !== "object" || schema === null || Array.isArray(schema))
4696
+ return schema;
4697
+ const current = schema;
4698
+ if (current.properties || current.$ref)
4699
+ return schema;
4700
+ const { typeChecker: checker } = ctx;
4701
+ const raw = checker.getTypeOfSymbolAtLocation(param, decl);
4702
+ const type = isOptional ? stripUndefinedFromType(raw, checker) : raw;
4703
+ const arms = objectArms(type, checker);
4704
+ if (arms.length === 0)
4705
+ return schema;
4706
+ const props = resolvedProperties(type, checker);
4707
+ const resolved = buildObjectSchema(props, checker, ctx, type);
4708
+ if (arms.length > 1) {
4709
+ const perArm = arms.map((arm) => armRequiredNames(arm, checker));
4710
+ const requiredInAll = new Set(props.map((p) => p.getName()).filter((name) => perArm.every((set) => set.has(name))));
4711
+ const required = props.map((p) => p.getName()).filter((n) => requiredInAll.has(n));
4712
+ if (required.length)
4713
+ resolved.required = required;
4714
+ else
4715
+ delete resolved.required;
4716
+ const emitted = new Set(Object.keys(resolved.properties));
4717
+ const anyOf = perArmRequired(perArm, requiredInAll, emitted);
4718
+ if (anyOf)
4719
+ resolved.anyOf = anyOf;
4720
+ }
4721
+ resolved["x-ts-type"] = renderTypeText(type, checker, decl);
4722
+ return resolved;
4723
+ }
4724
+ function perArmRequired(perArm, shared, emitted) {
4725
+ const distinct = new Map;
4726
+ for (const set of perArm) {
4727
+ const extra = [...set].filter((name) => emitted.has(name) && !shared.has(name));
4728
+ if (extra.length === 0)
4729
+ return;
4730
+ distinct.set(extra.join("\x00"), extra);
4731
+ }
4732
+ if (distinct.size < 2)
4733
+ return;
4734
+ return [...distinct.values()].map((required) => ({ required }));
4735
+ }
4736
+ function annotateBindingElements(pattern, param, jsdocTags) {
4737
+ const schema = param.schema;
4738
+ const properties = schema?.properties;
4739
+ if (!properties)
4618
4740
  return;
4619
- const counts = new Map;
4620
- for (const p of prefixes)
4621
- counts.set(p, (counts.get(p) ?? 0) + 1);
4622
- return Array.from(counts.entries()).sort((a, b) => b[1] - a[1])[0]?.[0];
4741
+ for (const element of pattern.elements) {
4742
+ const propertyName = bindingElementKey(element);
4743
+ if (!propertyName)
4744
+ continue;
4745
+ const prop = properties[propertyName];
4746
+ if (!prop || typeof prop !== "object" || Array.isArray(prop))
4747
+ continue;
4748
+ const propSchema = prop;
4749
+ const description = getParamDescription(propertyName, jsdocTags, param.name);
4750
+ if (description && propSchema.description === undefined) {
4751
+ propSchema.description = description;
4752
+ }
4753
+ if (element.initializer) {
4754
+ const extracted = extractLiteralDefault(element.initializer);
4755
+ if (extracted.literal)
4756
+ propSchema.default = extracted.value;
4757
+ else
4758
+ propSchema["x-ts-default"] = extracted.text;
4759
+ }
4760
+ }
4623
4761
  }
4624
4762
  function extractLiteralDefault(initializer) {
4625
4763
  if (ts7.isStringLiteral(initializer)) {
@@ -5361,14 +5499,23 @@ function schemaFromTypeNode(node, ctx) {
5361
5499
  }
5362
5500
  function parametersFromAst(decl, ctx) {
5363
5501
  const jsdocTags = ts11.getJSDocTags(decl);
5502
+ const taken = new Set(decl.parameters.flatMap((p) => ts11.isIdentifier(p.name) ? [p.name.text] : []));
5364
5503
  return decl.parameters.map((p) => {
5365
- const name = ts11.isIdentifier(p.name) ? p.name.text : p.name.getText();
5504
+ const pattern = bindingPatternKind(p);
5505
+ let name;
5506
+ if (pattern) {
5507
+ name = destructuredParamName(pattern, taken);
5508
+ taken.add(name);
5509
+ } else {
5510
+ name = ts11.isIdentifier(p.name) ? p.name.text : p.name.getText();
5511
+ }
5366
5512
  const isOptional = !!p.questionToken || !!p.initializer;
5367
5513
  const param = {
5368
5514
  name,
5369
5515
  schema: schemaFromTypeNode(p.type, ctx),
5370
5516
  required: !isOptional && !p.dotDotDotToken,
5371
- ...p.dotDotDotToken ? { rest: true } : {}
5517
+ ...p.dotDotDotToken ? { rest: true } : {},
5518
+ ...pattern ? { "x-ts-destructured": true } : {}
5372
5519
  };
5373
5520
  const description = getParamDescription(name, jsdocTags);
5374
5521
  if (description)
@@ -6063,543 +6210,6 @@ function serializeValue(name, node, symbol, jsdocNode, type, typeNode, ctx) {
6063
6210
  };
6064
6211
  }
6065
6212
 
6066
- // src/types/schema-normalizer.ts
6067
- import { JSON_SCHEMA_DRAFT } from "@openpkg-ts/spec";
6068
- var TS_PRIMITIVE_NORMALIZATIONS = {
6069
- void: () => ({ type: "null", "x-ts-type": "void" }),
6070
- never: () => ({ not: {} }),
6071
- any: () => ({ "x-ts-type": "any" }),
6072
- unknown: () => ({ "x-ts-type": "unknown" }),
6073
- undefined: () => ({ type: "null", "x-ts-type": "undefined" }),
6074
- bigint: () => ({ type: "integer", "x-ts-type": "bigint" }),
6075
- symbol: () => ({ type: "string", "x-ts-type": "symbol" })
6076
- };
6077
- function normalizeSchema(schema, options = {}) {
6078
- const { includeSchemaField = false } = options;
6079
- const normalized = normalizeSchemaInternal(schema, options);
6080
- if (includeSchemaField && typeof normalized === "object") {
6081
- return {
6082
- $schema: JSON_SCHEMA_DRAFT,
6083
- ...normalized
6084
- };
6085
- }
6086
- return normalized;
6087
- }
6088
- function normalizeSchemaInternal(schema, options) {
6089
- const result = normalizeSchemaDispatch(schema, options);
6090
- if (schema && typeof schema === "object" && !Array.isArray(schema)) {
6091
- const s = schema;
6092
- if (s.deprecated === true && result.deprecated === undefined) {
6093
- result.deprecated = true;
6094
- }
6095
- if (s.readOnly === true && result.readOnly === undefined) {
6096
- result.readOnly = true;
6097
- }
6098
- for (const key of Object.keys(s)) {
6099
- if (key.startsWith("x-") && s[key] !== undefined && result[key] === undefined) {
6100
- result[key] = s[key];
6101
- }
6102
- }
6103
- if (Array.isArray(s.typeArguments) && s.typeArguments.length > 0 && result["x-ts-type-arguments"] === undefined) {
6104
- result["x-ts-type-arguments"] = s.typeArguments.map((arg) => normalizeSchemaInternal(arg, options));
6105
- }
6106
- }
6107
- return result;
6108
- }
6109
- function normalizeSchemaDispatch(schema, options) {
6110
- if (typeof schema === "string") {
6111
- return normalizeStringType(schema);
6112
- }
6113
- if (schema == null) {
6114
- return {};
6115
- }
6116
- if (typeof schema !== "object") {
6117
- return {};
6118
- }
6119
- if ("anyOf" in schema && Array.isArray(schema.anyOf)) {
6120
- return normalizeCombinator("anyOf", schema.anyOf, schema, options);
6121
- }
6122
- if ("allOf" in schema && Array.isArray(schema.allOf)) {
6123
- return normalizeCombinator("allOf", schema.allOf, schema, options);
6124
- }
6125
- if ("oneOf" in schema && Array.isArray(schema.oneOf)) {
6126
- return normalizeCombinator("oneOf", schema.oneOf, schema, options);
6127
- }
6128
- if ("$ref" in schema && typeof schema.$ref === "string") {
6129
- return normalizeRef(schema, options);
6130
- }
6131
- if ("type" in schema && typeof schema.type === "string") {
6132
- return normalizeTypedSchema(schema, options);
6133
- }
6134
- return normalizeGenericObject(schema, options);
6135
- }
6136
- function normalizeStringType(type) {
6137
- const specialNormalization = TS_PRIMITIVE_NORMALIZATIONS[type];
6138
- if (specialNormalization) {
6139
- return specialNormalization();
6140
- }
6141
- if (["string", "number", "boolean", "integer", "null", "object", "array"].includes(type)) {
6142
- return { type };
6143
- }
6144
- return { "x-ts-type": type };
6145
- }
6146
- function normalizeTypedSchema(schema, options) {
6147
- const { type } = schema;
6148
- const specialNormalization = TS_PRIMITIVE_NORMALIZATIONS[type];
6149
- if (specialNormalization) {
6150
- const normalized = specialNormalization();
6151
- return mergeSchemaFields(normalized, schema, ["type"]);
6152
- }
6153
- if (type === "function") {
6154
- return normalizeFunctionType(schema, options);
6155
- }
6156
- if (type === "tuple") {
6157
- return normalizeTupleType(schema, options);
6158
- }
6159
- if (type === "array") {
6160
- return normalizeArrayType(schema, options);
6161
- }
6162
- if (type === "object") {
6163
- return normalizeObjectType(schema, options);
6164
- }
6165
- if (["string", "number", "boolean", "integer", "null"].includes(type)) {
6166
- return normalizeStandardType(schema, options);
6167
- }
6168
- const result = { "x-ts-type": type };
6169
- return mergeSchemaFields(result, schema, ["type"]);
6170
- }
6171
- function normalizeFunctionType(schema, options) {
6172
- const result = {
6173
- "x-ts-function": true
6174
- };
6175
- if ("signatures" in schema && Array.isArray(schema.signatures)) {
6176
- result["x-ts-signatures"] = schema.signatures.map((sig) => normalizeSignature(sig, options));
6177
- }
6178
- if ("description" in schema && schema.description) {
6179
- result.description = schema.description;
6180
- }
6181
- return result;
6182
- }
6183
- function normalizeSignature(signature, options) {
6184
- const result = {};
6185
- if (signature.parameters) {
6186
- result.parameters = signature.parameters.map((param) => ({
6187
- name: param.name,
6188
- schema: normalizeSchemaInternal(param.schema, options),
6189
- ...param.required !== undefined ? { required: param.required } : {},
6190
- ...param.description ? { description: param.description } : {},
6191
- ...param.default !== undefined ? { default: param.default } : {},
6192
- ...param.rest ? { rest: param.rest } : {}
6193
- }));
6194
- }
6195
- if (signature.returns) {
6196
- result.returns = {
6197
- schema: normalizeSchemaInternal(signature.returns.schema, options),
6198
- ...signature.returns.description ? { description: signature.returns.description } : {}
6199
- };
6200
- }
6201
- if (signature.description) {
6202
- result.description = signature.description;
6203
- }
6204
- if (signature.typeParameters) {
6205
- result.typeParameters = signature.typeParameters;
6206
- }
6207
- return result;
6208
- }
6209
- function normalizeTupleType(schema, options) {
6210
- const result = { type: "array" };
6211
- const prefix = Array.isArray(schema.prefixItems) ? schema.prefixItems : Array.isArray(schema.prefixedItems) ? schema.prefixedItems : undefined;
6212
- if (prefix) {
6213
- result.prefixItems = prefix.map((item) => normalizeSchemaInternal(item, options));
6214
- } else if ("items" in schema && Array.isArray(schema.items)) {
6215
- result.prefixItems = schema.items.map((item) => normalizeSchemaInternal(item, options));
6216
- result.minItems = schema.items.length;
6217
- result.maxItems = schema.items.length;
6218
- }
6219
- if ("minItems" in schema && typeof schema.minItems === "number") {
6220
- result.minItems = schema.minItems;
6221
- }
6222
- if ("maxItems" in schema && typeof schema.maxItems === "number") {
6223
- result.maxItems = schema.maxItems;
6224
- }
6225
- if ("description" in schema && schema.description) {
6226
- result.description = schema.description;
6227
- }
6228
- return result;
6229
- }
6230
- function normalizeArrayType(schema, options) {
6231
- const result = { type: "array" };
6232
- if ("items" in schema && schema.items && !Array.isArray(schema.items)) {
6233
- result.items = normalizeSchemaInternal(schema.items, options);
6234
- }
6235
- const arrayPrefix = Array.isArray(schema.prefixItems) ? schema.prefixItems : Array.isArray(schema.prefixedItems) ? schema.prefixedItems : undefined;
6236
- if (arrayPrefix) {
6237
- result.prefixItems = arrayPrefix.map((item) => normalizeSchemaInternal(item, options));
6238
- }
6239
- if ("minItems" in schema && typeof schema.minItems === "number") {
6240
- result.minItems = schema.minItems;
6241
- }
6242
- if ("maxItems" in schema && typeof schema.maxItems === "number") {
6243
- result.maxItems = schema.maxItems;
6244
- }
6245
- if (schema.uniqueItems === true) {
6246
- result.uniqueItems = true;
6247
- }
6248
- if (schema.contains && typeof schema.contains === "object") {
6249
- result.contains = normalizeSchemaInternal(schema.contains, options);
6250
- }
6251
- for (const keyword of ["title", "default"]) {
6252
- if (keyword in schema && schema[keyword] !== undefined) {
6253
- result[keyword] = schema[keyword];
6254
- }
6255
- }
6256
- if ("description" in schema && schema.description) {
6257
- result.description = schema.description;
6258
- }
6259
- return result;
6260
- }
6261
- function normalizeObjectType(schema, options) {
6262
- const result = { type: "object" };
6263
- if ("properties" in schema && schema.properties) {
6264
- const properties = schema.properties;
6265
- result.properties = Object.fromEntries(Object.entries(properties).map(([key, value]) => [
6266
- key,
6267
- normalizeSchemaInternal(value, options)
6268
- ]));
6269
- }
6270
- if ("required" in schema && Array.isArray(schema.required)) {
6271
- result.required = schema.required;
6272
- }
6273
- if ("additionalProperties" in schema) {
6274
- if (typeof schema.additionalProperties === "boolean") {
6275
- result.additionalProperties = schema.additionalProperties;
6276
- } else if (schema.additionalProperties) {
6277
- result.additionalProperties = normalizeSchemaInternal(schema.additionalProperties, options);
6278
- }
6279
- }
6280
- for (const keyword of ["patternProperties", "$defs"]) {
6281
- const value = schema[keyword];
6282
- if (value && typeof value === "object" && !Array.isArray(value)) {
6283
- result[keyword] = Object.fromEntries(Object.entries(value).map(([key, nested]) => [
6284
- key,
6285
- normalizeSchemaInternal(nested, options)
6286
- ]));
6287
- }
6288
- }
6289
- if (schema.propertyNames && typeof schema.propertyNames === "object") {
6290
- result.propertyNames = normalizeSchemaInternal(schema.propertyNames, options);
6291
- }
6292
- for (const keyword of ["title", "default", "examples", "minProperties", "maxProperties"]) {
6293
- if (keyword in schema && schema[keyword] !== undefined) {
6294
- result[keyword] = schema[keyword];
6295
- }
6296
- }
6297
- if ("description" in schema && schema.description) {
6298
- result.description = schema.description;
6299
- }
6300
- return result;
6301
- }
6302
- function normalizeStandardType(schema, options) {
6303
- const result = { type: schema.type };
6304
- const validationKeywords = [
6305
- "enum",
6306
- "const",
6307
- "format",
6308
- "pattern",
6309
- "minimum",
6310
- "maximum",
6311
- "exclusiveMinimum",
6312
- "exclusiveMaximum",
6313
- "multipleOf",
6314
- "minLength",
6315
- "maxLength",
6316
- "description",
6317
- "default",
6318
- "examples",
6319
- "title"
6320
- ];
6321
- for (const keyword of validationKeywords) {
6322
- if (keyword in schema && schema[keyword] !== undefined) {
6323
- result[keyword] = schema[keyword];
6324
- }
6325
- }
6326
- for (const key of Object.keys(schema)) {
6327
- if (key.startsWith("x-") && schema[key] !== undefined) {
6328
- const value = schema[key];
6329
- if (isSchemaLike(value)) {
6330
- result[key] = normalizeSchemaInternal(value, options);
6331
- } else if (typeof value === "object" && value !== null && !Array.isArray(value)) {
6332
- result[key] = normalizeGenericObject(value, options);
6333
- } else {
6334
- result[key] = value;
6335
- }
6336
- }
6337
- }
6338
- return result;
6339
- }
6340
- function normalizeRef(schema, options) {
6341
- const result = { $ref: schema.$ref };
6342
- if (schema.typeArguments && schema.typeArguments.length > 0) {
6343
- result["x-ts-type-arguments"] = schema.typeArguments.map((arg) => normalizeSchemaInternal(arg, options));
6344
- }
6345
- for (const key of Object.keys(schema)) {
6346
- if (key.startsWith("x-ts-") && schema[key] !== undefined) {
6347
- result[key] = schema[key];
6348
- }
6349
- }
6350
- return result;
6351
- }
6352
- function normalizeCombinator(keyword, schemas, originalSchema, options) {
6353
- let branches = schemas.map((s) => normalizeSchemaInternal(s, options));
6354
- if (keyword !== "allOf") {
6355
- const seen = new Set;
6356
- branches = branches.filter((b) => {
6357
- const key = JSON.stringify(b);
6358
- if (seen.has(key))
6359
- return false;
6360
- seen.add(key);
6361
- return true;
6362
- });
6363
- if (branches.length === 1) {
6364
- const single = { ...branches[0] };
6365
- if ("description" in originalSchema && originalSchema.description && !single.description) {
6366
- single.description = originalSchema.description;
6367
- }
6368
- return single;
6369
- }
6370
- }
6371
- const result = { [keyword]: branches };
6372
- if ((keyword === "anyOf" || keyword === "oneOf") && "discriminator" in originalSchema && originalSchema.discriminator) {
6373
- result.discriminator = originalSchema.discriminator;
6374
- }
6375
- if ("description" in originalSchema && originalSchema.description) {
6376
- result.description = originalSchema.description;
6377
- }
6378
- return result;
6379
- }
6380
- function normalizeGenericObject(schema, options) {
6381
- const result = {};
6382
- for (const [key, value] of Object.entries(schema)) {
6383
- if (value == null)
6384
- continue;
6385
- if (isSchemaLike(value)) {
6386
- result[key] = normalizeSchemaInternal(value, options);
6387
- } else if (Array.isArray(value)) {
6388
- result[key] = value.map((item) => isSchemaLike(item) ? normalizeSchemaInternal(item, options) : item);
6389
- } else if (typeof value === "object") {
6390
- result[key] = normalizeGenericObject(value, options);
6391
- } else {
6392
- result[key] = value;
6393
- }
6394
- }
6395
- return result;
6396
- }
6397
- function isSchemaLike(value) {
6398
- if (typeof value !== "object" || value == null)
6399
- return false;
6400
- if (typeof value === "string")
6401
- return true;
6402
- const obj = value;
6403
- return "type" in obj || "$ref" in obj || "anyOf" in obj || "allOf" in obj || "oneOf" in obj || "properties" in obj || "items" in obj || "prefixItems" in obj || "prefixedItems" in obj;
6404
- }
6405
- function mergeSchemaFields(target, source, excludeKeys) {
6406
- if (typeof source !== "object" || source == null) {
6407
- return target;
6408
- }
6409
- const excludeSet = new Set(excludeKeys);
6410
- const result = { ...target };
6411
- for (const [key, value] of Object.entries(source)) {
6412
- if (!excludeSet.has(key) && value !== undefined) {
6413
- if (!(key in result)) {
6414
- result[key] = value;
6415
- }
6416
- }
6417
- }
6418
- return result;
6419
- }
6420
- function isVendorSchemaExport(exp) {
6421
- return !!exp.tags?.some((t) => t.name === "schema-source" && t.text === "standard-json-schema");
6422
- }
6423
- function normalizeExport(exp, options = {}) {
6424
- const result = { ...exp };
6425
- const vendorSchema = isVendorSchemaExport(exp);
6426
- if (exp.schema && !vendorSchema) {
6427
- result.schema = normalizeSchema(exp.schema, options);
6428
- }
6429
- if (exp.signatures) {
6430
- result.signatures = exp.signatures.map((sig) => normalizeSignatureSpec(sig, options));
6431
- }
6432
- if (exp.members) {
6433
- result.members = exp.members.map((member) => normalizeMember(member, options));
6434
- }
6435
- if (!vendorSchema && shouldGenerateMembersSchema(exp.kind) && exp.members && exp.members.length > 0) {
6436
- result.schema = withOpenArms(normalizeMembers(exp.members, options), result.schema);
6437
- }
6438
- return result;
6439
- }
6440
- function withOpenArms(membersSchema, provided) {
6441
- const arms = provided?.allOf;
6442
- return Array.isArray(arms) ? { allOf: [membersSchema, ...arms] } : membersSchema;
6443
- }
6444
- function normalizeType(type, options = {}) {
6445
- const result = { ...type };
6446
- if (type.schema) {
6447
- result.schema = normalizeSchema(type.schema, options);
6448
- }
6449
- if (type.members) {
6450
- result.members = type.members.map((member) => normalizeMember(member, options));
6451
- }
6452
- if (shouldGenerateMembersSchema(type.kind) && type.members && type.members.length > 0) {
6453
- result.schema = withOpenArms(normalizeMembers(type.members, options), result.schema);
6454
- }
6455
- return result;
6456
- }
6457
- function shouldGenerateMembersSchema(kind) {
6458
- return kind === "interface" || kind === "class";
6459
- }
6460
- function normalizeSignatureSpec(signature, options) {
6461
- const result = { ...signature };
6462
- if (signature.parameters) {
6463
- result.parameters = signature.parameters.map((param) => ({
6464
- ...param,
6465
- schema: normalizeSchema(param.schema, options)
6466
- }));
6467
- }
6468
- if (signature.returns) {
6469
- result.returns = {
6470
- ...signature.returns,
6471
- schema: normalizeSchema(signature.returns.schema, options)
6472
- };
6473
- }
6474
- return result;
6475
- }
6476
- function normalizeMember(member, options) {
6477
- const result = { ...member };
6478
- if (member.schema) {
6479
- result.schema = normalizeSchema(member.schema, options);
6480
- }
6481
- if (member.signatures) {
6482
- result.signatures = member.signatures.map((sig) => normalizeSignatureSpec(sig, options));
6483
- }
6484
- return result;
6485
- }
6486
- function normalizeMembers(members, options = {}) {
6487
- const properties = {};
6488
- const required = [];
6489
- let additionalProperties;
6490
- let numberIndexSchema;
6491
- for (const member of members) {
6492
- const { name, kind } = member;
6493
- if (kind === "index" || kind === "index-signature") {
6494
- if (name === "[number]") {
6495
- numberIndexSchema = normalizeMemberToSchema(member, options);
6496
- } else {
6497
- additionalProperties = normalizeMemberToSchema(member, options);
6498
- }
6499
- continue;
6500
- }
6501
- if (!name)
6502
- continue;
6503
- const memberSchema = normalizeMemberToSchema(member, options);
6504
- properties[name] = memberSchema;
6505
- if (!isOptionalMember(member)) {
6506
- required.push(name);
6507
- }
6508
- }
6509
- const result = {
6510
- type: "object",
6511
- properties
6512
- };
6513
- if (required.length > 0) {
6514
- result.required = required;
6515
- }
6516
- if (additionalProperties !== undefined) {
6517
- result.additionalProperties = additionalProperties;
6518
- }
6519
- if (numberIndexSchema !== undefined) {
6520
- result.patternProperties = { "^\\d+$": numberIndexSchema };
6521
- result["x-ts-index-key"] = "number";
6522
- }
6523
- return result;
6524
- }
6525
- function memberDocExtras(member) {
6526
- const extras = {};
6527
- if (member.description) {
6528
- extras.description = member.description;
6529
- }
6530
- if (member.flags?.readonly === true) {
6531
- extras.readOnly = true;
6532
- }
6533
- if (member.deprecated) {
6534
- extras.deprecated = true;
6535
- const reason = member.deprecationReason ?? member.tags?.find((t) => t.name === "deprecated")?.text;
6536
- if (reason?.trim()) {
6537
- extras["x-deprecated-reason"] = reason;
6538
- }
6539
- }
6540
- return extras;
6541
- }
6542
- function normalizeMemberToSchema(member, options) {
6543
- const { kind, schema, signatures } = member;
6544
- if (kind === "method" || kind === "call-signature") {
6545
- return normalizeMethodMember(member, options);
6546
- }
6547
- if (kind === "getter") {
6548
- const baseSchema2 = schema ? normalizeSchemaInternal(schema, options) : {};
6549
- return {
6550
- ...baseSchema2,
6551
- "x-ts-accessor": "getter",
6552
- ...memberDocExtras(member)
6553
- };
6554
- }
6555
- if (kind === "setter") {
6556
- const baseSchema2 = schema ? normalizeSchemaInternal(schema, options) : {};
6557
- return {
6558
- ...baseSchema2,
6559
- "x-ts-accessor": "setter",
6560
- ...memberDocExtras(member)
6561
- };
6562
- }
6563
- if (kind === "index" || kind === "index-signature") {
6564
- if (schema && typeof schema === "object" && "additionalProperties" in schema) {
6565
- return normalizeSchemaInternal(schema.additionalProperties, options);
6566
- }
6567
- return schema ? normalizeSchemaInternal(schema, options) : {};
6568
- }
6569
- if (signatures && signatures.length > 0) {
6570
- return normalizeMethodMember(member, options);
6571
- }
6572
- const baseSchema = schema ? normalizeSchemaInternal(schema, options) : {};
6573
- const extras = memberDocExtras(member);
6574
- return Object.keys(extras).length > 0 ? { ...baseSchema, ...extras } : baseSchema;
6575
- }
6576
- function normalizeMethodMember(member, options) {
6577
- const result = {
6578
- "x-ts-function": true
6579
- };
6580
- if (member.flags?.methodSyntax === true) {
6581
- result["x-ts-method"] = true;
6582
- }
6583
- const memberTypeText = member.schema && typeof member.schema === "object" ? member.schema["x-ts-type"] : undefined;
6584
- if (typeof memberTypeText === "string") {
6585
- result["x-ts-type"] = memberTypeText;
6586
- }
6587
- if (member.signatures && member.signatures.length > 0) {
6588
- result["x-ts-signatures"] = member.signatures.map((sig) => normalizeSignature(sig, options));
6589
- }
6590
- Object.assign(result, memberDocExtras(member));
6591
- return result;
6592
- }
6593
- function isOptionalMember(member) {
6594
- if (member.flags?.optional === true) {
6595
- return true;
6596
- }
6597
- if (member.name?.endsWith("?")) {
6598
- return true;
6599
- }
6600
- return false;
6601
- }
6602
-
6603
6213
  // src/primitives/get.ts
6604
6214
  async function getExport(options) {
6605
6215
  const { entryFile, exportName, baseDir, content, maxTypeDepth } = options;
@@ -8966,7 +8576,7 @@ import {
8966
8576
  validateSpec as validateSpec2
8967
8577
  } from "@openpkg-ts/spec";
8968
8578
  // src/schema/json-schema.ts
8969
- import { JSON_SCHEMA_DRAFT as JSON_SCHEMA_DRAFT2 } from "@openpkg-ts/spec";
8579
+ import { JSON_SCHEMA_DRAFT } from "@openpkg-ts/spec";
8970
8580
 
8971
8581
  // src/schema/ref-walker.ts
8972
8582
  var INTERNAL_REF_PREFIX = "#/types/";
@@ -9102,7 +8712,7 @@ function exportToJsonSchema(subject, spec, options = {}) {
9102
8712
  if (Object.keys(defs).length > 0)
9103
8713
  doc.$defs = defs;
9104
8714
  if (includeSchemaField)
9105
- return { $schema: JSON_SCHEMA_DRAFT2, ...doc };
8715
+ return { $schema: JSON_SCHEMA_DRAFT, ...doc };
9106
8716
  return doc;
9107
8717
  }
9108
8718
  function toJsonSchema(spec, options = {}) {
@@ -9139,7 +8749,7 @@ function toJsonSchema(spec, options = {}) {
9139
8749
  }
9140
8750
  const doc = { $defs: defs };
9141
8751
  if (includeSchemaField)
9142
- return { $schema: JSON_SCHEMA_DRAFT2, ...doc };
8752
+ return { $schema: JSON_SCHEMA_DRAFT, ...doc };
9143
8753
  return doc;
9144
8754
  }
9145
8755
 
@@ -9514,6 +9124,7 @@ export {
9514
9124
  isSymbolDeprecated,
9515
9125
  isStandardJSONSchema,
9516
9126
  isSchemaType,
9127
+ isRequiredOnlyAnyOf,
9517
9128
  isRemoteInput,
9518
9129
  isReadonlyPropertySymbol,
9519
9130
  isPureRefSchema,