@keq-request/cli 5.0.0-alpha.13 → 5.0.0-alpha.14

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 CHANGED
@@ -1,5 +1,14 @@
1
1
  ## 5.0.0-alpha.6 (2025-09-17)
2
2
 
3
+ ## 5.0.0-alpha.14
4
+
5
+ ### Patch Changes
6
+
7
+ - 0459484: the type exported add type prefix
8
+ - 0459484: unabled add ignore rule by cli
9
+ - 0459484: triggers type coercion when form-data requestBody filed is not string or file
10
+ - keq@5.0.0-alpha.14
11
+
3
12
  ## 5.0.0-alpha.13
4
13
 
5
14
  ### 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) throw new Error("Please run compile task first.");
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
  }
@@ -1405,15 +1409,17 @@ function toComment(msg) {
1405
1409
 
1406
1410
  // src/tasks/utils/dependency.ts
1407
1411
  var DependencyIdentifier = class {
1408
- constructor(name, alias) {
1412
+ constructor(name, alias, type = false) {
1409
1413
  this.name = name;
1410
1414
  this.alias = alias;
1415
+ this.type = type;
1411
1416
  }
1412
1417
  toCode() {
1418
+ const $type = this.type ? "type " : "";
1413
1419
  if (this.alias) {
1414
- return `${this.name} as ${this.alias}`;
1420
+ return `${$type}${this.name} as ${this.alias}`;
1415
1421
  }
1416
- return this.name;
1422
+ return `${$type}${this.name}`;
1417
1423
  }
1418
1424
  };
1419
1425
  var Dependency = class {
@@ -1424,6 +1430,11 @@ var Dependency = class {
1424
1430
  constructor(source, identifiers, belongTo, options) {
1425
1431
  this.source = source;
1426
1432
  this.identifiers = identifiers.map((i) => typeof i === "string" ? new DependencyIdentifier(i) : i);
1433
+ if (options?.type) {
1434
+ for (const identifier of this.identifiers) {
1435
+ identifier.type = true;
1436
+ }
1437
+ }
1427
1438
  this.export = !!options?.export;
1428
1439
  this.belongTo = belongTo;
1429
1440
  }
@@ -1614,7 +1625,7 @@ async function compileSchemaDefinition(options) {
1614
1625
  }
1615
1626
 
1616
1627
  // src/tasks/compile/utils/compile-operation-definition.ts
1617
- var R17 = __toESM(require("ramda"), 1);
1628
+ var R18 = __toESM(require("ramda"), 1);
1618
1629
 
1619
1630
  // src/renderer/operation-type/index.ts
1620
1631
  var R15 = __toESM(require("ramda"), 1);
@@ -1740,7 +1751,12 @@ function generateParameters(name, parameters, alias) {
1740
1751
  }
1741
1752
 
1742
1753
  // src/renderer/operation-request/index.ts
1754
+ var R17 = __toESM(require("ramda"), 1);
1755
+
1756
+ // src/renderer/operation-request/request-body.ts
1743
1757
  var R16 = __toESM(require("ramda"), 1);
1758
+
1759
+ // src/renderer/operation-request/error-to-comment.ts
1744
1760
  function errorToComment(err, mediaType) {
1745
1761
  const $err = String(err).split("\n").map(((line) => ` * ${line}`)).join("\n");
1746
1762
  return [
@@ -1750,6 +1766,34 @@ function errorToComment(err, mediaType) {
1750
1766
  " */"
1751
1767
  ].join("\n");
1752
1768
  }
1769
+
1770
+ // src/renderer/operation-request/request-body.ts
1771
+ function requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
1772
+ try {
1773
+ const $propertyName = JSON.stringify(propertyName);
1774
+ const schema = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
1775
+ if (schema.type === "string" && schema.format === "binary" || schema.contentMediaType === "application/octet-stream") {
1776
+ return `if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
1777
+ } else if (schema.type === "string" || schema.type === "array" && schema.items && schema.items.type === "string") {
1778
+ return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
1779
+ } else if (schema.type === "number" || schema.type === "integer") {
1780
+ return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, String(args[${$propertyName}]))`;
1781
+ }
1782
+ 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 */)`;
1783
+ } catch (err) {
1784
+ return errorToComment(err, mediaType);
1785
+ }
1786
+ }
1787
+ function requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
1788
+ if (mediaType === "application/json") {
1789
+ const $propertyName = JSON.stringify(propertyName);
1790
+ return `if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
1791
+ } else if (mediaType === "multipart/form-data") {
1792
+ return requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition);
1793
+ } else {
1794
+ throw new Error(`Unsupported media type: ${mediaType}`);
1795
+ }
1796
+ }
1753
1797
  function requestBodyRenderer(operationDefinition, typeName) {
1754
1798
  const { operation } = operationDefinition;
1755
1799
  const requestBodyContent = operation.requestBody?.content || {};
@@ -1760,22 +1804,10 @@ function requestBodyRenderer(operationDefinition, typeName) {
1760
1804
  if (schema.type !== "object") return;
1761
1805
  const properties = schema.properties || {};
1762
1806
  return Object.entries(properties).map(([propertyName, propertySchema]) => {
1763
- const $propertyName = JSON.stringify(propertyName);
1764
- if (mediaType === "application/json") {
1765
- return ` if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
1766
- } else if (mediaType === "multipart/form-data") {
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
- }
1807
+ return indent(
1808
+ 2,
1809
+ requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition)
1810
+ );
1779
1811
  }).join("\n");
1780
1812
  } catch (err) {
1781
1813
  return indent(2, errorToComment(err, mediaType));
@@ -1783,6 +1815,8 @@ function requestBodyRenderer(operationDefinition, typeName) {
1783
1815
  }).filter(R16.isNotNil).join("\n");
1784
1816
  return $requestBody;
1785
1817
  }
1818
+
1819
+ // src/renderer/operation-request/index.ts
1786
1820
  function requestHeadersRenderer(operationDefinition, typeName) {
1787
1821
  const { operation } = operationDefinition;
1788
1822
  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 +1826,7 @@ function requestQueryRenderer(operationDefinition, qs, typeName) {
1792
1826
  const { operation } = operationDefinition;
1793
1827
  const $query = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "query").map((p) => {
1794
1828
  const option = qs(p);
1795
- const $option = !option || R16.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1829
+ const $option = !option || R17.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1796
1830
  return ` if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
1797
1831
  }).concat("").join("\n");
1798
1832
  return $query;
@@ -1824,9 +1858,9 @@ function operationDeclarationRenderer(operationDefinition, typeName) {
1824
1858
  const { operationId } = operationDefinition;
1825
1859
  const mediaTypes = getRequestMediaTypes(operationDefinition);
1826
1860
  if (mediaTypes.length === 0) {
1827
- return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, never>>`;
1861
+ return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends never = never>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, CONTENT_TYPE>>`;
1828
1862
  } else if (mediaTypes.length === 1) {
1829
- return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, ${JSON.stringify(mediaTypes[0])}>>`;
1863
+ 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
1864
  } else if (mediaTypes.length > 1) {
1831
1865
  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
1866
  }
@@ -1838,6 +1872,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1838
1872
  if (!operation.responses) return "";
1839
1873
  const typeName = typeNameFactory(operationDefinition);
1840
1874
  const moduleName = operationDefinition.module.name;
1875
+ const $method = method.toLowerCase();
1841
1876
  const $queryParameters = requestQueryRenderer(operationDefinition, qs, typeName);
1842
1877
  const $headerParameters = requestHeadersRenderer(operationDefinition, typeName);
1843
1878
  const $pathParameters = requestPathParametersRenderer(operationDefinition, typeName);
@@ -1853,7 +1888,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1853
1888
  "",
1854
1889
  "/* @anchor:operation-declaration */",
1855
1890
  `export ${$operationDeclaration} {`,
1856
- ` const req = request.post<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1891
+ ` const req = request.${$method}<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1857
1892
  " .option('module', { name: moduleName, pathname, method })",
1858
1893
  "",
1859
1894
  $mediaType || void 0,
@@ -1874,13 +1909,13 @@ async function operationRequestRenderer(operationDefinition, options) {
1874
1909
  " /* @anchor:body:end */",
1875
1910
  "",
1876
1911
  " /* @anchor:operation-return */",
1877
- ` return req as ReturnType<typeof ${operationId}>`,
1912
+ ` return req as ReturnType<typeof ${operationId}<STATUS${$operationDeclaration.includes("CONTENT_TYPE") ? ", CONTENT_TYPE" : ""}>>`,
1878
1913
  "}",
1879
1914
  "",
1880
1915
  `${operationId}.pathname = pathname`,
1881
1916
  `${operationId}.method = method`,
1882
1917
  "/* @anchor:file:end */"
1883
- ].filter(R16.isNotNil).join("\n");
1918
+ ].filter(R17.isNotNil).join("\n");
1884
1919
  }
1885
1920
 
1886
1921
  // src/tasks/compile/utils/compile-operation-definition.ts
@@ -1977,7 +2012,8 @@ async function compileOperationDefinition(options) {
1977
2012
  "Operation",
1978
2013
  typeName("ResponseBodies"),
1979
2014
  typeName("RequestParameters")
1980
- ]
2015
+ ],
2016
+ { type: true }
1981
2017
  );
1982
2018
  artifact.addDependence(
1983
2019
  typeArtifact,
@@ -1986,11 +2022,11 @@ async function compileOperationDefinition(options) {
1986
2022
  `${typeName("RequestHeaders")}`,
1987
2023
  `${typeName("RequestBodies")}`
1988
2024
  ],
1989
- { export: true }
2025
+ { export: true, type: true }
1990
2026
  );
1991
2027
  return await compiler.hooks.afterCompileOperationRequest.promise(artifact, operationDefinition, task);
1992
2028
  }
1993
- const artifacts = R17.unnest(
2029
+ const artifacts = R18.unnest(
1994
2030
  await Promise.all(
1995
2031
  operationDefinitions.map(async (operationDefinition) => {
1996
2032
  const typeArtifact = await createTypeArtifact(operationDefinition);
@@ -1999,7 +2035,7 @@ async function compileOperationDefinition(options) {
1999
2035
  })
2000
2036
  )
2001
2037
  );
2002
- const operationDefinitionsGroupByModuleName = R17.groupBy(
2038
+ const operationDefinitionsGroupByModuleName = R18.groupBy(
2003
2039
  (operationDefinition) => operationDefinition.module.name,
2004
2040
  operationDefinitions
2005
2041
  );
@@ -2146,7 +2182,7 @@ function createInteractiveTask(options) {
2146
2182
  }
2147
2183
 
2148
2184
  // src/compiler/intercepter/perfect-error-message.ts
2149
- var R18 = __toESM(require("ramda"), 1);
2185
+ var R19 = __toESM(require("ramda"), 1);
2150
2186
  function perfectErrorMessage() {
2151
2187
  return {
2152
2188
  register: (tap) => {
@@ -2178,8 +2214,8 @@ function perfectErrorMessage() {
2178
2214
  }
2179
2215
  if (tap.type === "async") {
2180
2216
  tap.fn = (...args) => {
2181
- const callback = R18.last(args);
2182
- return fn(...R18.init(args), (err, result) => {
2217
+ const callback = R19.last(args);
2218
+ return fn(...R19.init(args), (err, result) => {
2183
2219
  prefix(err);
2184
2220
  return callback(err, result);
2185
2221
  });
@@ -2270,11 +2306,11 @@ var Compiler = class {
2270
2306
  [
2271
2307
  createSetupTask(this, options),
2272
2308
  createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
2273
- createValidateTask(this, { enabled: !!options.build }),
2309
+ createValidateTask(this),
2274
2310
  createInteractiveTask({ enabled: !!options.interactive, ...typeof options.interactive === "object" ? options.interactive : { mode: "except" } }),
2275
2311
  createShakingTask(this, { enabled: !!options.build, ...typeof options.build === "object" ? options.build.shaking : void 0 }),
2276
2312
  createCompileTask(this, { enabled: !!options.build }),
2277
- createPersistTask(this, { enabled: !!options.build })
2313
+ createPersistTask(this)
2278
2314
  ],
2279
2315
  {
2280
2316
  concurrent: false,
@@ -2360,8 +2396,8 @@ program.command("ignore").addArgument(
2360
2396
  }
2361
2397
  });
2362
2398
  } else {
2363
- if (!options.module && !options.method && !options.pathname) {
2364
- throw new Error("at least one of '--module', '--method' or '--pathname' must be specified");
2399
+ if (!options.method && !options.pathname) {
2400
+ throw new Error("at least one of '-i --interactive', '--method' or '--pathname' must be specified");
2365
2401
  }
2366
2402
  const moduleNames = options.module || ["*"];
2367
2403
  compiler = new Compiler({