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