@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/cli.js CHANGED
@@ -1077,7 +1077,10 @@ function createPersistArtifactTask() {
1077
1077
  title: "Write files",
1078
1078
  task: async (context, task) => {
1079
1079
  if (!context.setup) throw new Error("Please run setup task first.");
1080
- if (!context.compiled) throw new Error("Please run compile task first.");
1080
+ if (!context.compiled) {
1081
+ task.skip("No compiled artifacts to persist.");
1082
+ return;
1083
+ }
1081
1084
  const rc = context.setup.rc;
1082
1085
  const artifacts = context.compiled.artifacts;
1083
1086
  for (const artifact of artifacts) {
@@ -1208,6 +1211,7 @@ function generateArray(schema, alias) {
1208
1211
  return "any[]";
1209
1212
  }
1210
1213
  function indent(space, text) {
1214
+ if (text === "") return "";
1211
1215
  const indentation = " ".repeat(space);
1212
1216
  return text.split("\n").map((line) => `${indentation}${line}`).join("\n");
1213
1217
  }
@@ -1382,15 +1386,17 @@ function toComment(msg) {
1382
1386
 
1383
1387
  // src/tasks/utils/dependency.ts
1384
1388
  var DependencyIdentifier = class {
1385
- constructor(name, alias) {
1389
+ constructor(name, alias, type = false) {
1386
1390
  this.name = name;
1387
1391
  this.alias = alias;
1392
+ this.type = type;
1388
1393
  }
1389
1394
  toCode() {
1395
+ const $type = this.type ? "type " : "";
1390
1396
  if (this.alias) {
1391
- return `${this.name} as ${this.alias}`;
1397
+ return `${$type}${this.name} as ${this.alias}`;
1392
1398
  }
1393
- return this.name;
1399
+ return `${$type}${this.name}`;
1394
1400
  }
1395
1401
  };
1396
1402
  var Dependency = class {
@@ -1401,6 +1407,11 @@ var Dependency = class {
1401
1407
  constructor(source, identifiers, belongTo, options) {
1402
1408
  this.source = source;
1403
1409
  this.identifiers = identifiers.map((i) => typeof i === "string" ? new DependencyIdentifier(i) : i);
1410
+ if (options?.type) {
1411
+ for (const identifier of this.identifiers) {
1412
+ identifier.type = true;
1413
+ }
1414
+ }
1404
1415
  this.export = !!options?.export;
1405
1416
  this.belongTo = belongTo;
1406
1417
  }
@@ -1591,7 +1602,7 @@ async function compileSchemaDefinition(options) {
1591
1602
  }
1592
1603
 
1593
1604
  // src/tasks/compile/utils/compile-operation-definition.ts
1594
- import * as R17 from "ramda";
1605
+ import * as R18 from "ramda";
1595
1606
 
1596
1607
  // src/renderer/operation-type/index.ts
1597
1608
  import * as R15 from "ramda";
@@ -1717,7 +1728,12 @@ function generateParameters(name, parameters, alias) {
1717
1728
  }
1718
1729
 
1719
1730
  // src/renderer/operation-request/index.ts
1731
+ import * as R17 from "ramda";
1732
+
1733
+ // src/renderer/operation-request/request-body.ts
1720
1734
  import * as R16 from "ramda";
1735
+
1736
+ // src/renderer/operation-request/error-to-comment.ts
1721
1737
  function errorToComment(err, mediaType) {
1722
1738
  const $err = String(err).split("\n").map(((line) => ` * ${line}`)).join("\n");
1723
1739
  return [
@@ -1727,6 +1743,34 @@ function errorToComment(err, mediaType) {
1727
1743
  " */"
1728
1744
  ].join("\n");
1729
1745
  }
1746
+
1747
+ // src/renderer/operation-request/request-body.ts
1748
+ function requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
1749
+ try {
1750
+ const $propertyName = JSON.stringify(propertyName);
1751
+ const schema = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
1752
+ if (schema.type === "string" && schema.format === "binary" || schema.contentMediaType === "application/octet-stream") {
1753
+ return `if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
1754
+ } else if (schema.type === "string" || schema.type === "array" && schema.items && schema.items.type === "string") {
1755
+ return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
1756
+ } else if (schema.type === "number" || schema.type === "integer") {
1757
+ return `if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, String(args[${$propertyName}]))`;
1758
+ }
1759
+ 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 */)`;
1760
+ } catch (err) {
1761
+ return errorToComment(err, mediaType);
1762
+ }
1763
+ }
1764
+ function requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition) {
1765
+ if (mediaType === "application/json") {
1766
+ const $propertyName = JSON.stringify(propertyName);
1767
+ return `if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
1768
+ } else if (mediaType === "multipart/form-data") {
1769
+ return requestBodyFormDataPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition);
1770
+ } else {
1771
+ throw new Error(`Unsupported media type: ${mediaType}`);
1772
+ }
1773
+ }
1730
1774
  function requestBodyRenderer(operationDefinition, typeName) {
1731
1775
  const { operation } = operationDefinition;
1732
1776
  const requestBodyContent = operation.requestBody?.content || {};
@@ -1737,22 +1781,10 @@ function requestBodyRenderer(operationDefinition, typeName) {
1737
1781
  if (schema.type !== "object") return;
1738
1782
  const properties = schema.properties || {};
1739
1783
  return Object.entries(properties).map(([propertyName, propertySchema]) => {
1740
- const $propertyName = JSON.stringify(propertyName);
1741
- if (mediaType === "application/json") {
1742
- return ` if (args && ${$propertyName} in args) req.send({ ${$propertyName}: args[${$propertyName}] })`;
1743
- } else if (mediaType === "multipart/form-data") {
1744
- try {
1745
- const schema2 = JsonSchemaUtils.isRef(propertySchema) ? SwaggerUtils.dereferenceDeep(propertySchema.$ref, operationDefinition.document.swagger) : propertySchema;
1746
- if (schema2.type === "string" && schema2.format === "binary" || schema2.contentMediaType === "application/octet-stream") {
1747
- return ` if (args && ${$propertyName} in args && args[${$propertyName}]) req.attach(${$propertyName}, args[${$propertyName}])`;
1748
- }
1749
- return ` if (args && ${$propertyName} in args && args[${$propertyName}] !== undefined) req.field(${$propertyName}, args[${$propertyName}])`;
1750
- } catch (err) {
1751
- return indent(2, errorToComment(err, mediaType));
1752
- }
1753
- } else {
1754
- throw new Error(`Unsupported media type: ${mediaType}`);
1755
- }
1784
+ return indent(
1785
+ 2,
1786
+ requestBodyPropertyRenderer(propertyName, propertySchema, mediaType, operationDefinition)
1787
+ );
1756
1788
  }).join("\n");
1757
1789
  } catch (err) {
1758
1790
  return indent(2, errorToComment(err, mediaType));
@@ -1760,6 +1792,8 @@ function requestBodyRenderer(operationDefinition, typeName) {
1760
1792
  }).filter(R16.isNotNil).join("\n");
1761
1793
  return $requestBody;
1762
1794
  }
1795
+
1796
+ // src/renderer/operation-request/index.ts
1763
1797
  function requestHeadersRenderer(operationDefinition, typeName) {
1764
1798
  const { operation } = operationDefinition;
1765
1799
  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");
@@ -1769,7 +1803,7 @@ function requestQueryRenderer(operationDefinition, qs, typeName) {
1769
1803
  const { operation } = operationDefinition;
1770
1804
  const $query = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "query").map((p) => {
1771
1805
  const option = qs(p);
1772
- const $option = !option || R16.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1806
+ const $option = !option || R17.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1773
1807
  return ` if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
1774
1808
  }).concat("").join("\n");
1775
1809
  return $query;
@@ -1801,9 +1835,9 @@ function operationDeclarationRenderer(operationDefinition, typeName) {
1801
1835
  const { operationId } = operationDefinition;
1802
1836
  const mediaTypes = getRequestMediaTypes(operationDefinition);
1803
1837
  if (mediaTypes.length === 0) {
1804
- return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, never>>`;
1838
+ return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends never = never>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, CONTENT_TYPE>>`;
1805
1839
  } else if (mediaTypes.length === 1) {
1806
- return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, ${JSON.stringify(mediaTypes[0])}>>`;
1840
+ 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>>`;
1807
1841
  } else if (mediaTypes.length > 1) {
1808
1842
  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>>`;
1809
1843
  }
@@ -1815,6 +1849,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1815
1849
  if (!operation.responses) return "";
1816
1850
  const typeName = typeNameFactory(operationDefinition);
1817
1851
  const moduleName = operationDefinition.module.name;
1852
+ const $method = method.toLowerCase();
1818
1853
  const $queryParameters = requestQueryRenderer(operationDefinition, qs, typeName);
1819
1854
  const $headerParameters = requestHeadersRenderer(operationDefinition, typeName);
1820
1855
  const $pathParameters = requestPathParametersRenderer(operationDefinition, typeName);
@@ -1830,7 +1865,7 @@ async function operationRequestRenderer(operationDefinition, options) {
1830
1865
  "",
1831
1866
  "/* @anchor:operation-declaration */",
1832
1867
  `export ${$operationDeclaration} {`,
1833
- ` const req = request.post<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1868
+ ` const req = request.${$method}<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1834
1869
  " .option('module', { name: moduleName, pathname, method })",
1835
1870
  "",
1836
1871
  $mediaType || void 0,
@@ -1851,13 +1886,13 @@ async function operationRequestRenderer(operationDefinition, options) {
1851
1886
  " /* @anchor:body:end */",
1852
1887
  "",
1853
1888
  " /* @anchor:operation-return */",
1854
- ` return req as ReturnType<typeof ${operationId}>`,
1889
+ ` return req as ReturnType<typeof ${operationId}<STATUS${$operationDeclaration.includes("CONTENT_TYPE") ? ", CONTENT_TYPE" : ""}>>`,
1855
1890
  "}",
1856
1891
  "",
1857
1892
  `${operationId}.pathname = pathname`,
1858
1893
  `${operationId}.method = method`,
1859
1894
  "/* @anchor:file:end */"
1860
- ].filter(R16.isNotNil).join("\n");
1895
+ ].filter(R17.isNotNil).join("\n");
1861
1896
  }
1862
1897
 
1863
1898
  // src/tasks/compile/utils/compile-operation-definition.ts
@@ -1954,7 +1989,8 @@ async function compileOperationDefinition(options) {
1954
1989
  "Operation",
1955
1990
  typeName("ResponseBodies"),
1956
1991
  typeName("RequestParameters")
1957
- ]
1992
+ ],
1993
+ { type: true }
1958
1994
  );
1959
1995
  artifact.addDependence(
1960
1996
  typeArtifact,
@@ -1963,11 +1999,11 @@ async function compileOperationDefinition(options) {
1963
1999
  `${typeName("RequestHeaders")}`,
1964
2000
  `${typeName("RequestBodies")}`
1965
2001
  ],
1966
- { export: true }
2002
+ { export: true, type: true }
1967
2003
  );
1968
2004
  return await compiler.hooks.afterCompileOperationRequest.promise(artifact, operationDefinition, task);
1969
2005
  }
1970
- const artifacts = R17.unnest(
2006
+ const artifacts = R18.unnest(
1971
2007
  await Promise.all(
1972
2008
  operationDefinitions.map(async (operationDefinition) => {
1973
2009
  const typeArtifact = await createTypeArtifact(operationDefinition);
@@ -1976,7 +2012,7 @@ async function compileOperationDefinition(options) {
1976
2012
  })
1977
2013
  )
1978
2014
  );
1979
- const operationDefinitionsGroupByModuleName = R17.groupBy(
2015
+ const operationDefinitionsGroupByModuleName = R18.groupBy(
1980
2016
  (operationDefinition) => operationDefinition.module.name,
1981
2017
  operationDefinitions
1982
2018
  );
@@ -2123,7 +2159,7 @@ function createInteractiveTask(options) {
2123
2159
  }
2124
2160
 
2125
2161
  // src/compiler/intercepter/perfect-error-message.ts
2126
- import * as R18 from "ramda";
2162
+ import * as R19 from "ramda";
2127
2163
  function perfectErrorMessage() {
2128
2164
  return {
2129
2165
  register: (tap) => {
@@ -2155,8 +2191,8 @@ function perfectErrorMessage() {
2155
2191
  }
2156
2192
  if (tap.type === "async") {
2157
2193
  tap.fn = (...args) => {
2158
- const callback = R18.last(args);
2159
- return fn(...R18.init(args), (err, result) => {
2194
+ const callback = R19.last(args);
2195
+ return fn(...R19.init(args), (err, result) => {
2160
2196
  prefix(err);
2161
2197
  return callback(err, result);
2162
2198
  });
@@ -2247,11 +2283,11 @@ var Compiler = class {
2247
2283
  [
2248
2284
  createSetupTask(this, options),
2249
2285
  createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
2250
- createValidateTask(this, { enabled: !!options.build }),
2286
+ createValidateTask(this),
2251
2287
  createInteractiveTask({ enabled: !!options.interactive, ...typeof options.interactive === "object" ? options.interactive : { mode: "except" } }),
2252
2288
  createShakingTask(this, { enabled: !!options.build, ...typeof options.build === "object" ? options.build.shaking : void 0 }),
2253
2289
  createCompileTask(this, { enabled: !!options.build }),
2254
- createPersistTask(this, { enabled: !!options.build })
2290
+ createPersistTask(this)
2255
2291
  ],
2256
2292
  {
2257
2293
  concurrent: false,
@@ -2337,8 +2373,8 @@ program.command("ignore").addArgument(
2337
2373
  }
2338
2374
  });
2339
2375
  } else {
2340
- if (!options.module && !options.method && !options.pathname) {
2341
- throw new Error("at least one of '--module', '--method' or '--pathname' must be specified");
2376
+ if (!options.method && !options.pathname) {
2377
+ throw new Error("at least one of '-i --interactive', '--method' or '--pathname' must be specified");
2342
2378
  }
2343
2379
  const moduleNames = options.module || ["*"];
2344
2380
  compiler = new Compiler({