@keq-request/cli 5.0.0-alpha.13 → 5.0.0-alpha.15
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 +16 -0
- package/dist/cli.cjs +77 -39
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +77 -39
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +75 -37
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +75 -37
- package/dist/index.js.map +1 -1
- package/dist/plugins/body-fallback/index.d.ts.map +1 -1
- package/dist/plugins/eslint/index.d.ts.map +1 -1
- package/dist/plugins/prettier/index.d.ts.map +1 -1
- package/dist/plugins.cjs +2 -0
- package/dist/plugins.cjs.map +1 -1
- package/dist/plugins.js +2 -0
- package/dist/plugins.js.map +1 -1
- package/dist/renderer/json-schema/index.d.ts.map +1 -1
- package/dist/renderer/operation-request/error-to-comment.d.ts +2 -0
- package/dist/renderer/operation-request/error-to-comment.d.ts.map +1 -0
- package/dist/renderer/operation-request/index.d.ts.map +1 -1
- package/dist/renderer/operation-request/request-body.d.ts +4 -0
- package/dist/renderer/operation-request/request-body.d.ts.map +1 -0
- package/dist/renderer/utils/generate-schema.d.ts.map +1 -1
- package/dist/tasks/compile/utils/compile-operation-definition.d.ts.map +1 -1
- package/dist/tasks/persist/index.d.ts.map +1 -1
- package/dist/tasks/utils/dependency.d.ts +3 -1
- package/dist/tasks/utils/dependency.d.ts.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
## 5.0.0-alpha.6 (2025-09-17)
|
|
2
2
|
|
|
3
|
+
## 5.0.0-alpha.15
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9ca7672: Empty schema objects caused code generation errors.
|
|
8
|
+
- keq@5.0.0-alpha.15
|
|
9
|
+
|
|
10
|
+
## 5.0.0-alpha.14
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 0459484: the type exported add type prefix
|
|
15
|
+
- 0459484: unabled add ignore rule by cli
|
|
16
|
+
- 0459484: triggers type coercion when form-data requestBody filed is not string or file
|
|
17
|
+
- keq@5.0.0-alpha.14
|
|
18
|
+
|
|
3
19
|
## 5.0.0-alpha.13
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/cli.cjs
CHANGED
|
@@ -1100,7 +1100,10 @@ function createPersistArtifactTask() {
|
|
|
1100
1100
|
title: "Write files",
|
|
1101
1101
|
task: async (context, task) => {
|
|
1102
1102
|
if (!context.setup) throw new Error("Please run setup task first.");
|
|
1103
|
-
if (!context.compiled)
|
|
1103
|
+
if (!context.compiled) {
|
|
1104
|
+
task.skip("No compiled artifacts to persist.");
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1104
1107
|
const rc = context.setup.rc;
|
|
1105
1108
|
const artifacts = context.compiled.artifacts;
|
|
1106
1109
|
for (const artifact of artifacts) {
|
|
@@ -1231,6 +1234,7 @@ function generateArray(schema, alias) {
|
|
|
1231
1234
|
return "any[]";
|
|
1232
1235
|
}
|
|
1233
1236
|
function indent(space, text) {
|
|
1237
|
+
if (text === "") return "";
|
|
1234
1238
|
const indentation = " ".repeat(space);
|
|
1235
1239
|
return text.split("\n").map((line) => `${indentation}${line}`).join("\n");
|
|
1236
1240
|
}
|
|
@@ -1304,11 +1308,13 @@ async function jsonSchemaRenderer(schemaDefinition) {
|
|
|
1304
1308
|
].filter(R12.isNotNil).join("\n");
|
|
1305
1309
|
}
|
|
1306
1310
|
if (JsonSchemaUtils.isNonArray(schemaDefinition.schema) && schemaDefinition.schema.type === "object") {
|
|
1311
|
+
const $schema = generateSchema(schemaDefinition.schema);
|
|
1312
|
+
const $declaration = $schema.startsWith("{") ? `export interface ${schemaDefinition.name} ${$schema}` : `export type ${schemaDefinition.name} = ${$schema}`;
|
|
1307
1313
|
return [
|
|
1308
1314
|
"/* @anchor:file:start */",
|
|
1309
1315
|
"",
|
|
1310
1316
|
$comment || void 0,
|
|
1311
|
-
|
|
1317
|
+
$declaration,
|
|
1312
1318
|
"",
|
|
1313
1319
|
"/* @anchor:file:end */"
|
|
1314
1320
|
].filter(R12.isNotNil).join("\n");
|
|
@@ -1405,15 +1411,17 @@ function toComment(msg) {
|
|
|
1405
1411
|
|
|
1406
1412
|
// src/tasks/utils/dependency.ts
|
|
1407
1413
|
var DependencyIdentifier = class {
|
|
1408
|
-
constructor(name, alias) {
|
|
1414
|
+
constructor(name, alias, type = false) {
|
|
1409
1415
|
this.name = name;
|
|
1410
1416
|
this.alias = alias;
|
|
1417
|
+
this.type = type;
|
|
1411
1418
|
}
|
|
1412
1419
|
toCode() {
|
|
1420
|
+
const $type = this.type ? "type " : "";
|
|
1413
1421
|
if (this.alias) {
|
|
1414
|
-
return `${this.name} as ${this.alias}`;
|
|
1422
|
+
return `${$type}${this.name} as ${this.alias}`;
|
|
1415
1423
|
}
|
|
1416
|
-
return this.name
|
|
1424
|
+
return `${$type}${this.name}`;
|
|
1417
1425
|
}
|
|
1418
1426
|
};
|
|
1419
1427
|
var Dependency = class {
|
|
@@ -1424,6 +1432,11 @@ var Dependency = class {
|
|
|
1424
1432
|
constructor(source, identifiers, belongTo, options) {
|
|
1425
1433
|
this.source = source;
|
|
1426
1434
|
this.identifiers = identifiers.map((i) => typeof i === "string" ? new DependencyIdentifier(i) : i);
|
|
1435
|
+
if (options?.type) {
|
|
1436
|
+
for (const identifier of this.identifiers) {
|
|
1437
|
+
identifier.type = true;
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1427
1440
|
this.export = !!options?.export;
|
|
1428
1441
|
this.belongTo = belongTo;
|
|
1429
1442
|
}
|
|
@@ -1614,7 +1627,7 @@ async function compileSchemaDefinition(options) {
|
|
|
1614
1627
|
}
|
|
1615
1628
|
|
|
1616
1629
|
// src/tasks/compile/utils/compile-operation-definition.ts
|
|
1617
|
-
var
|
|
1630
|
+
var R18 = __toESM(require("ramda"), 1);
|
|
1618
1631
|
|
|
1619
1632
|
// src/renderer/operation-type/index.ts
|
|
1620
1633
|
var R15 = __toESM(require("ramda"), 1);
|
|
@@ -1740,7 +1753,12 @@ function generateParameters(name, parameters, alias) {
|
|
|
1740
1753
|
}
|
|
1741
1754
|
|
|
1742
1755
|
// src/renderer/operation-request/index.ts
|
|
1756
|
+
var R17 = __toESM(require("ramda"), 1);
|
|
1757
|
+
|
|
1758
|
+
// src/renderer/operation-request/request-body.ts
|
|
1743
1759
|
var R16 = __toESM(require("ramda"), 1);
|
|
1760
|
+
|
|
1761
|
+
// src/renderer/operation-request/error-to-comment.ts
|
|
1744
1762
|
function errorToComment(err, mediaType) {
|
|
1745
1763
|
const $err = String(err).split("\n").map(((line) => ` * ${line}`)).join("\n");
|
|
1746
1764
|
return [
|
|
@@ -1750,6 +1768,34 @@ function errorToComment(err, mediaType) {
|
|
|
1750
1768
|
" */"
|
|
1751
1769
|
].join("\n");
|
|
1752
1770
|
}
|
|
1771
|
+
|
|
1772
|
+
// src/renderer/operation-request/request-body.ts
|
|
1773
|
+
function requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
|
|
1774
|
+
try {
|
|
1775
|
+
const $propertyName = JSON.stringify(propertyName);
|
|
1776
|
+
const schema = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
|
|
1777
|
+
if (schema.type === "string" && schema.format === "binary" || schema.contentMediaType === "application/octet-stream") {
|
|
1778
|
+
return `if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
|
|
1779
|
+
} else if (schema.type === "string" || schema.type === "array" && schema.items && schema.items.type === "string") {
|
|
1780
|
+
return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
|
|
1781
|
+
} else if (schema.type === "number" || schema.type === "integer") {
|
|
1782
|
+
return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, String(args[${$propertyName}]))`;
|
|
1783
|
+
}
|
|
1784
|
+
return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, String(args[${$propertyName}]) /* type is non-string in schema; triggers type coercion here */)`;
|
|
1785
|
+
} catch (err) {
|
|
1786
|
+
return errorToComment(err, mediaType);
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
function requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
|
|
1790
|
+
if (mediaType === "application/json") {
|
|
1791
|
+
const $propertyName = JSON.stringify(propertyName);
|
|
1792
|
+
return `if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
|
|
1793
|
+
} else if (mediaType === "multipart/form-data") {
|
|
1794
|
+
return requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition);
|
|
1795
|
+
} else {
|
|
1796
|
+
throw new Error(`Unsupported media type: ${mediaType}`);
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1753
1799
|
function requestBodyRenderer(operationDefinition, typeName) {
|
|
1754
1800
|
const { operation } = operationDefinition;
|
|
1755
1801
|
const requestBodyContent = operation.requestBody?.content || {};
|
|
@@ -1760,22 +1806,10 @@ function requestBodyRenderer(operationDefinition, typeName) {
|
|
|
1760
1806
|
if (schema.type !== "object") return;
|
|
1761
1807
|
const properties = schema.properties || {};
|
|
1762
1808
|
return Object.entries(properties).map(([propertyName, propertySchema]) => {
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
try {
|
|
1768
|
-
const schema2 = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
|
|
1769
|
-
if (schema2.type === "string" && schema2.format === "binary" || schema2.contentMediaType === "application/octet-stream") {
|
|
1770
|
-
return ` if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
|
|
1771
|
-
}
|
|
1772
|
-
return ` if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
|
|
1773
|
-
} catch (err) {
|
|
1774
|
-
return indent(2, errorToComment(err, mediaType));
|
|
1775
|
-
}
|
|
1776
|
-
} else {
|
|
1777
|
-
throw new Error(`Unsupported media type: ${mediaType}`);
|
|
1778
|
-
}
|
|
1809
|
+
return indent(
|
|
1810
|
+
2,
|
|
1811
|
+
requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition)
|
|
1812
|
+
);
|
|
1779
1813
|
}).join("\n");
|
|
1780
1814
|
} catch (err) {
|
|
1781
1815
|
return indent(2, errorToComment(err, mediaType));
|
|
@@ -1783,6 +1817,8 @@ function requestBodyRenderer(operationDefinition, typeName) {
|
|
|
1783
1817
|
}).filter(R16.isNotNil).join("\n");
|
|
1784
1818
|
return $requestBody;
|
|
1785
1819
|
}
|
|
1820
|
+
|
|
1821
|
+
// src/renderer/operation-request/index.ts
|
|
1786
1822
|
function requestHeadersRenderer(operationDefinition, typeName) {
|
|
1787
1823
|
const { operation } = operationDefinition;
|
|
1788
1824
|
const $headers = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "header").map((p) => ` if (args && ${JSON.stringify(p.name)} in args) req.header(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}])`).concat("").join("\n");
|
|
@@ -1792,7 +1828,7 @@ function requestQueryRenderer(operationDefinition, qs, typeName) {
|
|
|
1792
1828
|
const { operation } = operationDefinition;
|
|
1793
1829
|
const $query = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "query").map((p) => {
|
|
1794
1830
|
const option = qs(p);
|
|
1795
|
-
const $option = !option ||
|
|
1831
|
+
const $option = !option || R17.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
|
|
1796
1832
|
return ` if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
|
|
1797
1833
|
}).concat("").join("\n");
|
|
1798
1834
|
return $query;
|
|
@@ -1824,9 +1860,9 @@ function operationDeclarationRenderer(operationDefinition, typeName) {
|
|
|
1824
1860
|
const { operationId } = operationDefinition;
|
|
1825
1861
|
const mediaTypes = getRequestMediaTypes(operationDefinition);
|
|
1826
1862
|
if (mediaTypes.length === 0) {
|
|
1827
|
-
return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS,
|
|
1863
|
+
return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends never = never>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, CONTENT_TYPE>>`;
|
|
1828
1864
|
} else if (mediaTypes.length === 1) {
|
|
1829
|
-
return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS,
|
|
1865
|
+
return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends ${JSON.stringify(mediaTypes[0])} = ${JSON.stringify(mediaTypes[0])}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, CONTENT_TYPE>>`;
|
|
1830
1866
|
} else if (mediaTypes.length > 1) {
|
|
1831
1867
|
return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends ${typeName("RequestParameters")}["content-type"]>(args?: Extract<${typeName("RequestParameters")}, { "content-type": CONTENT_TYPE }>): Keq<Operation<STATUS, CONTENT_TYPE>>`;
|
|
1832
1868
|
}
|
|
@@ -1838,6 +1874,7 @@ async function operationRequestRenderer(operationDefinition, options) {
|
|
|
1838
1874
|
if (!operation.responses) return "";
|
|
1839
1875
|
const typeName = typeNameFactory(operationDefinition);
|
|
1840
1876
|
const moduleName = operationDefinition.module.name;
|
|
1877
|
+
const $method = method.toLowerCase();
|
|
1841
1878
|
const $queryParameters = requestQueryRenderer(operationDefinition, qs, typeName);
|
|
1842
1879
|
const $headerParameters = requestHeadersRenderer(operationDefinition, typeName);
|
|
1843
1880
|
const $pathParameters = requestPathParametersRenderer(operationDefinition, typeName);
|
|
@@ -1853,7 +1890,7 @@ async function operationRequestRenderer(operationDefinition, options) {
|
|
|
1853
1890
|
"",
|
|
1854
1891
|
"/* @anchor:operation-declaration */",
|
|
1855
1892
|
`export ${$operationDeclaration} {`,
|
|
1856
|
-
` const req = request
|
|
1893
|
+
` const req = request.${$method}<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
|
|
1857
1894
|
" .option('module', { name: moduleName, pathname, method })",
|
|
1858
1895
|
"",
|
|
1859
1896
|
$mediaType || void 0,
|
|
@@ -1874,13 +1911,13 @@ async function operationRequestRenderer(operationDefinition, options) {
|
|
|
1874
1911
|
" /* @anchor:body:end */",
|
|
1875
1912
|
"",
|
|
1876
1913
|
" /* @anchor:operation-return */",
|
|
1877
|
-
` return req as ReturnType<typeof ${operationId}
|
|
1914
|
+
` return req as ReturnType<typeof ${operationId}<STATUS${$operationDeclaration.includes("CONTENT_TYPE") ? ", CONTENT_TYPE" : ""}>>`,
|
|
1878
1915
|
"}",
|
|
1879
1916
|
"",
|
|
1880
1917
|
`${operationId}.pathname = pathname`,
|
|
1881
1918
|
`${operationId}.method = method`,
|
|
1882
1919
|
"/* @anchor:file:end */"
|
|
1883
|
-
].filter(
|
|
1920
|
+
].filter(R17.isNotNil).join("\n");
|
|
1884
1921
|
}
|
|
1885
1922
|
|
|
1886
1923
|
// src/tasks/compile/utils/compile-operation-definition.ts
|
|
@@ -1977,7 +2014,8 @@ async function compileOperationDefinition(options) {
|
|
|
1977
2014
|
"Operation",
|
|
1978
2015
|
typeName("ResponseBodies"),
|
|
1979
2016
|
typeName("RequestParameters")
|
|
1980
|
-
]
|
|
2017
|
+
],
|
|
2018
|
+
{ type: true }
|
|
1981
2019
|
);
|
|
1982
2020
|
artifact.addDependence(
|
|
1983
2021
|
typeArtifact,
|
|
@@ -1986,11 +2024,11 @@ async function compileOperationDefinition(options) {
|
|
|
1986
2024
|
`${typeName("RequestHeaders")}`,
|
|
1987
2025
|
`${typeName("RequestBodies")}`
|
|
1988
2026
|
],
|
|
1989
|
-
{ export: true }
|
|
2027
|
+
{ export: true, type: true }
|
|
1990
2028
|
);
|
|
1991
2029
|
return await compiler.hooks.afterCompileOperationRequest.promise(artifact, operationDefinition, task);
|
|
1992
2030
|
}
|
|
1993
|
-
const artifacts =
|
|
2031
|
+
const artifacts = R18.unnest(
|
|
1994
2032
|
await Promise.all(
|
|
1995
2033
|
operationDefinitions.map(async (operationDefinition) => {
|
|
1996
2034
|
const typeArtifact = await createTypeArtifact(operationDefinition);
|
|
@@ -1999,7 +2037,7 @@ async function compileOperationDefinition(options) {
|
|
|
1999
2037
|
})
|
|
2000
2038
|
)
|
|
2001
2039
|
);
|
|
2002
|
-
const operationDefinitionsGroupByModuleName =
|
|
2040
|
+
const operationDefinitionsGroupByModuleName = R18.groupBy(
|
|
2003
2041
|
(operationDefinition) => operationDefinition.module.name,
|
|
2004
2042
|
operationDefinitions
|
|
2005
2043
|
);
|
|
@@ -2146,7 +2184,7 @@ function createInteractiveTask(options) {
|
|
|
2146
2184
|
}
|
|
2147
2185
|
|
|
2148
2186
|
// src/compiler/intercepter/perfect-error-message.ts
|
|
2149
|
-
var
|
|
2187
|
+
var R19 = __toESM(require("ramda"), 1);
|
|
2150
2188
|
function perfectErrorMessage() {
|
|
2151
2189
|
return {
|
|
2152
2190
|
register: (tap) => {
|
|
@@ -2178,8 +2216,8 @@ function perfectErrorMessage() {
|
|
|
2178
2216
|
}
|
|
2179
2217
|
if (tap.type === "async") {
|
|
2180
2218
|
tap.fn = (...args) => {
|
|
2181
|
-
const callback =
|
|
2182
|
-
return fn(...
|
|
2219
|
+
const callback = R19.last(args);
|
|
2220
|
+
return fn(...R19.init(args), (err, result) => {
|
|
2183
2221
|
prefix(err);
|
|
2184
2222
|
return callback(err, result);
|
|
2185
2223
|
});
|
|
@@ -2270,11 +2308,11 @@ var Compiler = class {
|
|
|
2270
2308
|
[
|
|
2271
2309
|
createSetupTask(this, options),
|
|
2272
2310
|
createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
|
|
2273
|
-
createValidateTask(this
|
|
2311
|
+
createValidateTask(this),
|
|
2274
2312
|
createInteractiveTask({ enabled: !!options.interactive, ...typeof options.interactive === "object" ? options.interactive : { mode: "except" } }),
|
|
2275
2313
|
createShakingTask(this, { enabled: !!options.build, ...typeof options.build === "object" ? options.build.shaking : void 0 }),
|
|
2276
2314
|
createCompileTask(this, { enabled: !!options.build }),
|
|
2277
|
-
createPersistTask(this
|
|
2315
|
+
createPersistTask(this)
|
|
2278
2316
|
],
|
|
2279
2317
|
{
|
|
2280
2318
|
concurrent: false,
|
|
@@ -2360,8 +2398,8 @@ program.command("ignore").addArgument(
|
|
|
2360
2398
|
}
|
|
2361
2399
|
});
|
|
2362
2400
|
} else {
|
|
2363
|
-
if (!options.
|
|
2364
|
-
throw new Error("at least one of '--
|
|
2401
|
+
if (!options.method && !options.pathname) {
|
|
2402
|
+
throw new Error("at least one of '-i --interactive', '--method' or '--pathname' must be specified");
|
|
2365
2403
|
}
|
|
2366
2404
|
const moduleNames = options.module || ["*"];
|
|
2367
2405
|
compiler = new Compiler({
|