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