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