@moostjs/swagger 0.6.31 → 0.6.32

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.cjs CHANGED
@@ -557,12 +557,7 @@ function createSchemaResolution(type) {
557
557
  }
558
558
  function schemaFromFunction(fn) {
559
559
  const ctor = fn;
560
- if (typeof ctor.toJsonSchema === "function") return {
561
- kind: "component",
562
- schema: asSwaggerSchema(ctor.toJsonSchema()),
563
- typeRef: ctor,
564
- suggestedName: ctor.name
565
- };
560
+ if (typeof ctor.toJsonSchema === "function") return componentFromToJsonSchema(ctor, ctor.name);
566
561
  if (fn.length === 0) try {
567
562
  const result = fn();
568
563
  if (result && result !== fn) return createSchemaResolution(result);
@@ -574,18 +569,58 @@ function schemaFromInstance(obj) {
574
569
  schema: cloneSchema(obj)
575
570
  };
576
571
  const ctor = obj.constructor;
577
- if (ctor && typeof ctor.toJsonSchema === "function") return {
578
- kind: "component",
579
- schema: asSwaggerSchema(ctor.toJsonSchema()),
580
- typeRef: ctor,
581
- suggestedName: ctor.name
582
- };
583
- if (typeof obj.toJsonSchema === "function") return {
572
+ if (ctor && typeof ctor.toJsonSchema === "function") return componentFromToJsonSchema(ctor, ctor.name);
573
+ if (typeof obj.toJsonSchema === "function") return componentFromToJsonSchema(obj, getTypeName(obj));
574
+ }
575
+ /**
576
+ * Resolves a `toJsonSchema()` host to a named component resolution, or
577
+ * `undefined` when an atscript annotated type's schema emit is disabled
578
+ * (see {@link callToJsonSchema}).
579
+ */ function componentFromToJsonSchema(host, suggestedName) {
580
+ const schema = callToJsonSchema(host);
581
+ return schema ? {
584
582
  kind: "component",
585
- schema: asSwaggerSchema(obj.toJsonSchema()),
586
- typeRef: obj,
587
- suggestedName: getTypeName(obj)
588
- };
583
+ schema,
584
+ typeRef: host,
585
+ suggestedName
586
+ } : void 0;
587
+ }
588
+ /**
589
+ * Structural (duck-typed) guard for atscript annotated types.
590
+ *
591
+ * Mirrors the `isAnnotatedType` contract from
592
+ * `atscript/packages/typescript/src/runtime/annotated-type.ts`: an annotated
593
+ * type carries a truthy `__is_atscript_annotated_type` marker, and codegen
594
+ * always renders a `static toJsonSchema()` alongside it.
595
+ *
596
+ * NOTE: this must stay a structural check — `@moostjs/swagger` cannot import
597
+ * any `@atscript/*` package because atscript depends on moost (the import
598
+ * would be circular).
599
+ */ function isAtscriptAnnotatedType(type) {
600
+ if (!type || typeof type !== "function" && typeof type !== "object") return false;
601
+ const candidate = type;
602
+ return !!candidate.__is_atscript_annotated_type && typeof candidate.toJsonSchema === "function";
603
+ }
604
+ let atscriptJsonSchemaHintLogged = false;
605
+ /**
606
+ * Calls `toJsonSchema()` guarding the atscript case: when the atscript project
607
+ * does not enable `emit.jsonSchema` (`jsonSchema: false`, the default of the
608
+ * `@atscript/typescript` plugin — see its `plugin.ts` options doc), the
609
+ * generated `toJsonSchema()` is a stub that throws at runtime. In that case a
610
+ * one-time hint is logged and `undefined` is returned so callers fall back to
611
+ * the generic (pre-atscript) resolution. Non-annotated types keep the previous
612
+ * behavior: errors propagate as before.
613
+ */ function callToJsonSchema(host) {
614
+ try {
615
+ return asSwaggerSchema(host.toJsonSchema());
616
+ } catch (error) {
617
+ if (!isAtscriptAnnotatedType(host)) throw error;
618
+ if (!atscriptJsonSchemaHintLogged) {
619
+ atscriptJsonSchemaHintLogged = true;
620
+ console.warn("[moost-swagger] toJsonSchema() of an atscript annotated type threw; falling back to a generic schema. Enable jsonSchema emit in the @atscript/typescript plugin options (jsonSchema: \"bundle\" or \"lazy\") to get real schemas in the OpenAPI spec.");
621
+ }
622
+ return;
623
+ }
589
624
  }
590
625
  function asSwaggerSchema(schema) {
591
626
  if (!schema || typeof schema !== "object") return {};
package/dist/index.mjs CHANGED
@@ -528,12 +528,7 @@ function createSchemaResolution(type) {
528
528
  }
529
529
  function schemaFromFunction(fn) {
530
530
  const ctor = fn;
531
- if (typeof ctor.toJsonSchema === "function") return {
532
- kind: "component",
533
- schema: asSwaggerSchema(ctor.toJsonSchema()),
534
- typeRef: ctor,
535
- suggestedName: ctor.name
536
- };
531
+ if (typeof ctor.toJsonSchema === "function") return componentFromToJsonSchema(ctor, ctor.name);
537
532
  if (fn.length === 0) try {
538
533
  const result = fn();
539
534
  if (result && result !== fn) return createSchemaResolution(result);
@@ -545,18 +540,58 @@ function schemaFromInstance(obj) {
545
540
  schema: cloneSchema(obj)
546
541
  };
547
542
  const ctor = obj.constructor;
548
- if (ctor && typeof ctor.toJsonSchema === "function") return {
549
- kind: "component",
550
- schema: asSwaggerSchema(ctor.toJsonSchema()),
551
- typeRef: ctor,
552
- suggestedName: ctor.name
553
- };
554
- if (typeof obj.toJsonSchema === "function") return {
543
+ if (ctor && typeof ctor.toJsonSchema === "function") return componentFromToJsonSchema(ctor, ctor.name);
544
+ if (typeof obj.toJsonSchema === "function") return componentFromToJsonSchema(obj, getTypeName(obj));
545
+ }
546
+ /**
547
+ * Resolves a `toJsonSchema()` host to a named component resolution, or
548
+ * `undefined` when an atscript annotated type's schema emit is disabled
549
+ * (see {@link callToJsonSchema}).
550
+ */ function componentFromToJsonSchema(host, suggestedName) {
551
+ const schema = callToJsonSchema(host);
552
+ return schema ? {
555
553
  kind: "component",
556
- schema: asSwaggerSchema(obj.toJsonSchema()),
557
- typeRef: obj,
558
- suggestedName: getTypeName(obj)
559
- };
554
+ schema,
555
+ typeRef: host,
556
+ suggestedName
557
+ } : void 0;
558
+ }
559
+ /**
560
+ * Structural (duck-typed) guard for atscript annotated types.
561
+ *
562
+ * Mirrors the `isAnnotatedType` contract from
563
+ * `atscript/packages/typescript/src/runtime/annotated-type.ts`: an annotated
564
+ * type carries a truthy `__is_atscript_annotated_type` marker, and codegen
565
+ * always renders a `static toJsonSchema()` alongside it.
566
+ *
567
+ * NOTE: this must stay a structural check — `@moostjs/swagger` cannot import
568
+ * any `@atscript/*` package because atscript depends on moost (the import
569
+ * would be circular).
570
+ */ function isAtscriptAnnotatedType(type) {
571
+ if (!type || typeof type !== "function" && typeof type !== "object") return false;
572
+ const candidate = type;
573
+ return !!candidate.__is_atscript_annotated_type && typeof candidate.toJsonSchema === "function";
574
+ }
575
+ let atscriptJsonSchemaHintLogged = false;
576
+ /**
577
+ * Calls `toJsonSchema()` guarding the atscript case: when the atscript project
578
+ * does not enable `emit.jsonSchema` (`jsonSchema: false`, the default of the
579
+ * `@atscript/typescript` plugin — see its `plugin.ts` options doc), the
580
+ * generated `toJsonSchema()` is a stub that throws at runtime. In that case a
581
+ * one-time hint is logged and `undefined` is returned so callers fall back to
582
+ * the generic (pre-atscript) resolution. Non-annotated types keep the previous
583
+ * behavior: errors propagate as before.
584
+ */ function callToJsonSchema(host) {
585
+ try {
586
+ return asSwaggerSchema(host.toJsonSchema());
587
+ } catch (error) {
588
+ if (!isAtscriptAnnotatedType(host)) throw error;
589
+ if (!atscriptJsonSchemaHintLogged) {
590
+ atscriptJsonSchemaHintLogged = true;
591
+ console.warn("[moost-swagger] toJsonSchema() of an atscript annotated type threw; falling back to a generic schema. Enable jsonSchema emit in the @atscript/typescript plugin options (jsonSchema: \"bundle\" or \"lazy\") to get real schemas in the OpenAPI spec.");
592
+ }
593
+ return;
594
+ }
560
595
  }
561
596
  function asSwaggerSchema(schema) {
562
597
  if (!schema || typeof schema !== "object") return {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moostjs/swagger",
3
- "version": "0.6.31",
3
+ "version": "0.6.32",
4
4
  "description": "@moostjs/swagger",
5
5
  "keywords": [
6
6
  "composables",
@@ -47,8 +47,8 @@
47
47
  "peerDependencies": {
48
48
  "@wooksjs/event-http": "^0.7.20",
49
49
  "@wooksjs/http-static": "^0.7.20",
50
- "@moostjs/event-http": "^0.6.31",
51
- "moost": "^0.6.31"
50
+ "moost": "^0.6.32",
51
+ "@moostjs/event-http": "^0.6.32"
52
52
  },
53
53
  "scripts": {
54
54
  "pub": "pnpm publish --access public",