@amritk/helpers 0.5.0 → 0.6.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/dist/generate-type-definition.d.ts +5 -0
- package/dist/generate-type-definition.js +10 -10
- package/dist/ref-to-name.d.ts +10 -7
- package/dist/ref-to-name.js +3 -3
- package/package.json +1 -1
- package/src/generate-type-definition.test.ts +72 -53
- package/src/generate-type-definition.ts +12 -7
- package/src/ref-to-name.test.ts +20 -11
- package/src/ref-to-name.ts +16 -12
|
@@ -3,6 +3,11 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
|
3
3
|
export type TypeOptions = {
|
|
4
4
|
/** When true, every property, array, and record in the generated types is emitted as readonly. */
|
|
5
5
|
readonly readonly?: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Suffix appended to every generated type name derived from a `$ref`.
|
|
8
|
+
* Defaults to `''` (no suffix). Set to e.g. `'Object'` to emit `ContactObject`.
|
|
9
|
+
*/
|
|
10
|
+
readonly typeSuffix?: string;
|
|
6
11
|
};
|
|
7
12
|
/**
|
|
8
13
|
* Generates a TypeScript type definition from a JSON Schema.
|
|
@@ -158,15 +158,15 @@ var refToFilename = (ref) => {
|
|
|
158
158
|
};
|
|
159
159
|
|
|
160
160
|
// src/ref-to-name.ts
|
|
161
|
-
var kebabToPascal = (kebab) => {
|
|
161
|
+
var kebabToPascal = (kebab, suffix) => {
|
|
162
162
|
const words = kebab.split("-");
|
|
163
163
|
let pascalCase = "";
|
|
164
164
|
for (const word of words) {
|
|
165
165
|
pascalCase += word.charAt(0).toUpperCase() + word.slice(1);
|
|
166
166
|
}
|
|
167
|
-
return pascalCase +
|
|
167
|
+
return pascalCase + suffix;
|
|
168
168
|
};
|
|
169
|
-
var refToName = (ref) => kebabToPascal(refToFilename(ref));
|
|
169
|
+
var refToName = (ref, suffix = "") => kebabToPascal(refToFilename(ref), suffix);
|
|
170
170
|
|
|
171
171
|
// src/safe-accessor.ts
|
|
172
172
|
var JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
@@ -297,13 +297,13 @@ var getUnbrandedType = (schema, options = {}) => {
|
|
|
297
297
|
if (!schema.$ref.startsWith("#")) {
|
|
298
298
|
return "unknown";
|
|
299
299
|
}
|
|
300
|
-
return refToName(schema.$ref);
|
|
300
|
+
return refToName(schema.$ref, options.typeSuffix);
|
|
301
301
|
}
|
|
302
302
|
if (schema.$dynamicRef) {
|
|
303
303
|
if (schema.$dynamicRef === "#meta") {
|
|
304
|
-
return
|
|
304
|
+
return `Schema${options.typeSuffix ?? ""}`;
|
|
305
305
|
}
|
|
306
|
-
return refToName(schema.$dynamicRef);
|
|
306
|
+
return refToName(schema.$dynamicRef, options.typeSuffix);
|
|
307
307
|
}
|
|
308
308
|
if (schema.const !== undefined) {
|
|
309
309
|
return JSON.stringify(schema.const);
|
|
@@ -350,7 +350,7 @@ var getUnbrandedType = (schema, options = {}) => {
|
|
|
350
350
|
if (conditionalResult) {
|
|
351
351
|
const baseType = getTypeScriptType(conditionalResult.schema, options);
|
|
352
352
|
if (conditionalResult.thenRef) {
|
|
353
|
-
return `(${baseType}) & ${refToName(conditionalResult.thenRef)}`;
|
|
353
|
+
return `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}`;
|
|
354
354
|
}
|
|
355
355
|
return baseType;
|
|
356
356
|
}
|
|
@@ -543,12 +543,12 @@ var generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
|
543
543
|
if (isSchemaObject(schema) && Array.isArray(schema.allOf)) {
|
|
544
544
|
for (const entry of schema.allOf) {
|
|
545
545
|
if (isSchemaObject(entry) && entry.$ref) {
|
|
546
|
-
allOfIntersections.push(refToName(entry.$ref));
|
|
546
|
+
allOfIntersections.push(refToName(entry.$ref, options.typeSuffix));
|
|
547
547
|
}
|
|
548
548
|
}
|
|
549
549
|
}
|
|
550
550
|
if (isSchemaObject(schema) && typeof schema.$ref === "string" && schema.$ref.startsWith("#")) {
|
|
551
|
-
allOfIntersections.push(refToName(schema.$ref));
|
|
551
|
+
allOfIntersections.push(refToName(schema.$ref, options.typeSuffix));
|
|
552
552
|
}
|
|
553
553
|
let result = "";
|
|
554
554
|
if (jsDocTitle && jsDocDescription) {
|
|
@@ -558,7 +558,7 @@ var generateTypeDefinition = (schema, typeName, options = {}) => {
|
|
|
558
558
|
` + properties + `
|
|
559
559
|
}`;
|
|
560
560
|
if (conditionalThenRef) {
|
|
561
|
-
typeBody += " & " + refToName(conditionalThenRef);
|
|
561
|
+
typeBody += " & " + refToName(conditionalThenRef, options.typeSuffix);
|
|
562
562
|
}
|
|
563
563
|
for (const intersectionType of allOfIntersections) {
|
|
564
564
|
typeBody += " & " + intersectionType;
|
package/dist/ref-to-name.d.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Converts a JSON Schema $ref to a type name.
|
|
3
|
-
* Derives the filename via `refToFilename` then converts to PascalCase
|
|
4
|
-
* an
|
|
3
|
+
* Derives the filename via `refToFilename` then converts to PascalCase,
|
|
4
|
+
* appending an optional suffix.
|
|
5
5
|
*
|
|
6
6
|
* Handles all ref forms: internal `#/$defs/...`, `#/definitions/...`, and URI refs.
|
|
7
7
|
*
|
|
8
8
|
* @param ref - The $ref string
|
|
9
|
-
* @
|
|
9
|
+
* @param suffix - Optional suffix appended to the PascalCase name. Defaults to
|
|
10
|
+
* `''` (no suffix). Pass e.g. `'Object'` to get `ContactObject`.
|
|
11
|
+
* @returns The type name in PascalCase with the suffix applied
|
|
10
12
|
*
|
|
11
13
|
* @example
|
|
12
14
|
* ```ts
|
|
13
|
-
* refToName('#/$defs/contact') // '
|
|
14
|
-
* refToName('#/$defs/server-variable') // '
|
|
15
|
-
* refToName('
|
|
15
|
+
* refToName('#/$defs/contact') // 'Contact'
|
|
16
|
+
* refToName('#/$defs/server-variable') // 'ServerVariable'
|
|
17
|
+
* refToName('#/$defs/contact', 'Object') // 'ContactObject'
|
|
18
|
+
* refToName('http://asyncapi.com/definitions/3.1.0/channel.json') // 'Channel'
|
|
16
19
|
* ```
|
|
17
20
|
*/
|
|
18
|
-
export declare const refToName: (ref: string) => string;
|
|
21
|
+
export declare const refToName: (ref: string, suffix?: string) => string;
|
package/dist/ref-to-name.js
CHANGED
|
@@ -40,15 +40,15 @@ var refToFilename = (ref) => {
|
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
// src/ref-to-name.ts
|
|
43
|
-
var kebabToPascal = (kebab) => {
|
|
43
|
+
var kebabToPascal = (kebab, suffix) => {
|
|
44
44
|
const words = kebab.split("-");
|
|
45
45
|
let pascalCase = "";
|
|
46
46
|
for (const word of words) {
|
|
47
47
|
pascalCase += word.charAt(0).toUpperCase() + word.slice(1);
|
|
48
48
|
}
|
|
49
|
-
return pascalCase +
|
|
49
|
+
return pascalCase + suffix;
|
|
50
50
|
};
|
|
51
|
-
var refToName = (ref) => kebabToPascal(refToFilename(ref));
|
|
51
|
+
var refToName = (ref, suffix = "") => kebabToPascal(refToFilename(ref), suffix);
|
|
52
52
|
export {
|
|
53
53
|
refToName
|
|
54
54
|
};
|
package/package.json
CHANGED
|
@@ -164,9 +164,9 @@ describe('generateTypeDefinition', () => {
|
|
|
164
164
|
},
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
const result = generateTypeDefinition(schema, '
|
|
167
|
+
const result = generateTypeDefinition(schema, 'Generic')
|
|
168
168
|
|
|
169
|
-
expect(result).toStrictEqual('export type
|
|
169
|
+
expect(result).toStrictEqual('export type Generic = {\n' + ' metadata?: object;\n' + '};')
|
|
170
170
|
})
|
|
171
171
|
|
|
172
172
|
it('generates type for complex nested structure with arrays and objects', () => {
|
|
@@ -220,9 +220,9 @@ describe('generateTypeDefinition', () => {
|
|
|
220
220
|
properties: {},
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
const result = generateTypeDefinition(schema, '
|
|
223
|
+
const result = generateTypeDefinition(schema, 'Empty')
|
|
224
224
|
|
|
225
|
-
expect(result).toStrictEqual('export type
|
|
225
|
+
expect(result).toStrictEqual('export type Empty = {\n' + '\n' + '};')
|
|
226
226
|
})
|
|
227
227
|
|
|
228
228
|
it('generates type for object with no type specified', () => {
|
|
@@ -575,18 +575,18 @@ describe('generateTypeDefinition', () => {
|
|
|
575
575
|
required: ['title', 'version'],
|
|
576
576
|
}
|
|
577
577
|
|
|
578
|
-
const result = generateTypeDefinition(info, '
|
|
578
|
+
const result = generateTypeDefinition(info, 'Info')
|
|
579
579
|
|
|
580
580
|
expect(result).toStrictEqual(
|
|
581
581
|
'/**\n' +
|
|
582
|
-
'*
|
|
582
|
+
'* Info\n' +
|
|
583
583
|
'*\n' +
|
|
584
584
|
'* https://spec.openapis.org/oas/v3.1#info-object\n' +
|
|
585
585
|
'*/\n' +
|
|
586
|
-
'export type
|
|
586
|
+
'export type Info = {\n' +
|
|
587
587
|
' title: string;\n' +
|
|
588
588
|
' summary?: string;\n' +
|
|
589
|
-
' contact?:
|
|
589
|
+
' contact?: Contact;\n' +
|
|
590
590
|
' version: string;\n' +
|
|
591
591
|
'};',
|
|
592
592
|
)
|
|
@@ -611,18 +611,18 @@ describe('generateTypeDefinition', () => {
|
|
|
611
611
|
},
|
|
612
612
|
}
|
|
613
613
|
|
|
614
|
-
const result = generateTypeDefinition(components, '
|
|
614
|
+
const result = generateTypeDefinition(components, 'Components')
|
|
615
615
|
|
|
616
616
|
expect(result).toStrictEqual(
|
|
617
|
-
'export type
|
|
618
|
-
' responses?: Record<string,
|
|
619
|
-
' parameters?: Record<string,
|
|
620
|
-
' pathItems?: Record<string,
|
|
617
|
+
'export type Components = {\n' +
|
|
618
|
+
' responses?: Record<string, Response>;\n' +
|
|
619
|
+
' parameters?: Record<string, Parameter>;\n' +
|
|
620
|
+
' pathItems?: Record<string, PathItem>;\n' +
|
|
621
621
|
'};',
|
|
622
622
|
)
|
|
623
623
|
})
|
|
624
624
|
|
|
625
|
-
it('generates type for object with paths property as Record<string,
|
|
625
|
+
it('generates type for object with paths property as Record<string, PathItem>', () => {
|
|
626
626
|
const document: JSONSchema.Object = {
|
|
627
627
|
$comment: 'https://spec.openapis.org/oas/v3.1#openapi-object',
|
|
628
628
|
type: 'object',
|
|
@@ -647,8 +647,8 @@ describe('generateTypeDefinition', () => {
|
|
|
647
647
|
'*/\n' +
|
|
648
648
|
'export type Document = {\n' +
|
|
649
649
|
' openapi: string;\n' +
|
|
650
|
-
' info:
|
|
651
|
-
' paths?: Record<string,
|
|
650
|
+
' info: Info;\n' +
|
|
651
|
+
' paths?: Record<string, PathItem>;\n' +
|
|
652
652
|
'};',
|
|
653
653
|
)
|
|
654
654
|
})
|
|
@@ -702,12 +702,12 @@ describe('generateTypeDefinition', () => {
|
|
|
702
702
|
'*/\n' +
|
|
703
703
|
'export type Document = {\n' +
|
|
704
704
|
' openapi: string;\n' +
|
|
705
|
-
' info:
|
|
705
|
+
' info: Info;\n' +
|
|
706
706
|
' jsonSchemaDialect?: string;\n' +
|
|
707
|
-
' servers?:
|
|
708
|
-
' paths?: Record<string,
|
|
709
|
-
' webhooks?: Record<string,
|
|
710
|
-
' components?:
|
|
707
|
+
' servers?: Server[];\n' +
|
|
708
|
+
' paths?: Record<string, PathItem>;\n' +
|
|
709
|
+
' webhooks?: Record<string, PathItem>;\n' +
|
|
710
|
+
' components?: Components;\n' +
|
|
711
711
|
'};',
|
|
712
712
|
)
|
|
713
713
|
})
|
|
@@ -723,15 +723,15 @@ describe('generateTypeDefinition', () => {
|
|
|
723
723
|
},
|
|
724
724
|
}
|
|
725
725
|
|
|
726
|
-
const result = generateTypeDefinition(paths, '
|
|
726
|
+
const result = generateTypeDefinition(paths, 'Paths')
|
|
727
727
|
|
|
728
728
|
expect(result).toStrictEqual(
|
|
729
729
|
'/**\n' +
|
|
730
|
-
'*
|
|
730
|
+
'* Paths\n' +
|
|
731
731
|
'*\n' +
|
|
732
732
|
'* https://spec.openapis.org/oas/v3.1#paths-object\n' +
|
|
733
733
|
'*/\n' +
|
|
734
|
-
'export type
|
|
734
|
+
'export type Paths = Record<string, PathItem>;',
|
|
735
735
|
)
|
|
736
736
|
})
|
|
737
737
|
|
|
@@ -743,7 +743,7 @@ describe('generateTypeDefinition', () => {
|
|
|
743
743
|
name: { type: 'string' as const },
|
|
744
744
|
},
|
|
745
745
|
}
|
|
746
|
-
const result = generateTypeDefinition(schema, '
|
|
746
|
+
const result = generateTypeDefinition(schema, 'InfoExtensions')
|
|
747
747
|
expect(result).toContain("'x-linkedin'?: string;")
|
|
748
748
|
expect(result).toContain('name?: string;')
|
|
749
749
|
})
|
|
@@ -767,11 +767,9 @@ describe('generateTypeDefinition', () => {
|
|
|
767
767
|
},
|
|
768
768
|
}
|
|
769
769
|
|
|
770
|
-
const result = generateTypeDefinition(schema, '
|
|
770
|
+
const result = generateTypeDefinition(schema, 'TypeHttp')
|
|
771
771
|
|
|
772
|
-
expect(result).toStrictEqual(
|
|
773
|
-
'export type TypeHttpObject = {\n' + ' type: "http";\n' + ' scheme: string;\n' + '};',
|
|
774
|
-
)
|
|
772
|
+
expect(result).toStrictEqual('export type TypeHttp = {\n' + ' type: "http";\n' + ' scheme: string;\n' + '};')
|
|
775
773
|
})
|
|
776
774
|
|
|
777
775
|
it('generates required property from then properties without explicit required', () => {
|
|
@@ -792,10 +790,10 @@ describe('generateTypeDefinition', () => {
|
|
|
792
790
|
},
|
|
793
791
|
}
|
|
794
792
|
|
|
795
|
-
const result = generateTypeDefinition(schema, '
|
|
793
|
+
const result = generateTypeDefinition(schema, 'TypeHttp')
|
|
796
794
|
|
|
797
795
|
expect(result).toStrictEqual(
|
|
798
|
-
'export type
|
|
796
|
+
'export type TypeHttp = {\n' + ' type: "http";\n' + ' bearerFormat: string;\n' + '};',
|
|
799
797
|
)
|
|
800
798
|
})
|
|
801
799
|
|
|
@@ -823,9 +821,9 @@ describe('generateTypeDefinition', () => {
|
|
|
823
821
|
},
|
|
824
822
|
}
|
|
825
823
|
|
|
826
|
-
const result = generateTypeDefinition(schema, '
|
|
824
|
+
const result = generateTypeDefinition(schema, 'TypeHttp')
|
|
827
825
|
|
|
828
|
-
expect(result).toContain('*
|
|
826
|
+
expect(result).toContain('* TypeHttp')
|
|
829
827
|
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#security-scheme-object')
|
|
830
828
|
expect(result).toContain('type: "http";')
|
|
831
829
|
expect(result).toContain('scheme: string;')
|
|
@@ -847,13 +845,13 @@ describe('generateTypeDefinition', () => {
|
|
|
847
845
|
allOf: [{ $ref: '#/$defs/type-apikey' }, { $ref: '#/$defs/type-http' }, { $ref: '#/$defs/type-oauth2' }],
|
|
848
846
|
}
|
|
849
847
|
|
|
850
|
-
const result = generateTypeDefinition(securityScheme, '
|
|
848
|
+
const result = generateTypeDefinition(securityScheme, 'SecurityScheme')
|
|
851
849
|
|
|
852
850
|
expect(result).toStrictEqual(
|
|
853
|
-
'export type
|
|
851
|
+
'export type SecurityScheme = {\n' +
|
|
854
852
|
' type: "apiKey" | "http" | "oauth2";\n' +
|
|
855
853
|
' description?: string;\n' +
|
|
856
|
-
'} &
|
|
854
|
+
'} & TypeApikey & TypeHttp & TypeOauth2;',
|
|
857
855
|
)
|
|
858
856
|
})
|
|
859
857
|
|
|
@@ -870,12 +868,12 @@ describe('generateTypeDefinition', () => {
|
|
|
870
868
|
allOf: [{ $ref: '#/$defs/type-apikey' }, { $ref: '#/$defs/type-http' }],
|
|
871
869
|
}
|
|
872
870
|
|
|
873
|
-
const result = generateTypeDefinition(securityScheme, '
|
|
871
|
+
const result = generateTypeDefinition(securityScheme, 'SecurityScheme')
|
|
874
872
|
|
|
875
|
-
expect(result).toContain('*
|
|
873
|
+
expect(result).toContain('* SecurityScheme')
|
|
876
874
|
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#security-scheme-object')
|
|
877
875
|
expect(result).toContain('type: "apiKey" | "http" | "oauth2";')
|
|
878
|
-
expect(result).toContain('} &
|
|
876
|
+
expect(result).toContain('} & TypeApikey & TypeHttp;')
|
|
879
877
|
})
|
|
880
878
|
|
|
881
879
|
it('generates record type for patternProperties-only schema without explicit type', () => {
|
|
@@ -885,9 +883,9 @@ describe('generateTypeDefinition', () => {
|
|
|
885
883
|
},
|
|
886
884
|
}
|
|
887
885
|
|
|
888
|
-
const result = generateTypeDefinition(schema, '
|
|
886
|
+
const result = generateTypeDefinition(schema, 'SpecificationExtensions')
|
|
889
887
|
|
|
890
|
-
expect(result).toStrictEqual('export type
|
|
888
|
+
expect(result).toStrictEqual('export type SpecificationExtensions = Record<`x-${string}`, unknown>;')
|
|
891
889
|
})
|
|
892
890
|
|
|
893
891
|
it('generates Record<string, never> for patternProperties-only schema with false boolean value', () => {
|
|
@@ -899,9 +897,9 @@ describe('generateTypeDefinition', () => {
|
|
|
899
897
|
},
|
|
900
898
|
}
|
|
901
899
|
|
|
902
|
-
const result = generateTypeDefinition(schema, '
|
|
900
|
+
const result = generateTypeDefinition(schema, 'Restricted')
|
|
903
901
|
|
|
904
|
-
expect(result).toStrictEqual('export type
|
|
902
|
+
expect(result).toStrictEqual('export type Restricted = Record<`x-${string}`, never>;')
|
|
905
903
|
})
|
|
906
904
|
|
|
907
905
|
it('generates Schema type for property with $dynamicRef pointing to #meta', () => {
|
|
@@ -930,7 +928,7 @@ describe('generateTypeDefinition', () => {
|
|
|
930
928
|
|
|
931
929
|
const result = generateTypeDefinition(schema, 'ContentContainer')
|
|
932
930
|
|
|
933
|
-
expect(result).toContain('content?:
|
|
931
|
+
expect(result).toContain('content?: Schema')
|
|
934
932
|
})
|
|
935
933
|
|
|
936
934
|
it('generates union type for schema with array of types', () => {
|
|
@@ -1058,12 +1056,12 @@ describe('generateTypeDefinition', () => {
|
|
|
1058
1056
|
},
|
|
1059
1057
|
}
|
|
1060
1058
|
|
|
1061
|
-
const result = generateTypeDefinition(schema, '
|
|
1059
|
+
const result = generateTypeDefinition(schema, 'Callback')
|
|
1062
1060
|
|
|
1063
1061
|
expect(result).toContain('/**')
|
|
1064
|
-
expect(result).toContain('*
|
|
1062
|
+
expect(result).toContain('* Callback')
|
|
1065
1063
|
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#callback-object')
|
|
1066
|
-
expect(result).toContain('[key: string]:
|
|
1064
|
+
expect(result).toContain('[key: string]: PathItem')
|
|
1067
1065
|
})
|
|
1068
1066
|
|
|
1069
1067
|
it('generates type for product schema with required, optional, and array fields', () => {
|
|
@@ -1151,7 +1149,7 @@ describe('generateTypeDefinition', () => {
|
|
|
1151
1149
|
|
|
1152
1150
|
const result = generateTypeDefinition(schema, 'Parameters')
|
|
1153
1151
|
|
|
1154
|
-
expect(result).toBe('export type Parameters = (
|
|
1152
|
+
expect(result).toBe('export type Parameters = (Parameter | Reference)[];')
|
|
1155
1153
|
})
|
|
1156
1154
|
|
|
1157
1155
|
it('emits JSDoc for non-object schemas with a $comment URL', () => {
|
|
@@ -1165,7 +1163,7 @@ describe('generateTypeDefinition', () => {
|
|
|
1165
1163
|
|
|
1166
1164
|
expect(result).toContain('/**')
|
|
1167
1165
|
expect(result).toContain('* https://spec.openapis.org/oas/v3.1#contact-object')
|
|
1168
|
-
expect(result).toContain('export type Contacts =
|
|
1166
|
+
expect(result).toContain('export type Contacts = Server[];')
|
|
1169
1167
|
})
|
|
1170
1168
|
|
|
1171
1169
|
it('emits JSDoc for non-object schemas with a plain-text $comment', () => {
|
|
@@ -1179,7 +1177,7 @@ describe('generateTypeDefinition', () => {
|
|
|
1179
1177
|
|
|
1180
1178
|
expect(result).toContain('/**')
|
|
1181
1179
|
expect(result).toContain('A list of parameters applicable to the operation.')
|
|
1182
|
-
expect(result).toContain('export type Parameters =
|
|
1180
|
+
expect(result).toContain('export type Parameters = Parameter[];')
|
|
1183
1181
|
})
|
|
1184
1182
|
|
|
1185
1183
|
it('generates unknown for external $ref', () => {
|
|
@@ -1188,9 +1186,9 @@ describe('generateTypeDefinition', () => {
|
|
|
1188
1186
|
$ref: 'http://json-schema.org/draft-04/schema#/properties/maximum',
|
|
1189
1187
|
}
|
|
1190
1188
|
|
|
1191
|
-
const result = generateTypeDefinition(schema, '
|
|
1189
|
+
const result = generateTypeDefinition(schema, 'Maximum')
|
|
1192
1190
|
|
|
1193
|
-
expect(result).toBe('export type
|
|
1191
|
+
expect(result).toBe('export type Maximum = unknown;')
|
|
1194
1192
|
})
|
|
1195
1193
|
|
|
1196
1194
|
it('does not emit a trailing blank line in JSDoc when there is no @see link', () => {
|
|
@@ -1202,7 +1200,7 @@ describe('generateTypeDefinition', () => {
|
|
|
1202
1200
|
},
|
|
1203
1201
|
}
|
|
1204
1202
|
|
|
1205
|
-
const result = generateTypeDefinition(schema, '
|
|
1203
|
+
const result = generateTypeDefinition(schema, 'PlainComment')
|
|
1206
1204
|
|
|
1207
1205
|
expect(result).toMatch(/\* A plain-text description with no URL\.\n\*\//)
|
|
1208
1206
|
expect(result).not.toContain('* \n*/')
|
|
@@ -1314,4 +1312,25 @@ describe('generateTypeDefinition', () => {
|
|
|
1314
1312
|
expect(result).toContain('id: string;')
|
|
1315
1313
|
})
|
|
1316
1314
|
})
|
|
1315
|
+
|
|
1316
|
+
describe('typeSuffix', () => {
|
|
1317
|
+
const schema: JSONSchema = {
|
|
1318
|
+
type: 'object',
|
|
1319
|
+
properties: {
|
|
1320
|
+
contact: { $ref: '#/$defs/contact' },
|
|
1321
|
+
},
|
|
1322
|
+
required: ['contact'],
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
it('appends the suffix to ref-derived type names', () => {
|
|
1326
|
+
const result = generateTypeDefinition(schema, 'Document', { typeSuffix: 'Object' })
|
|
1327
|
+
expect(result).toContain('contact: ContactObject;')
|
|
1328
|
+
})
|
|
1329
|
+
|
|
1330
|
+
it('emits no suffix by default', () => {
|
|
1331
|
+
const result = generateTypeDefinition(schema, 'Document')
|
|
1332
|
+
expect(result).toContain('contact: Contact;')
|
|
1333
|
+
expect(result).not.toContain('ContactObject')
|
|
1334
|
+
})
|
|
1335
|
+
})
|
|
1317
1336
|
})
|
|
@@ -14,6 +14,11 @@ type ConditionalObjectResult = {
|
|
|
14
14
|
export type TypeOptions = {
|
|
15
15
|
/** When true, every property, array, and record in the generated types is emitted as readonly. */
|
|
16
16
|
readonly readonly?: boolean
|
|
17
|
+
/**
|
|
18
|
+
* Suffix appended to every generated type name derived from a `$ref`.
|
|
19
|
+
* Defaults to `''` (no suffix). Set to e.g. `'Object'` to emit `ContactObject`.
|
|
20
|
+
*/
|
|
21
|
+
readonly typeSuffix?: string
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
const getConditionalObjectSchema = (schema: JSONSchema): ConditionalObjectResult | null => {
|
|
@@ -164,16 +169,16 @@ const getUnbrandedType = (schema: JSONSchema, options: TypeOptions = {}): string
|
|
|
164
169
|
if (!schema.$ref.startsWith('#')) {
|
|
165
170
|
return 'unknown'
|
|
166
171
|
}
|
|
167
|
-
return refToName(schema.$ref)
|
|
172
|
+
return refToName(schema.$ref, options.typeSuffix)
|
|
168
173
|
}
|
|
169
174
|
|
|
170
175
|
// Handle $dynamicRef (used for recursive schemas)
|
|
171
176
|
if (schema.$dynamicRef) {
|
|
172
177
|
// For #meta, this refers to the Schema type itself (JSON Schema 2020-12 $dynamicAnchor pattern)
|
|
173
178
|
if (schema.$dynamicRef === '#meta') {
|
|
174
|
-
return
|
|
179
|
+
return `Schema${options.typeSuffix ?? ''}`
|
|
175
180
|
}
|
|
176
|
-
return refToName(schema.$dynamicRef)
|
|
181
|
+
return refToName(schema.$dynamicRef, options.typeSuffix)
|
|
177
182
|
}
|
|
178
183
|
|
|
179
184
|
// Handle const - literal type
|
|
@@ -235,7 +240,7 @@ const getUnbrandedType = (schema: JSONSchema, options: TypeOptions = {}): string
|
|
|
235
240
|
if (conditionalResult) {
|
|
236
241
|
const baseType = getTypeScriptType(conditionalResult.schema, options)
|
|
237
242
|
if (conditionalResult.thenRef) {
|
|
238
|
-
return `(${baseType}) & ${refToName(conditionalResult.thenRef)}`
|
|
243
|
+
return `(${baseType}) & ${refToName(conditionalResult.thenRef, options.typeSuffix)}`
|
|
239
244
|
}
|
|
240
245
|
return baseType
|
|
241
246
|
}
|
|
@@ -493,7 +498,7 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string, opt
|
|
|
493
498
|
if (isSchemaObject(schema) && Array.isArray(schema.allOf)) {
|
|
494
499
|
for (const entry of schema.allOf) {
|
|
495
500
|
if (isSchemaObject(entry) && entry.$ref) {
|
|
496
|
-
allOfIntersections.push(refToName(entry.$ref))
|
|
501
|
+
allOfIntersections.push(refToName(entry.$ref, options.typeSuffix))
|
|
497
502
|
}
|
|
498
503
|
}
|
|
499
504
|
}
|
|
@@ -501,7 +506,7 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string, opt
|
|
|
501
506
|
// JSON Schema 2019-09+ allows $ref as a sibling to other keywords.
|
|
502
507
|
// Treat it as an additional intersection type (e.g. for specification-extensions).
|
|
503
508
|
if (isSchemaObject(schema) && typeof schema.$ref === 'string' && schema.$ref.startsWith('#')) {
|
|
504
|
-
allOfIntersections.push(refToName(schema.$ref))
|
|
509
|
+
allOfIntersections.push(refToName(schema.$ref, options.typeSuffix))
|
|
505
510
|
}
|
|
506
511
|
|
|
507
512
|
let result = ''
|
|
@@ -512,7 +517,7 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string, opt
|
|
|
512
517
|
let typeBody = '{\n' + properties + '\n}'
|
|
513
518
|
|
|
514
519
|
if (conditionalThenRef) {
|
|
515
|
-
typeBody += ' & ' + refToName(conditionalThenRef)
|
|
520
|
+
typeBody += ' & ' + refToName(conditionalThenRef, options.typeSuffix)
|
|
516
521
|
}
|
|
517
522
|
|
|
518
523
|
for (const intersectionType of allOfIntersections) {
|
package/src/ref-to-name.test.ts
CHANGED
|
@@ -4,45 +4,54 @@ import { refToName } from './ref-to-name'
|
|
|
4
4
|
|
|
5
5
|
describe('ref-to-name', () => {
|
|
6
6
|
it('converts simple ref to PascalCase', () => {
|
|
7
|
-
expect(refToName('#/$defs/contact')).toBe('
|
|
7
|
+
expect(refToName('#/$defs/contact')).toBe('Contact')
|
|
8
8
|
})
|
|
9
9
|
|
|
10
10
|
it('converts kebab-case ref to PascalCase', () => {
|
|
11
|
-
expect(refToName('#/$defs/server-variable')).toBe('
|
|
11
|
+
expect(refToName('#/$defs/server-variable')).toBe('ServerVariable')
|
|
12
12
|
})
|
|
13
13
|
|
|
14
14
|
it('converts multi-word kebab-case ref to PascalCase', () => {
|
|
15
|
-
expect(refToName('#/$defs/external-documentation')).toBe('
|
|
15
|
+
expect(refToName('#/$defs/external-documentation')).toBe('ExternalDocumentation')
|
|
16
16
|
})
|
|
17
17
|
|
|
18
18
|
it('handles refs with multiple path segments', () => {
|
|
19
|
-
expect(refToName('#/components/schemas/user-profile')).toBe('
|
|
19
|
+
expect(refToName('#/components/schemas/user-profile')).toBe('UserProfile')
|
|
20
20
|
})
|
|
21
21
|
|
|
22
22
|
it('handles single word refs', () => {
|
|
23
|
-
expect(refToName('#/$defs/info')).toBe('
|
|
23
|
+
expect(refToName('#/$defs/info')).toBe('Info')
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
it('handles refs with numbers', () => {
|
|
27
|
-
expect(refToName('#/$defs/oauth2-flow')).toBe('
|
|
27
|
+
expect(refToName('#/$defs/oauth2-flow')).toBe('Oauth2Flow')
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
it('converts uppercase acronym keys to PascalCase via kebab normalization', () => {
|
|
31
|
-
expect(refToName('#/$defs/APIKey')).toBe('
|
|
31
|
+
expect(refToName('#/$defs/APIKey')).toBe('ApiKey')
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
it('derives type name from a URI ref', () => {
|
|
35
|
-
expect(refToName('http://asyncapi.com/definitions/3.1.0/channel.json')).toBe('
|
|
36
|
-
expect(refToName('http://asyncapi.com/definitions/3.1.0/info.json')).toBe('
|
|
35
|
+
expect(refToName('http://asyncapi.com/definitions/3.1.0/channel.json')).toBe('Channel')
|
|
36
|
+
expect(refToName('http://asyncapi.com/definitions/3.1.0/info.json')).toBe('Info')
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
it('derives type name from a URI ref with a fragment', () => {
|
|
40
40
|
expect(refToName('http://asyncapi.com/bindings/sns/0.1.0/channel.json#/definitions/queue')).toBe(
|
|
41
|
-
'
|
|
41
|
+
'BindingsSns010ChannelQueue',
|
|
42
42
|
)
|
|
43
43
|
})
|
|
44
44
|
|
|
45
45
|
it('handles draft-07 schema URI', () => {
|
|
46
|
-
expect(refToName('http://json-schema.org/draft-07/schema')).toBe('
|
|
46
|
+
expect(refToName('http://json-schema.org/draft-07/schema')).toBe('Draft07Schema')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('appends the suffix when one is provided', () => {
|
|
50
|
+
expect(refToName('#/$defs/contact', 'Object')).toBe('ContactObject')
|
|
51
|
+
expect(refToName('#/$defs/server-variable', 'Object')).toBe('ServerVariableObject')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('treats an empty suffix the same as omitting it', () => {
|
|
55
|
+
expect(refToName('#/$defs/contact', '')).toBe('Contact')
|
|
47
56
|
})
|
|
48
57
|
})
|
package/src/ref-to-name.ts
CHANGED
|
@@ -1,38 +1,42 @@
|
|
|
1
1
|
import { refToFilename } from './ref-to-filename'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Converts a kebab-case filename to PascalCase
|
|
4
|
+
* Converts a kebab-case filename to PascalCase, appending an optional suffix.
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```ts
|
|
8
|
-
* kebabToPascal('server-variable') // '
|
|
9
|
-
* kebabToPascal('channel') // '
|
|
8
|
+
* kebabToPascal('server-variable') // 'ServerVariable'
|
|
9
|
+
* kebabToPascal('channel') // 'Channel'
|
|
10
|
+
* kebabToPascal('channel', 'Object') // 'ChannelObject'
|
|
10
11
|
* ```
|
|
11
12
|
*/
|
|
12
|
-
const kebabToPascal = (kebab: string): string => {
|
|
13
|
+
const kebabToPascal = (kebab: string, suffix: string): string => {
|
|
13
14
|
const words = kebab.split('-')
|
|
14
15
|
let pascalCase = ''
|
|
15
16
|
for (const word of words) {
|
|
16
17
|
pascalCase += word.charAt(0).toUpperCase() + word.slice(1)
|
|
17
18
|
}
|
|
18
|
-
return pascalCase +
|
|
19
|
+
return pascalCase + suffix
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Converts a JSON Schema $ref to a type name.
|
|
23
|
-
* Derives the filename via `refToFilename` then converts to PascalCase
|
|
24
|
-
* an
|
|
24
|
+
* Derives the filename via `refToFilename` then converts to PascalCase,
|
|
25
|
+
* appending an optional suffix.
|
|
25
26
|
*
|
|
26
27
|
* Handles all ref forms: internal `#/$defs/...`, `#/definitions/...`, and URI refs.
|
|
27
28
|
*
|
|
28
29
|
* @param ref - The $ref string
|
|
29
|
-
* @
|
|
30
|
+
* @param suffix - Optional suffix appended to the PascalCase name. Defaults to
|
|
31
|
+
* `''` (no suffix). Pass e.g. `'Object'` to get `ContactObject`.
|
|
32
|
+
* @returns The type name in PascalCase with the suffix applied
|
|
30
33
|
*
|
|
31
34
|
* @example
|
|
32
35
|
* ```ts
|
|
33
|
-
* refToName('#/$defs/contact') // '
|
|
34
|
-
* refToName('#/$defs/server-variable') // '
|
|
35
|
-
* refToName('
|
|
36
|
+
* refToName('#/$defs/contact') // 'Contact'
|
|
37
|
+
* refToName('#/$defs/server-variable') // 'ServerVariable'
|
|
38
|
+
* refToName('#/$defs/contact', 'Object') // 'ContactObject'
|
|
39
|
+
* refToName('http://asyncapi.com/definitions/3.1.0/channel.json') // 'Channel'
|
|
36
40
|
* ```
|
|
37
41
|
*/
|
|
38
|
-
export const refToName = (ref: string): string => kebabToPascal(refToFilename(ref))
|
|
42
|
+
export const refToName = (ref: string, suffix = ''): string => kebabToPascal(refToFilename(ref), suffix)
|