@amritk/helpers 0.6.0 → 0.6.2

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.
@@ -431,6 +431,31 @@ var getUnbrandedType = (schema, options = {}) => {
431
431
  case "object":
432
432
  if (schema.properties) {
433
433
  const readonlyPrefix = options.readonly ? "readonly " : "";
434
+ const hasDescriptions = Object.values(schema.properties).some((p) => isSchemaObject(p) && (typeof p.description === "string" || typeof p.$comment === "string"));
435
+ if (hasDescriptions) {
436
+ let properties2 = "";
437
+ let first2 = true;
438
+ for (const key in schema.properties) {
439
+ const propSchema = schema.properties[key];
440
+ const isRequired = schema.required?.includes(key) ?? false;
441
+ const optional = isRequired ? "" : "?";
442
+ const propType = getTypeScriptType(propSchema, options);
443
+ const inlineDescription = isSchemaObject(propSchema) && typeof propSchema.description === "string" ? propSchema.description : isSchemaObject(propSchema) && typeof propSchema.$comment === "string" ? propSchema.$comment : undefined;
444
+ if (!first2)
445
+ properties2 += `
446
+ `;
447
+ first2 = false;
448
+ if (inlineDescription) {
449
+ properties2 += " /** " + inlineDescription + ` */
450
+ ` + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
451
+ } else {
452
+ properties2 += " " + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
453
+ }
454
+ }
455
+ return `{
456
+ ` + properties2 + `
457
+ }`;
458
+ }
434
459
  let properties = "";
435
460
  let first = true;
436
461
  for (const key in schema.properties) {
@@ -472,8 +497,9 @@ var generateTypeDefinition = (schema, typeName, options = {}) => {
472
497
  if (!isObjectLikeSchema(schema)) {
473
498
  const tsType = getTypeScriptType(schema, options);
474
499
  let result = "";
475
- if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === "string") {
476
- result += buildJsDocBlock(typeName, schema.$comment);
500
+ const topLevelComment = isSchemaObject(schema) && typeof schema.description === "string" && schema.description || isSchemaObject(schema) && typeof schema.$comment === "string" && schema.$comment || undefined;
501
+ if (topLevelComment) {
502
+ result += buildJsDocBlock(typeName, topLevelComment);
477
503
  }
478
504
  result += `export type ${typeName} = ${tsType};`;
479
505
  return result;
@@ -484,9 +510,10 @@ var generateTypeDefinition = (schema, typeName, options = {}) => {
484
510
  const conditionalThenRef = conditionalResult?.thenRef ?? null;
485
511
  let jsDocTitle;
486
512
  let jsDocDescription;
487
- if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === "string") {
513
+ const topLevelComment = isSchemaObject(schema) && typeof schema.description === "string" && schema.description || isSchemaObject(schema) && typeof schema.$comment === "string" && schema.$comment || undefined;
514
+ if (topLevelComment) {
488
515
  jsDocTitle = typeName;
489
- jsDocDescription = schema.$comment;
516
+ jsDocDescription = topLevelComment;
490
517
  }
491
518
  const hasProperties2 = normalizedSchema.properties && Object.keys(normalizedSchema.properties).length > 0;
492
519
  const hasAdditionalProperties2 = normalizedSchema.additionalProperties && typeof normalizedSchema.additionalProperties === "object";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -876,6 +876,39 @@ describe('generateTypeDefinition', () => {
876
876
  expect(result).toContain('} & TypeApikey & TypeHttp;')
877
877
  })
878
878
 
879
+ it('preserves property descriptions as JSDoc comments when allOf contains an inline object schema', () => {
880
+ const schema: JSONSchema = {
881
+ allOf: [
882
+ { $ref: '#/$defs/baseTargetConfig' },
883
+ {
884
+ type: 'object',
885
+ additionalProperties: false,
886
+ properties: {
887
+ packageName: {
888
+ type: 'string',
889
+ description: 'Import/package name for TypeScript and Node packages.',
890
+ },
891
+ packageManager: {
892
+ type: 'string',
893
+ description: 'TypeScript package manager preference for generated package metadata.',
894
+ },
895
+ publish: {
896
+ $ref: '#/$defs/npmPublishConfig',
897
+ description: 'npm publishing configuration.',
898
+ },
899
+ },
900
+ required: ['publish'],
901
+ },
902
+ ],
903
+ }
904
+
905
+ const result = generateTypeDefinition(schema, 'TypeScriptTargetConfig')
906
+
907
+ expect(result).toContain('/** Import/package name for TypeScript and Node packages. */')
908
+ expect(result).toContain('/** TypeScript package manager preference for generated package metadata. */')
909
+ expect(result).toContain('/** npm publishing configuration. */')
910
+ })
911
+
879
912
  it('generates record type for patternProperties-only schema without explicit type', () => {
880
913
  const schema: JSONSchema = {
881
914
  patternProperties: {
@@ -1085,7 +1118,12 @@ describe('generateTypeDefinition', () => {
1085
1118
  const result = generateTypeDefinition(schema, 'Product')
1086
1119
 
1087
1120
  expect(result).toBe(
1088
- 'export type Product = {\n' +
1121
+ '/**\n' +
1122
+ '* Product\n' +
1123
+ '*\n' +
1124
+ '* A product available for purchase in the catalog.\n' +
1125
+ '*/\n' +
1126
+ 'export type Product = {\n' +
1089
1127
  ' /** Unique product identifier (UUID). */\n' +
1090
1128
  ' id: string;\n' +
1091
1129
  ' /** Display name shown to customers. */\n' +
@@ -1109,7 +1147,14 @@ describe('generateTypeDefinition', () => {
1109
1147
 
1110
1148
  const result = generateTypeDefinition(schema, 'ThemeColor')
1111
1149
 
1112
- expect(result).toBe('export type ThemeColor = "red" | "green" | "blue" | "yellow" | "purple";')
1150
+ expect(result).toBe(
1151
+ '/**\n' +
1152
+ '* ThemeColor\n' +
1153
+ '*\n' +
1154
+ '* One of the supported theme colors.\n' +
1155
+ '*/\n' +
1156
+ 'export type ThemeColor = "red" | "green" | "blue" | "yellow" | "purple";',
1157
+ )
1113
1158
  })
1114
1159
 
1115
1160
  it('generates type for geo coordinate with min/max constraints on required number fields', () => {
@@ -1128,7 +1173,12 @@ describe('generateTypeDefinition', () => {
1128
1173
  const result = generateTypeDefinition(schema, 'GeoCoordinate')
1129
1174
 
1130
1175
  expect(result).toBe(
1131
- 'export type GeoCoordinate = {\n' +
1176
+ '/**\n' +
1177
+ '* GeoCoordinate\n' +
1178
+ '*\n' +
1179
+ '* A geographic coordinate pair.\n' +
1180
+ '*/\n' +
1181
+ 'export type GeoCoordinate = {\n' +
1132
1182
  ' /** Degrees latitude, from -90 to 90. */\n' +
1133
1183
  ' latitude: number;\n' +
1134
1184
  ' /** Degrees longitude, from -180 to 180. */\n' +
@@ -342,6 +342,45 @@ const getUnbrandedType = (schema: JSONSchema, options: TypeOptions = {}): string
342
342
  case 'object':
343
343
  if (schema.properties) {
344
344
  const readonlyPrefix = options.readonly ? 'readonly ' : ''
345
+ const hasDescriptions = Object.values(schema.properties).some(
346
+ (p) => isSchemaObject(p) && (typeof p.description === 'string' || typeof p.$comment === 'string'),
347
+ )
348
+
349
+ if (hasDescriptions) {
350
+ let properties = ''
351
+ let first = true
352
+ for (const key in schema.properties) {
353
+ // schema.properties[key] is safe: key comes from iterating schema.properties
354
+ const propSchema = schema.properties[key]!
355
+ const isRequired = schema.required?.includes(key) ?? false
356
+ const optional = isRequired ? '' : '?'
357
+ const propType = getTypeScriptType(propSchema, options)
358
+ const inlineDescription =
359
+ isSchemaObject(propSchema) && typeof propSchema.description === 'string'
360
+ ? propSchema.description
361
+ : isSchemaObject(propSchema) && typeof propSchema.$comment === 'string'
362
+ ? propSchema.$comment
363
+ : undefined
364
+ if (!first) properties += '\n'
365
+ first = false
366
+ if (inlineDescription) {
367
+ properties +=
368
+ ' /** ' +
369
+ inlineDescription +
370
+ ' */\n ' +
371
+ readonlyPrefix +
372
+ safeKey(key) +
373
+ optional +
374
+ ': ' +
375
+ propType +
376
+ ';'
377
+ } else {
378
+ properties += ' ' + readonlyPrefix + safeKey(key) + optional + ': ' + propType + ';'
379
+ }
380
+ }
381
+ return '{\n' + properties + '\n}'
382
+ }
383
+
345
384
  let properties = ''
346
385
  let first = true
347
386
  for (const key in schema.properties) {
@@ -396,8 +435,12 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string, opt
396
435
  const tsType = getTypeScriptType(schema, options)
397
436
  let result = ''
398
437
 
399
- if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === 'string') {
400
- result += buildJsDocBlock(typeName, schema.$comment)
438
+ const topLevelComment =
439
+ (isSchemaObject(schema) && typeof schema.description === 'string' && schema.description) ||
440
+ (isSchemaObject(schema) && typeof schema.$comment === 'string' && schema.$comment) ||
441
+ undefined
442
+ if (topLevelComment) {
443
+ result += buildJsDocBlock(typeName, topLevelComment)
401
444
  }
402
445
 
403
446
  result += `export type ${typeName} = ${tsType};`
@@ -411,9 +454,13 @@ export const generateTypeDefinition = (schema: JSONSchema, typeName: string, opt
411
454
  let jsDocTitle: string | undefined
412
455
  let jsDocDescription: string | undefined
413
456
 
414
- if (isSchemaObject(schema) && schema.$comment && typeof schema.$comment === 'string') {
457
+ const topLevelComment =
458
+ (isSchemaObject(schema) && typeof schema.description === 'string' && schema.description) ||
459
+ (isSchemaObject(schema) && typeof schema.$comment === 'string' && schema.$comment) ||
460
+ undefined
461
+ if (topLevelComment) {
415
462
  jsDocTitle = typeName
416
- jsDocDescription = schema.$comment
463
+ jsDocDescription = topLevelComment
417
464
  }
418
465
 
419
466
  const hasProperties = normalizedSchema.properties && Object.keys(normalizedSchema.properties).length > 0