ts-class-to-openapi 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -4,10 +4,10 @@ import path from 'path';
4
4
  const TS_CONFIG_DEFAULT_PATH = path.resolve(process.cwd(), 'tsconfig.json');
5
5
  const jsPrimitives = {
6
6
  String: { type: 'String', value: 'string' },
7
- Number: { type: 'Number', value: 'number' },
7
+ Number: { type: 'Number', value: 'number', format: 'float' },
8
8
  Boolean: { type: 'Boolean', value: 'boolean' },
9
9
  Symbol: { type: 'Symbol', value: 'symbol' },
10
- BigInt: { type: 'BigInt', value: 'integer' },
10
+ BigInt: { type: 'BigInt', value: 'integer', format: 'int64' },
11
11
  null: { type: 'null', value: 'null' },
12
12
  Object: { type: 'Object', value: 'object' },
13
13
  Array: { type: 'Array', value: 'array' },
@@ -513,9 +513,9 @@ class SchemaTransformer {
513
513
  }
514
514
  // Apply decorators if present
515
515
  this.applyDecorators(property.decorators, schema, property.name);
516
- // If no decorators are present, apply sensible defaults based on TypeScript types
516
+ // If no decorators are present, apply type-based format specifications
517
517
  if (property.decorators.length === 0) {
518
- this.applySensibleDefaults(property, schema);
518
+ this.applyTypeBasedFormats(property, schema);
519
519
  }
520
520
  // Determine if property should be required based on decorators and optional status
521
521
  this.determineRequiredStatus(property, schema);
@@ -727,56 +727,35 @@ class SchemaTransformer {
727
727
  * @param schema - The schema object to modify
728
728
  * @private
729
729
  */
730
- applySensibleDefaults(property, schema) {
730
+ /**
731
+ * Applies OpenAPI format specifications based on TypeScript types.
732
+ * This method is called when no decorators are present to set appropriate
733
+ * format values for primitive types according to OpenAPI specification.
734
+ *
735
+ * @param property - The property information containing type details
736
+ * @param schema - The schema object to modify
737
+ * @private
738
+ */
739
+ applyTypeBasedFormats(property, schema) {
731
740
  const propertyName = property.name;
732
- property.type.toLowerCase();
733
- // Add examples based on property names and types
741
+ const propertyType = property.type.toLowerCase();
734
742
  const propertySchema = schema.properties[propertyName];
735
- // Add common format hints based on property names
736
- if (propertyName.includes('email') && propertySchema.type === 'string') {
737
- propertySchema.format = 'email';
738
- }
739
- else if (propertyName.includes('password') &&
740
- propertySchema.type === 'string') {
741
- propertySchema.format = 'password';
742
- propertySchema.minLength = 8;
743
- }
744
- else if (propertyName.includes('url') &&
745
- propertySchema.type === 'string') {
746
- propertySchema.format = 'uri';
747
- }
748
- else if (propertyName.includes('phone') &&
749
- propertySchema.type === 'string') {
750
- propertySchema.pattern = '^[+]?[1-9]\\d{1,14}$';
751
- }
752
- // Add reasonable constraints based on common property names
753
- if (propertySchema.type === 'string') {
754
- if (propertyName === 'name' ||
755
- propertyName === 'firstName' ||
756
- propertyName === 'lastName') {
757
- propertySchema.minLength = 1;
758
- propertySchema.maxLength = 100;
759
- }
760
- else if (propertyName === 'description' || propertyName === 'bio') {
761
- propertySchema.maxLength = 500;
762
- }
763
- else if (propertyName === 'title') {
764
- propertySchema.minLength = 1;
765
- propertySchema.maxLength = 200;
766
- }
767
- }
768
- if (propertySchema.type === 'integer' || propertySchema.type === 'number') {
769
- if (propertyName === 'age') {
770
- propertySchema.minimum = 0;
771
- propertySchema.maximum = 150;
772
- }
773
- else if (propertyName === 'id') {
774
- propertySchema.minimum = 1;
775
- }
776
- else if (propertyName.includes('count') ||
777
- propertyName.includes('quantity')) {
778
- propertySchema.minimum = 0;
779
- }
743
+ switch (propertyType) {
744
+ case constants.jsPrimitives.Number.value:
745
+ propertySchema.format = constants.jsPrimitives.Number.format;
746
+ break;
747
+ case constants.jsPrimitives.BigInt.value:
748
+ propertySchema.format = constants.jsPrimitives.BigInt.format;
749
+ break;
750
+ case constants.jsPrimitives.Date.value:
751
+ propertySchema.format = constants.jsPrimitives.Date.format;
752
+ break;
753
+ case constants.jsPrimitives.Buffer.value:
754
+ case constants.jsPrimitives.Uint8Array.value:
755
+ case constants.jsPrimitives.File.value:
756
+ case constants.jsPrimitives.UploadFile.value:
757
+ propertySchema.format = constants.jsPrimitives.UploadFile.format;
758
+ break;
780
759
  }
781
760
  }
782
761
  /**
package/dist/index.js CHANGED
@@ -6,10 +6,10 @@ var path = require('path');
6
6
  const TS_CONFIG_DEFAULT_PATH = path.resolve(process.cwd(), 'tsconfig.json');
7
7
  const jsPrimitives = {
8
8
  String: { type: 'String', value: 'string' },
9
- Number: { type: 'Number', value: 'number' },
9
+ Number: { type: 'Number', value: 'number', format: 'float' },
10
10
  Boolean: { type: 'Boolean', value: 'boolean' },
11
11
  Symbol: { type: 'Symbol', value: 'symbol' },
12
- BigInt: { type: 'BigInt', value: 'integer' },
12
+ BigInt: { type: 'BigInt', value: 'integer', format: 'int64' },
13
13
  null: { type: 'null', value: 'null' },
14
14
  Object: { type: 'Object', value: 'object' },
15
15
  Array: { type: 'Array', value: 'array' },
@@ -515,9 +515,9 @@ class SchemaTransformer {
515
515
  }
516
516
  // Apply decorators if present
517
517
  this.applyDecorators(property.decorators, schema, property.name);
518
- // If no decorators are present, apply sensible defaults based on TypeScript types
518
+ // If no decorators are present, apply type-based format specifications
519
519
  if (property.decorators.length === 0) {
520
- this.applySensibleDefaults(property, schema);
520
+ this.applyTypeBasedFormats(property, schema);
521
521
  }
522
522
  // Determine if property should be required based on decorators and optional status
523
523
  this.determineRequiredStatus(property, schema);
@@ -729,56 +729,35 @@ class SchemaTransformer {
729
729
  * @param schema - The schema object to modify
730
730
  * @private
731
731
  */
732
- applySensibleDefaults(property, schema) {
732
+ /**
733
+ * Applies OpenAPI format specifications based on TypeScript types.
734
+ * This method is called when no decorators are present to set appropriate
735
+ * format values for primitive types according to OpenAPI specification.
736
+ *
737
+ * @param property - The property information containing type details
738
+ * @param schema - The schema object to modify
739
+ * @private
740
+ */
741
+ applyTypeBasedFormats(property, schema) {
733
742
  const propertyName = property.name;
734
- property.type.toLowerCase();
735
- // Add examples based on property names and types
743
+ const propertyType = property.type.toLowerCase();
736
744
  const propertySchema = schema.properties[propertyName];
737
- // Add common format hints based on property names
738
- if (propertyName.includes('email') && propertySchema.type === 'string') {
739
- propertySchema.format = 'email';
740
- }
741
- else if (propertyName.includes('password') &&
742
- propertySchema.type === 'string') {
743
- propertySchema.format = 'password';
744
- propertySchema.minLength = 8;
745
- }
746
- else if (propertyName.includes('url') &&
747
- propertySchema.type === 'string') {
748
- propertySchema.format = 'uri';
749
- }
750
- else if (propertyName.includes('phone') &&
751
- propertySchema.type === 'string') {
752
- propertySchema.pattern = '^[+]?[1-9]\\d{1,14}$';
753
- }
754
- // Add reasonable constraints based on common property names
755
- if (propertySchema.type === 'string') {
756
- if (propertyName === 'name' ||
757
- propertyName === 'firstName' ||
758
- propertyName === 'lastName') {
759
- propertySchema.minLength = 1;
760
- propertySchema.maxLength = 100;
761
- }
762
- else if (propertyName === 'description' || propertyName === 'bio') {
763
- propertySchema.maxLength = 500;
764
- }
765
- else if (propertyName === 'title') {
766
- propertySchema.minLength = 1;
767
- propertySchema.maxLength = 200;
768
- }
769
- }
770
- if (propertySchema.type === 'integer' || propertySchema.type === 'number') {
771
- if (propertyName === 'age') {
772
- propertySchema.minimum = 0;
773
- propertySchema.maximum = 150;
774
- }
775
- else if (propertyName === 'id') {
776
- propertySchema.minimum = 1;
777
- }
778
- else if (propertyName.includes('count') ||
779
- propertyName.includes('quantity')) {
780
- propertySchema.minimum = 0;
781
- }
745
+ switch (propertyType) {
746
+ case constants.jsPrimitives.Number.value:
747
+ propertySchema.format = constants.jsPrimitives.Number.format;
748
+ break;
749
+ case constants.jsPrimitives.BigInt.value:
750
+ propertySchema.format = constants.jsPrimitives.BigInt.format;
751
+ break;
752
+ case constants.jsPrimitives.Date.value:
753
+ propertySchema.format = constants.jsPrimitives.Date.format;
754
+ break;
755
+ case constants.jsPrimitives.Buffer.value:
756
+ case constants.jsPrimitives.Uint8Array.value:
757
+ case constants.jsPrimitives.File.value:
758
+ case constants.jsPrimitives.UploadFile.value:
759
+ propertySchema.format = constants.jsPrimitives.UploadFile.format;
760
+ break;
782
761
  }
783
762
  }
784
763
  /**
package/dist/run.js CHANGED
@@ -6,10 +6,10 @@ var path = require('path');
6
6
  const TS_CONFIG_DEFAULT_PATH = path.resolve(process.cwd(), 'tsconfig.json');
7
7
  const jsPrimitives = {
8
8
  String: { type: 'String', value: 'string' },
9
- Number: { type: 'Number', value: 'number' },
9
+ Number: { type: 'Number', value: 'number', format: 'float' },
10
10
  Boolean: { type: 'Boolean', value: 'boolean' },
11
11
  Symbol: { type: 'Symbol', value: 'symbol' },
12
- BigInt: { type: 'BigInt', value: 'integer' },
12
+ BigInt: { type: 'BigInt', value: 'integer', format: 'int64' },
13
13
  null: { type: 'null', value: 'null' },
14
14
  Object: { type: 'Object', value: 'object' },
15
15
  Array: { type: 'Array', value: 'array' },
@@ -515,9 +515,9 @@ class SchemaTransformer {
515
515
  }
516
516
  // Apply decorators if present
517
517
  this.applyDecorators(property.decorators, schema, property.name);
518
- // If no decorators are present, apply sensible defaults based on TypeScript types
518
+ // If no decorators are present, apply type-based format specifications
519
519
  if (property.decorators.length === 0) {
520
- this.applySensibleDefaults(property, schema);
520
+ this.applyTypeBasedFormats(property, schema);
521
521
  }
522
522
  // Determine if property should be required based on decorators and optional status
523
523
  this.determineRequiredStatus(property, schema);
@@ -729,56 +729,35 @@ class SchemaTransformer {
729
729
  * @param schema - The schema object to modify
730
730
  * @private
731
731
  */
732
- applySensibleDefaults(property, schema) {
732
+ /**
733
+ * Applies OpenAPI format specifications based on TypeScript types.
734
+ * This method is called when no decorators are present to set appropriate
735
+ * format values for primitive types according to OpenAPI specification.
736
+ *
737
+ * @param property - The property information containing type details
738
+ * @param schema - The schema object to modify
739
+ * @private
740
+ */
741
+ applyTypeBasedFormats(property, schema) {
733
742
  const propertyName = property.name;
734
- property.type.toLowerCase();
735
- // Add examples based on property names and types
743
+ const propertyType = property.type.toLowerCase();
736
744
  const propertySchema = schema.properties[propertyName];
737
- // Add common format hints based on property names
738
- if (propertyName.includes('email') && propertySchema.type === 'string') {
739
- propertySchema.format = 'email';
740
- }
741
- else if (propertyName.includes('password') &&
742
- propertySchema.type === 'string') {
743
- propertySchema.format = 'password';
744
- propertySchema.minLength = 8;
745
- }
746
- else if (propertyName.includes('url') &&
747
- propertySchema.type === 'string') {
748
- propertySchema.format = 'uri';
749
- }
750
- else if (propertyName.includes('phone') &&
751
- propertySchema.type === 'string') {
752
- propertySchema.pattern = '^[+]?[1-9]\\d{1,14}$';
753
- }
754
- // Add reasonable constraints based on common property names
755
- if (propertySchema.type === 'string') {
756
- if (propertyName === 'name' ||
757
- propertyName === 'firstName' ||
758
- propertyName === 'lastName') {
759
- propertySchema.minLength = 1;
760
- propertySchema.maxLength = 100;
761
- }
762
- else if (propertyName === 'description' || propertyName === 'bio') {
763
- propertySchema.maxLength = 500;
764
- }
765
- else if (propertyName === 'title') {
766
- propertySchema.minLength = 1;
767
- propertySchema.maxLength = 200;
768
- }
769
- }
770
- if (propertySchema.type === 'integer' || propertySchema.type === 'number') {
771
- if (propertyName === 'age') {
772
- propertySchema.minimum = 0;
773
- propertySchema.maximum = 150;
774
- }
775
- else if (propertyName === 'id') {
776
- propertySchema.minimum = 1;
777
- }
778
- else if (propertyName.includes('count') ||
779
- propertyName.includes('quantity')) {
780
- propertySchema.minimum = 0;
781
- }
745
+ switch (propertyType) {
746
+ case constants.jsPrimitives.Number.value:
747
+ propertySchema.format = constants.jsPrimitives.Number.format;
748
+ break;
749
+ case constants.jsPrimitives.BigInt.value:
750
+ propertySchema.format = constants.jsPrimitives.BigInt.format;
751
+ break;
752
+ case constants.jsPrimitives.Date.value:
753
+ propertySchema.format = constants.jsPrimitives.Date.format;
754
+ break;
755
+ case constants.jsPrimitives.Buffer.value:
756
+ case constants.jsPrimitives.Uint8Array.value:
757
+ case constants.jsPrimitives.File.value:
758
+ case constants.jsPrimitives.UploadFile.value:
759
+ propertySchema.format = constants.jsPrimitives.UploadFile.format;
760
+ break;
782
761
  }
783
762
  }
784
763
  /**
@@ -282,7 +282,16 @@ declare class SchemaTransformer {
282
282
  * @param schema - The schema object to modify
283
283
  * @private
284
284
  */
285
- private applySensibleDefaults;
285
+ /**
286
+ * Applies OpenAPI format specifications based on TypeScript types.
287
+ * This method is called when no decorators are present to set appropriate
288
+ * format values for primitive types according to OpenAPI specification.
289
+ *
290
+ * @param property - The property information containing type details
291
+ * @param schema - The schema object to modify
292
+ * @private
293
+ */
294
+ private applyTypeBasedFormats;
286
295
  /**
287
296
  * Determines if a property should be required based on decorators and optional status.
288
297
  *
@@ -15,6 +15,7 @@ declare const constants: {
15
15
  Number: {
16
16
  type: string;
17
17
  value: string;
18
+ format: string;
18
19
  };
19
20
  Boolean: {
20
21
  type: string;
@@ -27,6 +28,7 @@ declare const constants: {
27
28
  BigInt: {
28
29
  type: string;
29
30
  value: string;
31
+ format: string;
30
32
  };
31
33
  null: {
32
34
  type: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-class-to-openapi",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Transform TypeScript classes into OpenAPI 3.1.0 schema objects, which support class-validator decorators",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",