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