@keq-request/cli 5.0.0-alpha.12 → 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.
Files changed (33) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/cli.cjs +94 -52
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +94 -52
  5. package/dist/cli.js.map +1 -1
  6. package/dist/index.cjs +94 -50
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +1 -0
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +93 -50
  11. package/dist/index.js.map +1 -1
  12. package/dist/plugins/body-fallback/index.d.ts.map +1 -1
  13. package/dist/plugins/eslint/index.d.ts.map +1 -1
  14. package/dist/plugins/prettier/index.d.ts.map +1 -1
  15. package/dist/plugins.cjs +13 -21
  16. package/dist/plugins.cjs.map +1 -1
  17. package/dist/plugins.js +13 -21
  18. package/dist/plugins.js.map +1 -1
  19. package/dist/renderer/operation-request/error-to-comment.d.ts +2 -0
  20. package/dist/renderer/operation-request/error-to-comment.d.ts.map +1 -0
  21. package/dist/renderer/operation-request/index.d.ts.map +1 -1
  22. package/dist/renderer/operation-request/request-body.d.ts +4 -0
  23. package/dist/renderer/operation-request/request-body.d.ts.map +1 -0
  24. package/dist/renderer/request/index.d.ts.map +1 -1
  25. package/dist/renderer/utils/generate-schema.d.ts.map +1 -1
  26. package/dist/tasks/compile/index.d.ts.map +1 -1
  27. package/dist/tasks/compile/utils/compile-operation-definition.d.ts.map +1 -1
  28. package/dist/tasks/compile/utils/compile-schema-definition.d.ts.map +1 -1
  29. package/dist/tasks/persist/index.d.ts.map +1 -1
  30. package/dist/tasks/utils/dependency.d.ts +3 -1
  31. package/dist/tasks/utils/dependency.d.ts.map +1 -1
  32. package/dist/types/runtime-config.d.ts +2 -2
  33. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
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
+
12
+ ## 5.0.0-alpha.13
13
+
14
+ ### Patch Changes
15
+
16
+ - 9d481d8: Generation will no longer be skipped when the object schema contains only the `additionalProperties` key.
17
+ - 5150d57: The generated type file has an error when no requestBody definition in swagger.
18
+ - keq@5.0.0-alpha.13
19
+
3
20
  ## 5.0.0-alpha.12
4
21
 
5
22
  ### 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,11 +1234,12 @@ 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
  }
