@amritk/helpers 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/derive-root-type-name.d.ts +13 -6
- package/dist/derive-root-type-name.js +23 -10
- package/package.json +1 -1
- package/src/derive-root-type-name.ts +22 -10
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|

|
|
10
10
|

|
|
11
11
|

|
|
12
|
-

|
|
13
13
|
|
|
14
14
|
</div>
|
|
15
15
|
|
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Derives the root type name for a generated schema
|
|
2
|
+
* Derives the root type name for a generated schema.
|
|
3
3
|
*
|
|
4
4
|
* We name the root after the schema itself instead of a generic "Document" so
|
|
5
5
|
* the generated types and parsers read naturally (e.g. an OpenAPI schema yields
|
|
6
|
-
* `OpenApi` / `parseOpenApi
|
|
7
|
-
*
|
|
6
|
+
* `OpenApi` / `parseOpenApi`, and a `spec-plan.json` file yields `SpecPlan` /
|
|
7
|
+
* `parseSpecPlan`). The schema's `title` keyword wins when present; otherwise
|
|
8
|
+
* the caller can supply the schema's filename (without extension) as a
|
|
9
|
+
* `fallbackName` so the name still reflects the source. When neither yields a
|
|
10
|
+
* usable identifier we fall back to "Document" to keep output deterministic.
|
|
8
11
|
*
|
|
9
12
|
* @param schema - The root JSON Schema. A boolean schema or one without a
|
|
10
|
-
* string `title`
|
|
11
|
-
* @
|
|
13
|
+
* string `title` uses `fallbackName`.
|
|
14
|
+
* @param fallbackName - Optional name (typically the schema's base filename)
|
|
15
|
+
* used when the schema has no usable `title`. PascalCased the same way.
|
|
16
|
+
* @returns A PascalCase type name derived from the title or fallback, or
|
|
17
|
+
* "Document".
|
|
12
18
|
*
|
|
13
19
|
* @example
|
|
14
20
|
* ```ts
|
|
15
21
|
* deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
|
|
16
22
|
* deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
|
|
23
|
+
* deriveRootTypeName({ type: 'object' }, 'spec-plan') // 'SpecPlan'
|
|
17
24
|
* deriveRootTypeName({ type: 'object' }) // 'Document'
|
|
18
25
|
* ```
|
|
19
26
|
*/
|
|
20
|
-
export declare const deriveRootTypeName: (schema: unknown) => string;
|
|
27
|
+
export declare const deriveRootTypeName: (schema: unknown, fallbackName?: string) => string;
|
|
@@ -12,28 +12,41 @@ const titleToPascalCase = (title) => {
|
|
|
12
12
|
return pascal.replace(/^\d+/, '');
|
|
13
13
|
};
|
|
14
14
|
/**
|
|
15
|
-
* Derives the root type name for a generated schema
|
|
15
|
+
* Derives the root type name for a generated schema.
|
|
16
16
|
*
|
|
17
17
|
* We name the root after the schema itself instead of a generic "Document" so
|
|
18
18
|
* the generated types and parsers read naturally (e.g. an OpenAPI schema yields
|
|
19
|
-
* `OpenApi` / `parseOpenApi
|
|
20
|
-
*
|
|
19
|
+
* `OpenApi` / `parseOpenApi`, and a `spec-plan.json` file yields `SpecPlan` /
|
|
20
|
+
* `parseSpecPlan`). The schema's `title` keyword wins when present; otherwise
|
|
21
|
+
* the caller can supply the schema's filename (without extension) as a
|
|
22
|
+
* `fallbackName` so the name still reflects the source. When neither yields a
|
|
23
|
+
* usable identifier we fall back to "Document" to keep output deterministic.
|
|
21
24
|
*
|
|
22
25
|
* @param schema - The root JSON Schema. A boolean schema or one without a
|
|
23
|
-
* string `title`
|
|
24
|
-
* @
|
|
26
|
+
* string `title` uses `fallbackName`.
|
|
27
|
+
* @param fallbackName - Optional name (typically the schema's base filename)
|
|
28
|
+
* used when the schema has no usable `title`. PascalCased the same way.
|
|
29
|
+
* @returns A PascalCase type name derived from the title or fallback, or
|
|
30
|
+
* "Document".
|
|
25
31
|
*
|
|
26
32
|
* @example
|
|
27
33
|
* ```ts
|
|
28
34
|
* deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
|
|
29
35
|
* deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
|
|
36
|
+
* deriveRootTypeName({ type: 'object' }, 'spec-plan') // 'SpecPlan'
|
|
30
37
|
* deriveRootTypeName({ type: 'object' }) // 'Document'
|
|
31
38
|
* ```
|
|
32
39
|
*/
|
|
33
|
-
export const deriveRootTypeName = (schema) => {
|
|
34
|
-
if (
|
|
35
|
-
|
|
40
|
+
export const deriveRootTypeName = (schema, fallbackName) => {
|
|
41
|
+
if (isObject(schema) && typeof schema['title'] === 'string') {
|
|
42
|
+
const fromTitle = titleToPascalCase(schema['title']);
|
|
43
|
+
if (fromTitle)
|
|
44
|
+
return fromTitle;
|
|
36
45
|
}
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
if (typeof fallbackName === 'string') {
|
|
47
|
+
const fromFilename = titleToPascalCase(fallbackName);
|
|
48
|
+
if (fromFilename)
|
|
49
|
+
return fromFilename;
|
|
50
|
+
}
|
|
51
|
+
return 'Document';
|
|
39
52
|
};
|
package/package.json
CHANGED
|
@@ -14,29 +14,41 @@ const titleToPascalCase = (title: string): string => {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* Derives the root type name for a generated schema
|
|
17
|
+
* Derives the root type name for a generated schema.
|
|
18
18
|
*
|
|
19
19
|
* We name the root after the schema itself instead of a generic "Document" so
|
|
20
20
|
* the generated types and parsers read naturally (e.g. an OpenAPI schema yields
|
|
21
|
-
* `OpenApi` / `parseOpenApi
|
|
22
|
-
*
|
|
21
|
+
* `OpenApi` / `parseOpenApi`, and a `spec-plan.json` file yields `SpecPlan` /
|
|
22
|
+
* `parseSpecPlan`). The schema's `title` keyword wins when present; otherwise
|
|
23
|
+
* the caller can supply the schema's filename (without extension) as a
|
|
24
|
+
* `fallbackName` so the name still reflects the source. When neither yields a
|
|
25
|
+
* usable identifier we fall back to "Document" to keep output deterministic.
|
|
23
26
|
*
|
|
24
27
|
* @param schema - The root JSON Schema. A boolean schema or one without a
|
|
25
|
-
* string `title`
|
|
26
|
-
* @
|
|
28
|
+
* string `title` uses `fallbackName`.
|
|
29
|
+
* @param fallbackName - Optional name (typically the schema's base filename)
|
|
30
|
+
* used when the schema has no usable `title`. PascalCased the same way.
|
|
31
|
+
* @returns A PascalCase type name derived from the title or fallback, or
|
|
32
|
+
* "Document".
|
|
27
33
|
*
|
|
28
34
|
* @example
|
|
29
35
|
* ```ts
|
|
30
36
|
* deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
|
|
31
37
|
* deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
|
|
38
|
+
* deriveRootTypeName({ type: 'object' }, 'spec-plan') // 'SpecPlan'
|
|
32
39
|
* deriveRootTypeName({ type: 'object' }) // 'Document'
|
|
33
40
|
* ```
|
|
34
41
|
*/
|
|
35
|
-
export const deriveRootTypeName = (schema: unknown): string => {
|
|
36
|
-
if (
|
|
37
|
-
|
|
42
|
+
export const deriveRootTypeName = (schema: unknown, fallbackName?: string): string => {
|
|
43
|
+
if (isObject(schema) && typeof schema['title'] === 'string') {
|
|
44
|
+
const fromTitle = titleToPascalCase(schema['title'])
|
|
45
|
+
if (fromTitle) return fromTitle
|
|
38
46
|
}
|
|
39
47
|
|
|
40
|
-
|
|
41
|
-
|
|
48
|
+
if (typeof fallbackName === 'string') {
|
|
49
|
+
const fromFilename = titleToPascalCase(fallbackName)
|
|
50
|
+
if (fromFilename) return fromFilename
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return 'Document'
|
|
42
54
|
}
|