@t0.labs/daxta 0.3.3 → 0.3.4

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.
@@ -134,6 +134,22 @@ function slug(value, index) {
134
134
  .slice(0, 72);
135
135
  return base || `example-${index + 1}`;
136
136
  }
137
+ function valueFingerprint(value) {
138
+ try {
139
+ return JSON.stringify(value) ?? 'undefined';
140
+ }
141
+ catch {
142
+ return Object.prototype.toString.call(value);
143
+ }
144
+ }
145
+ /** Stable shared key so request + response examples from the same hit stay paired. */
146
+ function exampleKeyForHit(hit, index) {
147
+ const base = slug(exampleLabel(hit), index);
148
+ return `${base}-${index}`;
149
+ }
150
+ function putExample(store, key, summary, value) {
151
+ store[key] = { summary, value };
152
+ }
137
153
  function isPrimaryHit(hit) {
138
154
  if (!hit.test)
139
155
  return false;
@@ -144,15 +160,6 @@ function isPrimaryHit(hit) {
144
160
  return true;
145
161
  return methods.includes(hit.method);
146
162
  }
147
- function addUniqueExample(store, name, value) {
148
- const serialized = JSON.stringify(value);
149
- if (Object.values(store).some((example) => JSON.stringify(example.value) === serialized))
150
- return;
151
- let key = slug(name, Object.keys(store).length);
152
- if (store[key])
153
- key = `${key}-${Object.keys(store).length}`;
154
- store[key] = { summary: name, value };
155
- }
156
163
  function pathParamsFromHit(hit) {
157
164
  if (hit.pathParams && Object.keys(hit.pathParams).length)
158
165
  return hit.pathParams;
@@ -350,7 +357,16 @@ function sortScenarios(scenarios) {
350
357
  function dedupeScenarios(scenarios) {
351
358
  const seen = new Set();
352
359
  return scenarios.filter((scenario) => {
353
- const key = JSON.stringify([scenario.group, scenario.name, scenario.status, scenario.reqBody, scenario.query, scenario.pathParams]);
360
+ // Include resBody so a new response keeps its matching request as its own scenario.
361
+ const key = JSON.stringify([
362
+ scenario.group,
363
+ scenario.name,
364
+ scenario.status,
365
+ scenario.reqBody,
366
+ scenario.resBody,
367
+ scenario.query,
368
+ scenario.pathParams,
369
+ ]);
354
370
  if (seen.has(key))
355
371
  return false;
356
372
  seen.add(key);
@@ -440,14 +456,16 @@ function buildSpec(hits) {
440
456
  const parameters = buildParameters(pathTemplate, opHits);
441
457
  if (parameters.length)
442
458
  operation.parameters = parameters;
443
- for (const hit of opHits) {
459
+ for (let index = 0; index < opHits.length; index += 1) {
460
+ const hit = opHits[index];
444
461
  const label = exampleLabel(hit);
462
+ const exampleKey = exampleKeyForHit(hit, index);
445
463
  if (hit.reqBody !== undefined) {
446
464
  operation.requestBody ??= { required: method === 'post' || method === 'put', content: { 'application/json': { schema: inferSchema(hit.reqBody), examples: {} } } };
447
465
  const media = operation.requestBody.content['application/json'];
448
466
  media.schema = mergeSchema(media.schema, inferSchema(hit.reqBody));
449
467
  media.examples ??= {};
450
- addUniqueExample(media.examples, label, hit.reqBody);
468
+ putExample(media.examples, exampleKey, label, hit.reqBody);
451
469
  }
452
470
  const status = String(hit.status);
453
471
  operation.responses[status] ??= {
@@ -458,7 +476,7 @@ function buildSpec(hits) {
458
476
  const responseMedia = operation.responses[status].content['application/json'];
459
477
  responseMedia.schema = mergeSchema(responseMedia.schema, inferSchema(hit.resBody));
460
478
  responseMedia.examples ??= {};
461
- addUniqueExample(responseMedia.examples, label, hit.resBody);
479
+ putExample(responseMedia.examples, exampleKey, label, hit.resBody);
462
480
  }
463
481
  }
464
482
  operation['x-scenarios'] = sortScenarios(dedupeScenarios(opHits.map(toScenario)));
@@ -517,13 +535,16 @@ function scenarioSignature(scenario) {
517
535
  pathParamKeys: sortedKeys(scenario.pathParams),
518
536
  reqSchema: signatureSchema(scenario.reqBody),
519
537
  resSchema: signatureSchema(scenario.resBody),
538
+ // Value fingerprints so body content changes invalidate the cached operation.
539
+ reqValue: valueFingerprint(scenario.reqBody),
540
+ resValue: valueFingerprint(scenario.resBody),
520
541
  });
521
542
  }
522
- function exampleSummaries(operation) {
523
- const summaries = [];
543
+ function exampleFingerprints(operation) {
544
+ const fingerprints = [];
524
545
  const collect = (examples) => {
525
- for (const example of Object.values(examples ?? {})) {
526
- summaries.push(String(example?.summary ?? ''));
546
+ for (const [key, example] of Object.entries(examples ?? {})) {
547
+ fingerprints.push(`${key}:${example?.summary ?? ''}:${valueFingerprint(example?.value)}`);
527
548
  }
528
549
  };
529
550
  const requestBody = operation.requestBody;
@@ -532,7 +553,7 @@ function exampleSummaries(operation) {
532
553
  for (const response of Object.values(responses)) {
533
554
  collect(response?.content?.['application/json']?.examples);
534
555
  }
535
- return summaries.sort((left, right) => left.localeCompare(right));
556
+ return fingerprints.sort((left, right) => left.localeCompare(right));
536
557
  }
537
558
  function operationSignature(operation) {
538
559
  const scenarios = (operation['x-scenarios'] ?? []).map(scenarioSignature).sort((left, right) => left.localeCompare(right));
@@ -548,7 +569,7 @@ function operationSignature(operation) {
548
569
  security: operation.security,
549
570
  docs: operation['x-docs'],
550
571
  responseStatuses: Object.keys(operation.responses ?? {}).sort(),
551
- examples: exampleSummaries(operation),
572
+ examples: exampleFingerprints(operation),
552
573
  scenarios,
553
574
  });
554
575
  }
@@ -565,6 +586,8 @@ function hitSignature(hit) {
565
586
  pathParamKeys: sortedKeys(hit.pathParams),
566
587
  reqSchema: signatureSchema(hit.reqBody),
567
588
  resSchema: signatureSchema(hit.resBody),
589
+ reqValue: valueFingerprint(hit.reqBody),
590
+ resValue: valueFingerprint(hit.resBody),
568
591
  });
569
592
  }
570
593
  function hitsStructureSame(existing, fresh) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t0.labs/daxta",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "DAxTA \u2014 docs from Nest integration traffic + DTO field CLI / Try-it",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",