1237
1241
  function generateObject(schema, alias) {
1238
- if (!schema.properties || !Object.keys(schema.properties).length) {
1242
+ if ((!schema.properties || R11.isEmpty(schema.properties)) && (!schema.additionalProperties || R11.isEmpty(schema.additionalProperties))) {
1239
1243
  return "object";
1240
1244
  }
1241
1245
  const $properties = Object.entries(schema.properties || {}).map(([propertyName, propertySchema]) => {
@@ -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
  }
@@ -1595,7 +1606,10 @@ async function compileSchemaDefinition(options) {
1595
1606
  const artifact = new Artifact({
1596
1607
  id: filepath,
1597
1608
  filepath,
1598
- content: ""
1609
+ content: [
1610
+ "/* @anchor:file:start */",
1611
+ "/* @anchor:file:end */"
1612
+ ].join("\n")
1599
1613
  });
1600
1614
  for (const schemaDefinition of schemaDefinitions2 || []) {
1601
1615
  const dependentArtifact = artifacts.find(isArtifactCompiledBy(schemaDefinition));
@@ -1611,7 +1625,7 @@ async function compileSchemaDefinition(options) {
1611
1625
  }
1612
1626
 
1613
1627
  // src/tasks/compile/utils/compile-operation-definition.ts
1614
- var R17 = __toESM(require("ramda"), 1);
1628
+ var R18 = __toESM(require("ramda"), 1);
1615
1629
 
1616
1630
  // src/renderer/operation-type/index.ts
1617
1631
  var R15 = __toESM(require("ramda"), 1);
@@ -1708,7 +1722,7 @@ async function operationTypeRenderer(operationDefinition, alias = R15.identity)
1708
1722
  $parameterBodies || void 0,
1709
1723
  $requestParameters,
1710
1724
  "",
1711
- `export interface Operation<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends keyof ${typeName("ParameterBodies")}> extends KeqOperation {`,
1725
+ `export interface Operation<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends ${$parameterBodies ? `keyof ${typeName("ParameterBodies")}` : "string"} > extends KeqOperation {`,
1712
1726
  ` requestParams: ${typeName("RouteParameters")} & { [key: string]: KeqPathParameterInit }`,
1713
1727
  ` requestQuery: ${typeName("RequestQuery")} & { [key: string]: KeqQueryInit }`,
1714
1728
  ` requestHeaders: ${typeName("RequestHeaders")} & { [key: string]: string | number }`,
@@ -1737,7 +1751,12 @@ function generateParameters(name, parameters, alias) {
1737
1751
  }
1738
1752
 
1739
1753
  // src/renderer/operation-request/index.ts
1754
+ var R17 = __toESM(require("ramda"), 1);
1755
+
1756
+ // src/renderer/operation-request/request-body.ts
1740
1757
  var R16 = __toESM(require("ramda"), 1);
1758
+
1759
+ // src/renderer/operation-request/error-to-comment.ts
1741
1760
  function errorToComment(err, mediaType) {
1742
1761
  const $err = String(err).split("\n").map(((line) => ` * ${line}`)).join("\n");
1743
1762
  return [
@@ -1747,6 +1766,34 @@ function errorToComment(err, mediaType) {
1747
1766
  " */"
1748
1767
  ].join("\n");
1749
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
+ }
1750
1797
  function requestBodyRenderer(operationDefinition, typeName) {
1751
1798
  const { operation } = operationDefinition;
1752
1799
  const requestBodyContent = operation.requestBody?.content || {};
@@ -1757,22 +1804,10 @@ function requestBodyRenderer(operationDefinition, typeName) {
1757
1804
  if (schema.type !== "object") return;
1758
1805
  const properties = schema.properties || {};
1759
1806
  return Object.entries(properties).map(([propertyName, propertySchema]) => {
1760
- const $propertyName = JSON.stringify(propertyName);
1761
- if (mediaType === "application/json") {
1762
- return ` if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
1763
- } else if (mediaType === "multipart/form-data") {
1764
- try {
1765
- const schema2 = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
1766
- if (schema2.type === "string" && schema2.format === "binary" || schema2.contentMediaType === "application/octet-stream") {
1767
- return ` if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
1768
- }
1769
- return ` if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
1770
- } catch (err) {
1771
- return indent(2, errorToComment(err, mediaType));
1772
- }
1773
- } else {
1774
- throw new Error(`Unsupported media type: ${mediaType}`);
1775
- }
1807
+ return indent(
1808
+ 2,
1809
+ requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition)
1810
+ );
1776
1811
  }).join("\n");
1777
1812
  } catch (err) {
1778
1813
  return indent(2, errorToComment(err, mediaType));
@@ -1780,6 +1815,8 @@ function requestBodyRenderer(operationDefinition, typeName) {
1780
1815
  }).filter(R16.isNotNil).join("\n");
1781
1816
  return $requestBody;
1782
1817
  }
1818
+
1819
+ // src/renderer/operation-request/index.ts
1783
1820
  function requestHeadersRenderer(operationDefinition, typeName) {
1784
1821
  const { operation } = operationDefinition;
1785
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");
@@ -1789,7 +1826,7 @@ function requestQueryRenderer(operationDefinition, qs, typeName) {
1789
1826
  const { operation } = operationDefinition;
1790
1827
  const $query = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "query").map((p) => {
1791
1828
  const option = qs(p);
1792
- const $option = !option || R16.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1829
+ const $option = !option || R17.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1793
1830
  return ` if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
1794
1831
  }).concat("").join("\n");
1795
1832
  return $query;
@@ -1821,9 +1858,9 @@ function operationDeclarationRenderer(operationDefinition, typeName) {
1821
1858
  const { operationId } = operationDefinition;
1822
1859
  const mediaTypes = getRequestMediaTypes(operationDefinition);
1823
1860
  if (mediaTypes.length === 0) {
1824
- 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>>`;
1825
1862
  } else if (mediaTypes.length === 1) {
1826
- 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>>`;
1827
1864
  } else if (mediaTypes.length > 1) {
1828
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>>`;
1829
1866
  }
@@ -1835,6 +1872,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1835
1872
  if (!operation.responses) return "";
1836
1873
  const typeName = typeNameFactory(operationDefinition);
1837
1874
  const moduleName = operationDefinition.module.name;
1875
+ const $method = method.toLowerCase();
1838
1876
  const $queryParameters = requestQueryRenderer(operationDefinition, qs, typeName);
1839
1877
  const $headerParameters = requestHeadersRenderer(operationDefinition, typeName);
1840
1878
  const $pathParameters = requestPathParametersRenderer(operationDefinition, typeName);
@@ -1850,7 +1888,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1850
1888
  "",
1851
1889
  "/* @anchor:operation-declaration */",
1852
1890
  `export ${$operationDeclaration} {`,
1853
- ` const req = request.post<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1891
+ ` const req = request.${$method}<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1854
1892
  " .option('module', { name: moduleName, pathname, method })",
1855
1893
  "",
1856
1894
  $mediaType || void 0,
@@ -1871,13 +1909,13 @@ async function operationRequestRenderer(operationDefinition, options) {
1871
1909
  " /* @anchor:body:end */",
1872
1910
  "",
1873
1911
  " /* @anchor:operation-return */",
1874
- ` return req as ReturnType<typeof ${operationId}>`,
1912
+ ` return req as ReturnType<typeof ${operationId}<STATUS${$operationDeclaration.includes("CONTENT_TYPE") ? ", CONTENT_TYPE" : ""}>>`,
1875
1913
  "}",
1876
1914
  "",
1877
1915
  `${operationId}.pathname = pathname`,
1878
1916
  `${operationId}.method = method`,
1879
1917
  "/* @anchor:file:end */"
1880
- ].filter(R16.isNotNil).join("\n");
1918
+ ].filter(R17.isNotNil).join("\n");
1881
1919
  }
1882
1920
 
1883
1921
  // src/tasks/compile/utils/compile-operation-definition.ts
@@ -1974,7 +2012,8 @@ async function compileOperationDefinition(options) {
1974
2012
  "Operation",
1975
2013
  typeName("ResponseBodies"),
1976
2014
  typeName("RequestParameters")
1977
- ]
2015
+ ],
2016
+ { type: true }
1978
2017
  );
1979
2018
  artifact.addDependence(
1980
2019
  typeArtifact,
@@ -1983,11 +2022,11 @@ async function compileOperationDefinition(options) {
1983
2022
  `${typeName("RequestHeaders")}`,
1984
2023
  `${typeName("RequestBodies")}`
1985
2024
  ],
1986
- { export: true }
2025
+ { export: true, type: true }
1987
2026
  );
1988
2027
  return await compiler.hooks.afterCompileOperationRequest.promise(artifact, operationDefinition, task);
1989
2028
  }
1990
- const artifacts = R17.unnest(
2029
+ const artifacts = R18.unnest(
1991
2030
  await Promise.all(
1992
2031
  operationDefinitions.map(async (operationDefinition) => {
1993
2032
  const typeArtifact = await createTypeArtifact(operationDefinition);
@@ -1996,7 +2035,7 @@ async function compileOperationDefinition(options) {
1996
2035
  })
1997
2036
  )
1998
2037
  );
1999
- const operationDefinitionsGroupByModuleName = R17.groupBy(
2038
+ const operationDefinitionsGroupByModuleName = R18.groupBy(
2000
2039
  (operationDefinition) => operationDefinition.module.name,
2001
2040
  operationDefinitions
2002
2041
  );
@@ -2005,7 +2044,10 @@ async function compileOperationDefinition(options) {
2005
2044
  const artifact = new Artifact({
2006
2045
  id: filepath,
2007
2046
  filepath,
2008
- content: ""
2047
+ content: [
2048
+ "/* @anchor:file:start */",
2049
+ "/* @anchor:file:end */"
2050
+ ].join("\n")
2009
2051
  });
2010
2052
  for (const operationDefinition of operationDefinitions2 || []) {
2011
2053
  const dependentArtifact = artifacts.find((artifact2) => artifact2.filepath === genOperationRequestFilepath(operationDefinition));
@@ -2023,14 +2065,12 @@ async function compileOperationDefinition(options) {
2023
2065
  // src/renderer/request/index.ts
2024
2066
  async function requestRenderer() {
2025
2067
  return [
2026
- "import { KeqRequest } from 'keq'",
2027
- "",
2028
2068
  "/* @anchor:file:start */",
2029
2069
  "",
2030
2070
  "/* @anchor:request-declaration */",
2031
2071
  "export const request = new KeqRequest()",
2032
2072
  "",
2033
- "/* @anchor:file:end"
2073
+ "/* @anchor:file:end */"
2034
2074
  ].join("\n");
2035
2075
  }
2036
2076
 
@@ -2043,13 +2083,15 @@ function main6(compiler) {
2043
2083
  const rc = context.setup.rc;
2044
2084
  const matcher = context.setup.matcher;
2045
2085
  const documents = context.shaken.documents.filter((document) => !matcher.isModuleIgnored(document.module));
2046
- const requestArtifact = await compiler.hooks.afterCompileKeqRequest.promise(
2047
- new Artifact({
2048
- id: "request",
2049
- filepath: "request",
2050
- content: await requestRenderer(),
2051
- extensionName: ".ts"
2052
- }),
2086
+ let requestArtifact = new Artifact({
2087
+ id: "request",
2088
+ filepath: "request",
2089
+ content: await requestRenderer(),
2090
+ extensionName: ".ts"
2091
+ });
2092
+ requestArtifact.addDependence("keq", ["KeqRequest"]);
2093
+ requestArtifact = await compiler.hooks.afterCompileKeqRequest.promise(
2094
+ requestArtifact,
2053
2095
  task
2054
2096
  );
2055
2097
  const schemaDefinitions = documents.flatMap((document) => document.schemas);
@@ -2140,7 +2182,7 @@ function createInteractiveTask(options) {
2140
2182
  }
2141
2183
 
2142
2184
  // src/compiler/intercepter/perfect-error-message.ts
2143
- var R18 = __toESM(require("ramda"), 1);
2185
+ var R19 = __toESM(require("ramda"), 1);
2144
2186
  function perfectErrorMessage() {
2145
2187
  return {
2146
2188
  register: (tap) => {
@@ -2172,8 +2214,8 @@ function perfectErrorMessage() {
2172
2214
  }
2173
2215
  if (tap.type === "async") {
2174
2216
  tap.fn = (...args) => {
2175
- const callback = R18.last(args);
2176
- return fn(...R18.init(args), (err, result) => {
2217
+ const callback = R19.last(args);
2218
+ return fn(...R19.init(args), (err, result) => {
2177
2219
  prefix(err);
2178
2220
  return callback(err, result);
2179
2221
  });
@@ -2264,11 +2306,11 @@ var Compiler = class {
2264
2306
  [
2265
2307
  createSetupTask(this, options),
2266
2308
  createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
2267
- createValidateTask(this, { enabled: !!options.build }),
2309
+ createValidateTask(this),
2268
2310
  createInteractiveTask({ enabled: !!options.interactive, ...typeof options.interactive === "object" ? options.interactive : { mode: "except" } }),
2269
2311
  createShakingTask(this, { enabled: !!options.build, ...typeof options.build === "object" ? options.build.shaking : void 0 }),
2270
2312
  createCompileTask(this, { enabled: !!options.build }),
2271
- createPersistTask(this, { enabled: !!options.build })
2313
+ createPersistTask(this)
2272
2314
  ],
2273
2315
  {
2274
2316
  concurrent: false,
@@ -2354,8 +2396,8 @@ program.command("ignore").addArgument(
2354
2396
  }
2355
2397
  });
2356
2398
  } else {
2357
- if (!options.module && !options.method && !options.pathname) {
2358
- 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");
2359
2401
  }
2360
2402
  const moduleNames = options.module || ["*"];
2361
2403
  compiler = new Compiler({