@amritk/helpers 0.6.2 → 0.7.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/build-dynamic-ref-map.d.ts +1 -0
- package/dist/build-dynamic-ref-map.d.ts.map +1 -0
- package/dist/build-dynamic-ref-map.js +31 -107
- package/dist/derive-root-type-name.d.ts +1 -0
- package/dist/derive-root-type-name.d.ts.map +1 -0
- package/dist/derive-root-type-name.js +37 -17
- package/dist/extract-dynamic-anchor-defs.d.ts +20 -0
- package/dist/extract-dynamic-anchor-defs.d.ts.map +1 -0
- package/dist/extract-dynamic-anchor-defs.js +30 -0
- package/dist/extract-refs.d.ts +1 -0
- package/dist/extract-refs.d.ts.map +1 -0
- package/dist/extract-refs.js +64 -33
- package/dist/generate-index-barrel.d.ts +29 -0
- package/dist/generate-index-barrel.d.ts.map +1 -0
- package/dist/generate-index-barrel.js +43 -0
- package/dist/generate-type-definition.d.ts +1 -0
- package/dist/generate-type-definition.d.ts.map +1 -0
- package/dist/generate-type-definition.js +449 -567
- package/dist/has-ref.d.ts +1 -0
- package/dist/has-ref.d.ts.map +1 -0
- package/dist/has-ref.js +7 -6
- package/dist/is-object.d.ts +1 -0
- package/dist/is-object.d.ts.map +1 -0
- package/dist/is-object.js +14 -5
- package/dist/mjst-extension.d.ts +1 -0
- package/dist/mjst-extension.d.ts.map +1 -0
- package/dist/mjst-extension.js +50 -122
- package/dist/parse-documentation.d.ts +1 -0
- package/dist/parse-documentation.d.ts.map +1 -0
- package/dist/parse-documentation.js +124 -87
- package/dist/ref-to-filename.d.ts +1 -0
- package/dist/ref-to-filename.d.ts.map +1 -0
- package/dist/ref-to-filename.js +106 -42
- package/dist/ref-to-name.d.ts +1 -0
- package/dist/ref-to-name.d.ts.map +1 -0
- package/dist/ref-to-name.js +39 -53
- package/dist/resolve-dynamic-refs.d.ts +1 -0
- package/dist/resolve-dynamic-refs.d.ts.map +1 -0
- package/dist/resolve-dynamic-refs.js +36 -28
- package/dist/resolve-ref.d.ts +1 -0
- package/dist/resolve-ref.d.ts.map +1 -0
- package/dist/resolve-ref.js +74 -39
- package/dist/safe-accessor.d.ts +1 -0
- package/dist/safe-accessor.d.ts.map +1 -0
- package/dist/safe-accessor.js +42 -19
- package/dist/schema-guards.d.ts +2 -1
- package/dist/schema-guards.d.ts.map +1 -0
- package/dist/schema-guards.js +119 -120
- package/dist/upgrade-draft07-schema.d.ts +1 -0
- package/dist/upgrade-draft07-schema.d.ts.map +1 -0
- package/dist/upgrade-draft07-schema.js +152 -130
- package/dist/validate-array.d.ts +1 -0
- package/dist/validate-array.d.ts.map +1 -0
- package/dist/validate-array.js +12 -14
- package/dist/validate-record.d.ts +1 -0
- package/dist/validate-record.d.ts.map +1 -0
- package/dist/validate-record.js +15 -17
- package/dist/walk-ref-graph.d.ts +54 -0
- package/dist/walk-ref-graph.d.ts.map +1 -0
- package/dist/walk-ref-graph.js +123 -0
- package/package.json +9 -88
- package/src/extract-dynamic-anchor-defs.test.ts +29 -0
- package/src/extract-dynamic-anchor-defs.ts +33 -0
- package/src/generate-index-barrel.test.ts +41 -0
- package/src/generate-index-barrel.ts +62 -0
- package/src/walk-ref-graph.test.ts +118 -0
- package/src/walk-ref-graph.ts +184 -0
package/dist/has-ref.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"has-ref.d.ts","sourceRoot":"","sources":["../src/has-ref.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,eAAO,MAAM,MAAM,UAAW,OAAO,KAAG,KAAK,IAAI;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAQzF,CAAA"}
|
package/dist/has-ref.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
/** Type guard to check if a value is a non-null, non-array object with a string $ref property */
|
|
2
|
+
export const hasRef = (value) => {
|
|
3
|
+
return (typeof value === 'object' &&
|
|
4
|
+
value !== null &&
|
|
5
|
+
!Array.isArray(value) &&
|
|
6
|
+
'$ref' in value &&
|
|
7
|
+
typeof value.$ref === 'string');
|
|
7
8
|
};
|
package/dist/is-object.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-object.d.ts","sourceRoot":"","sources":["../src/is-object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,QAAQ,UAAW,OAAO,KAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CACV,CAAA"}
|
package/dist/is-object.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Returns true if the provided value is a plain object.
|
|
3
|
+
* Optimized for JSON data validation: checks that value is truthy, typeof object,
|
|
4
|
+
* and not an array. This correctly handles all JSON value types.
|
|
5
|
+
*
|
|
6
|
+
* Examples:
|
|
7
|
+
* isObject({}) // true
|
|
8
|
+
* isObject({ a: 1 }) // true
|
|
9
|
+
* isObject([]) // false (Array)
|
|
10
|
+
* isObject(null) // false
|
|
11
|
+
* isObject(123) // false
|
|
12
|
+
* isObject('string') // false
|
|
13
|
+
*/
|
|
14
|
+
export const isObject = (value) => !!value && typeof value === 'object' && !Array.isArray(value);
|
package/dist/mjst-extension.d.ts
CHANGED
|
@@ -41,3 +41,4 @@ export declare const getMjstPrimitive: (schema: JSONSchema) => string | undefine
|
|
|
41
41
|
* present and safe to embed in generated output. Returns undefined otherwise.
|
|
42
42
|
*/
|
|
43
43
|
export declare const getMjstBrand: (schema: JSONSchema) => string | undefined;
|
|
44
|
+
//# sourceMappingURL=mjst-extension.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mjst-extension.d.ts","sourceRoot":"","sources":["../src/mjst-extension.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAIjE;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,WAAW,CAAA;AAE1C;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CACxB,CAAA;AAwBD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,WAAY,UAAU,KAAG,MAAM,GAAG,SAG/D,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,WAAY,UAAU,KAAG,MAAM,GAAG,SAG9D,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,WAAY,UAAU,KAAG,MAAM,GAAG,SAG1D,CAAA"}
|
package/dist/mjst-extension.js
CHANGED
|
@@ -1,123 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
var hasAdditionalProperties = (schema) => {
|
|
53
|
-
return isSchemaObject(schema) && "additionalProperties" in schema;
|
|
54
|
-
};
|
|
55
|
-
var hasMinLength = (schema) => {
|
|
56
|
-
return isSchemaObject(schema) && "minLength" in schema && typeof schema.minLength === "number";
|
|
57
|
-
};
|
|
58
|
-
var hasMaxLength = (schema) => {
|
|
59
|
-
return isSchemaObject(schema) && "maxLength" in schema && typeof schema.maxLength === "number";
|
|
60
|
-
};
|
|
61
|
-
var hasMinimum = (schema) => {
|
|
62
|
-
return isSchemaObject(schema) && "minimum" in schema && typeof schema.minimum === "number";
|
|
63
|
-
};
|
|
64
|
-
var hasMaximum = (schema) => {
|
|
65
|
-
return isSchemaObject(schema) && "maximum" in schema && typeof schema.maximum === "number";
|
|
66
|
-
};
|
|
67
|
-
var hasExclusiveMinimum = (schema) => {
|
|
68
|
-
return isSchemaObject(schema) && "exclusiveMinimum" in schema && typeof schema.exclusiveMinimum === "number";
|
|
69
|
-
};
|
|
70
|
-
var hasExclusiveMaximum = (schema) => {
|
|
71
|
-
return isSchemaObject(schema) && "exclusiveMaximum" in schema && typeof schema.exclusiveMaximum === "number";
|
|
72
|
-
};
|
|
73
|
-
var hasMultipleOf = (schema) => {
|
|
74
|
-
return isSchemaObject(schema) && "multipleOf" in schema && typeof schema.multipleOf === "number";
|
|
75
|
-
};
|
|
76
|
-
var hasMinItems = (schema) => {
|
|
77
|
-
return isSchemaObject(schema) && "minItems" in schema && typeof schema.minItems === "number";
|
|
78
|
-
};
|
|
79
|
-
var hasMaxItems = (schema) => {
|
|
80
|
-
return isSchemaObject(schema) && "maxItems" in schema && typeof schema.maxItems === "number";
|
|
81
|
-
};
|
|
82
|
-
var hasUniqueItems = (schema) => {
|
|
83
|
-
return isSchemaObject(schema) && "uniqueItems" in schema && typeof schema.uniqueItems === "boolean";
|
|
84
|
-
};
|
|
85
|
-
var hasMinProperties = (schema) => {
|
|
86
|
-
return isSchemaObject(schema) && "minProperties" in schema && typeof schema.minProperties === "number";
|
|
87
|
-
};
|
|
88
|
-
var hasMaxProperties = (schema) => {
|
|
89
|
-
return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
// src/mjst-extension.ts
|
|
93
|
-
var MJST_EXTENSION_KEY = "x-mjst";
|
|
94
|
-
var IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
95
|
-
var SUPPORTED_PRIMITIVES = new Set(["bigint"]);
|
|
96
|
-
var SAFE_BRAND = /^[\w$ -]+$/;
|
|
97
|
-
var readExtensionString = (schema, field) => {
|
|
98
|
-
if (!isSchemaObject(schema))
|
|
99
|
-
return;
|
|
100
|
-
const extension = schema[MJST_EXTENSION_KEY];
|
|
101
|
-
if (typeof extension !== "object" || extension === null)
|
|
102
|
-
return;
|
|
103
|
-
const value = extension[field];
|
|
104
|
-
return typeof value === "string" ? value : undefined;
|
|
105
|
-
};
|
|
106
|
-
var getMjstInstanceOf = (schema) => {
|
|
107
|
-
const instanceOf = readExtensionString(schema, "instanceOf");
|
|
108
|
-
return instanceOf !== undefined && IDENTIFIER.test(instanceOf) ? instanceOf : undefined;
|
|
109
|
-
};
|
|
110
|
-
var getMjstPrimitive = (schema) => {
|
|
111
|
-
const primitive = readExtensionString(schema, "primitive");
|
|
112
|
-
return primitive !== undefined && SUPPORTED_PRIMITIVES.has(primitive) ? primitive : undefined;
|
|
113
|
-
};
|
|
114
|
-
var getMjstBrand = (schema) => {
|
|
115
|
-
const brand = readExtensionString(schema, "brand");
|
|
116
|
-
return brand !== undefined && SAFE_BRAND.test(brand) ? brand : undefined;
|
|
117
|
-
};
|
|
118
|
-
export {
|
|
119
|
-
getMjstPrimitive,
|
|
120
|
-
getMjstInstanceOf,
|
|
121
|
-
getMjstBrand,
|
|
122
|
-
MJST_EXTENSION_KEY
|
|
1
|
+
import { isSchemaObject } from './schema-guards.js';
|
|
2
|
+
/**
|
|
3
|
+
* Vendor extension keyword carrying mjst-specific runtime hints that plain JSON
|
|
4
|
+
* Schema cannot express on its own. Adapters (TypeBox, Zod, ...) emit it when a
|
|
5
|
+
* source construct has no native JSON Schema equivalent, and the generators read
|
|
6
|
+
* it to produce the right TypeScript type and runtime checks.
|
|
7
|
+
*/
|
|
8
|
+
export const MJST_EXTENSION_KEY = 'x-mjst';
|
|
9
|
+
// Only identifier-safe class names are honoured, so a malicious or malformed
|
|
10
|
+
// schema cannot inject arbitrary code into the generated output.
|
|
11
|
+
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
12
|
+
// The non-JSON primitives we know how to generate type/runtime handling for.
|
|
13
|
+
// Anything outside this set is ignored so unknown hints degrade gracefully.
|
|
14
|
+
const SUPPORTED_PRIMITIVES = new Set(['bigint']);
|
|
15
|
+
// Brand names are embedded inside a single-quoted string literal in generated
|
|
16
|
+
// output, so we only allow characters that cannot break out of the literal.
|
|
17
|
+
const SAFE_BRAND = /^[\w$ -]+$/;
|
|
18
|
+
const readExtensionString = (schema, field) => {
|
|
19
|
+
if (!isSchemaObject(schema))
|
|
20
|
+
return undefined;
|
|
21
|
+
const extension = schema[MJST_EXTENSION_KEY];
|
|
22
|
+
if (typeof extension !== 'object' || extension === null)
|
|
23
|
+
return undefined;
|
|
24
|
+
const value = extension[field];
|
|
25
|
+
return typeof value === 'string' ? value : undefined;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Reads the `instanceOf` class name from a schema's `x-mjst` extension, when it
|
|
29
|
+
* is present and a safe identifier. Returns undefined otherwise so callers fall
|
|
30
|
+
* back to ordinary type handling.
|
|
31
|
+
*/
|
|
32
|
+
export const getMjstInstanceOf = (schema) => {
|
|
33
|
+
const instanceOf = readExtensionString(schema, 'instanceOf');
|
|
34
|
+
return instanceOf !== undefined && IDENTIFIER.test(instanceOf) ? instanceOf : undefined;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Reads the `primitive` type name from a schema's `x-mjst` extension, when it is
|
|
38
|
+
* one we support (e.g. `'bigint'`). Returns undefined otherwise.
|
|
39
|
+
*/
|
|
40
|
+
export const getMjstPrimitive = (schema) => {
|
|
41
|
+
const primitive = readExtensionString(schema, 'primitive');
|
|
42
|
+
return primitive !== undefined && SUPPORTED_PRIMITIVES.has(primitive) ? primitive : undefined;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Reads the nominal `brand` name from a schema's `x-mjst` extension, when it is
|
|
46
|
+
* present and safe to embed in generated output. Returns undefined otherwise.
|
|
47
|
+
*/
|
|
48
|
+
export const getMjstBrand = (schema) => {
|
|
49
|
+
const brand = readExtensionString(schema, 'brand');
|
|
50
|
+
return brand !== undefined && SAFE_BRAND.test(brand) ? brand : undefined;
|
|
123
51
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-documentation.d.ts","sourceRoot":"","sources":["../src/parse-documentation.ts"],"names":[],"mappings":"AAAA,KAAK,qBAAqB,GAAG;IAC3B,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,OAAO,CAAA;CACpB,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAA;CAClD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,0BACN,MAAM,cACjB,MAAM,uBACG,MAAM,KAC1B,mBAAmB,GAAG,IAuJxB,CAAA"}
|
|
@@ -1,95 +1,132 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
sectionTitle += " ";
|
|
15
|
-
const word = titleWords[i] ?? "";
|
|
16
|
-
sectionTitle += word.charAt(0).toUpperCase() + word.slice(1);
|
|
17
|
-
}
|
|
18
|
-
const headingLevelMatch = markdown.match(/^(#{2,5})\s+\S.*Object\s*$/m);
|
|
19
|
-
const headingHashes = headingLevelMatch?.[1] ?? "####";
|
|
20
|
-
const subHeadingHashes = headingHashes + "#";
|
|
21
|
-
const escapedHashes = headingHashes.replace(/#/g, "\\#");
|
|
22
|
-
const sectionRegex = new RegExp(`${escapedHashes}\\s+${sectionTitle}\\s*\\n([\\s\\S]*?)(?=\\n${escapedHashes}\\s|$)`, "i");
|
|
23
|
-
const sectionMatch = markdown.match(sectionRegex);
|
|
24
|
-
if (!sectionMatch) {
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
const sectionContent = sectionMatch?.[1];
|
|
28
|
-
if (!sectionContent) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
const escapedSubHashes = subHeadingHashes.replace(/#/g, "\\#");
|
|
32
|
-
const descriptionMatch = sectionContent.match(new RegExp(`^([\\s\\S]*?)(?=\\n?${escapedSubHashes}|$)`));
|
|
33
|
-
const description = descriptionMatch?.[1]?.trim().replace(/\n/g, " ") || "";
|
|
34
|
-
const fixedFieldsRegex = new RegExp(`${escapedSubHashes} Fixed Fields\\s*\\n([\\s\\S]*)`);
|
|
35
|
-
const fixedFieldsMatch = sectionContent.match(fixedFieldsRegex);
|
|
36
|
-
const properties = {};
|
|
37
|
-
if (fixedFieldsMatch?.[1]) {
|
|
38
|
-
const fixedFieldsContent = fixedFieldsMatch[1];
|
|
39
|
-
const lines = fixedFieldsContent.split(`
|
|
40
|
-
`);
|
|
41
|
-
let inTable = false;
|
|
42
|
-
for (const line of lines) {
|
|
43
|
-
if (line.includes("Field Name") || line.includes("---|")) {
|
|
44
|
-
inTable = true;
|
|
45
|
-
continue;
|
|
1
|
+
/**
|
|
2
|
+
* Fetches and parses OpenAPI specification documentation from the markdown source.
|
|
3
|
+
*
|
|
4
|
+
* When a section has no Fixed Fields table (e.g. Header Object delegates to Parameter Object),
|
|
5
|
+
* pass `fallbackCommentUrl` to inherit property documentation from another section.
|
|
6
|
+
*/
|
|
7
|
+
export const parseDocumentation = (markdownDocumentation, commentUrl, fallbackCommentUrl) => {
|
|
8
|
+
try {
|
|
9
|
+
const markdown = markdownDocumentation;
|
|
10
|
+
// Extract the fragment ID from the URL (e.g., #info-object)
|
|
11
|
+
const fragmentId = commentUrl.split('#')[1];
|
|
12
|
+
if (!fragmentId) {
|
|
13
|
+
return null;
|
|
46
14
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
15
|
+
// Extract the base URL (everything before the #)
|
|
16
|
+
const baseUrl = commentUrl.split('#')[0];
|
|
17
|
+
// Convert fragment ID to title case (e.g., "info-object" -> "Info Object")
|
|
18
|
+
const titleWords = fragmentId.split('-');
|
|
19
|
+
let sectionTitle = '';
|
|
20
|
+
for (let i = 0; i < titleWords.length; i++) {
|
|
21
|
+
if (i > 0)
|
|
22
|
+
sectionTitle += ' ';
|
|
23
|
+
const word = titleWords[i] ?? '';
|
|
24
|
+
sectionTitle += word.charAt(0).toUpperCase() + word.slice(1);
|
|
50
25
|
}
|
|
51
|
-
|
|
52
|
-
|
|
26
|
+
// Detect the heading level used for object sections in this markdown file.
|
|
27
|
+
// OAS 3.1 uses #### while OAS 3.2 uses ###, so we probe for both.
|
|
28
|
+
const headingLevelMatch = markdown.match(/^(#{2,5})\s+\S.*Object\s*$/m);
|
|
29
|
+
const headingHashes = headingLevelMatch?.[1] ?? '####';
|
|
30
|
+
const subHeadingHashes = headingHashes + '#';
|
|
31
|
+
// Find the section in the markdown
|
|
32
|
+
// Match the section heading and capture everything until the next same-level heading or end of file
|
|
33
|
+
const escapedHashes = headingHashes.replace(/#/g, '\\#');
|
|
34
|
+
const sectionRegex = new RegExp(`${escapedHashes}\\s+${sectionTitle}\\s*\\n([\\s\\S]*?)(?=\\n${escapedHashes}\\s|$)`, 'i');
|
|
35
|
+
const sectionMatch = markdown.match(sectionRegex);
|
|
36
|
+
if (!sectionMatch) {
|
|
37
|
+
return null;
|
|
53
38
|
}
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
39
|
+
const sectionContent = sectionMatch?.[1];
|
|
40
|
+
if (!sectionContent) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
// Extract the description (paragraphs before the "Fixed Fields" sub-heading)
|
|
44
|
+
// \n? handles the case where sectionContent starts directly with the sub-heading (no leading newline),
|
|
45
|
+
// which happens when the section heading is followed by a blank line consumed by \s* in sectionRegex.
|
|
46
|
+
const escapedSubHashes = subHeadingHashes.replace(/#/g, '\\#');
|
|
47
|
+
const descriptionMatch = sectionContent.match(new RegExp(`^([\\s\\S]*?)(?=\\n?${escapedSubHashes}|$)`));
|
|
48
|
+
const description = descriptionMatch?.[1]?.trim().replace(/\n/g, ' ') || '';
|
|
49
|
+
// Extract all Fixed Fields tables within the section (some objects like Encoding Object
|
|
50
|
+
// split their fields across multiple sub-tables under different sub-sub-headings).
|
|
51
|
+
// Capture everything from the first "Fixed Fields" sub-heading to the end of the section —
|
|
52
|
+
// the section content is already bounded by the outer section regex so we don't need
|
|
53
|
+
// a stop condition here.
|
|
54
|
+
const fixedFieldsRegex = new RegExp(`${escapedSubHashes} Fixed Fields\\s*\\n([\\s\\S]*)`);
|
|
55
|
+
const fixedFieldsMatch = sectionContent.match(fixedFieldsRegex);
|
|
56
|
+
const properties = {};
|
|
57
|
+
if (fixedFieldsMatch?.[1]) {
|
|
58
|
+
const fixedFieldsContent = fixedFieldsMatch[1];
|
|
59
|
+
// Parse all markdown table rows across all sub-tables within the Fixed Fields section
|
|
60
|
+
const lines = fixedFieldsContent.split('\n');
|
|
61
|
+
let inTable = false;
|
|
62
|
+
for (const line of lines) {
|
|
63
|
+
// Skip the header row and separator row
|
|
64
|
+
if (line.includes('Field Name') || line.includes('---|')) {
|
|
65
|
+
inTable = true;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
// A ###### sub-heading resets inTable so we re-enter on the next header row
|
|
69
|
+
if (line.startsWith('#')) {
|
|
70
|
+
inTable = false;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!inTable || !line.trim() || !line.includes('|')) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
// Parse table row: | fieldName | type | description |
|
|
77
|
+
// First, replace escaped pipes (\|) with a placeholder to avoid splitting on them
|
|
78
|
+
const lineWithPlaceholder = line.replace(/\\\|/g, '___PIPE___');
|
|
79
|
+
const cells = lineWithPlaceholder
|
|
80
|
+
.split('|')
|
|
81
|
+
.map((cell) => cell.replace(/___PIPE___/g, '|').trim())
|
|
82
|
+
.filter((cell) => cell);
|
|
83
|
+
if (cells.length >= 3) {
|
|
84
|
+
const fieldName = cells[0]?.replace(/<[^>]*>/g, '').trim(); // Remove HTML tags
|
|
85
|
+
// Some OpenAPI tables include an "Applies To" column before Description.
|
|
86
|
+
// We always want the right-most column as the property description.
|
|
87
|
+
const descriptionCellIndex = cells.length >= 4 ? 3 : 2;
|
|
88
|
+
let fieldDescription = cells[descriptionCellIndex]?.trim() || '';
|
|
89
|
+
const isRequired = fieldDescription.includes('REQUIRED');
|
|
90
|
+
// Replace relative anchor links with full URLs using the base URL
|
|
91
|
+
fieldDescription = fieldDescription.replace(/\(#([^)]+)\)/g, `(${baseUrl}#$1)`);
|
|
92
|
+
// Only accept field names that look like valid identifiers (camelCase or plain words).
|
|
93
|
+
// This filters out rows from non-field tables (e.g. type/contentType default tables)
|
|
94
|
+
// where the "field name" column contains type annotations like `string` or [_absent_].
|
|
95
|
+
const isValidFieldName = fieldName &&
|
|
96
|
+
!fieldName.includes('Field Name') &&
|
|
97
|
+
!fieldName.startsWith('`') &&
|
|
98
|
+
!fieldName.startsWith('[') &&
|
|
99
|
+
/^[a-zA-Z]/.test(fieldName);
|
|
100
|
+
if (isValidFieldName) {
|
|
101
|
+
properties[fieldName] = {
|
|
102
|
+
description: fieldDescription,
|
|
103
|
+
isRequired,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Format title as "Info object" instead of "Info Object"
|
|
110
|
+
const title = sectionTitle.replace(/\sObject$/, ' object');
|
|
111
|
+
// If no properties were found and a fallback URL is provided, inherit properties from it
|
|
112
|
+
if (Object.keys(properties).length === 0 && fallbackCommentUrl) {
|
|
113
|
+
const fallback = parseDocumentation(markdownDocumentation, fallbackCommentUrl);
|
|
114
|
+
if (fallback && Object.keys(fallback.properties).length > 0) {
|
|
115
|
+
return {
|
|
116
|
+
title,
|
|
117
|
+
description,
|
|
118
|
+
properties: fallback.properties,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
69
121
|
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
const title = sectionTitle.replace(/\sObject$/, " object");
|
|
73
|
-
if (Object.keys(properties).length === 0 && fallbackCommentUrl) {
|
|
74
|
-
const fallback = parseDocumentation(markdownDocumentation, fallbackCommentUrl);
|
|
75
|
-
if (fallback && Object.keys(fallback.properties).length > 0) {
|
|
76
122
|
return {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
123
|
+
title,
|
|
124
|
+
description,
|
|
125
|
+
properties,
|
|
80
126
|
};
|
|
81
|
-
}
|
|
82
127
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
};
|
|
88
|
-
} catch (error) {
|
|
89
|
-
console.error("Failed to fetch OpenAPI documentation:", error);
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
export {
|
|
94
|
-
parseDocumentation
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.error('Failed to fetch OpenAPI documentation:', error);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
95
132
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ref-to-filename.d.ts","sourceRoot":"","sources":["../src/ref-to-filename.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAM1B,CAAA;AA0DlB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,aAAa,QAAS,MAAM,KAAG,MAiB3C,CAAA"}
|