@openpkg-ts/sdk 0.55.0 → 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/browser.js +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +72 -558
- package/dist/shared/chunk-jfjmy1r0.js +1063 -0
- package/package.json +1 -1
- package/dist/shared/chunk-pwvdnach.js +0 -519
|
@@ -0,0 +1,1063 @@
|
|
|
1
|
+
// src/core/format.ts
|
|
2
|
+
function getMemberBadges(member) {
|
|
3
|
+
const badges = [];
|
|
4
|
+
const visibility = member.visibility ?? "public";
|
|
5
|
+
if (visibility !== "public") {
|
|
6
|
+
badges.push(visibility);
|
|
7
|
+
}
|
|
8
|
+
const flags = member.flags;
|
|
9
|
+
if (flags?.static)
|
|
10
|
+
badges.push("static");
|
|
11
|
+
if (flags?.readonly)
|
|
12
|
+
badges.push("readonly");
|
|
13
|
+
if (flags?.async)
|
|
14
|
+
badges.push("async");
|
|
15
|
+
if (flags?.abstract)
|
|
16
|
+
badges.push("abstract");
|
|
17
|
+
return badges;
|
|
18
|
+
}
|
|
19
|
+
function formatBadges(badges) {
|
|
20
|
+
return badges.join(" ");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/types/schema-normalizer.ts
|
|
24
|
+
import { JSON_SCHEMA_DRAFT } from "@openpkg-ts/spec";
|
|
25
|
+
var TS_PRIMITIVE_NORMALIZATIONS = {
|
|
26
|
+
void: () => ({ type: "null", "x-ts-type": "void" }),
|
|
27
|
+
never: () => ({ not: {} }),
|
|
28
|
+
any: () => ({ "x-ts-type": "any" }),
|
|
29
|
+
unknown: () => ({ "x-ts-type": "unknown" }),
|
|
30
|
+
undefined: () => ({ type: "null", "x-ts-type": "undefined" }),
|
|
31
|
+
bigint: () => ({ type: "integer", "x-ts-type": "bigint" }),
|
|
32
|
+
symbol: () => ({ type: "string", "x-ts-type": "symbol" })
|
|
33
|
+
};
|
|
34
|
+
function normalizeSchema(schema, options = {}) {
|
|
35
|
+
const { includeSchemaField = false } = options;
|
|
36
|
+
const normalized = normalizeSchemaInternal(schema, options);
|
|
37
|
+
if (includeSchemaField && typeof normalized === "object") {
|
|
38
|
+
return {
|
|
39
|
+
$schema: JSON_SCHEMA_DRAFT,
|
|
40
|
+
...normalized
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return normalized;
|
|
44
|
+
}
|
|
45
|
+
function normalizeSchemaInternal(schema, options) {
|
|
46
|
+
const result = normalizeSchemaDispatch(schema, options);
|
|
47
|
+
if (schema && typeof schema === "object" && !Array.isArray(schema)) {
|
|
48
|
+
const s = schema;
|
|
49
|
+
if (s.deprecated === true && result.deprecated === undefined) {
|
|
50
|
+
result.deprecated = true;
|
|
51
|
+
}
|
|
52
|
+
if (s.readOnly === true && result.readOnly === undefined) {
|
|
53
|
+
result.readOnly = true;
|
|
54
|
+
}
|
|
55
|
+
for (const key of Object.keys(s)) {
|
|
56
|
+
if (key.startsWith("x-") && s[key] !== undefined && result[key] === undefined) {
|
|
57
|
+
result[key] = s[key];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(s.typeArguments) && s.typeArguments.length > 0 && result["x-ts-type-arguments"] === undefined) {
|
|
61
|
+
result["x-ts-type-arguments"] = s.typeArguments.map((arg) => normalizeSchemaInternal(arg, options));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
function normalizeSchemaDispatch(schema, options) {
|
|
67
|
+
if (typeof schema === "string") {
|
|
68
|
+
return normalizeStringType(schema);
|
|
69
|
+
}
|
|
70
|
+
if (schema == null) {
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
if (typeof schema !== "object") {
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
if ("anyOf" in schema && Array.isArray(schema.anyOf) && !isRequiredOnlyAnyOf(schema.anyOf)) {
|
|
77
|
+
return normalizeCombinator("anyOf", schema.anyOf, schema, options);
|
|
78
|
+
}
|
|
79
|
+
if ("allOf" in schema && Array.isArray(schema.allOf)) {
|
|
80
|
+
return normalizeCombinator("allOf", schema.allOf, schema, options);
|
|
81
|
+
}
|
|
82
|
+
if ("oneOf" in schema && Array.isArray(schema.oneOf)) {
|
|
83
|
+
return normalizeCombinator("oneOf", schema.oneOf, schema, options);
|
|
84
|
+
}
|
|
85
|
+
if ("$ref" in schema && typeof schema.$ref === "string") {
|
|
86
|
+
return normalizeRef(schema, options);
|
|
87
|
+
}
|
|
88
|
+
if ("type" in schema && typeof schema.type === "string") {
|
|
89
|
+
return normalizeTypedSchema(schema, options);
|
|
90
|
+
}
|
|
91
|
+
return normalizeGenericObject(schema, options);
|
|
92
|
+
}
|
|
93
|
+
function normalizeStringType(type) {
|
|
94
|
+
const specialNormalization = TS_PRIMITIVE_NORMALIZATIONS[type];
|
|
95
|
+
if (specialNormalization) {
|
|
96
|
+
return specialNormalization();
|
|
97
|
+
}
|
|
98
|
+
if (["string", "number", "boolean", "integer", "null", "object", "array"].includes(type)) {
|
|
99
|
+
return { type };
|
|
100
|
+
}
|
|
101
|
+
return { "x-ts-type": type };
|
|
102
|
+
}
|
|
103
|
+
function normalizeTypedSchema(schema, options) {
|
|
104
|
+
const { type } = schema;
|
|
105
|
+
const specialNormalization = TS_PRIMITIVE_NORMALIZATIONS[type];
|
|
106
|
+
if (specialNormalization) {
|
|
107
|
+
const normalized = specialNormalization();
|
|
108
|
+
return mergeSchemaFields(normalized, schema, ["type"]);
|
|
109
|
+
}
|
|
110
|
+
if (type === "function") {
|
|
111
|
+
return normalizeFunctionType(schema, options);
|
|
112
|
+
}
|
|
113
|
+
if (type === "tuple") {
|
|
114
|
+
return normalizeTupleType(schema, options);
|
|
115
|
+
}
|
|
116
|
+
if (type === "array") {
|
|
117
|
+
return normalizeArrayType(schema, options);
|
|
118
|
+
}
|
|
119
|
+
if (type === "object") {
|
|
120
|
+
return normalizeObjectType(schema, options);
|
|
121
|
+
}
|
|
122
|
+
if (["string", "number", "boolean", "integer", "null"].includes(type)) {
|
|
123
|
+
return normalizeStandardType(schema, options);
|
|
124
|
+
}
|
|
125
|
+
const result = { "x-ts-type": type };
|
|
126
|
+
return mergeSchemaFields(result, schema, ["type"]);
|
|
127
|
+
}
|
|
128
|
+
function normalizeFunctionType(schema, options) {
|
|
129
|
+
const result = {
|
|
130
|
+
"x-ts-function": true
|
|
131
|
+
};
|
|
132
|
+
if ("signatures" in schema && Array.isArray(schema.signatures)) {
|
|
133
|
+
result["x-ts-signatures"] = schema.signatures.map((sig) => normalizeSignature(sig, options));
|
|
134
|
+
}
|
|
135
|
+
if ("description" in schema && schema.description) {
|
|
136
|
+
result.description = schema.description;
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
function normalizeSignature(signature, options) {
|
|
141
|
+
const result = {};
|
|
142
|
+
if (signature.parameters) {
|
|
143
|
+
result.parameters = signature.parameters.map((param) => ({
|
|
144
|
+
name: param.name,
|
|
145
|
+
schema: normalizeSchemaInternal(param.schema, options),
|
|
146
|
+
...param.required !== undefined ? { required: param.required } : {},
|
|
147
|
+
...param.description ? { description: param.description } : {},
|
|
148
|
+
...param.default !== undefined ? { default: param.default } : {},
|
|
149
|
+
...param.rest ? { rest: param.rest } : {},
|
|
150
|
+
...param["x-ts-destructured"] ? { "x-ts-destructured": true } : {}
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
if (signature.returns) {
|
|
154
|
+
result.returns = {
|
|
155
|
+
schema: normalizeSchemaInternal(signature.returns.schema, options),
|
|
156
|
+
...signature.returns.description ? { description: signature.returns.description } : {}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (signature.description) {
|
|
160
|
+
result.description = signature.description;
|
|
161
|
+
}
|
|
162
|
+
if (signature.typeParameters) {
|
|
163
|
+
result.typeParameters = signature.typeParameters;
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
function normalizeTupleType(schema, options) {
|
|
168
|
+
const result = { type: "array" };
|
|
169
|
+
const prefix = Array.isArray(schema.prefixItems) ? schema.prefixItems : Array.isArray(schema.prefixedItems) ? schema.prefixedItems : undefined;
|
|
170
|
+
if (prefix) {
|
|
171
|
+
result.prefixItems = prefix.map((item) => normalizeSchemaInternal(item, options));
|
|
172
|
+
} else if ("items" in schema && Array.isArray(schema.items)) {
|
|
173
|
+
result.prefixItems = schema.items.map((item) => normalizeSchemaInternal(item, options));
|
|
174
|
+
result.minItems = schema.items.length;
|
|
175
|
+
result.maxItems = schema.items.length;
|
|
176
|
+
}
|
|
177
|
+
if ("minItems" in schema && typeof schema.minItems === "number") {
|
|
178
|
+
result.minItems = schema.minItems;
|
|
179
|
+
}
|
|
180
|
+
if ("maxItems" in schema && typeof schema.maxItems === "number") {
|
|
181
|
+
result.maxItems = schema.maxItems;
|
|
182
|
+
}
|
|
183
|
+
if ("description" in schema && schema.description) {
|
|
184
|
+
result.description = schema.description;
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
function normalizeArrayType(schema, options) {
|
|
189
|
+
const result = { type: "array" };
|
|
190
|
+
if ("items" in schema && schema.items && !Array.isArray(schema.items)) {
|
|
191
|
+
result.items = normalizeSchemaInternal(schema.items, options);
|
|
192
|
+
}
|
|
193
|
+
const arrayPrefix = Array.isArray(schema.prefixItems) ? schema.prefixItems : Array.isArray(schema.prefixedItems) ? schema.prefixedItems : undefined;
|
|
194
|
+
if (arrayPrefix) {
|
|
195
|
+
result.prefixItems = arrayPrefix.map((item) => normalizeSchemaInternal(item, options));
|
|
196
|
+
}
|
|
197
|
+
if ("minItems" in schema && typeof schema.minItems === "number") {
|
|
198
|
+
result.minItems = schema.minItems;
|
|
199
|
+
}
|
|
200
|
+
if ("maxItems" in schema && typeof schema.maxItems === "number") {
|
|
201
|
+
result.maxItems = schema.maxItems;
|
|
202
|
+
}
|
|
203
|
+
if (schema.uniqueItems === true) {
|
|
204
|
+
result.uniqueItems = true;
|
|
205
|
+
}
|
|
206
|
+
if (schema.contains && typeof schema.contains === "object") {
|
|
207
|
+
result.contains = normalizeSchemaInternal(schema.contains, options);
|
|
208
|
+
}
|
|
209
|
+
for (const keyword of ["title", "default"]) {
|
|
210
|
+
if (keyword in schema && schema[keyword] !== undefined) {
|
|
211
|
+
result[keyword] = schema[keyword];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if ("description" in schema && schema.description) {
|
|
215
|
+
result.description = schema.description;
|
|
216
|
+
}
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
function normalizeObjectType(schema, options) {
|
|
220
|
+
const result = { type: "object" };
|
|
221
|
+
if ("properties" in schema && schema.properties) {
|
|
222
|
+
const properties = schema.properties;
|
|
223
|
+
result.properties = Object.fromEntries(Object.entries(properties).map(([key, value]) => [
|
|
224
|
+
key,
|
|
225
|
+
normalizeSchemaInternal(value, options)
|
|
226
|
+
]));
|
|
227
|
+
}
|
|
228
|
+
if ("required" in schema && Array.isArray(schema.required)) {
|
|
229
|
+
result.required = schema.required;
|
|
230
|
+
}
|
|
231
|
+
if (Array.isArray(schema.anyOf) && isRequiredOnlyAnyOf(schema.anyOf)) {
|
|
232
|
+
result.anyOf = schema.anyOf;
|
|
233
|
+
}
|
|
234
|
+
if ("additionalProperties" in schema) {
|
|
235
|
+
if (typeof schema.additionalProperties === "boolean") {
|
|
236
|
+
result.additionalProperties = schema.additionalProperties;
|
|
237
|
+
} else if (schema.additionalProperties) {
|
|
238
|
+
result.additionalProperties = normalizeSchemaInternal(schema.additionalProperties, options);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
for (const keyword of ["patternProperties", "$defs"]) {
|
|
242
|
+
const value = schema[keyword];
|
|
243
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
244
|
+
result[keyword] = Object.fromEntries(Object.entries(value).map(([key, nested]) => [
|
|
245
|
+
key,
|
|
246
|
+
normalizeSchemaInternal(nested, options)
|
|
247
|
+
]));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (schema.propertyNames && typeof schema.propertyNames === "object") {
|
|
251
|
+
result.propertyNames = normalizeSchemaInternal(schema.propertyNames, options);
|
|
252
|
+
}
|
|
253
|
+
for (const keyword of ["title", "default", "examples", "minProperties", "maxProperties"]) {
|
|
254
|
+
if (keyword in schema && schema[keyword] !== undefined) {
|
|
255
|
+
result[keyword] = schema[keyword];
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if ("description" in schema && schema.description) {
|
|
259
|
+
result.description = schema.description;
|
|
260
|
+
}
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
function normalizeStandardType(schema, options) {
|
|
264
|
+
const result = { type: schema.type };
|
|
265
|
+
const validationKeywords = [
|
|
266
|
+
"enum",
|
|
267
|
+
"const",
|
|
268
|
+
"format",
|
|
269
|
+
"pattern",
|
|
270
|
+
"minimum",
|
|
271
|
+
"maximum",
|
|
272
|
+
"exclusiveMinimum",
|
|
273
|
+
"exclusiveMaximum",
|
|
274
|
+
"multipleOf",
|
|
275
|
+
"minLength",
|
|
276
|
+
"maxLength",
|
|
277
|
+
"description",
|
|
278
|
+
"default",
|
|
279
|
+
"examples",
|
|
280
|
+
"title"
|
|
281
|
+
];
|
|
282
|
+
for (const keyword of validationKeywords) {
|
|
283
|
+
if (keyword in schema && schema[keyword] !== undefined) {
|
|
284
|
+
result[keyword] = schema[keyword];
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
for (const key of Object.keys(schema)) {
|
|
288
|
+
if (key.startsWith("x-") && schema[key] !== undefined) {
|
|
289
|
+
const value = schema[key];
|
|
290
|
+
if (isSchemaLike(value)) {
|
|
291
|
+
result[key] = normalizeSchemaInternal(value, options);
|
|
292
|
+
} else if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
293
|
+
result[key] = normalizeGenericObject(value, options);
|
|
294
|
+
} else {
|
|
295
|
+
result[key] = value;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
function normalizeRef(schema, options) {
|
|
302
|
+
const result = { $ref: schema.$ref };
|
|
303
|
+
if (schema.typeArguments && schema.typeArguments.length > 0) {
|
|
304
|
+
result["x-ts-type-arguments"] = schema.typeArguments.map((arg) => normalizeSchemaInternal(arg, options));
|
|
305
|
+
}
|
|
306
|
+
for (const key of Object.keys(schema)) {
|
|
307
|
+
if (key.startsWith("x-ts-") && schema[key] !== undefined) {
|
|
308
|
+
result[key] = schema[key];
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
function isRequiredOnlyAnyOf(arms) {
|
|
314
|
+
return arms.length > 0 && arms.every((arm) => typeof arm === "object" && arm !== null && Object.keys(arm).length === 1 && Array.isArray(arm.required));
|
|
315
|
+
}
|
|
316
|
+
function normalizeCombinator(keyword, schemas, originalSchema, options) {
|
|
317
|
+
let branches = schemas.map((s) => normalizeSchemaInternal(s, options));
|
|
318
|
+
if (keyword !== "allOf") {
|
|
319
|
+
const seen = new Set;
|
|
320
|
+
branches = branches.filter((b) => {
|
|
321
|
+
const key = JSON.stringify(b);
|
|
322
|
+
if (seen.has(key))
|
|
323
|
+
return false;
|
|
324
|
+
seen.add(key);
|
|
325
|
+
return true;
|
|
326
|
+
});
|
|
327
|
+
if (branches.length === 1) {
|
|
328
|
+
const single = { ...branches[0] };
|
|
329
|
+
if ("description" in originalSchema && originalSchema.description && !single.description) {
|
|
330
|
+
single.description = originalSchema.description;
|
|
331
|
+
}
|
|
332
|
+
return single;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
const result = { [keyword]: branches };
|
|
336
|
+
if ((keyword === "anyOf" || keyword === "oneOf") && "discriminator" in originalSchema && originalSchema.discriminator) {
|
|
337
|
+
result.discriminator = originalSchema.discriminator;
|
|
338
|
+
}
|
|
339
|
+
if ("description" in originalSchema && originalSchema.description) {
|
|
340
|
+
result.description = originalSchema.description;
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
function normalizeGenericObject(schema, options) {
|
|
345
|
+
const result = {};
|
|
346
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
347
|
+
if (value == null)
|
|
348
|
+
continue;
|
|
349
|
+
if (isSchemaLike(value)) {
|
|
350
|
+
result[key] = normalizeSchemaInternal(value, options);
|
|
351
|
+
} else if (Array.isArray(value)) {
|
|
352
|
+
result[key] = value.map((item) => isSchemaLike(item) ? normalizeSchemaInternal(item, options) : item);
|
|
353
|
+
} else if (typeof value === "object") {
|
|
354
|
+
result[key] = normalizeGenericObject(value, options);
|
|
355
|
+
} else {
|
|
356
|
+
result[key] = value;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
function isSchemaLike(value) {
|
|
362
|
+
if (typeof value !== "object" || value == null)
|
|
363
|
+
return false;
|
|
364
|
+
if (typeof value === "string")
|
|
365
|
+
return true;
|
|
366
|
+
const obj = value;
|
|
367
|
+
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;
|
|
368
|
+
}
|
|
369
|
+
function mergeSchemaFields(target, source, excludeKeys) {
|
|
370
|
+
if (typeof source !== "object" || source == null) {
|
|
371
|
+
return target;
|
|
372
|
+
}
|
|
373
|
+
const excludeSet = new Set(excludeKeys);
|
|
374
|
+
const result = { ...target };
|
|
375
|
+
for (const [key, value] of Object.entries(source)) {
|
|
376
|
+
if (!excludeSet.has(key) && value !== undefined) {
|
|
377
|
+
if (!(key in result)) {
|
|
378
|
+
result[key] = value;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
function isVendorSchemaExport(exp) {
|
|
385
|
+
return !!exp.tags?.some((t) => t.name === "schema-source" && t.text === "standard-json-schema");
|
|
386
|
+
}
|
|
387
|
+
function normalizeExport(exp, options = {}) {
|
|
388
|
+
const result = { ...exp };
|
|
389
|
+
const vendorSchema = isVendorSchemaExport(exp);
|
|
390
|
+
if (exp.schema && !vendorSchema) {
|
|
391
|
+
result.schema = normalizeSchema(exp.schema, options);
|
|
392
|
+
}
|
|
393
|
+
if (exp.signatures) {
|
|
394
|
+
result.signatures = exp.signatures.map((sig) => normalizeSignatureSpec(sig, options));
|
|
395
|
+
}
|
|
396
|
+
if (exp.members) {
|
|
397
|
+
result.members = exp.members.map((member) => normalizeMember(member, options));
|
|
398
|
+
}
|
|
399
|
+
if (!vendorSchema && shouldGenerateMembersSchema(exp.kind) && exp.members && exp.members.length > 0) {
|
|
400
|
+
result.schema = withOpenArms(normalizeMembers(exp.members, options), result.schema);
|
|
401
|
+
}
|
|
402
|
+
return result;
|
|
403
|
+
}
|
|
404
|
+
function withOpenArms(membersSchema, provided) {
|
|
405
|
+
const arms = provided?.allOf;
|
|
406
|
+
return Array.isArray(arms) ? { allOf: [membersSchema, ...arms] } : membersSchema;
|
|
407
|
+
}
|
|
408
|
+
function normalizeType(type, options = {}) {
|
|
409
|
+
const result = { ...type };
|
|
410
|
+
if (type.schema) {
|
|
411
|
+
result.schema = normalizeSchema(type.schema, options);
|
|
412
|
+
}
|
|
413
|
+
if (type.members) {
|
|
414
|
+
result.members = type.members.map((member) => normalizeMember(member, options));
|
|
415
|
+
}
|
|
416
|
+
if (shouldGenerateMembersSchema(type.kind) && type.members && type.members.length > 0) {
|
|
417
|
+
result.schema = withOpenArms(normalizeMembers(type.members, options), result.schema);
|
|
418
|
+
}
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
function shouldGenerateMembersSchema(kind) {
|
|
422
|
+
return kind === "interface" || kind === "class";
|
|
423
|
+
}
|
|
424
|
+
function normalizeSignatureSpec(signature, options) {
|
|
425
|
+
const result = { ...signature };
|
|
426
|
+
if (signature.parameters) {
|
|
427
|
+
result.parameters = signature.parameters.map((param) => ({
|
|
428
|
+
...param,
|
|
429
|
+
schema: normalizeSchema(param.schema, options)
|
|
430
|
+
}));
|
|
431
|
+
}
|
|
432
|
+
if (signature.returns) {
|
|
433
|
+
result.returns = {
|
|
434
|
+
...signature.returns,
|
|
435
|
+
schema: normalizeSchema(signature.returns.schema, options)
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
return result;
|
|
439
|
+
}
|
|
440
|
+
function normalizeMember(member, options) {
|
|
441
|
+
const result = { ...member };
|
|
442
|
+
if (member.schema) {
|
|
443
|
+
result.schema = normalizeSchema(member.schema, options);
|
|
444
|
+
}
|
|
445
|
+
if (member.signatures) {
|
|
446
|
+
result.signatures = member.signatures.map((sig) => normalizeSignatureSpec(sig, options));
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
449
|
+
}
|
|
450
|
+
function normalizeMembers(members, options = {}) {
|
|
451
|
+
const properties = {};
|
|
452
|
+
const required = [];
|
|
453
|
+
let additionalProperties;
|
|
454
|
+
let numberIndexSchema;
|
|
455
|
+
for (const member of members) {
|
|
456
|
+
const { name, kind } = member;
|
|
457
|
+
if (kind === "index" || kind === "index-signature") {
|
|
458
|
+
if (name === "[number]") {
|
|
459
|
+
numberIndexSchema = normalizeMemberToSchema(member, options);
|
|
460
|
+
} else {
|
|
461
|
+
additionalProperties = normalizeMemberToSchema(member, options);
|
|
462
|
+
}
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
if (!name)
|
|
466
|
+
continue;
|
|
467
|
+
const memberSchema = normalizeMemberToSchema(member, options);
|
|
468
|
+
properties[name] = memberSchema;
|
|
469
|
+
if (!isOptionalMember(member)) {
|
|
470
|
+
required.push(name);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const result = {
|
|
474
|
+
type: "object",
|
|
475
|
+
properties
|
|
476
|
+
};
|
|
477
|
+
if (required.length > 0) {
|
|
478
|
+
result.required = required;
|
|
479
|
+
}
|
|
480
|
+
if (additionalProperties !== undefined) {
|
|
481
|
+
result.additionalProperties = additionalProperties;
|
|
482
|
+
}
|
|
483
|
+
if (numberIndexSchema !== undefined) {
|
|
484
|
+
result.patternProperties = { "^\\d+$": numberIndexSchema };
|
|
485
|
+
result["x-ts-index-key"] = "number";
|
|
486
|
+
}
|
|
487
|
+
return result;
|
|
488
|
+
}
|
|
489
|
+
function memberDocExtras(member) {
|
|
490
|
+
const extras = {};
|
|
491
|
+
if (member.description) {
|
|
492
|
+
extras.description = member.description;
|
|
493
|
+
}
|
|
494
|
+
if (member.flags?.readonly === true) {
|
|
495
|
+
extras.readOnly = true;
|
|
496
|
+
}
|
|
497
|
+
if (member.deprecated) {
|
|
498
|
+
extras.deprecated = true;
|
|
499
|
+
const reason = member.deprecationReason ?? member.tags?.find((t) => t.name === "deprecated")?.text;
|
|
500
|
+
if (reason?.trim()) {
|
|
501
|
+
extras["x-deprecated-reason"] = reason;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return extras;
|
|
505
|
+
}
|
|
506
|
+
function normalizeMemberToSchema(member, options) {
|
|
507
|
+
const { kind, schema, signatures } = member;
|
|
508
|
+
if (kind === "method" || kind === "call-signature") {
|
|
509
|
+
return normalizeMethodMember(member, options);
|
|
510
|
+
}
|
|
511
|
+
if (kind === "getter") {
|
|
512
|
+
const baseSchema2 = schema ? normalizeSchemaInternal(schema, options) : {};
|
|
513
|
+
return {
|
|
514
|
+
...baseSchema2,
|
|
515
|
+
"x-ts-accessor": "getter",
|
|
516
|
+
...memberDocExtras(member)
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
if (kind === "setter") {
|
|
520
|
+
const baseSchema2 = schema ? normalizeSchemaInternal(schema, options) : {};
|
|
521
|
+
return {
|
|
522
|
+
...baseSchema2,
|
|
523
|
+
"x-ts-accessor": "setter",
|
|
524
|
+
...memberDocExtras(member)
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
if (kind === "index" || kind === "index-signature") {
|
|
528
|
+
if (schema && typeof schema === "object" && "additionalProperties" in schema) {
|
|
529
|
+
return normalizeSchemaInternal(schema.additionalProperties, options);
|
|
530
|
+
}
|
|
531
|
+
return schema ? normalizeSchemaInternal(schema, options) : {};
|
|
532
|
+
}
|
|
533
|
+
if (signatures && signatures.length > 0) {
|
|
534
|
+
return normalizeMethodMember(member, options);
|
|
535
|
+
}
|
|
536
|
+
const baseSchema = schema ? normalizeSchemaInternal(schema, options) : {};
|
|
537
|
+
const extras = memberDocExtras(member);
|
|
538
|
+
return Object.keys(extras).length > 0 ? { ...baseSchema, ...extras } : baseSchema;
|
|
539
|
+
}
|
|
540
|
+
function normalizeMethodMember(member, options) {
|
|
541
|
+
const result = {
|
|
542
|
+
"x-ts-function": true
|
|
543
|
+
};
|
|
544
|
+
if (member.flags?.methodSyntax === true) {
|
|
545
|
+
result["x-ts-method"] = true;
|
|
546
|
+
}
|
|
547
|
+
const memberTypeText = member.schema && typeof member.schema === "object" ? member.schema["x-ts-type"] : undefined;
|
|
548
|
+
if (typeof memberTypeText === "string") {
|
|
549
|
+
result["x-ts-type"] = memberTypeText;
|
|
550
|
+
}
|
|
551
|
+
if (member.signatures && member.signatures.length > 0) {
|
|
552
|
+
result["x-ts-signatures"] = member.signatures.map((sig) => normalizeSignature(sig, options));
|
|
553
|
+
}
|
|
554
|
+
Object.assign(result, memberDocExtras(member));
|
|
555
|
+
return result;
|
|
556
|
+
}
|
|
557
|
+
function isOptionalMember(member) {
|
|
558
|
+
if (member.flags?.optional === true) {
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
if (member.name?.endsWith("?")) {
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// src/core/query.ts
|
|
568
|
+
import {
|
|
569
|
+
DISPLAY_KIND_ORDER
|
|
570
|
+
} from "@openpkg-ts/spec";
|
|
571
|
+
function formatFunctionSchema(schema) {
|
|
572
|
+
const sigs = schema["x-ts-signatures"];
|
|
573
|
+
if (!sigs?.length)
|
|
574
|
+
return "(...args: unknown[]) => unknown";
|
|
575
|
+
const sig = sigs[0];
|
|
576
|
+
const params = formatParameters(sig);
|
|
577
|
+
const ret = sig.returns ? formatSchema(sig.returns.schema) : "void";
|
|
578
|
+
return `${params} => ${ret}`;
|
|
579
|
+
}
|
|
580
|
+
function formatSchema(schema, options) {
|
|
581
|
+
if (!schema)
|
|
582
|
+
return "unknown";
|
|
583
|
+
if (typeof schema === "string")
|
|
584
|
+
return schema;
|
|
585
|
+
const depth = options?._depth ?? 0;
|
|
586
|
+
const maxDepth = options?.maxDepth ?? 3;
|
|
587
|
+
if (depth >= maxDepth)
|
|
588
|
+
return "...";
|
|
589
|
+
const nextOpts = { ...options, _depth: depth + 1 };
|
|
590
|
+
const withPackage = (typeStr) => {
|
|
591
|
+
if (options?.includePackage && typeof schema === "object" && "x-ts-package" in schema) {
|
|
592
|
+
const pkg = schema["x-ts-package"];
|
|
593
|
+
return `${typeStr} (from ${pkg})`;
|
|
594
|
+
}
|
|
595
|
+
return typeStr;
|
|
596
|
+
};
|
|
597
|
+
if (typeof schema === "object" && schema !== null) {
|
|
598
|
+
if ("x-ts-type" in schema && typeof schema["x-ts-type"] === "string") {
|
|
599
|
+
const tsType = schema["x-ts-type"];
|
|
600
|
+
if (tsType === "function" || schema["x-ts-function"]) {
|
|
601
|
+
return withPackage(formatFunctionSchema(schema));
|
|
602
|
+
}
|
|
603
|
+
return withPackage(tsType);
|
|
604
|
+
}
|
|
605
|
+
if ("x-ts-function" in schema && schema["x-ts-function"]) {
|
|
606
|
+
return withPackage(formatFunctionSchema(schema));
|
|
607
|
+
}
|
|
608
|
+
if ("x-ts-type-predicate" in schema) {
|
|
609
|
+
const pred = schema["x-ts-type-predicate"];
|
|
610
|
+
return `${pred.parameterName} is ${formatSchema(pred.type, nextOpts)}`;
|
|
611
|
+
}
|
|
612
|
+
if ("$ref" in schema && typeof schema.$ref === "string") {
|
|
613
|
+
const baseName = schema.$ref.replace("#/types/", "");
|
|
614
|
+
if ("x-ts-type-arguments" in schema && Array.isArray(schema["x-ts-type-arguments"])) {
|
|
615
|
+
const args = schema["x-ts-type-arguments"].map((s) => formatSchema(s, nextOpts)).join(", ");
|
|
616
|
+
return withPackage(`${baseName}<${args}>`);
|
|
617
|
+
}
|
|
618
|
+
return withPackage(baseName);
|
|
619
|
+
}
|
|
620
|
+
if ("anyOf" in schema && Array.isArray(schema.anyOf) && !isRequiredOnlyAnyOf(schema.anyOf)) {
|
|
621
|
+
const threshold = options?.collapseUnionThreshold ?? 5;
|
|
622
|
+
const members = schema.anyOf;
|
|
623
|
+
if (members.length > threshold) {
|
|
624
|
+
const shown = members.slice(0, 3);
|
|
625
|
+
const remaining = members.length - 3;
|
|
626
|
+
const shownStr = shown.map((s) => formatSchema(s, nextOpts)).join(" | ");
|
|
627
|
+
return `${shownStr} | ... and ${remaining} more`;
|
|
628
|
+
}
|
|
629
|
+
return members.map((s) => formatSchema(s, nextOpts)).join(" | ");
|
|
630
|
+
}
|
|
631
|
+
if ("allOf" in schema && Array.isArray(schema.allOf)) {
|
|
632
|
+
return schema.allOf.map((s) => formatSchema(s, nextOpts)).join(" & ");
|
|
633
|
+
}
|
|
634
|
+
if ("type" in schema && schema.type === "array") {
|
|
635
|
+
const items = "items" in schema ? formatSchema(schema.items, nextOpts) : "unknown";
|
|
636
|
+
return `${items}[]`;
|
|
637
|
+
}
|
|
638
|
+
if ("type" in schema && schema.type === "tuple" && "items" in schema) {
|
|
639
|
+
const items = schema.items.map((s) => formatSchema(s, nextOpts)).join(", ");
|
|
640
|
+
return `[${items}]`;
|
|
641
|
+
}
|
|
642
|
+
if ("type" in schema && schema.type === "object") {
|
|
643
|
+
if ("properties" in schema && schema.properties) {
|
|
644
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
645
|
+
const props = Object.entries(schema.properties).map(([k, v]) => `${k}${required.has(k) ? "" : "?"}: ${formatSchema(v, nextOpts)}`).join("; ");
|
|
646
|
+
return `{ ${props} }`;
|
|
647
|
+
}
|
|
648
|
+
return "object";
|
|
649
|
+
}
|
|
650
|
+
if ("const" in schema && schema.const !== undefined) {
|
|
651
|
+
const v = schema.const;
|
|
652
|
+
return withPackage(typeof v === "string" ? `"${v}"` : String(v));
|
|
653
|
+
}
|
|
654
|
+
if ("enum" in schema && Array.isArray(schema.enum)) {
|
|
655
|
+
const vals = schema.enum.map((v) => typeof v === "string" ? `"${v}"` : String(v));
|
|
656
|
+
return withPackage(vals.join(" | "));
|
|
657
|
+
}
|
|
658
|
+
if ("type" in schema && typeof schema.type === "string") {
|
|
659
|
+
return withPackage(schema.type);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return "unknown";
|
|
663
|
+
}
|
|
664
|
+
function formatTypeParameters(typeParams) {
|
|
665
|
+
if (!typeParams?.length)
|
|
666
|
+
return "";
|
|
667
|
+
const params = typeParams.map((tp) => {
|
|
668
|
+
let str = "";
|
|
669
|
+
if ("const" in tp && tp.const)
|
|
670
|
+
str += "const ";
|
|
671
|
+
if (tp.variance === "in")
|
|
672
|
+
str += "in ";
|
|
673
|
+
else if (tp.variance === "out")
|
|
674
|
+
str += "out ";
|
|
675
|
+
else if (tp.variance === "inout")
|
|
676
|
+
str += "in out ";
|
|
677
|
+
str += tp.name;
|
|
678
|
+
if (tp.constraint)
|
|
679
|
+
str += ` extends ${tp.constraint}`;
|
|
680
|
+
if (tp.default)
|
|
681
|
+
str += ` = ${tp.default}`;
|
|
682
|
+
return str;
|
|
683
|
+
});
|
|
684
|
+
return `<${params.join(", ")}>`;
|
|
685
|
+
}
|
|
686
|
+
function formatParameters(sig) {
|
|
687
|
+
if (!sig?.parameters?.length)
|
|
688
|
+
return "()";
|
|
689
|
+
const params = sig.parameters.map((p) => {
|
|
690
|
+
const optional = p.required === false ? "?" : "";
|
|
691
|
+
const rest = p.rest ? "..." : "";
|
|
692
|
+
const type = formatSchema(p.schema);
|
|
693
|
+
return `${rest}${p.name}${optional}: ${type}`;
|
|
694
|
+
});
|
|
695
|
+
return `(${params.join(", ")})`;
|
|
696
|
+
}
|
|
697
|
+
function formatReturnType(sig) {
|
|
698
|
+
if (!sig?.returns)
|
|
699
|
+
return "void";
|
|
700
|
+
return formatSchema(sig.returns.schema);
|
|
701
|
+
}
|
|
702
|
+
function buildSignatureString(exp, sigIndex = 0) {
|
|
703
|
+
const sig = exp.signatures?.[sigIndex];
|
|
704
|
+
const typeParams = formatTypeParameters(exp.typeParameters || sig?.typeParameters);
|
|
705
|
+
switch (exp.kind) {
|
|
706
|
+
case "function": {
|
|
707
|
+
const params = formatParameters(sig);
|
|
708
|
+
const returnType = formatReturnType(sig);
|
|
709
|
+
return `function ${exp.name}${typeParams}${params}: ${returnType}`;
|
|
710
|
+
}
|
|
711
|
+
case "class": {
|
|
712
|
+
const ext = exp.extends ? ` extends ${exp.extends}` : "";
|
|
713
|
+
const impl = exp.implements?.length ? ` implements ${exp.implements.join(", ")}` : "";
|
|
714
|
+
return `class ${exp.name}${typeParams}${ext}${impl}`;
|
|
715
|
+
}
|
|
716
|
+
case "interface": {
|
|
717
|
+
const ext = exp.extends ? ` extends ${exp.extends}` : "";
|
|
718
|
+
return `interface ${exp.name}${typeParams}${ext}`;
|
|
719
|
+
}
|
|
720
|
+
case "type": {
|
|
721
|
+
const typeValue = typeof exp.type === "string" ? exp.type : formatSchema(exp.schema);
|
|
722
|
+
return `type ${exp.name}${typeParams} = ${typeValue}`;
|
|
723
|
+
}
|
|
724
|
+
case "enum": {
|
|
725
|
+
return `enum ${exp.name}`;
|
|
726
|
+
}
|
|
727
|
+
case "variable": {
|
|
728
|
+
const typeValue = typeof exp.type === "string" ? exp.type : formatSchema(exp.schema);
|
|
729
|
+
return `const ${exp.name}: ${typeValue}`;
|
|
730
|
+
}
|
|
731
|
+
default:
|
|
732
|
+
return exp.name;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
function resolveTypeRef(ref, spec) {
|
|
736
|
+
const id = ref.replace("#/types/", "");
|
|
737
|
+
return spec.types?.find((t) => t.id === id);
|
|
738
|
+
}
|
|
739
|
+
function isMethod(member) {
|
|
740
|
+
return !!member.signatures?.length;
|
|
741
|
+
}
|
|
742
|
+
function isProperty(member) {
|
|
743
|
+
return !member.signatures?.length;
|
|
744
|
+
}
|
|
745
|
+
function getMethods(members) {
|
|
746
|
+
return members?.filter(isMethod) ?? [];
|
|
747
|
+
}
|
|
748
|
+
function getProperties(members) {
|
|
749
|
+
return members?.filter(isProperty) ?? [];
|
|
750
|
+
}
|
|
751
|
+
function groupByVisibility(members) {
|
|
752
|
+
const groups = {
|
|
753
|
+
public: [],
|
|
754
|
+
protected: [],
|
|
755
|
+
private: []
|
|
756
|
+
};
|
|
757
|
+
for (const member of members ?? []) {
|
|
758
|
+
const visibility = member.visibility ?? "public";
|
|
759
|
+
groups[visibility].push(member);
|
|
760
|
+
}
|
|
761
|
+
return groups;
|
|
762
|
+
}
|
|
763
|
+
function sortByName(items) {
|
|
764
|
+
return [...items].sort((a, b) => a.name.localeCompare(b.name));
|
|
765
|
+
}
|
|
766
|
+
var KIND_ORDER = [
|
|
767
|
+
...DISPLAY_KIND_ORDER,
|
|
768
|
+
"namespace",
|
|
769
|
+
"module",
|
|
770
|
+
"reference",
|
|
771
|
+
"external"
|
|
772
|
+
];
|
|
773
|
+
function groupByKind(items) {
|
|
774
|
+
const groups = {};
|
|
775
|
+
for (const item of items) {
|
|
776
|
+
if (!groups[item.kind])
|
|
777
|
+
groups[item.kind] = [];
|
|
778
|
+
groups[item.kind].push(item);
|
|
779
|
+
}
|
|
780
|
+
return groups;
|
|
781
|
+
}
|
|
782
|
+
function formatConditionalType(condType) {
|
|
783
|
+
const check = formatSchema(condType.checkType);
|
|
784
|
+
const ext = formatSchema(condType.extendsType);
|
|
785
|
+
const trueT = formatSchema(condType.trueType);
|
|
786
|
+
const falseT = formatSchema(condType.falseType);
|
|
787
|
+
return `${check} extends ${ext} ? ${trueT} : ${falseT}`;
|
|
788
|
+
}
|
|
789
|
+
function formatMappedType(mappedType) {
|
|
790
|
+
const keyStr = formatSchema(mappedType.keyType);
|
|
791
|
+
const valueStr = formatSchema(mappedType.valueType);
|
|
792
|
+
let readonlyMod = "";
|
|
793
|
+
if (mappedType.readonly === true || mappedType.readonly === "add") {
|
|
794
|
+
readonlyMod = "readonly ";
|
|
795
|
+
} else if (mappedType.readonly === "remove") {
|
|
796
|
+
readonlyMod = "-readonly ";
|
|
797
|
+
}
|
|
798
|
+
let optionalMod = "";
|
|
799
|
+
if (mappedType.optional === true || mappedType.optional === "add") {
|
|
800
|
+
optionalMod = "?";
|
|
801
|
+
} else if (mappedType.optional === "remove") {
|
|
802
|
+
optionalMod = "-?";
|
|
803
|
+
}
|
|
804
|
+
return `{ ${readonlyMod}[${keyStr}]${optionalMod}: ${valueStr} }`;
|
|
805
|
+
}
|
|
806
|
+
function findExport(spec, name) {
|
|
807
|
+
const exp = spec.exports.find((e) => e.name === name || e.id === name);
|
|
808
|
+
if (!exp)
|
|
809
|
+
throw new Error(`Export not found: ${name}`);
|
|
810
|
+
return exp;
|
|
811
|
+
}
|
|
812
|
+
function filterExports(spec, names) {
|
|
813
|
+
const ids = new Set(names);
|
|
814
|
+
return spec.exports.filter((e) => ids.has(e.name) || ids.has(e.id));
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
// src/core/search.ts
|
|
818
|
+
import { KIND_LABELS } from "@openpkg-ts/spec";
|
|
819
|
+
var defaultSlugify = (name) => name.toLowerCase().replace(/[^a-z0-9]+/g, "-");
|
|
820
|
+
function extractKeywords(exp, options = {}) {
|
|
821
|
+
const keywords = new Set;
|
|
822
|
+
keywords.add(exp.name);
|
|
823
|
+
keywords.add(exp.name.toLowerCase());
|
|
824
|
+
const camelParts = exp.name.split(/(?=[A-Z])/);
|
|
825
|
+
for (const part of camelParts) {
|
|
826
|
+
if (part.length > 2)
|
|
827
|
+
keywords.add(part.toLowerCase());
|
|
828
|
+
}
|
|
829
|
+
if (exp.tags) {
|
|
830
|
+
for (const tag of exp.tags) {
|
|
831
|
+
keywords.add(tag.name.replace("@", ""));
|
|
832
|
+
const tagWords = tag.text.split(/\s+/);
|
|
833
|
+
for (const word of tagWords) {
|
|
834
|
+
if (word.length > 2)
|
|
835
|
+
keywords.add(word.toLowerCase());
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
if (exp.description) {
|
|
840
|
+
const descWords = exp.description.toLowerCase().split(/\W+/).filter((w) => w.length > 2);
|
|
841
|
+
for (const word of descWords) {
|
|
842
|
+
keywords.add(word);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (options.includeMembers && exp.members) {
|
|
846
|
+
for (const member of exp.members) {
|
|
847
|
+
if (member.name) {
|
|
848
|
+
keywords.add(member.name.toLowerCase());
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
if (options.includeParameters && exp.signatures) {
|
|
853
|
+
for (const sig of exp.signatures) {
|
|
854
|
+
for (const param of sig.parameters || []) {
|
|
855
|
+
keywords.add(param.name.toLowerCase());
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
return Array.from(keywords);
|
|
860
|
+
}
|
|
861
|
+
function buildContent(exp, options = {}) {
|
|
862
|
+
const parts = [];
|
|
863
|
+
parts.push(exp.name);
|
|
864
|
+
if (exp.description) {
|
|
865
|
+
parts.push(exp.description);
|
|
866
|
+
}
|
|
867
|
+
if (options.includeSignatures !== false) {
|
|
868
|
+
parts.push(buildSignatureString(exp));
|
|
869
|
+
}
|
|
870
|
+
if (exp.tags) {
|
|
871
|
+
parts.push(...exp.tags.map((t) => `${t.name} ${t.text}`));
|
|
872
|
+
}
|
|
873
|
+
if (options.includeMembers !== false && exp.members) {
|
|
874
|
+
const props = getProperties(exp.members);
|
|
875
|
+
const methods = getMethods(exp.members);
|
|
876
|
+
for (const prop of props) {
|
|
877
|
+
if (prop.name) {
|
|
878
|
+
parts.push(prop.name);
|
|
879
|
+
if (prop.description)
|
|
880
|
+
parts.push(prop.description);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
for (const method of methods) {
|
|
884
|
+
if (method.name) {
|
|
885
|
+
parts.push(method.name);
|
|
886
|
+
if (method.description)
|
|
887
|
+
parts.push(method.description);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
if (options.includeParameters !== false && exp.signatures) {
|
|
892
|
+
for (const sig of exp.signatures) {
|
|
893
|
+
for (const param of sig.parameters || []) {
|
|
894
|
+
parts.push(param.name);
|
|
895
|
+
if (param.description)
|
|
896
|
+
parts.push(param.description);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return parts.join(" ");
|
|
901
|
+
}
|
|
902
|
+
function createSearchRecord(exp, options = {}) {
|
|
903
|
+
const { baseUrl = "/api", slugify = defaultSlugify } = options;
|
|
904
|
+
return {
|
|
905
|
+
id: exp.id,
|
|
906
|
+
name: exp.name,
|
|
907
|
+
kind: exp.kind,
|
|
908
|
+
signature: buildSignatureString(exp),
|
|
909
|
+
description: exp.description,
|
|
910
|
+
content: buildContent(exp, options),
|
|
911
|
+
keywords: extractKeywords(exp, options),
|
|
912
|
+
url: `${baseUrl}/${slugify(exp.name)}`,
|
|
913
|
+
deprecated: exp.deprecated === true
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
function toSearchIndex(spec, options = {}) {
|
|
917
|
+
const records = spec.exports.map((exp) => createSearchRecord(exp, options));
|
|
918
|
+
return {
|
|
919
|
+
records,
|
|
920
|
+
version: spec.meta.version || "0.0.0",
|
|
921
|
+
generatedAt: new Date().toISOString(),
|
|
922
|
+
packageName: spec.meta.name
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
function toPagefindRecords(spec, options = {}) {
|
|
926
|
+
const { baseUrl = "/api", slugify = defaultSlugify, weights = {} } = options;
|
|
927
|
+
const { name: nameWeight = 10, description: descWeight = 5, signature: sigWeight = 3 } = weights;
|
|
928
|
+
return spec.exports.map((exp) => {
|
|
929
|
+
const content = buildContent(exp, options);
|
|
930
|
+
const signature = buildSignatureString(exp);
|
|
931
|
+
const filters = {
|
|
932
|
+
kind: [exp.kind]
|
|
933
|
+
};
|
|
934
|
+
if (exp.deprecated) {
|
|
935
|
+
filters.deprecated = ["true"];
|
|
936
|
+
}
|
|
937
|
+
if (exp.tags?.length) {
|
|
938
|
+
filters.tags = exp.tags.map((t) => t.name.replace("@", ""));
|
|
939
|
+
}
|
|
940
|
+
return {
|
|
941
|
+
url: `${baseUrl}/${slugify(exp.name)}`,
|
|
942
|
+
content,
|
|
943
|
+
word_count: content.split(/\s+/).length,
|
|
944
|
+
filters,
|
|
945
|
+
meta: {
|
|
946
|
+
title: exp.name,
|
|
947
|
+
kind: exp.kind,
|
|
948
|
+
description: exp.description?.slice(0, 160),
|
|
949
|
+
signature
|
|
950
|
+
},
|
|
951
|
+
weighted_sections: [
|
|
952
|
+
{ weight: nameWeight, text: exp.name },
|
|
953
|
+
...exp.description ? [{ weight: descWeight, text: exp.description }] : [],
|
|
954
|
+
{ weight: sigWeight, text: signature }
|
|
955
|
+
]
|
|
956
|
+
};
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
function toAlgoliaRecords(spec, options = {}) {
|
|
960
|
+
const { baseUrl = "/api", slugify = defaultSlugify } = options;
|
|
961
|
+
return spec.exports.map((exp) => ({
|
|
962
|
+
objectID: exp.id,
|
|
963
|
+
name: exp.name,
|
|
964
|
+
kind: exp.kind,
|
|
965
|
+
description: exp.description,
|
|
966
|
+
signature: buildSignatureString(exp),
|
|
967
|
+
content: buildContent(exp, options),
|
|
968
|
+
tags: (exp.tags || []).map((t) => t.name.replace("@", "")),
|
|
969
|
+
deprecated: exp.deprecated === true,
|
|
970
|
+
url: `${baseUrl}/${slugify(exp.name)}`,
|
|
971
|
+
hierarchy: {
|
|
972
|
+
lvl0: spec.meta.name,
|
|
973
|
+
lvl1: KIND_LABELS[exp.kind],
|
|
974
|
+
lvl2: exp.name
|
|
975
|
+
}
|
|
976
|
+
}));
|
|
977
|
+
}
|
|
978
|
+
function toSearchIndexJSON(spec, options = {}) {
|
|
979
|
+
const index = toSearchIndex(spec, options);
|
|
980
|
+
return options.pretty ? JSON.stringify(index, null, 2) : JSON.stringify(index);
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
// src/core/query-builder.ts
|
|
984
|
+
class QueryBuilder {
|
|
985
|
+
spec;
|
|
986
|
+
predicates = [];
|
|
987
|
+
constructor(spec) {
|
|
988
|
+
this.spec = spec;
|
|
989
|
+
}
|
|
990
|
+
byKind(...kinds) {
|
|
991
|
+
if (kinds.length > 0) {
|
|
992
|
+
this.predicates.push((exp) => kinds.includes(exp.kind));
|
|
993
|
+
}
|
|
994
|
+
return this;
|
|
995
|
+
}
|
|
996
|
+
byName(pattern) {
|
|
997
|
+
if (typeof pattern === "string") {
|
|
998
|
+
this.predicates.push((exp) => exp.name === pattern);
|
|
999
|
+
} else {
|
|
1000
|
+
this.predicates.push((exp) => pattern.test(exp.name));
|
|
1001
|
+
}
|
|
1002
|
+
return this;
|
|
1003
|
+
}
|
|
1004
|
+
byTag(...tags) {
|
|
1005
|
+
if (tags.length > 0) {
|
|
1006
|
+
this.predicates.push((exp) => {
|
|
1007
|
+
const expTags = exp.tags?.map((t) => t.name) ?? [];
|
|
1008
|
+
return tags.some((tag) => expTags.includes(tag));
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
return this;
|
|
1012
|
+
}
|
|
1013
|
+
deprecated(include) {
|
|
1014
|
+
if (include !== undefined) {
|
|
1015
|
+
this.predicates.push((exp) => (exp.deprecated ?? false) === include);
|
|
1016
|
+
}
|
|
1017
|
+
return this;
|
|
1018
|
+
}
|
|
1019
|
+
withDescription() {
|
|
1020
|
+
this.predicates.push((exp) => Boolean(exp.description?.trim()));
|
|
1021
|
+
return this;
|
|
1022
|
+
}
|
|
1023
|
+
search(term) {
|
|
1024
|
+
const lower = term.toLowerCase();
|
|
1025
|
+
this.predicates.push((exp) => exp.name.toLowerCase().includes(lower) || (exp.description?.toLowerCase().includes(lower) ?? false));
|
|
1026
|
+
return this;
|
|
1027
|
+
}
|
|
1028
|
+
where(predicate) {
|
|
1029
|
+
this.predicates.push(predicate);
|
|
1030
|
+
return this;
|
|
1031
|
+
}
|
|
1032
|
+
byModule(modulePath) {
|
|
1033
|
+
this.predicates.push((exp) => exp.source?.file?.includes(modulePath) ?? false);
|
|
1034
|
+
return this;
|
|
1035
|
+
}
|
|
1036
|
+
matches(exp) {
|
|
1037
|
+
return this.predicates.every((p) => p(exp));
|
|
1038
|
+
}
|
|
1039
|
+
find() {
|
|
1040
|
+
return this.spec.exports.filter((exp) => this.matches(exp));
|
|
1041
|
+
}
|
|
1042
|
+
first() {
|
|
1043
|
+
return this.spec.exports.find((exp) => this.matches(exp));
|
|
1044
|
+
}
|
|
1045
|
+
count() {
|
|
1046
|
+
return this.spec.exports.filter((exp) => this.matches(exp)).length;
|
|
1047
|
+
}
|
|
1048
|
+
ids() {
|
|
1049
|
+
return this.find().map((exp) => exp.id);
|
|
1050
|
+
}
|
|
1051
|
+
toSpec() {
|
|
1052
|
+
return {
|
|
1053
|
+
...this.spec,
|
|
1054
|
+
exports: this.find(),
|
|
1055
|
+
types: this.spec.types ? [...this.spec.types] : undefined
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
function query(spec) {
|
|
1060
|
+
return new QueryBuilder(spec);
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
export { getMemberBadges, formatBadges, normalizeSchema, isRequiredOnlyAnyOf, normalizeExport, normalizeType, normalizeMembers, formatSchema, formatTypeParameters, formatParameters, formatReturnType, buildSignatureString, resolveTypeRef, isMethod, isProperty, getMethods, getProperties, groupByVisibility, sortByName, KIND_ORDER, groupByKind, formatConditionalType, formatMappedType, findExport, filterExports, toSearchIndex, toPagefindRecords, toAlgoliaRecords, toSearchIndexJSON, QueryBuilder, query };
|