@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/dist/index.cjs CHANGED
@@ -1122,7 +1122,10 @@ function createPersistArtifactTask() {
1122
1122
  title: "Write files",
1123
1123
  task: async (context, task) => {
1124
1124
  if (!context.setup) throw new Error("Please run setup task first.");
1125
- if (!context.compiled) throw new Error("Please run compile task first.");
1125
+ if (!context.compiled) {
1126
+ task.skip("No compiled artifacts to persist.");
1127
+ return;
1128
+ }
1126
1129
  const rc = context.setup.rc;
1127
1130
  const artifacts = context.compiled.artifacts;
1128
1131
  for (const artifact of artifacts) {
@@ -1253,6 +1256,7 @@ function generateArray(schema, alias) {
1253
1256
  return "any[]";
1254
1257
  }
1255
1258
  function indent(space, text) {
1259
+ if (text === "") return "";
1256
1260
  const indentation = " ".repeat(space);
1257
1261
  return text.split("\n").map((line) => `${indentation}${line}`).join("\n");
1258
1262
  }
@@ -1427,15 +1431,17 @@ function toComment(msg) {
1427
1431
 
1428
1432
  // src/tasks/utils/dependency.ts
1429
1433
  var DependencyIdentifier = class {
1430
- constructor(name, alias) {
1434
+ constructor(name, alias, type = false) {
1431
1435
  this.name = name;
1432
1436
  this.alias = alias;
1437
+ this.type = type;
1433
1438
  }
1434
1439
  toCode() {
1440
+ const $type = this.type ? "type " : "";
1435
1441
  if (this.alias) {
1436
- return `${this.name} as ${this.alias}`;
1442
+ return `${$type}${this.name} as ${this.alias}`;
1437
1443
  }
1438
- return this.name;
1444
+ return `${$type}${this.name}`;
1439
1445
  }
1440
1446
  };
1441
1447
  var Dependency = class {
@@ -1446,6 +1452,11 @@ var Dependency = class {
1446
1452
  constructor(source, identifiers, belongTo, options) {
1447
1453
  this.source = source;
1448
1454
  this.identifiers = identifiers.map((i) => typeof i === "string" ? new DependencyIdentifier(i) : i);
1455
+ if (options?.type) {
1456
+ for (const identifier of this.identifiers) {
1457
+ identifier.type = true;
1458
+ }
1459
+ }
1449
1460
  this.export = !!options?.export;
1450
1461
  this.belongTo = belongTo;
1451
1462
  }
@@ -1636,7 +1647,7 @@ async function compileSchemaDefinition(options) {
1636
1647
  }
1637
1648
 
1638
1649
  // src/tasks/compile/utils/compile-operation-definition.ts
1639
- var R17 = __toESM(require("ramda"), 1);
1650
+ var R18 = __toESM(require("ramda"), 1);
1640
1651
 
1641
1652
  // src/renderer/operation-type/index.ts
1642
1653
  var R15 = __toESM(require("ramda"), 1);
@@ -1762,7 +1773,12 @@ function generateParameters(name, parameters, alias) {
1762
1773
  }
1763
1774
 
1764
1775
  // src/renderer/operation-request/index.ts
1776
+ var R17 = __toESM(require("ramda"), 1);
1777
+
1778
+ // src/renderer/operation-request/request-body.ts
1765
1779
  var R16 = __toESM(require("ramda"), 1);
1780
+
1781
+ // src/renderer/operation-request/error-to-comment.ts
1766
1782
  function errorToComment(err, mediaType) {
1767
1783
  const $err = String(err).split("\n").map(((line) => ` * ${line}`)).join("\n");
1768
1784
  return [
@@ -1772,6 +1788,34 @@ function errorToComment(err, mediaType) {
1772
1788
  " */"
1773
1789
  ].join("\n");
1774
1790
  }
1791
+
1792
+ // src/renderer/operation-request/request-body.ts
1793
+ function requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
1794
+ try {
1795
+ const $propertyName = JSON.stringify(propertyName);
1796
+ const schema = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
1797
+ if (schema.type === "string" && schema.format === "binary" || schema.contentMediaType === "application/octet-stream") {
1798
+ return `if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
1799
+ } else if (schema.type === "string" || schema.type === "array" && schema.items && schema.items.type === "string") {
1800
+ return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
1801
+ } else if (schema.type === "number" || schema.type === "integer") {
1802
+ return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, String(args[${$propertyName}]))`;
1803
+ }
1804
+ 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 */)`;
1805
+ } catch (err) {
1806
+ return errorToComment(err, mediaType);
1807
+ }
1808
+ }
1809
+ function requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
1810
+ if (mediaType === "application/json") {
1811
+ const $propertyName = JSON.stringify(propertyName);
1812
+ return `if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
1813
+ } else if (mediaType === "multipart/form-data") {
1814
+ return requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition);
1815
+ } else {
1816
+ throw new Error(`Unsupported media type: ${mediaType}`);
1817
+ }
1818
+ }
1775
1819
  function requestBodyRenderer(operationDefinition, typeName) {
1776
1820
  const { operation } = operationDefinition;
1777
1821
  const requestBodyContent = operation.requestBody?.content || {};
@@ -1782,22 +1826,10 @@ function requestBodyRenderer(operationDefinition, typeName) {
1782
1826
  if (schema.type !== "object") return;
1783
1827
  const properties = schema.properties || {};
1784
1828
  return Object.entries(properties).map(([propertyName, propertySchema]) => {
1785
- const $propertyName = JSON.stringify(propertyName);
1786
- if (mediaType === "application/json") {
1787
- return ` if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
1788
- } else if (mediaType === "multipart/form-data") {
1789
- try {
1790
- const schema2 = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
1791
- if (schema2.type === "string" && schema2.format === "binary" || schema2.contentMediaType === "application/octet-stream") {
1792
- return ` if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
1793
- }
1794
- return ` if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
1795
- } catch (err) {
1796
- return indent(2, errorToComment(err, mediaType));
1797
- }
1798
- } else {
1799
- throw new Error(`Unsupported media type: ${mediaType}`);
1800
- }
1829
+ return indent(
1830
+ 2,
1831
+ requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition)
1832
+ );
1801
1833
  }).join("\n");
1802
1834
  } catch (err) {
1803
1835
  return indent(2, errorToComment(err, mediaType));
@@ -1805,6 +1837,8 @@ function requestBodyRenderer(operationDefinition, typeName) {
1805
1837
  }).filter(R16.isNotNil).join("\n");
1806
1838
  return $requestBody;
1807
1839
  }
1840
+
1841
+ // src/renderer/operation-request/index.ts
1808
1842
  function requestHeadersRenderer(operationDefinition, typeName) {
1809
1843
  const { operation } = operationDefinition;
1810
1844
  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");
@@ -1814,7 +1848,7 @@ function requestQueryRenderer(operationDefinition, qs, typeName) {
1814
1848
  const { operation } = operationDefinition;
1815
1849
  const $query = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "query").map((p) => {
1816
1850
  const option = qs(p);
1817
- const $option = !option || R16.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1851
+ const $option = !option || R17.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1818
1852
  return ` if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
1819
1853
  }).concat("").join("\n");
1820
1854
  return $query;
@@ -1846,9 +1880,9 @@ function operationDeclarationRenderer(operationDefinition, typeName) {
1846
1880
  const { operationId } = operationDefinition;
1847
1881
  const mediaTypes = getRequestMediaTypes(operationDefinition);
1848
1882
  if (mediaTypes.length === 0) {
1849
- return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, never>>`;
1883
+ return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends never = never>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, CONTENT_TYPE>>`;
1850
1884
  } else if (mediaTypes.length === 1) {
1851
- return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, ${JSON.stringify(mediaTypes[0])}>>`;
1885
+ 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>>`;
1852
1886
  } else if (mediaTypes.length > 1) {
1853
1887
  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>>`;
1854
1888
  }
@@ -1860,6 +1894,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1860
1894
  if (!operation.responses) return "";
1861
1895
  const typeName = typeNameFactory(operationDefinition);
1862
1896
  const moduleName = operationDefinition.module.name;
1897
+ const $method = method.toLowerCase();
1863
1898
  const $queryParameters = requestQueryRenderer(operationDefinition, qs, typeName);
1864
1899
  const $headerParameters = requestHeadersRenderer(operationDefinition, typeName);
1865
1900
  const $pathParameters = requestPathParametersRenderer(operationDefinition, typeName);
@@ -1875,7 +1910,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1875
1910
  "",
1876
1911
  "/* @anchor:operation-declaration */",
1877
1912
  `export ${$operationDeclaration} {`,
1878
- ` const req = request.post<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1913
+ ` const req = request.${$method}<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1879
1914
  " .option('module', { name: moduleName, pathname, method })",
1880
1915
  "",
1881
1916
  $mediaType || void 0,
@@ -1896,13 +1931,13 @@ async function operationRequestRenderer(operationDefinition, options) {
1896
1931
  " /* @anchor:body:end */",
1897
1932
  "",
1898
1933
  " /* @anchor:operation-return */",
1899
- ` return req as ReturnType<typeof ${operationId}>`,
1934
+ ` return req as ReturnType<typeof ${operationId}<STATUS${$operationDeclaration.includes("CONTENT_TYPE") ? ", CONTENT_TYPE" : ""}>>`,
1900
1935
  "}",
1901
1936
  "",
1902
1937
  `${operationId}.pathname = pathname`,
1903
1938
  `${operationId}.method = method`,
1904
1939
  "/* @anchor:file:end */"
1905
- ].filter(R16.isNotNil).join("\n");
1940
+ ].filter(R17.isNotNil).join("\n");
1906
1941
  }
1907
1942
 
1908
1943
  // src/tasks/compile/utils/compile-operation-definition.ts
@@ -1999,7 +2034,8 @@ async function compileOperationDefinition(options) {
1999
2034
  "Operation",
2000
2035
  typeName("ResponseBodies"),
2001
2036
  typeName("RequestParameters")
2002
- ]
2037
+ ],
2038
+ { type: true }
2003
2039
  );
2004
2040
  artifact.addDependence(
2005
2041
  typeArtifact,
@@ -2008,11 +2044,11 @@ async function compileOperationDefinition(options) {
2008
2044
  `${typeName("RequestHeaders")}`,
2009
2045
  `${typeName("RequestBodies")}`
2010
2046
  ],
2011
- { export: true }
2047
+ { export: true, type: true }
2012
2048
  );
2013
2049
  return await compiler.hooks.afterCompileOperationRequest.promise(artifact, operationDefinition, task);
2014
2050
  }
2015
- const artifacts = R17.unnest(
2051
+ const artifacts = R18.unnest(
2016
2052
  await Promise.all(
2017
2053
  operationDefinitions.map(async (operationDefinition) => {
2018
2054
  const typeArtifact = await createTypeArtifact(operationDefinition);
@@ -2021,7 +2057,7 @@ async function compileOperationDefinition(options) {
2021
2057
  })
2022
2058
  )
2023
2059
  );
2024
- const operationDefinitionsGroupByModuleName = R17.groupBy(
2060
+ const operationDefinitionsGroupByModuleName = R18.groupBy(
2025
2061
  (operationDefinition) => operationDefinition.module.name,
2026
2062
  operationDefinitions
2027
2063
  );
@@ -2168,7 +2204,7 @@ function createInteractiveTask(options) {
2168
2204
  }
2169
2205
 
2170
2206
  // src/compiler/intercepter/perfect-error-message.ts
2171
- var R18 = __toESM(require("ramda"), 1);
2207
+ var R19 = __toESM(require("ramda"), 1);
2172
2208
  function perfectErrorMessage() {
2173
2209
  return {
2174
2210
  register: (tap) => {
@@ -2200,8 +2236,8 @@ function perfectErrorMessage() {
2200
2236
  }
2201
2237
  if (tap.type === "async") {
2202
2238
  tap.fn = (...args) => {
2203
- const callback = R18.last(args);
2204
- return fn(...R18.init(args), (err, result) => {
2239
+ const callback = R19.last(args);
2240
+ return fn(...R19.init(args), (err, result) => {
2205
2241
  prefix(err);
2206
2242
  return callback(err, result);
2207
2243
  });
@@ -2292,11 +2328,11 @@ var Compiler = class {
2292
2328
  [
2293
2329
  createSetupTask(this, options),
2294
2330
  createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
2295
- createValidateTask(this, { enabled: !!options.build }),
2331
+ createValidateTask(this),
2296
2332
  createInteractiveTask({ enabled: !!options.interactive, ...typeof options.interactive === "object" ? options.interactive : { mode: "except" } }),
2297
2333
  createShakingTask(this, { enabled: !!options.build, ...typeof options.build === "object" ? options.build.shaking : void 0 }),
2298
2334
  createCompileTask(this, { enabled: !!options.build }),
2299
- createPersistTask(this, { enabled: !!options.build })
2335
+ createPersistTask(this)
2300
2336
  ],
2301
2337
  {
2302
2338
  concurrent: false,