@keq-request/cli 5.0.0-alpha.17 → 5.0.0-alpha.19
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/CHANGELOG.md +14 -0
- package/dist/cli.cjs +52 -52
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +52 -52
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +51 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +51 -51
- package/dist/index.js.map +1 -1
- package/dist/tasks/utils/api-document.d.ts +2 -2
- package/dist/tasks/utils/api-document.d.ts.map +1 -1
- package/dist/tasks/utils/api-document_v3_1.d.ts +2 -2
- package/dist/tasks/utils/api-document_v3_1.d.ts.map +1 -1
- package/dist/types/build-cli-options.d.ts +1 -1
- package/dist/types/runtime-config.d.ts +1 -1
- package/dist/utils/debugger.d.ts +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/dereference-deep.d.ts.map +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/dereference-operation.d.ts.map +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/dereference.d.ts.map +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/index.d.ts +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/index.d.ts.map +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/is-ref-defined.d.ts.map +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/remove-undefined-ref.d.ts.map +1 -1
- package/dist/utils/{swagger-utils → openapi-utils}/update-operation-id.d.ts.map +1 -1
- package/package.json +13 -13
- /package/dist/utils/{swagger-utils → openapi-utils}/dereference-deep.d.ts +0 -0
- /package/dist/utils/{swagger-utils → openapi-utils}/dereference-operation.d.ts +0 -0
- /package/dist/utils/{swagger-utils → openapi-utils}/dereference.d.ts +0 -0
- /package/dist/utils/{swagger-utils → openapi-utils}/is-ref-defined.d.ts +0 -0
- /package/dist/utils/{swagger-utils → openapi-utils}/remove-undefined-ref.d.ts +0 -0
- /package/dist/utils/{swagger-utils → openapi-utils}/update-operation-id.d.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -68,7 +68,7 @@ var RuntimeConfig = Type2.Object({
|
|
|
68
68
|
])),
|
|
69
69
|
debug: Type2.Optional(Type2.Boolean({ default: false })),
|
|
70
70
|
/**
|
|
71
|
-
* Whether to tolerate wrong swagger structure
|
|
71
|
+
* Whether to tolerate wrong openapi/swagger structure
|
|
72
72
|
*/
|
|
73
73
|
tolerant: Type2.Optional(Type2.Boolean({ default: false })),
|
|
74
74
|
plugins: Type2.Optional(Type2.Array(Type2.Any(), { default: [] }))
|
|
@@ -534,14 +534,14 @@ var logger = {
|
|
|
534
534
|
import { openapiShakingSync } from "@opendoc/openapi-shaking";
|
|
535
535
|
var ApiDocumentV3_1 = class _ApiDocumentV3_1 {
|
|
536
536
|
module;
|
|
537
|
-
|
|
538
|
-
constructor(
|
|
537
|
+
specification;
|
|
538
|
+
constructor(specification, module) {
|
|
539
539
|
this.module = module;
|
|
540
|
-
this.
|
|
540
|
+
this.specification = specification;
|
|
541
541
|
}
|
|
542
542
|
get schemas() {
|
|
543
543
|
const module = this.module;
|
|
544
|
-
return Object.entries(this.
|
|
544
|
+
return Object.entries(this.specification.components?.schemas || {}).map(([name, schema]) => new SchemaDefinition({
|
|
545
545
|
id: `#/components/schemas/${name}`,
|
|
546
546
|
name,
|
|
547
547
|
schema,
|
|
@@ -551,7 +551,7 @@ var ApiDocumentV3_1 = class _ApiDocumentV3_1 {
|
|
|
551
551
|
}
|
|
552
552
|
get operations() {
|
|
553
553
|
const module = this.module;
|
|
554
|
-
return Object.entries(this.
|
|
554
|
+
return Object.entries(this.specification.paths || {}).flatMap(([pathname, pathItem]) => Object.entries(pathItem || {}).filter(([method]) => SupportedMethods.includes(method.toLowerCase())).map(([method, operation]) => new OperationDefinition({
|
|
555
555
|
method,
|
|
556
556
|
pathname,
|
|
557
557
|
operation,
|
|
@@ -560,18 +560,18 @@ var ApiDocumentV3_1 = class _ApiDocumentV3_1 {
|
|
|
560
560
|
})));
|
|
561
561
|
}
|
|
562
562
|
isEmpty() {
|
|
563
|
-
return R6.isEmpty(this.
|
|
563
|
+
return R6.isEmpty(this.specification.paths);
|
|
564
564
|
}
|
|
565
565
|
dereference($ref) {
|
|
566
566
|
if ($ref.startsWith("#/")) {
|
|
567
567
|
return this.schemas.find((schema) => schema.id === $ref);
|
|
568
568
|
}
|
|
569
|
-
logger.warn(`The $ref(${$ref}) is not defined in ${this.module.name} swagger.`);
|
|
569
|
+
logger.warn(`The $ref(${$ref}) is not defined in ${this.module.name} openapi/swagger.`);
|
|
570
570
|
}
|
|
571
571
|
isRefDefined($ref) {
|
|
572
572
|
if ($ref.startsWith("#/")) {
|
|
573
573
|
const path9 = $ref.replace("#/", "").split("/");
|
|
574
|
-
return R6.path(path9, this.
|
|
574
|
+
return R6.path(path9, this.specification) !== void 0;
|
|
575
575
|
}
|
|
576
576
|
return false;
|
|
577
577
|
}
|
|
@@ -588,7 +588,7 @@ var ApiDocumentV3_1 = class _ApiDocumentV3_1 {
|
|
|
588
588
|
return filter2(operationDefinition);
|
|
589
589
|
};
|
|
590
590
|
const sharkedSwagger = openapiShakingSync(
|
|
591
|
-
this.
|
|
591
|
+
this.specification,
|
|
592
592
|
isAccepted,
|
|
593
593
|
{ tolerant: true }
|
|
594
594
|
);
|
|
@@ -611,14 +611,14 @@ var Debugger = class {
|
|
|
611
611
|
constructor(rc) {
|
|
612
612
|
this.rc = rc;
|
|
613
613
|
}
|
|
614
|
-
|
|
614
|
+
writeOpenapi(fullpath, openapi) {
|
|
615
615
|
if (this.rc?.debug) {
|
|
616
|
-
void fs4.writeJSON(fullpath,
|
|
616
|
+
void fs4.writeJSON(fullpath, openapi, { spaces: 2 });
|
|
617
617
|
}
|
|
618
618
|
}
|
|
619
619
|
};
|
|
620
620
|
|
|
621
|
-
// src/utils/
|
|
621
|
+
// src/utils/openapi-utils/dereference.ts
|
|
622
622
|
function dereference($ref, swagger) {
|
|
623
623
|
let value;
|
|
624
624
|
for (const key of $ref.split("/")) {
|
|
@@ -632,17 +632,17 @@ function dereference($ref, swagger) {
|
|
|
632
632
|
return value;
|
|
633
633
|
}
|
|
634
634
|
|
|
635
|
-
// src/utils/
|
|
635
|
+
// src/utils/openapi-utils/dereference-operation.ts
|
|
636
636
|
import * as R9 from "ramda";
|
|
637
637
|
import jsonpointer2 from "jsonpointer";
|
|
638
638
|
import { JSONPath as JSONPath4 } from "jsonpath-plus";
|
|
639
639
|
|
|
640
|
-
// src/utils/
|
|
640
|
+
// src/utils/openapi-utils/remove-undefined-ref.ts
|
|
641
641
|
import * as R8 from "ramda";
|
|
642
642
|
import jsonpointer from "jsonpointer";
|
|
643
643
|
import { JSONPath as JSONPath3 } from "jsonpath-plus";
|
|
644
644
|
|
|
645
|
-
// src/utils/
|
|
645
|
+
// src/utils/openapi-utils/is-ref-defined.ts
|
|
646
646
|
import * as R7 from "ramda";
|
|
647
647
|
function isRefDefined($ref, swagger) {
|
|
648
648
|
if ($ref.startsWith("#/")) {
|
|
@@ -652,7 +652,7 @@ function isRefDefined($ref, swagger) {
|
|
|
652
652
|
return false;
|
|
653
653
|
}
|
|
654
654
|
|
|
655
|
-
// src/utils/
|
|
655
|
+
// src/utils/openapi-utils/remove-undefined-ref.ts
|
|
656
656
|
function removeUndefinedRef(swagger) {
|
|
657
657
|
const shadow = R8.clone(swagger);
|
|
658
658
|
const matches = JSONPath3({
|
|
@@ -668,7 +668,7 @@ function removeUndefinedRef(swagger) {
|
|
|
668
668
|
return shadow;
|
|
669
669
|
}
|
|
670
670
|
|
|
671
|
-
// src/utils/
|
|
671
|
+
// src/utils/openapi-utils/dereference-operation.ts
|
|
672
672
|
function dereferencePathObject(swagger) {
|
|
673
673
|
const matches = JSONPath4({
|
|
674
674
|
path: "$.paths.*.$ref^",
|
|
@@ -676,7 +676,7 @@ function dereferencePathObject(swagger) {
|
|
|
676
676
|
resultType: "all"
|
|
677
677
|
});
|
|
678
678
|
for (const match of matches) {
|
|
679
|
-
const value =
|
|
679
|
+
const value = OpenapiUtils.dereference(match.value.$ref, swagger);
|
|
680
680
|
jsonpointer2.set(swagger, match.pointer, value);
|
|
681
681
|
}
|
|
682
682
|
}
|
|
@@ -687,7 +687,7 @@ function dereferenceRequestBody(swagger) {
|
|
|
687
687
|
resultType: "all"
|
|
688
688
|
});
|
|
689
689
|
for (const match of matches) {
|
|
690
|
-
const value =
|
|
690
|
+
const value = OpenapiUtils.dereference(match.value.$ref, swagger);
|
|
691
691
|
jsonpointer2.set(swagger, match.pointer, value);
|
|
692
692
|
}
|
|
693
693
|
}
|
|
@@ -705,7 +705,7 @@ function dereferenceResponses(swagger) {
|
|
|
705
705
|
})
|
|
706
706
|
];
|
|
707
707
|
for (const match of matches) {
|
|
708
|
-
const value =
|
|
708
|
+
const value = OpenapiUtils.dereference(match.value.$ref, swagger);
|
|
709
709
|
jsonpointer2.set(swagger, match.pointer, value);
|
|
710
710
|
}
|
|
711
711
|
}
|
|
@@ -716,7 +716,7 @@ function dereferenceParameters(swagger) {
|
|
|
716
716
|
resultType: "all"
|
|
717
717
|
});
|
|
718
718
|
for (const match of matches) {
|
|
719
|
-
const value =
|
|
719
|
+
const value = OpenapiUtils.dereference(match.value.$ref, swagger);
|
|
720
720
|
jsonpointer2.set(swagger, match.pointer, value);
|
|
721
721
|
}
|
|
722
722
|
}
|
|
@@ -729,7 +729,7 @@ function dereferenceOperation(swagger) {
|
|
|
729
729
|
return removeUndefinedRef(shadow);
|
|
730
730
|
}
|
|
731
731
|
|
|
732
|
-
// src/utils/
|
|
732
|
+
// src/utils/openapi-utils/update-operation-id.ts
|
|
733
733
|
import * as R10 from "ramda";
|
|
734
734
|
function updateOperationId(swagger, fn) {
|
|
735
735
|
const shadow = R10.clone(swagger);
|
|
@@ -776,7 +776,7 @@ var JsonSchemaUtils = class {
|
|
|
776
776
|
static isMixed = isMixed;
|
|
777
777
|
};
|
|
778
778
|
|
|
779
|
-
// src/utils/
|
|
779
|
+
// src/utils/openapi-utils/dereference-deep.ts
|
|
780
780
|
function dereferenceDeep($ref, swagger) {
|
|
781
781
|
const stack = [$ref];
|
|
782
782
|
let value;
|
|
@@ -796,8 +796,8 @@ function dereferenceDeep($ref, swagger) {
|
|
|
796
796
|
return value;
|
|
797
797
|
}
|
|
798
798
|
|
|
799
|
-
// src/utils/
|
|
800
|
-
var
|
|
799
|
+
// src/utils/openapi-utils/index.ts
|
|
800
|
+
var OpenapiUtils = class {
|
|
801
801
|
static isRefDefined = isRefDefined;
|
|
802
802
|
static dereference = dereference;
|
|
803
803
|
static dereferenceDeep = dereferenceDeep;
|
|
@@ -809,10 +809,10 @@ var SwaggerUtils = class {
|
|
|
809
809
|
// src/tasks/utils/api-document.ts
|
|
810
810
|
var ApiDocument = class _ApiDocument {
|
|
811
811
|
module;
|
|
812
|
-
|
|
813
|
-
constructor(
|
|
812
|
+
specification;
|
|
813
|
+
constructor(specification, module) {
|
|
814
814
|
this.module = module;
|
|
815
|
-
this.
|
|
815
|
+
this.specification = specification;
|
|
816
816
|
}
|
|
817
817
|
static cache = /* @__PURE__ */ new Map();
|
|
818
818
|
static async download(url) {
|
|
@@ -823,14 +823,14 @@ var ApiDocument = class _ApiDocument {
|
|
|
823
823
|
content = await res.text();
|
|
824
824
|
} catch (e) {
|
|
825
825
|
if (e instanceof Error) {
|
|
826
|
-
e.message = `Unable get the swagger file from ${url}: ${e.message}`;
|
|
826
|
+
e.message = `Unable get the openapi/swagger file from ${url}: ${e.message}`;
|
|
827
827
|
}
|
|
828
828
|
throw e;
|
|
829
829
|
}
|
|
830
830
|
try {
|
|
831
831
|
return JSON.parse(content);
|
|
832
832
|
} catch (e) {
|
|
833
|
-
throw new Error(`The swagger file get from url isn't json: ${url}`);
|
|
833
|
+
throw new Error(`The openapi/swagger file get from url isn't json: ${url}`);
|
|
834
834
|
}
|
|
835
835
|
}
|
|
836
836
|
static async read(filePath) {
|
|
@@ -848,17 +848,17 @@ var ApiDocument = class _ApiDocument {
|
|
|
848
848
|
if (this.cache.has(moduleDefinition.address)) {
|
|
849
849
|
return this.cache.get(moduleDefinition.address);
|
|
850
850
|
}
|
|
851
|
-
const
|
|
852
|
-
return new _ApiDocument(
|
|
851
|
+
const openapi = validUrl.isUri(address) ? await this.download(address) : await this.read(address);
|
|
852
|
+
return new _ApiDocument(openapi, moduleDefinition);
|
|
853
853
|
}
|
|
854
854
|
async validate() {
|
|
855
|
-
return await validate(this.
|
|
855
|
+
return await validate(this.specification);
|
|
856
856
|
}
|
|
857
857
|
// remove chinese and special symbols
|
|
858
858
|
fix() {
|
|
859
|
-
const
|
|
859
|
+
const openapi = fixSwagger(this.specification);
|
|
860
860
|
return new _ApiDocument(
|
|
861
|
-
|
|
861
|
+
openapi,
|
|
862
862
|
new ModuleDefinition(
|
|
863
863
|
this.module.name,
|
|
864
864
|
`file://${this.module.name}.fixed.json`
|
|
@@ -868,22 +868,22 @@ var ApiDocument = class _ApiDocument {
|
|
|
868
868
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
869
869
|
async toV3_1(rc) {
|
|
870
870
|
const debug = new Debugger(rc);
|
|
871
|
-
let
|
|
872
|
-
const { specification } = upgrade(
|
|
873
|
-
|
|
874
|
-
debug.
|
|
875
|
-
|
|
876
|
-
debug.
|
|
871
|
+
let spec = this.specification;
|
|
872
|
+
const { specification } = upgrade(spec);
|
|
873
|
+
spec = specification;
|
|
874
|
+
debug.writeOpenapi(`.keq/${this.module.name}.3_1.json`, spec);
|
|
875
|
+
spec = OpenapiUtils.dereferenceOperation(spec);
|
|
876
|
+
debug.writeOpenapi(`.keq/${this.module.name}.3_1.sharked.json`, spec);
|
|
877
877
|
if (rc?.operationIdFactory) {
|
|
878
878
|
const operationIdFactory = rc.operationIdFactory;
|
|
879
|
-
|
|
880
|
-
|
|
879
|
+
spec = OpenapiUtils.updateOperationId(
|
|
880
|
+
spec,
|
|
881
881
|
(method, pathname, operation) => operationIdFactory({ method, pathname, operation, module: this.module })
|
|
882
882
|
);
|
|
883
|
-
debug.
|
|
883
|
+
debug.writeOpenapi(`.keq/${this.module.name}.3_1.formatted.json`, spec);
|
|
884
884
|
}
|
|
885
885
|
return new ApiDocumentV3_1(
|
|
886
|
-
|
|
886
|
+
spec,
|
|
887
887
|
new ModuleDefinition(
|
|
888
888
|
this.module.name,
|
|
889
889
|
`file://${this.module.name}.v3_1.json`
|
|
@@ -969,12 +969,12 @@ function main3() {
|
|
|
969
969
|
task: async (ctx, task2) => {
|
|
970
970
|
const { valid, errors } = await document.validate();
|
|
971
971
|
if (!valid) {
|
|
972
|
-
const message = `${document.module.name} module swagger file does not conform to the openapi@3.1 standard specifications or have grammatical errors, which may cause unexpected errors:
|
|
972
|
+
const message = `${document.module.name} module openapi/swagger file does not conform to the openapi@3.1 standard specifications or have grammatical errors, which may cause unexpected errors:
|
|
973
973
|
${errors?.map((e) => ` - ${e.message}`).join("\n")}`;
|
|
974
974
|
task2.output = message;
|
|
975
975
|
}
|
|
976
976
|
const fixedDocument = document.fix();
|
|
977
|
-
debug.
|
|
977
|
+
debug.writeOpenapi(`.keq/${document.module.name}.fixed.json`, fixedDocument.specification);
|
|
978
978
|
const v3_1Document = await fixedDocument.toV3_1(rc);
|
|
979
979
|
ctx.validated.documents.push(v3_1Document);
|
|
980
980
|
}
|
|
@@ -1649,7 +1649,7 @@ function parameterBodies(operationDefinition, alias = R15.identity, typeName) {
|
|
|
1649
1649
|
if (operation.requestBody && !JsonSchemaUtils.isRef(operation.requestBody)) {
|
|
1650
1650
|
const $mediaTypes = Object.entries(operation.requestBody.content || {}).map(([mediaType, mediaTypeObject]) => [mediaType, mediaTypeObject.schema]).map(([mediaType, schemaOrRef]) => {
|
|
1651
1651
|
if (!schemaOrRef) return `${JSON.stringify(mediaType)}: unknown`;
|
|
1652
|
-
const schema = JsonSchemaUtils.isRef(schemaOrRef) ?
|
|
1652
|
+
const schema = JsonSchemaUtils.isRef(schemaOrRef) ? OpenapiUtils.dereferenceDeep(schemaOrRef.$ref, operationDefinition.document.specification) : schemaOrRef;
|
|
1653
1653
|
if (schema.type === "object" || schema.properties) {
|
|
1654
1654
|
return `${JSON.stringify(mediaType)}: ${generateSchema(schemaOrRef, alias)} & { [key: string]: any }`;
|
|
1655
1655
|
}
|
|
@@ -1749,7 +1749,7 @@ function errorToComment(err, mediaType) {
|
|
|
1749
1749
|
function requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
|
|
1750
1750
|
try {
|
|
1751
1751
|
const $propertyName = JSON.stringify(propertyName);
|
|
1752
|
-
const schema = JsonSchemaUtils.isRef(propertySchema) ?
|
|
1752
|
+
const schema = JsonSchemaUtils.isRef(propertySchema) ? OpenapiUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.specification) : propertySchema;
|
|
1753
1753
|
if (schema.type === "string" && schema.format === "binary" || schema.contentMediaType === "application/octet-stream") {
|
|
1754
1754
|
return `if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
|
|
1755
1755
|
} else if (schema.type === "string" || schema.type === "array" && schema.items && schema.items.type === "string") {
|
|
@@ -1778,7 +1778,7 @@ function requestBodyRenderer(operationDefinition, typeName) {
|
|
|
1778
1778
|
const $requestBody = Object.entries(requestBodyContent).map(([mediaType, mediaTypeObject]) => {
|
|
1779
1779
|
if (!mediaTypeObject.schema) return;
|
|
1780
1780
|
try {
|
|
1781
|
-
const schema = JsonSchemaUtils.isRef(mediaTypeObject.schema) ?
|
|
1781
|
+
const schema = JsonSchemaUtils.isRef(mediaTypeObject.schema) ? OpenapiUtils.dereferenceDeep(mediaTypeObject.schema.$ref, operationDefinition.document.specification) : mediaTypeObject.schema;
|
|
1782
1782
|
if (schema.type !== "object") return;
|
|
1783
1783
|
const properties = schema.properties || {};
|
|
1784
1784
|
return Object.entries(properties).map(([propertyName, propertySchema]) => {
|