@moostjs/swagger 0.6.9 → 0.6.10
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 +90 -115
- package/dist/index.mjs +50 -81
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -6,12 +7,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
9
|
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
return to;
|
|
17
22
|
};
|
|
@@ -21,12 +26,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
26
|
}) : target, mod));
|
|
22
27
|
|
|
23
28
|
//#endregion
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
let moost = require("moost");
|
|
30
|
+
let _moostjs_event_http = require("@moostjs/event-http");
|
|
31
|
+
let _wooksjs_event_http = require("@wooksjs/event-http");
|
|
32
|
+
let _wooksjs_http_static = require("@wooksjs/http-static");
|
|
33
|
+
let path = require("path");
|
|
34
|
+
path = __toESM(path);
|
|
35
|
+
let swagger_ui_dist = require("swagger-ui-dist");
|
|
30
36
|
|
|
31
37
|
//#region packages/swagger/src/swagger.mate.ts
|
|
32
38
|
/** Returns the shared `Mate` instance extended with Swagger/OpenAPI metadata fields. */ function getSwaggerMate() {
|
|
@@ -278,8 +284,7 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
278
284
|
}
|
|
279
285
|
const defaultCode = getDefaultStatusCode(handlerMethod);
|
|
280
286
|
if (!responses?.[defaultCode] && hmeta?.returnType) {
|
|
281
|
-
const
|
|
282
|
-
const schema = toSchemaOrRef(ensured);
|
|
287
|
+
const schema = toSchemaOrRef(ensureSchema(hmeta.returnType));
|
|
283
288
|
if (schema) {
|
|
284
289
|
responses = responses || {};
|
|
285
290
|
responses[defaultCode] = {
|
|
@@ -356,8 +361,7 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
356
361
|
for (const paramName of handler.registeredAs[0].args) {
|
|
357
362
|
const paramIndex = handler.meta.params.findIndex((param) => param.paramSource === "ROUTE" && param.paramName === paramName);
|
|
358
363
|
const paramMeta = handler.meta.params[paramIndex];
|
|
359
|
-
const
|
|
360
|
-
const schema = toSchemaOrRef(ensured) || { type: "string" };
|
|
364
|
+
const schema = toSchemaOrRef(ensureSchema(paramMeta?.type)) || { type: "string" };
|
|
361
365
|
addParam({
|
|
362
366
|
name: paramName,
|
|
363
367
|
in: "path",
|
|
@@ -384,8 +388,7 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
384
388
|
if (schema?.type === "object" && schema.properties) {
|
|
385
389
|
const requiredProps = new Set(schema.required || []);
|
|
386
390
|
for (const [key, value] of Object.entries(schema.properties)) {
|
|
387
|
-
const
|
|
388
|
-
const normalizedProperty = normalizeQueryParamSchema(propertySchema);
|
|
391
|
+
const normalizedProperty = normalizeQueryParamSchema(cloneSchema(value));
|
|
389
392
|
if (normalizedProperty) endpointSpec.parameters.push({
|
|
390
393
|
name: key,
|
|
391
394
|
in: "query",
|
|
@@ -395,8 +398,8 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
395
398
|
});
|
|
396
399
|
}
|
|
397
400
|
} else if (ensured) {
|
|
398
|
-
const schema
|
|
399
|
-
const normalized = schema
|
|
401
|
+
const schema = toSchemaOrRef(ensured);
|
|
402
|
+
const normalized = schema ? normalizeQueryParamSchema(schema) : void 0;
|
|
400
403
|
endpointSpec.parameters.push({
|
|
401
404
|
name: paramMeta.paramName || "",
|
|
402
405
|
in: "query",
|
|
@@ -433,13 +436,12 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
433
436
|
for (const controller of metadata) for (const handler of controller.handlers) {
|
|
434
437
|
const hh = handler.handler;
|
|
435
438
|
if (hh.type !== "HTTP" || handler.registeredAs.length === 0) continue;
|
|
436
|
-
const path
|
|
439
|
+
const path = handler.registeredAs[0].path;
|
|
437
440
|
const method = hh.method?.toLowerCase() || "get";
|
|
438
|
-
const opId = swaggerSpec.paths[path
|
|
441
|
+
const opId = swaggerSpec.paths[path]?.[method]?.operationId;
|
|
439
442
|
if (opId) {
|
|
440
443
|
if (!handlerOpIds.has(controller.type)) handlerOpIds.set(controller.type, /* @__PURE__ */ new Map());
|
|
441
|
-
|
|
442
|
-
methodMap?.set(handler.method, opId);
|
|
444
|
+
handlerOpIds.get(controller.type)?.set(handler.method, opId);
|
|
443
445
|
}
|
|
444
446
|
}
|
|
445
447
|
for (const { endpointSpec, httpMethod, config } of deferredLinks) {
|
|
@@ -479,16 +481,14 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
479
481
|
return swaggerSpec;
|
|
480
482
|
}
|
|
481
483
|
function resolveSwaggerSchemaFromConfig(type) {
|
|
482
|
-
if (type === void 0) return
|
|
483
|
-
|
|
484
|
-
return toSchemaOrRef(ensured);
|
|
484
|
+
if (type === void 0) return;
|
|
485
|
+
return toSchemaOrRef(ensureSchema(type));
|
|
485
486
|
}
|
|
486
487
|
function resolveResponseHeaders(headers) {
|
|
487
488
|
const resolved = {};
|
|
488
489
|
let hasAny = false;
|
|
489
490
|
for (const [name, header] of Object.entries(headers)) {
|
|
490
|
-
const
|
|
491
|
-
const entry = { schema };
|
|
491
|
+
const entry = { schema: resolveSwaggerSchemaFromConfig(header.type) || { type: "string" } };
|
|
492
492
|
if (header.description) entry.description = header.description;
|
|
493
493
|
if (header.required !== void 0) entry.required = header.required;
|
|
494
494
|
if (header.example !== void 0) entry.example = header.example;
|
|
@@ -498,13 +498,12 @@ function resolveResponseHeaders(headers) {
|
|
|
498
498
|
return hasAny ? resolved : void 0;
|
|
499
499
|
}
|
|
500
500
|
function toSchemaOrRef(result) {
|
|
501
|
-
if (!result) return
|
|
501
|
+
if (!result) return;
|
|
502
502
|
if (result.ref) return { $ref: result.ref };
|
|
503
503
|
return cloneSchema(result.schema);
|
|
504
504
|
}
|
|
505
505
|
function inferBodyContentType(schema, resolved) {
|
|
506
|
-
const
|
|
507
|
-
const schemaType = target?.type ?? schema.type;
|
|
506
|
+
const schemaType = (resolved ?? resolveSchemaFromRef(schema))?.type ?? schema.type;
|
|
508
507
|
if (typeof schemaType === "string" && [
|
|
509
508
|
"string",
|
|
510
509
|
"number",
|
|
@@ -521,7 +520,7 @@ const SIMPLE_QUERY_TYPES = new Set([
|
|
|
521
520
|
]);
|
|
522
521
|
function normalizeQueryParamSchema(schema) {
|
|
523
522
|
const target = resolveSchemaFromRef(schema) || schema;
|
|
524
|
-
if (!target) return
|
|
523
|
+
if (!target) return;
|
|
525
524
|
if (target.type === "array") return isArrayOfSimpleItems(target.items) ? schema : void 0;
|
|
526
525
|
return isSimpleSchema(schema) ? schema : void 0;
|
|
527
526
|
}
|
|
@@ -553,9 +552,9 @@ function resetSchemaRegistry() {
|
|
|
553
552
|
for (const key of Object.keys(globalSchemas)) delete globalSchemas[key];
|
|
554
553
|
}
|
|
555
554
|
function ensureSchema(type) {
|
|
556
|
-
if (type === void 0 || type === null) return
|
|
555
|
+
if (type === void 0 || type === null) return;
|
|
557
556
|
const resolution = createSchemaResolution(type);
|
|
558
|
-
if (!resolution) return
|
|
557
|
+
if (!resolution) return;
|
|
559
558
|
if (resolution.kind === "inline") return {
|
|
560
559
|
schema: cloneSchema(resolution.schema),
|
|
561
560
|
isComponent: false
|
|
@@ -570,23 +569,19 @@ function ensureSchema(type) {
|
|
|
570
569
|
};
|
|
571
570
|
}
|
|
572
571
|
function createSchemaResolution(type) {
|
|
573
|
-
if (type === void 0 || type === null) return
|
|
572
|
+
if (type === void 0 || type === null) return;
|
|
574
573
|
if (isSwaggerSchema(type)) return {
|
|
575
574
|
kind: "inline",
|
|
576
575
|
schema: cloneSchema(type)
|
|
577
576
|
};
|
|
578
577
|
if (Array.isArray(type)) {
|
|
579
|
-
if (type.length === 1) {
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
items: itemsSchema
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
}
|
|
578
|
+
if (type.length === 1) return {
|
|
579
|
+
kind: "inline",
|
|
580
|
+
schema: {
|
|
581
|
+
type: "array",
|
|
582
|
+
items: toSchemaOrRef(ensureSchema(type[0]))
|
|
583
|
+
}
|
|
584
|
+
};
|
|
590
585
|
return {
|
|
591
586
|
kind: "inline",
|
|
592
587
|
schema: { type: "array" }
|
|
@@ -608,24 +603,19 @@ function createSchemaResolution(type) {
|
|
|
608
603
|
const resolution = schemaFromInstance(type);
|
|
609
604
|
if (resolution) return resolution;
|
|
610
605
|
}
|
|
611
|
-
return void 0;
|
|
612
606
|
}
|
|
613
607
|
function schemaFromFunction(fn) {
|
|
614
608
|
const ctor = fn;
|
|
615
|
-
if (typeof ctor.toJsonSchema === "function") {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
suggestedName: ctor.name
|
|
622
|
-
};
|
|
623
|
-
}
|
|
609
|
+
if (typeof ctor.toJsonSchema === "function") return {
|
|
610
|
+
kind: "component",
|
|
611
|
+
schema: asSwaggerSchema(ctor.toJsonSchema()),
|
|
612
|
+
typeRef: ctor,
|
|
613
|
+
suggestedName: ctor.name
|
|
614
|
+
};
|
|
624
615
|
if (fn.length === 0) try {
|
|
625
616
|
const result = fn();
|
|
626
617
|
if (result && result !== fn) return createSchemaResolution(result);
|
|
627
618
|
} catch {}
|
|
628
|
-
return void 0;
|
|
629
619
|
}
|
|
630
620
|
function schemaFromInstance(obj) {
|
|
631
621
|
if (isSwaggerSchema(obj)) return {
|
|
@@ -633,25 +623,18 @@ function schemaFromInstance(obj) {
|
|
|
633
623
|
schema: cloneSchema(obj)
|
|
634
624
|
};
|
|
635
625
|
const ctor = obj.constructor;
|
|
636
|
-
if (ctor && typeof ctor.toJsonSchema === "function") {
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
kind: "component",
|
|
649
|
-
schema,
|
|
650
|
-
typeRef: obj,
|
|
651
|
-
suggestedName: getTypeName(obj)
|
|
652
|
-
};
|
|
653
|
-
}
|
|
654
|
-
return void 0;
|
|
626
|
+
if (ctor && typeof ctor.toJsonSchema === "function") return {
|
|
627
|
+
kind: "component",
|
|
628
|
+
schema: asSwaggerSchema(ctor.toJsonSchema()),
|
|
629
|
+
typeRef: ctor,
|
|
630
|
+
suggestedName: ctor.name
|
|
631
|
+
};
|
|
632
|
+
if (typeof obj.toJsonSchema === "function") return {
|
|
633
|
+
kind: "component",
|
|
634
|
+
schema: asSwaggerSchema(obj.toJsonSchema()),
|
|
635
|
+
typeRef: obj,
|
|
636
|
+
suggestedName: getTypeName(obj)
|
|
637
|
+
};
|
|
655
638
|
}
|
|
656
639
|
function asSwaggerSchema(schema) {
|
|
657
640
|
if (!schema || typeof schema !== "object") return {};
|
|
@@ -705,8 +688,7 @@ function ensureComponentName(typeRef, schema, suggestedName) {
|
|
|
705
688
|
}
|
|
706
689
|
function applySwaggerMetadata(typeRef, schema) {
|
|
707
690
|
try {
|
|
708
|
-
const
|
|
709
|
-
const meta = mate.read(typeRef);
|
|
691
|
+
const meta = getSwaggerMate().read(typeRef);
|
|
710
692
|
if (meta) {
|
|
711
693
|
if (meta.swaggerExample !== void 0 && schema.example === void 0) schema.example = meta.swaggerExample;
|
|
712
694
|
const title = meta.label || meta.id;
|
|
@@ -724,14 +706,12 @@ function applySwaggerMetadata(typeRef, schema) {
|
|
|
724
706
|
}
|
|
725
707
|
}
|
|
726
708
|
function sanitizeComponentName(name) {
|
|
727
|
-
|
|
728
|
-
return sanitized || "Schema";
|
|
709
|
+
return name.replaceAll(/[^A-Za-z0-9_.-]/g, "_") || "Schema";
|
|
729
710
|
}
|
|
730
711
|
function getTypeName(typeRef) {
|
|
731
712
|
if (typeof typeRef === "function" && typeRef.name) return typeRef.name;
|
|
732
713
|
const ctor = typeRef.constructor;
|
|
733
714
|
if (ctor && ctor !== Object && ctor.name) return ctor.name;
|
|
734
|
-
return void 0;
|
|
735
715
|
}
|
|
736
716
|
function isSwaggerSchema(candidate) {
|
|
737
717
|
if (!candidate || typeof candidate !== "object") return false;
|
|
@@ -792,20 +772,18 @@ function schemaFromPrimitiveCtor(fn) {
|
|
|
792
772
|
}
|
|
793
773
|
function resolveSchemaFromRef(schema) {
|
|
794
774
|
if (!schema.$ref) return schema;
|
|
795
|
-
|
|
796
|
-
return globalSchemas[refName];
|
|
775
|
+
return globalSchemas[schema.$ref.replace("#/components/schemas/", "")];
|
|
797
776
|
}
|
|
798
777
|
function cloneSchema(schema) {
|
|
799
778
|
return JSON.parse(JSON.stringify(schema));
|
|
800
779
|
}
|
|
801
780
|
function getDefaultStatusCode(httpMethod) {
|
|
802
|
-
|
|
781
|
+
return {
|
|
803
782
|
GET: 200,
|
|
804
783
|
PUT: 200,
|
|
805
784
|
POST: 201,
|
|
806
785
|
DELETE: 204
|
|
807
|
-
};
|
|
808
|
-
return defaultStatusCodes[httpMethod.toUpperCase()] || 200;
|
|
786
|
+
}[httpMethod.toUpperCase()] || 200;
|
|
809
787
|
}
|
|
810
788
|
const STATUS_DESCRIPTIONS = {
|
|
811
789
|
200: "OK",
|
|
@@ -906,7 +884,6 @@ function resolveOperationSecurity(cmeta, hmeta, schemes) {
|
|
|
906
884
|
collectSchemesFromTransports(cmeta.authTransports, schemes);
|
|
907
885
|
return transportsToSecurityRequirement(cmeta.authTransports);
|
|
908
886
|
}
|
|
909
|
-
return void 0;
|
|
910
887
|
}
|
|
911
888
|
|
|
912
889
|
//#endregion
|
|
@@ -937,7 +914,7 @@ function _ts_param(paramIndex, decorator) {
|
|
|
937
914
|
}
|
|
938
915
|
var SwaggerController = class {
|
|
939
916
|
processCors() {
|
|
940
|
-
if (this.opts.cors) (0,
|
|
917
|
+
if (this.opts.cors) (0, _wooksjs_event_http.useResponse)().enableCors(this.opts.cors === true ? void 0 : this.opts.cors);
|
|
941
918
|
}
|
|
942
919
|
serveIndex(url, location, status) {
|
|
943
920
|
this.processCors();
|
|
@@ -959,9 +936,9 @@ var SwaggerController = class {
|
|
|
959
936
|
|
|
960
937
|
<body>
|
|
961
938
|
<div id="swagger-ui"></div>
|
|
962
|
-
<script src="./swagger-ui-bundle.js" charset="UTF-8">
|
|
963
|
-
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8">
|
|
964
|
-
<script src="./swagger-initializer.js" charset="UTF-8">
|
|
939
|
+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> <\/script>
|
|
940
|
+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> <\/script>
|
|
941
|
+
<script src="./swagger-initializer.js" charset="UTF-8"> <\/script>
|
|
965
942
|
</body>
|
|
966
943
|
</html>`;
|
|
967
944
|
}
|
|
@@ -985,10 +962,8 @@ var SwaggerController = class {
|
|
|
985
962
|
}
|
|
986
963
|
async resolveSpec() {
|
|
987
964
|
if (!this.spec) {
|
|
988
|
-
const
|
|
989
|
-
|
|
990
|
-
const moost$1 = await instantiate(moost.Moost);
|
|
991
|
-
this.spec = mapToSwaggerSpec(moost$1.getControllersOverview(), this.opts);
|
|
965
|
+
const { instantiate } = (0, moost.useControllerContext)((0, moost.current)());
|
|
966
|
+
this.spec = mapToSwaggerSpec((await instantiate(moost.Moost)).getControllersOverview(), this.opts);
|
|
992
967
|
}
|
|
993
968
|
return this.spec;
|
|
994
969
|
}
|
|
@@ -1005,7 +980,7 @@ var SwaggerController = class {
|
|
|
1005
980
|
return this.serve(url.split("/").pop());
|
|
1006
981
|
}
|
|
1007
982
|
serve(path$1) {
|
|
1008
|
-
return (0,
|
|
983
|
+
return (0, _wooksjs_http_static.serveFile)(path$1, {
|
|
1009
984
|
baseDir: this.assetPath,
|
|
1010
985
|
cacheControl: {
|
|
1011
986
|
public: true,
|
|
@@ -1022,13 +997,13 @@ var SwaggerController = class {
|
|
|
1022
997
|
}
|
|
1023
998
|
};
|
|
1024
999
|
_ts_decorate([
|
|
1025
|
-
(0,
|
|
1026
|
-
(0,
|
|
1027
|
-
(0,
|
|
1028
|
-
(0,
|
|
1029
|
-
_ts_param(0, (0,
|
|
1030
|
-
_ts_param(1, (0,
|
|
1031
|
-
_ts_param(2, (0,
|
|
1000
|
+
(0, _moostjs_event_http.Get)(""),
|
|
1001
|
+
(0, _moostjs_event_http.Get)("//"),
|
|
1002
|
+
(0, _moostjs_event_http.Get)("index.html"),
|
|
1003
|
+
(0, _moostjs_event_http.SetHeader)("content-type", "text/html"),
|
|
1004
|
+
_ts_param(0, (0, _moostjs_event_http.Url)()),
|
|
1005
|
+
_ts_param(1, (0, _moostjs_event_http.HeaderRef)("location")),
|
|
1006
|
+
_ts_param(2, (0, _moostjs_event_http.StatusRef)()),
|
|
1032
1007
|
_ts_metadata("design:type", Function),
|
|
1033
1008
|
_ts_metadata("design:paramtypes", [
|
|
1034
1009
|
String,
|
|
@@ -1038,31 +1013,31 @@ _ts_decorate([
|
|
|
1038
1013
|
_ts_metadata("design:returntype", void 0)
|
|
1039
1014
|
], SwaggerController.prototype, "serveIndex", null);
|
|
1040
1015
|
_ts_decorate([
|
|
1041
|
-
(0,
|
|
1042
|
-
(0,
|
|
1016
|
+
(0, _moostjs_event_http.Get)(),
|
|
1017
|
+
(0, _moostjs_event_http.SetHeader)("content-type", "application/javascript"),
|
|
1043
1018
|
_ts_metadata("design:type", Function),
|
|
1044
1019
|
_ts_metadata("design:paramtypes", []),
|
|
1045
1020
|
_ts_metadata("design:returntype", void 0)
|
|
1046
1021
|
], SwaggerController.prototype, "swagger-initializer.js", null);
|
|
1047
1022
|
_ts_decorate([
|
|
1048
|
-
(0,
|
|
1023
|
+
(0, _moostjs_event_http.Get)(),
|
|
1049
1024
|
_ts_metadata("design:type", Function),
|
|
1050
1025
|
_ts_metadata("design:paramtypes", []),
|
|
1051
1026
|
_ts_metadata("design:returntype", Promise)
|
|
1052
1027
|
], SwaggerController.prototype, "spec.json", null);
|
|
1053
1028
|
_ts_decorate([
|
|
1054
|
-
(0,
|
|
1055
|
-
(0,
|
|
1029
|
+
(0, _moostjs_event_http.Get)(),
|
|
1030
|
+
(0, _moostjs_event_http.SetHeader)("content-type", "text/yaml"),
|
|
1056
1031
|
_ts_metadata("design:type", Function),
|
|
1057
1032
|
_ts_metadata("design:paramtypes", []),
|
|
1058
1033
|
_ts_metadata("design:returntype", Promise)
|
|
1059
1034
|
], SwaggerController.prototype, "spec.yaml", null);
|
|
1060
1035
|
_ts_decorate([
|
|
1061
|
-
(0,
|
|
1062
|
-
(0,
|
|
1063
|
-
(0,
|
|
1064
|
-
(0,
|
|
1065
|
-
_ts_param(0, (0,
|
|
1036
|
+
(0, _moostjs_event_http.Get)("swagger-ui-bundle.*(js|js\\.map)"),
|
|
1037
|
+
(0, _moostjs_event_http.Get)("swagger-ui-standalone-preset.*(js|js\\.map)"),
|
|
1038
|
+
(0, _moostjs_event_http.Get)("swagger-ui.*(css|css\\.map)"),
|
|
1039
|
+
(0, _moostjs_event_http.Get)("index.*(css|css\\.map)"),
|
|
1040
|
+
_ts_param(0, (0, _moostjs_event_http.Url)()),
|
|
1066
1041
|
_ts_metadata("design:type", Function),
|
|
1067
1042
|
_ts_metadata("design:paramtypes", [String]),
|
|
1068
1043
|
_ts_metadata("design:returntype", void 0)
|
package/dist/index.mjs
CHANGED
|
@@ -255,8 +255,7 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
255
255
|
}
|
|
256
256
|
const defaultCode = getDefaultStatusCode(handlerMethod);
|
|
257
257
|
if (!responses?.[defaultCode] && hmeta?.returnType) {
|
|
258
|
-
const
|
|
259
|
-
const schema = toSchemaOrRef(ensured);
|
|
258
|
+
const schema = toSchemaOrRef(ensureSchema(hmeta.returnType));
|
|
260
259
|
if (schema) {
|
|
261
260
|
responses = responses || {};
|
|
262
261
|
responses[defaultCode] = {
|
|
@@ -333,8 +332,7 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
333
332
|
for (const paramName of handler.registeredAs[0].args) {
|
|
334
333
|
const paramIndex = handler.meta.params.findIndex((param) => param.paramSource === "ROUTE" && param.paramName === paramName);
|
|
335
334
|
const paramMeta = handler.meta.params[paramIndex];
|
|
336
|
-
const
|
|
337
|
-
const schema = toSchemaOrRef(ensured) || { type: "string" };
|
|
335
|
+
const schema = toSchemaOrRef(ensureSchema(paramMeta?.type)) || { type: "string" };
|
|
338
336
|
addParam({
|
|
339
337
|
name: paramName,
|
|
340
338
|
in: "path",
|
|
@@ -361,8 +359,7 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
361
359
|
if (schema?.type === "object" && schema.properties) {
|
|
362
360
|
const requiredProps = new Set(schema.required || []);
|
|
363
361
|
for (const [key, value] of Object.entries(schema.properties)) {
|
|
364
|
-
const
|
|
365
|
-
const normalizedProperty = normalizeQueryParamSchema(propertySchema);
|
|
362
|
+
const normalizedProperty = normalizeQueryParamSchema(cloneSchema(value));
|
|
366
363
|
if (normalizedProperty) endpointSpec.parameters.push({
|
|
367
364
|
name: key,
|
|
368
365
|
in: "query",
|
|
@@ -372,8 +369,8 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
372
369
|
});
|
|
373
370
|
}
|
|
374
371
|
} else if (ensured) {
|
|
375
|
-
const schema
|
|
376
|
-
const normalized = schema
|
|
372
|
+
const schema = toSchemaOrRef(ensured);
|
|
373
|
+
const normalized = schema ? normalizeQueryParamSchema(schema) : void 0;
|
|
377
374
|
endpointSpec.parameters.push({
|
|
378
375
|
name: paramMeta.paramName || "",
|
|
379
376
|
in: "query",
|
|
@@ -415,8 +412,7 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
415
412
|
const opId = swaggerSpec.paths[path]?.[method]?.operationId;
|
|
416
413
|
if (opId) {
|
|
417
414
|
if (!handlerOpIds.has(controller.type)) handlerOpIds.set(controller.type, /* @__PURE__ */ new Map());
|
|
418
|
-
|
|
419
|
-
methodMap?.set(handler.method, opId);
|
|
415
|
+
handlerOpIds.get(controller.type)?.set(handler.method, opId);
|
|
420
416
|
}
|
|
421
417
|
}
|
|
422
418
|
for (const { endpointSpec, httpMethod, config } of deferredLinks) {
|
|
@@ -456,16 +452,14 @@ function mapToSwaggerSpec(metadata, options) {
|
|
|
456
452
|
return swaggerSpec;
|
|
457
453
|
}
|
|
458
454
|
function resolveSwaggerSchemaFromConfig(type) {
|
|
459
|
-
if (type === void 0) return
|
|
460
|
-
|
|
461
|
-
return toSchemaOrRef(ensured);
|
|
455
|
+
if (type === void 0) return;
|
|
456
|
+
return toSchemaOrRef(ensureSchema(type));
|
|
462
457
|
}
|
|
463
458
|
function resolveResponseHeaders(headers) {
|
|
464
459
|
const resolved = {};
|
|
465
460
|
let hasAny = false;
|
|
466
461
|
for (const [name, header] of Object.entries(headers)) {
|
|
467
|
-
const
|
|
468
|
-
const entry = { schema };
|
|
462
|
+
const entry = { schema: resolveSwaggerSchemaFromConfig(header.type) || { type: "string" } };
|
|
469
463
|
if (header.description) entry.description = header.description;
|
|
470
464
|
if (header.required !== void 0) entry.required = header.required;
|
|
471
465
|
if (header.example !== void 0) entry.example = header.example;
|
|
@@ -475,13 +469,12 @@ function resolveResponseHeaders(headers) {
|
|
|
475
469
|
return hasAny ? resolved : void 0;
|
|
476
470
|
}
|
|
477
471
|
function toSchemaOrRef(result) {
|
|
478
|
-
if (!result) return
|
|
472
|
+
if (!result) return;
|
|
479
473
|
if (result.ref) return { $ref: result.ref };
|
|
480
474
|
return cloneSchema(result.schema);
|
|
481
475
|
}
|
|
482
476
|
function inferBodyContentType(schema, resolved) {
|
|
483
|
-
const
|
|
484
|
-
const schemaType = target?.type ?? schema.type;
|
|
477
|
+
const schemaType = (resolved ?? resolveSchemaFromRef(schema))?.type ?? schema.type;
|
|
485
478
|
if (typeof schemaType === "string" && [
|
|
486
479
|
"string",
|
|
487
480
|
"number",
|
|
@@ -498,7 +491,7 @@ const SIMPLE_QUERY_TYPES = new Set([
|
|
|
498
491
|
]);
|
|
499
492
|
function normalizeQueryParamSchema(schema) {
|
|
500
493
|
const target = resolveSchemaFromRef(schema) || schema;
|
|
501
|
-
if (!target) return
|
|
494
|
+
if (!target) return;
|
|
502
495
|
if (target.type === "array") return isArrayOfSimpleItems(target.items) ? schema : void 0;
|
|
503
496
|
return isSimpleSchema(schema) ? schema : void 0;
|
|
504
497
|
}
|
|
@@ -530,9 +523,9 @@ function resetSchemaRegistry() {
|
|
|
530
523
|
for (const key of Object.keys(globalSchemas)) delete globalSchemas[key];
|
|
531
524
|
}
|
|
532
525
|
function ensureSchema(type) {
|
|
533
|
-
if (type === void 0 || type === null) return
|
|
526
|
+
if (type === void 0 || type === null) return;
|
|
534
527
|
const resolution = createSchemaResolution(type);
|
|
535
|
-
if (!resolution) return
|
|
528
|
+
if (!resolution) return;
|
|
536
529
|
if (resolution.kind === "inline") return {
|
|
537
530
|
schema: cloneSchema(resolution.schema),
|
|
538
531
|
isComponent: false
|
|
@@ -547,23 +540,19 @@ function ensureSchema(type) {
|
|
|
547
540
|
};
|
|
548
541
|
}
|
|
549
542
|
function createSchemaResolution(type) {
|
|
550
|
-
if (type === void 0 || type === null) return
|
|
543
|
+
if (type === void 0 || type === null) return;
|
|
551
544
|
if (isSwaggerSchema(type)) return {
|
|
552
545
|
kind: "inline",
|
|
553
546
|
schema: cloneSchema(type)
|
|
554
547
|
};
|
|
555
548
|
if (Array.isArray(type)) {
|
|
556
|
-
if (type.length === 1) {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
items: itemsSchema
|
|
564
|
-
}
|
|
565
|
-
};
|
|
566
|
-
}
|
|
549
|
+
if (type.length === 1) return {
|
|
550
|
+
kind: "inline",
|
|
551
|
+
schema: {
|
|
552
|
+
type: "array",
|
|
553
|
+
items: toSchemaOrRef(ensureSchema(type[0]))
|
|
554
|
+
}
|
|
555
|
+
};
|
|
567
556
|
return {
|
|
568
557
|
kind: "inline",
|
|
569
558
|
schema: { type: "array" }
|
|
@@ -585,24 +574,19 @@ function createSchemaResolution(type) {
|
|
|
585
574
|
const resolution = schemaFromInstance(type);
|
|
586
575
|
if (resolution) return resolution;
|
|
587
576
|
}
|
|
588
|
-
return void 0;
|
|
589
577
|
}
|
|
590
578
|
function schemaFromFunction(fn) {
|
|
591
579
|
const ctor = fn;
|
|
592
|
-
if (typeof ctor.toJsonSchema === "function") {
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
suggestedName: ctor.name
|
|
599
|
-
};
|
|
600
|
-
}
|
|
580
|
+
if (typeof ctor.toJsonSchema === "function") return {
|
|
581
|
+
kind: "component",
|
|
582
|
+
schema: asSwaggerSchema(ctor.toJsonSchema()),
|
|
583
|
+
typeRef: ctor,
|
|
584
|
+
suggestedName: ctor.name
|
|
585
|
+
};
|
|
601
586
|
if (fn.length === 0) try {
|
|
602
587
|
const result = fn();
|
|
603
588
|
if (result && result !== fn) return createSchemaResolution(result);
|
|
604
589
|
} catch {}
|
|
605
|
-
return void 0;
|
|
606
590
|
}
|
|
607
591
|
function schemaFromInstance(obj) {
|
|
608
592
|
if (isSwaggerSchema(obj)) return {
|
|
@@ -610,25 +594,18 @@ function schemaFromInstance(obj) {
|
|
|
610
594
|
schema: cloneSchema(obj)
|
|
611
595
|
};
|
|
612
596
|
const ctor = obj.constructor;
|
|
613
|
-
if (ctor && typeof ctor.toJsonSchema === "function") {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
kind: "component",
|
|
626
|
-
schema,
|
|
627
|
-
typeRef: obj,
|
|
628
|
-
suggestedName: getTypeName(obj)
|
|
629
|
-
};
|
|
630
|
-
}
|
|
631
|
-
return void 0;
|
|
597
|
+
if (ctor && typeof ctor.toJsonSchema === "function") return {
|
|
598
|
+
kind: "component",
|
|
599
|
+
schema: asSwaggerSchema(ctor.toJsonSchema()),
|
|
600
|
+
typeRef: ctor,
|
|
601
|
+
suggestedName: ctor.name
|
|
602
|
+
};
|
|
603
|
+
if (typeof obj.toJsonSchema === "function") return {
|
|
604
|
+
kind: "component",
|
|
605
|
+
schema: asSwaggerSchema(obj.toJsonSchema()),
|
|
606
|
+
typeRef: obj,
|
|
607
|
+
suggestedName: getTypeName(obj)
|
|
608
|
+
};
|
|
632
609
|
}
|
|
633
610
|
function asSwaggerSchema(schema) {
|
|
634
611
|
if (!schema || typeof schema !== "object") return {};
|
|
@@ -682,8 +659,7 @@ function ensureComponentName(typeRef, schema, suggestedName) {
|
|
|
682
659
|
}
|
|
683
660
|
function applySwaggerMetadata(typeRef, schema) {
|
|
684
661
|
try {
|
|
685
|
-
const
|
|
686
|
-
const meta = mate.read(typeRef);
|
|
662
|
+
const meta = getSwaggerMate().read(typeRef);
|
|
687
663
|
if (meta) {
|
|
688
664
|
if (meta.swaggerExample !== void 0 && schema.example === void 0) schema.example = meta.swaggerExample;
|
|
689
665
|
const title = meta.label || meta.id;
|
|
@@ -701,14 +677,12 @@ function applySwaggerMetadata(typeRef, schema) {
|
|
|
701
677
|
}
|
|
702
678
|
}
|
|
703
679
|
function sanitizeComponentName(name) {
|
|
704
|
-
|
|
705
|
-
return sanitized || "Schema";
|
|
680
|
+
return name.replaceAll(/[^A-Za-z0-9_.-]/g, "_") || "Schema";
|
|
706
681
|
}
|
|
707
682
|
function getTypeName(typeRef) {
|
|
708
683
|
if (typeof typeRef === "function" && typeRef.name) return typeRef.name;
|
|
709
684
|
const ctor = typeRef.constructor;
|
|
710
685
|
if (ctor && ctor !== Object && ctor.name) return ctor.name;
|
|
711
|
-
return void 0;
|
|
712
686
|
}
|
|
713
687
|
function isSwaggerSchema(candidate) {
|
|
714
688
|
if (!candidate || typeof candidate !== "object") return false;
|
|
@@ -769,20 +743,18 @@ function schemaFromPrimitiveCtor(fn) {
|
|
|
769
743
|
}
|
|
770
744
|
function resolveSchemaFromRef(schema) {
|
|
771
745
|
if (!schema.$ref) return schema;
|
|
772
|
-
|
|
773
|
-
return globalSchemas[refName];
|
|
746
|
+
return globalSchemas[schema.$ref.replace("#/components/schemas/", "")];
|
|
774
747
|
}
|
|
775
748
|
function cloneSchema(schema) {
|
|
776
749
|
return JSON.parse(JSON.stringify(schema));
|
|
777
750
|
}
|
|
778
751
|
function getDefaultStatusCode(httpMethod) {
|
|
779
|
-
|
|
752
|
+
return {
|
|
780
753
|
GET: 200,
|
|
781
754
|
PUT: 200,
|
|
782
755
|
POST: 201,
|
|
783
756
|
DELETE: 204
|
|
784
|
-
};
|
|
785
|
-
return defaultStatusCodes[httpMethod.toUpperCase()] || 200;
|
|
757
|
+
}[httpMethod.toUpperCase()] || 200;
|
|
786
758
|
}
|
|
787
759
|
const STATUS_DESCRIPTIONS = {
|
|
788
760
|
200: "OK",
|
|
@@ -883,7 +855,6 @@ function resolveOperationSecurity(cmeta, hmeta, schemes) {
|
|
|
883
855
|
collectSchemesFromTransports(cmeta.authTransports, schemes);
|
|
884
856
|
return transportsToSecurityRequirement(cmeta.authTransports);
|
|
885
857
|
}
|
|
886
|
-
return void 0;
|
|
887
858
|
}
|
|
888
859
|
|
|
889
860
|
//#endregion
|
|
@@ -936,9 +907,9 @@ var SwaggerController = class {
|
|
|
936
907
|
|
|
937
908
|
<body>
|
|
938
909
|
<div id="swagger-ui"></div>
|
|
939
|
-
<script src="./swagger-ui-bundle.js" charset="UTF-8">
|
|
940
|
-
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8">
|
|
941
|
-
<script src="./swagger-initializer.js" charset="UTF-8">
|
|
910
|
+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> <\/script>
|
|
911
|
+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> <\/script>
|
|
912
|
+
<script src="./swagger-initializer.js" charset="UTF-8"> <\/script>
|
|
942
913
|
</body>
|
|
943
914
|
</html>`;
|
|
944
915
|
}
|
|
@@ -962,10 +933,8 @@ var SwaggerController = class {
|
|
|
962
933
|
}
|
|
963
934
|
async resolveSpec() {
|
|
964
935
|
if (!this.spec) {
|
|
965
|
-
const
|
|
966
|
-
|
|
967
|
-
const moost = await instantiate(Moost);
|
|
968
|
-
this.spec = mapToSwaggerSpec(moost.getControllersOverview(), this.opts);
|
|
936
|
+
const { instantiate } = useControllerContext(current());
|
|
937
|
+
this.spec = mapToSwaggerSpec((await instantiate(Moost)).getControllersOverview(), this.opts);
|
|
969
938
|
}
|
|
970
939
|
return this.spec;
|
|
971
940
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/swagger",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.10",
|
|
4
4
|
"description": "@moostjs/swagger",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"composables",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"vitest": "3.2.4"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@wooksjs/event-http": "^0.7.
|
|
49
|
-
"@wooksjs/http-static": "^0.7.
|
|
50
|
-
"@moostjs/event-http": "^0.6.
|
|
51
|
-
"moost": "^0.6.
|
|
48
|
+
"@wooksjs/event-http": "^0.7.12",
|
|
49
|
+
"@wooksjs/http-static": "^0.7.12",
|
|
50
|
+
"@moostjs/event-http": "^0.6.10",
|
|
51
|
+
"moost": "^0.6.10"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"pub": "pnpm publish --access public",
|