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