@keq-request/cli 5.0.0-alpha.10 → 5.0.0-alpha.11

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.
Files changed (83) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/cli.cjs +673 -222
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +673 -222
  5. package/dist/cli.js.map +1 -1
  6. package/dist/compiler/compiler.d.ts +29 -0
  7. package/dist/compiler/compiler.d.ts.map +1 -0
  8. package/dist/compiler/index.d.ts +2 -0
  9. package/dist/compiler/index.d.ts.map +1 -0
  10. package/dist/compiler/intercepter/perfect-error-message.d.ts +3 -0
  11. package/dist/compiler/intercepter/perfect-error-message.d.ts.map +1 -0
  12. package/dist/compiler/intercepter/print-information.d.ts +5 -0
  13. package/dist/compiler/intercepter/print-information.d.ts.map +1 -0
  14. package/dist/index.cjs +620 -154
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.ts +9 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +609 -150
  19. package/dist/index.js.map +1 -1
  20. package/dist/plugins/body-fallback/index.d.ts +6 -0
  21. package/dist/plugins/body-fallback/index.d.ts.map +1 -0
  22. package/dist/plugins/eslint/index.d.ts +15 -0
  23. package/dist/plugins/eslint/index.d.ts.map +1 -0
  24. package/dist/plugins/index.d.ts +4 -0
  25. package/dist/plugins/index.d.ts.map +1 -0
  26. package/dist/plugins/prettier/index.d.ts +6 -0
  27. package/dist/plugins/prettier/index.d.ts.map +1 -0
  28. package/dist/plugins.cjs +159 -0
  29. package/dist/plugins.cjs.map +1 -0
  30. package/dist/plugins.js +120 -0
  31. package/dist/plugins.js.map +1 -0
  32. package/dist/renderer/json-schema/index.d.ts.map +1 -1
  33. package/dist/renderer/operation-request/index.d.ts.map +1 -1
  34. package/dist/renderer/operation-type/index.d.ts +2 -1
  35. package/dist/renderer/operation-type/index.d.ts.map +1 -1
  36. package/dist/renderer/request/index.d.ts.map +1 -1
  37. package/dist/tasks/compile/index.d.ts +3 -5
  38. package/dist/tasks/compile/index.d.ts.map +1 -1
  39. package/dist/tasks/compile/utils/compile-operation-definition.d.ts +5 -2
  40. package/dist/tasks/compile/utils/compile-operation-definition.d.ts.map +1 -1
  41. package/dist/tasks/compile/utils/compile-schema-definition.d.ts +5 -2
  42. package/dist/tasks/compile/utils/compile-schema-definition.d.ts.map +1 -1
  43. package/dist/tasks/download/index.d.ts +3 -3
  44. package/dist/tasks/download/index.d.ts.map +1 -1
  45. package/dist/tasks/index.d.ts +9 -11
  46. package/dist/tasks/index.d.ts.map +1 -1
  47. package/dist/tasks/interactive/index.d.ts +9 -6
  48. package/dist/tasks/interactive/index.d.ts.map +1 -1
  49. package/dist/tasks/persist/index.d.ts +3 -8
  50. package/dist/tasks/persist/index.d.ts.map +1 -1
  51. package/dist/tasks/setup/index.d.ts +7 -1
  52. package/dist/tasks/setup/index.d.ts.map +1 -1
  53. package/dist/tasks/shaking/index.d.ts +4 -5
  54. package/dist/tasks/shaking/index.d.ts.map +1 -1
  55. package/dist/tasks/types/base-task-options.d.ts +6 -0
  56. package/dist/tasks/types/base-task-options.d.ts.map +1 -0
  57. package/dist/tasks/types/file.d.ts +4 -0
  58. package/dist/tasks/types/file.d.ts.map +1 -0
  59. package/dist/tasks/types/index.d.ts +6 -0
  60. package/dist/tasks/types/index.d.ts.map +1 -0
  61. package/dist/tasks/types/task-context.d.ts +4 -0
  62. package/dist/tasks/types/task-context.d.ts.map +1 -1
  63. package/dist/tasks/types/task-wrapper.d.ts +4 -0
  64. package/dist/tasks/types/task-wrapper.d.ts.map +1 -0
  65. package/dist/tasks/utils/anchor.d.ts +20 -0
  66. package/dist/tasks/utils/anchor.d.ts.map +1 -0
  67. package/dist/tasks/utils/artifact.d.ts +2 -0
  68. package/dist/tasks/utils/artifact.d.ts.map +1 -1
  69. package/dist/tasks/utils/index.d.ts +11 -0
  70. package/dist/tasks/utils/index.d.ts.map +1 -0
  71. package/dist/tasks/utils/proxy-task-wrapper.d.ts +3 -0
  72. package/dist/tasks/utils/proxy-task-wrapper.d.ts.map +1 -0
  73. package/dist/tasks/validate/index.d.ts +3 -6
  74. package/dist/tasks/validate/index.d.ts.map +1 -1
  75. package/dist/types/index.d.ts +7 -0
  76. package/dist/types/index.d.ts.map +1 -0
  77. package/dist/types/plugin.d.ts +5 -0
  78. package/dist/types/plugin.d.ts.map +1 -0
  79. package/dist/types/runtime-config.d.ts +4 -1
  80. package/dist/types/runtime-config.d.ts.map +1 -1
  81. package/package.json +20 -4
  82. package/dist/tasks/append-ignore-rule/index.d.ts +0 -10
  83. package/dist/tasks/append-ignore-rule/index.d.ts.map +0 -1
package/dist/index.js CHANGED
@@ -1,12 +1,7 @@
1
- // src/tasks/index.ts
2
- import { Listr } from "listr2";
3
-
4
- // src/tasks/setup/index.ts
5
- import * as R3 from "ramda";
6
- import fs3 from "fs-extra";
7
- import path2 from "path";
8
- import { Value } from "@sinclair/typebox/value";
9
- import { cosmiconfig } from "cosmiconfig";
1
+ // src/define-keq-config.ts
2
+ function defineKeqConfig(config) {
3
+ return config;
4
+ }
10
5
 
11
6
  // src/types/runtime-config.ts
12
7
  import { Type as Type2 } from "@sinclair/typebox";
@@ -75,9 +70,21 @@ var RuntimeConfig = Type2.Object({
75
70
  /**
76
71
  * Whether to tolerate wrong swagger structure
77
72
  */
78
- tolerant: Type2.Optional(Type2.Boolean({ default: false }))
73
+ tolerant: Type2.Optional(Type2.Boolean({ default: false })),
74
+ plugins: Type2.Optional(Type2.Array(Type2.Any(), { default: [] }))
79
75
  });
80
76
 
77
+ // src/compiler/compiler.ts
78
+ import { AsyncSeriesHook, AsyncSeriesWaterfallHook, SyncHook } from "tapable";
79
+ import { Listr } from "listr2";
80
+
81
+ // src/tasks/setup/index.ts
82
+ import * as R3 from "ramda";
83
+ import fs3 from "fs-extra";
84
+ import path2 from "path";
85
+ import { Value } from "@sinclair/typebox/value";
86
+ import { cosmiconfig } from "cosmiconfig";
87
+
81
88
  // src/utils/ignore-matcher.ts
82
89
  import * as R from "ramda";
83
90
  import fs from "fs-extra";
@@ -226,9 +233,8 @@ function getProjectModuleSystem(pkgInfo) {
226
233
 
227
234
  // src/tasks/setup/index.ts
228
235
  var explore = cosmiconfig("keq");
229
- function createSetupTask(options) {
236
+ function main(compiler, options) {
230
237
  return {
231
- title: "Setup",
232
238
  task: async (context, task) => {
233
239
  const result = options?.config ? await explore.load(options.config) : await explore.search();
234
240
  if (!result || "isEmpty" in result && result.isEmpty) {
@@ -253,13 +259,23 @@ function createSetupTask(options) {
253
259
  const moduleSystem = getProjectModuleSystem(packageJsonInfo);
254
260
  rc.esm = moduleSystem === "esm";
255
261
  }
256
- let filter2 = new IgnoreMatcher([]);
262
+ let matcher = new IgnoreMatcher([]);
257
263
  if (result.filepath) {
258
264
  const ignoreFilepath = path2.resolve(path2.dirname(result.filepath), ".keqignore");
259
265
  if (await fs3.exists(ignoreFilepath)) {
260
- filter2 = await IgnoreMatcher.read(ignoreFilepath);
266
+ matcher = await IgnoreMatcher.read(ignoreFilepath);
261
267
  }
262
268
  }
269
+ const ignoreRules = options.ignore === false ? [] : options.ignore?.rules || [];
270
+ for (const rule of ignoreRules) {
271
+ matcher.append({
272
+ persist: !!rule.persist,
273
+ ignore: rule.ignore,
274
+ moduleName: rule.moduleName,
275
+ operationMethod: rule.operationMethod,
276
+ operationPathname: rule.operationPathname
277
+ });
278
+ }
263
279
  if (options?.modules && options.modules.length) {
264
280
  const notExistModules = options.modules.filter((moduleName) => !(moduleName in rc.modules));
265
281
  if (notExistModules.length) {
@@ -267,7 +283,7 @@ function createSetupTask(options) {
267
283
  }
268
284
  const ignoredModules = R3.difference(R3.keys(rc.modules), options.modules);
269
285
  for (const moduleName of ignoredModules) {
270
- filter2.append({
286
+ matcher.append({
271
287
  persist: false,
272
288
  ignore: true,
273
289
  moduleName,
@@ -276,10 +292,33 @@ function createSetupTask(options) {
276
292
  });
277
293
  }
278
294
  }
279
- context.setup = { rc, matcher: filter2 };
295
+ context.setup = { rc, matcher };
296
+ if (rc.plugins && rc.plugins.length) {
297
+ for (const plugin of rc.plugins) {
298
+ plugin.apply(compiler);
299
+ }
300
+ }
280
301
  }
281
302
  };
282
303
  }
304
+ function createSetupTask(compiler, options) {
305
+ return {
306
+ title: "Setup",
307
+ enabled: options?.enabled,
308
+ skip: options?.skip,
309
+ task: (context, task) => task.newListr(
310
+ [
311
+ main(compiler, options),
312
+ {
313
+ task: (context2, task2) => compiler.hooks.afterSetup.promise(task2)
314
+ }
315
+ ],
316
+ {
317
+ concurrent: false
318
+ }
319
+ )
320
+ };
321
+ }
283
322
 
284
323
  // src/tasks/download/index.ts
285
324
  import { PRESET_TIMER } from "listr2";
@@ -492,7 +531,7 @@ var logger = {
492
531
  };
493
532
 
494
533
  // src/tasks/utils/api-document_v3_1.ts
495
- import OpenapiShaking from "@opendoc/openapi-shaking";
534
+ import { openapiShakingSync } from "@opendoc/openapi-shaking";
496
535
  var ApiDocumentV3_1 = class _ApiDocumentV3_1 {
497
536
  module;
498
537
  swagger;
@@ -548,7 +587,7 @@ var ApiDocumentV3_1 = class _ApiDocumentV3_1 {
548
587
  });
549
588
  return filter2(operationDefinition);
550
589
  };
551
- const sharkedSwagger = OpenapiShaking.openapiShakingSync(
590
+ const sharkedSwagger = openapiShakingSync(
552
591
  this.swagger,
553
592
  isAccepted,
554
593
  { tolerant: true }
@@ -742,8 +781,8 @@ function dereferenceDeep($ref, swagger) {
742
781
  const stack = [$ref];
743
782
  let value;
744
783
  while (true) {
745
- const last = stack[stack.length - 1];
746
- value = dereference(last, swagger);
784
+ const last2 = stack[stack.length - 1];
785
+ value = dereference(last2, swagger);
747
786
  if (JsonSchemaUtils.isRef(value)) {
748
787
  if (!stack.includes(value.$ref)) {
749
788
  stack.push(value.$ref);
@@ -854,11 +893,8 @@ var ApiDocument = class _ApiDocument {
854
893
  };
855
894
 
856
895
  // src/tasks/download/index.ts
857
- function createDownloadTask(options) {
896
+ function main2(compiler, options) {
858
897
  return {
859
- title: "Download",
860
- enabled: options?.enabled,
861
- skip: options?.skip,
862
898
  task: (context, task) => {
863
899
  if (!context.setup) {
864
900
  throw new Error("Please run setup task first.");
@@ -896,13 +932,28 @@ function createDownloadTask(options) {
896
932
  }
897
933
  };
898
934
  }
899
-
900
- // src/tasks/validate/index.ts
901
- function createValidateTask(options) {
935
+ function createDownloadTask(compiler, options) {
902
936
  return {
903
- title: "Validate",
937
+ title: "Download",
904
938
  enabled: options?.enabled,
905
939
  skip: options?.skip,
940
+ task: (_, task) => task.newListr(
941
+ [
942
+ main2(compiler, options),
943
+ {
944
+ task: (context, task2) => compiler.hooks.afterDownload.promise(task2)
945
+ }
946
+ ],
947
+ {
948
+ concurrent: false
949
+ }
950
+ )
951
+ };
952
+ }
953
+
954
+ // src/tasks/validate/index.ts
955
+ function main3() {
956
+ return {
906
957
  task: (context, task) => {
907
958
  if (!context.setup) throw new Error("Please run setup task first.");
908
959
  if (!context.downloaded) throw new Error("Please run download task first.");
@@ -939,13 +990,28 @@ ${errors?.map((e) => ` - ${e.message}`).join("\n")}`;
939
990
  }
940
991
  };
941
992
  }
942
-
943
- // src/tasks/shaking/index.ts
944
- function createShakingTask(options) {
993
+ function createValidateTask(compiler, options) {
945
994
  return {
946
- title: "Shaking",
995
+ title: "Validate",
947
996
  enabled: options?.enabled,
948
997
  skip: options?.skip,
998
+ task: (context, task) => task.newListr(
999
+ [
1000
+ main3(),
1001
+ {
1002
+ task: (context2, task2) => compiler.hooks.afterValidate.promise(task2)
1003
+ }
1004
+ ],
1005
+ {
1006
+ concurrent: false
1007
+ }
1008
+ )
1009
+ };
1010
+ }
1011
+
1012
+ // src/tasks/shaking/index.ts
1013
+ function main4(compiler, options) {
1014
+ return {
949
1015
  task: (context, task) => {
950
1016
  if (!context.setup) throw new Error("Please run setup task first.");
951
1017
  if (!context.validated) throw new Error("Please run validate task first.");
@@ -983,15 +1049,31 @@ function createShakingTask(options) {
983
1049
  }
984
1050
  };
985
1051
  }
1052
+ function createShakingTask(compiler, options) {
1053
+ return {
1054
+ title: "Shaking",
1055
+ enabled: options?.enabled,
1056
+ skip: options?.skip,
1057
+ task: (context, task) => task.newListr(
1058
+ [
1059
+ main4(compiler, options),
1060
+ {
1061
+ task: (context2, task2) => compiler.hooks.afterShaking.promise(task2)
1062
+ }
1063
+ ],
1064
+ {
1065
+ concurrent: false
1066
+ }
1067
+ )
1068
+ };
1069
+ }
986
1070
 
987
1071
  // src/tasks/persist/index.ts
988
1072
  import * as path6 from "path";
989
1073
  import fs6 from "fs-extra";
990
- function createPersistArtifactTask(options) {
1074
+ function createPersistArtifactTask() {
991
1075
  return {
992
1076
  title: "Write files",
993
- enabled: options?.persistArtifacts,
994
- skip: options?.skip,
995
1077
  task: async (context, task) => {
996
1078
  if (!context.setup) throw new Error("Please run setup task first.");
997
1079
  if (!context.compiled) throw new Error("Please run compile task first.");
@@ -1002,21 +1084,21 @@ function createPersistArtifactTask(options) {
1002
1084
  }
1003
1085
  const total = artifacts.length;
1004
1086
  let completed = 0;
1005
- await Promise.all(artifacts.map(async (artifact) => {
1087
+ const files = await Promise.all(artifacts.map(async (artifact) => {
1006
1088
  const realpath = `./${path6.join(rc.outdir, artifact.filepath)}`;
1007
1089
  await fs6.ensureFile(realpath);
1008
1090
  await fs6.writeFile(realpath, artifact.toCode({ esm: !!rc.esm }));
1009
1091
  completed += 1;
1010
1092
  task.output = `Persisted ${completed}/${total} files`;
1093
+ return { path: path6.resolve(realpath) };
1011
1094
  }));
1095
+ context.persisted = { files };
1012
1096
  }
1013
1097
  };
1014
1098
  }
1015
- function createPersistIgnoreTask(options) {
1099
+ function createPersistIgnoreTask() {
1016
1100
  return {
1017
1101
  title: "Update .keqignore",
1018
- enabled: options?.persistIgnore,
1019
- skip: options?.skip,
1020
1102
  task: async (context, task) => {
1021
1103
  if (!context.setup) throw new Error("Please run setup task first.");
1022
1104
  const matcher = context.setup.matcher;
@@ -1024,15 +1106,12 @@ function createPersistIgnoreTask(options) {
1024
1106
  }
1025
1107
  };
1026
1108
  }
1027
- function createPersistTask(options) {
1109
+ function main5() {
1028
1110
  return {
1029
- title: "Persist",
1030
- enabled: options?.enabled,
1031
- skip: options?.skip,
1032
1111
  task: (context, task) => task.newListr(
1033
1112
  [
1034
- createPersistArtifactTask(options),
1035
- createPersistIgnoreTask(options)
1113
+ createPersistArtifactTask(),
1114
+ createPersistIgnoreTask()
1036
1115
  ],
1037
1116
  {
1038
1117
  concurrent: true,
@@ -1043,8 +1122,29 @@ function createPersistTask(options) {
1043
1122
  )
1044
1123
  };
1045
1124
  }
1125
+ function createPersistTask(compiler, options) {
1126
+ return {
1127
+ title: "Persist",
1128
+ enabled: options?.enabled,
1129
+ skip: options?.skip,
1130
+ task: (context, task) => task.newListr(
1131
+ [
1132
+ main5(),
1133
+ {
1134
+ task: (context2, task2) => compiler.hooks.afterPersist.promise(task2)
1135
+ }
1136
+ ],
1137
+ {
1138
+ concurrent: false
1139
+ }
1140
+ )
1141
+ };
1142
+ }
1046
1143
 
1047
1144
  // src/tasks/compile/utils/compile-schema-definition.ts
1145
+ import * as R14 from "ramda";
1146
+
1147
+ // src/renderer/json-schema/index.ts
1048
1148
  import * as R12 from "ramda";
1049
1149
 
1050
1150
  // src/renderer/utils/generate-schema.ts
@@ -1170,14 +1270,102 @@ async function jsonSchemaRenderer(schemaDefinition) {
1170
1270
  let $comment = generateComment(schemaDefinition.schema);
1171
1271
  if ($comment) $comment += "\n";
1172
1272
  if (typeof schemaDefinition.schema === "boolean") {
1173
- return `${$comment}type ${schemaDefinition.name} = unknown`;
1273
+ return [
1274
+ "/* @anchor:file:start */",
1275
+ "",
1276
+ $comment || void 0,
1277
+ `type ${schemaDefinition.name} = unknown`,
1278
+ "",
1279
+ "/* @anchor:file:end */"
1280
+ ].filter(R12.isNotNil).join("\n");
1174
1281
  }
1175
1282
  if (JsonSchemaUtils.isNonArray(schemaDefinition.schema) && schemaDefinition.schema.type === "object") {
1176
- return `${$comment}export interface ${schemaDefinition.name} ${generateSchema(schemaDefinition.schema)}`;
1283
+ return [
1284
+ "/* @anchor:file:start */",
1285
+ "",
1286
+ $comment || void 0,
1287
+ `export interface ${schemaDefinition.name} ${generateSchema(schemaDefinition.schema)}`,
1288
+ "",
1289
+ "/* @anchor:file:end */"
1290
+ ].filter(R12.isNotNil).join("\n");
1177
1291
  }
1178
- return `${$comment}export type ${schemaDefinition.name} = ${generateSchema(schemaDefinition.schema)}`;
1292
+ return [
1293
+ "/* @anchor:file:start */",
1294
+ "",
1295
+ $comment || void 0,
1296
+ `export type ${schemaDefinition.name} = ${generateSchema(schemaDefinition.schema)}`,
1297
+ "",
1298
+ "/* @anchor:file:end */"
1299
+ ].filter(R12.isNotNil).join("\n");
1179
1300
  }
1180
1301
 
1302
+ // src/tasks/utils/anchor.ts
1303
+ var AnchorBlock = class {
1304
+ constructor(artifact) {
1305
+ this.artifact = artifact;
1306
+ }
1307
+ /**
1308
+ * Append content to the end of the anchor block.
1309
+ */
1310
+ append(anchorName, content) {
1311
+ const lines = this.artifact.content.split("\n");
1312
+ const anchor = `@anchor:${anchorName}:end`;
1313
+ const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
1314
+ if (anchorIndex === -1) {
1315
+ throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
1316
+ }
1317
+ lines.splice(anchorIndex, 0, content);
1318
+ this.artifact.content = lines.join("\n");
1319
+ }
1320
+ prepend(anchorName, content) {
1321
+ const lines = this.artifact.content.split("\n");
1322
+ const anchor = `@anchor:${anchorName}:start`;
1323
+ const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
1324
+ if (anchorIndex === -1) {
1325
+ throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
1326
+ }
1327
+ lines.splice(anchorIndex + 1, 0, content);
1328
+ this.artifact.content = lines.join("\n");
1329
+ }
1330
+ replace(anchorName, content) {
1331
+ const lines = this.artifact.content.split("\n");
1332
+ const startIndex = lines.findIndex((line) => line.includes(`/* @anchor:${anchorName}:start */`));
1333
+ const endIndex = lines.findIndex((line) => line.includes(`/* @anchor:${anchorName}:end */`));
1334
+ if (startIndex === -1 || endIndex === -1 || endIndex <= startIndex) {
1335
+ throw new Error(`"@anchor:${anchorName}:start" or "@anchor:${anchorName}:end" not found in artifact "${this.artifact.filepath}".`);
1336
+ }
1337
+ lines.splice(startIndex + 1, endIndex - startIndex - 1, content);
1338
+ this.artifact.content = lines.join("\n");
1339
+ }
1340
+ };
1341
+ var Anchor = class {
1342
+ constructor(artifact) {
1343
+ this.artifact = artifact;
1344
+ this.block = new AnchorBlock(artifact);
1345
+ }
1346
+ block;
1347
+ append(anchorName, content) {
1348
+ const lines = this.artifact.content.split("\n");
1349
+ const anchor = `@anchor:${anchorName}`;
1350
+ const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
1351
+ if (anchorIndex === -1) {
1352
+ throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
1353
+ }
1354
+ lines.splice(anchorIndex + 1, 0, content);
1355
+ this.artifact.content = lines.join("\n");
1356
+ }
1357
+ prepend(anchorName, content) {
1358
+ const lines = this.artifact.content.split("\n");
1359
+ const anchor = `@anchor:${anchorName}`;
1360
+ const anchorIndex = lines.findIndex((line) => line.includes(`/* ${anchor} */`));
1361
+ if (anchorIndex === -1) {
1362
+ throw new Error(`"${anchor}" not found in artifact "${this.artifact.filepath}".`);
1363
+ }
1364
+ lines.splice(anchorIndex, 0, content);
1365
+ this.artifact.content = lines.join("\n");
1366
+ }
1367
+ };
1368
+
1181
1369
  // src/tasks/utils/artifact.ts
1182
1370
  import * as path8 from "path";
1183
1371
  import * as changeCase from "change-case";
@@ -1262,6 +1450,7 @@ var Artifact = class {
1262
1450
  warns = [];
1263
1451
  content;
1264
1452
  extensionName;
1453
+ anchor = new Anchor(this);
1265
1454
  constructor(options) {
1266
1455
  this.id = options.id;
1267
1456
  this.filepath = options.filepath;
@@ -1318,6 +1507,10 @@ var Artifact = class {
1318
1507
  }
1319
1508
  };
1320
1509
 
1510
+ // src/tasks/utils/json-schema.ts
1511
+ import * as R13 from "ramda";
1512
+ import { JSONPath as JSONPath5 } from "jsonpath-plus";
1513
+
1321
1514
  // src/tasks/compile/utils/compile-schema-definition.ts
1322
1515
  function genSchemaDefinitionFilepath(schemaDefinition) {
1323
1516
  const filename = `${schemaDefinition.name}.schema.ts`;
@@ -1342,17 +1535,18 @@ var isArtifactCompiledBy = function(schemaDefinition) {
1342
1535
  return (artifact) => artifact.id === genSchemaDefinitionFilepath(schemaDefinition);
1343
1536
  };
1344
1537
  async function compileSchemaDefinition(options) {
1345
- const { schemaDefinitions } = options;
1538
+ const { task, compiler, schemaDefinitions } = options;
1346
1539
  const artifacts = await Promise.all(
1347
1540
  schemaDefinitions.map(async (schemaDefinition) => {
1348
1541
  const content = await jsonSchemaRenderer(schemaDefinition);
1349
1542
  const filepath = genSchemaDefinitionFilepath(schemaDefinition);
1350
- return new Artifact({
1543
+ const artifact = new Artifact({
1351
1544
  id: filepath,
1352
1545
  filepath,
1353
1546
  content,
1354
1547
  extensionName: ".schema.ts"
1355
1548
  });
1549
+ return await compiler.hooks.afterCompileSchema.promise(artifact, schemaDefinition, task);
1356
1550
  })
1357
1551
  );
1358
1552
  for (const schemaDefinition of schemaDefinitions) {
@@ -1368,7 +1562,7 @@ async function compileSchemaDefinition(options) {
1368
1562
  artifact.addDependence(dependentArtifact, [dependentSchemaDefinition.name]);
1369
1563
  }
1370
1564
  }
1371
- const schemaDefinitionsGroupByModuleName = R12.groupBy(
1565
+ const schemaDefinitionsGroupByModuleName = R14.groupBy(
1372
1566
  (schemaDefinition) => schemaDefinition.module.name,
1373
1567
  schemaDefinitions
1374
1568
  );
@@ -1393,41 +1587,93 @@ async function compileSchemaDefinition(options) {
1393
1587
  }
1394
1588
 
1395
1589
  // src/tasks/compile/utils/compile-operation-definition.ts
1396
- import * as R15 from "ramda";
1590
+ import * as R17 from "ramda";
1397
1591
 
1398
1592
  // src/renderer/operation-type/index.ts
1399
- import * as R13 from "ramda";
1593
+ import * as R15 from "ramda";
1400
1594
  import * as changeCase2 from "change-case";
1401
1595
  function typeNameFactory(operationDefinition) {
1402
1596
  const pascalCaseOperationId = changeCase2.pascalCase(operationDefinition.operationId);
1403
1597
  return (name) => `${pascalCaseOperationId}${name}`;
1404
1598
  }
1405
- async function operationTypeRenderer(operationDefinition, alias = R13.identity) {
1406
- const { operation } = operationDefinition;
1407
- if (!operation.responses) return "";
1408
- const typeName = typeNameFactory(operationDefinition);
1599
+ function responseBodies(operation, alias = R15.identity, typeName) {
1600
+ if (!operation.responses || R15.isEmpty(operation.responses)) {
1601
+ return `export interface ${typeName("ResponseBodies")} {}`;
1602
+ }
1409
1603
  const $responses = Object.entries(operation.responses).map(([statusCode, response]) => {
1410
1604
  if (!JsonSchemaUtils.isRef(response)) {
1411
1605
  const $value = Object.values(response.content || {}).map((mediaTypeObject) => mediaTypeObject.schema).filter((schema) => !!schema).map((schema) => generateSchema(schema, alias)).join(" | ");
1412
1606
  return indent(2, `${statusCode}: ${$value || "void"}`);
1413
1607
  }
1414
1608
  }).join("\n");
1415
- let $requestBody = `export type ${typeName("RequestBody")} = {}`;
1609
+ return [
1610
+ `export interface ${typeName("ResponseBodies")} {`,
1611
+ $responses,
1612
+ "}"
1613
+ ].join("\n");
1614
+ }
1615
+ function requestBodies(operation, alias = R15.identity, typeName) {
1616
+ let $requestBodies = `export interface ${typeName("RequestBodies")} {}`;
1617
+ if (operation.requestBody && !JsonSchemaUtils.isRef(operation.requestBody)) {
1618
+ const $mediaTypes = Object.entries(operation.requestBody.content || {}).map(([mediaType, mediaTypeObject]) => [mediaType, mediaTypeObject.schema]).map(([mediaType, schema]) => {
1619
+ if (!schema) return `${JSON.stringify(mediaType)}: unknown`;
1620
+ return `${JSON.stringify(mediaType)}: ${generateSchema(schema, alias)}`;
1621
+ }).map((pair) => indent(2, pair));
1622
+ $requestBodies = [
1623
+ `export interface ${typeName("RequestBodies")} {`,
1624
+ ...$mediaTypes,
1625
+ "}"
1626
+ ].join("\n");
1627
+ }
1628
+ return $requestBodies;
1629
+ }
1630
+ function parameterBodies(operationDefinition, alias = R15.identity, typeName) {
1631
+ const { operation } = operationDefinition;
1632
+ let parameterBodies2 = "";
1416
1633
  if (operation.requestBody && !JsonSchemaUtils.isRef(operation.requestBody)) {
1417
- const $value = Object.entries(operation.requestBody.content || {}).map(([mediaType, mediaTypeObject]) => [mediaType, mediaTypeObject.schema]).filter(([, schema]) => !!schema).map(([mediaType, schema]) => {
1418
- if (mediaType === "multipart/form-data") {
1419
- return `FormData | ${generateSchema(schema, alias)}`;
1420
- } else if (mediaType === "application/x-www-form-urlencoded") {
1421
- return `URLSearchParams | ${generateSchema(schema, alias)}`;
1634
+ const $mediaTypes = Object.entries(operation.requestBody.content || {}).map(([mediaType, mediaTypeObject]) => [mediaType, mediaTypeObject.schema]).map(([mediaType, schemaOrRef]) => {
1635
+ if (!schemaOrRef) return `${JSON.stringify(mediaType)}: unknown`;
1636
+ const schema = JsonSchemaUtils.isRef(schemaOrRef) ? SwaggerUtils.dereferenceDeep(schemaOrRef.$ref, operationDefinition.document.swagger) : schemaOrRef;
1637
+ if (schema.type === "object" || schema.properties) {
1638
+ return `${JSON.stringify(mediaType)}: ${generateSchema(schemaOrRef, alias)} & { [key: string]: any }`;
1422
1639
  }
1423
- return generateSchema(schema, alias);
1424
- }).join(" | ");
1425
- $requestBody = `export type ${typeName("RequestBody")} = ${$value || "unknown"}`;
1640
+ return `${JSON.stringify(mediaType)}: { [key: string]: any }`;
1641
+ }).map((pair) => indent(2, pair));
1642
+ parameterBodies2 = [
1643
+ `interface ${typeName("ParameterBodies")} {`,
1644
+ ...$mediaTypes,
1645
+ "}",
1646
+ ""
1647
+ ].join("\n");
1648
+ }
1649
+ return parameterBodies2;
1650
+ }
1651
+ function requestParameters(operation, alias = R15.identity, typeName) {
1652
+ const mediaTypes = operation.requestBody && !JsonSchemaUtils.isRef(operation.requestBody) ? Object.keys(operation.requestBody.content || {}) : [];
1653
+ const base = `${typeName("RequestQuery")} & ${typeName("RouteParameters")} & ${typeName("RequestHeaders")}`;
1654
+ if (mediaTypes.length === 1) {
1655
+ return `export type ${typeName("RequestParameters")} = ${base} & ${typeName("RequestBodies")}[${JSON.stringify(mediaTypes[0])}]`;
1656
+ }
1657
+ if (mediaTypes.length > 1) {
1658
+ const unions = mediaTypes.map((mediaType) => `(${base} & ${typeName("RequestBodies")}[${JSON.stringify(mediaType)}] & { "content-type": ${JSON.stringify(mediaType)} })`).join("\n| ");
1659
+ return `export type ${typeName("RequestParameters")} = ${unions}`;
1426
1660
  }
1661
+ return `export type ${typeName("RequestParameters")} = ${base}`;
1662
+ }
1663
+ async function operationTypeRenderer(operationDefinition, alias = R15.identity) {
1664
+ const { operation } = operationDefinition;
1665
+ if (!operation.responses) return "";
1666
+ const typeName = typeNameFactory(operationDefinition);
1667
+ const $responseBodies = responseBodies(operation, alias, typeName);
1668
+ const $requestBodies = requestBodies(operation, alias, typeName);
1669
+ const $parameterBodies = parameterBodies(operationDefinition, alias, typeName);
1670
+ const $requestParameters = requestParameters(operation, alias, typeName);
1427
1671
  return [
1428
- `export interface ${typeName("ResponseBodies")} {`,
1429
- $responses,
1430
- "}",
1672
+ "/* @anchor:file:start */",
1673
+ "",
1674
+ $responseBodies,
1675
+ "",
1676
+ $requestBodies,
1431
1677
  "",
1432
1678
  generateParameters(`${typeName("RequestQuery")}`, operation.parameters?.filter((p) => !JsonSchemaUtils.isRef(p) && p.in === "query") || [], alias),
1433
1679
  "",
@@ -1435,18 +1681,19 @@ async function operationTypeRenderer(operationDefinition, alias = R13.identity)
1435
1681
  "",
1436
1682
  generateParameters(`${typeName("RequestHeaders")}`, operation.parameters?.filter((p) => !JsonSchemaUtils.isRef(p) && p.in === "header") || [], alias),
1437
1683
  "",
1438
- $requestBody,
1439
- "",
1440
- `export type ${typeName("RequestParameters")} = ${typeName("RequestQuery")} & ${typeName("RouteParameters")} & ${typeName("RequestHeaders")} & ${typeName("RequestBody")}`,
1684
+ $parameterBodies || void 0,
1685
+ $requestParameters,
1441
1686
  "",
1442
- `export interface Operation<STATUS extends keyof ${typeName("ResponseBodies")}> extends KeqOperation {`,
1443
- ` requestParams: ${typeName("RouteParameters")} & { [key: string]: KeqParamValue }`,
1444
- ` requestQuery: ${typeName("RequestQuery")} & { [key: string]: KeqQueryValue }`,
1687
+ `export interface Operation<STATUS extends keyof ${typeName("ResponseBodies")}, CONTENT_TYPE extends keyof ${typeName("ParameterBodies")}> extends KeqOperation {`,
1688
+ ` requestParams: ${typeName("RouteParameters")} & { [key: string]: KeqPathParameterInit }`,
1689
+ ` requestQuery: ${typeName("RequestQuery")} & { [key: string]: KeqQueryInit }`,
1445
1690
  ` requestHeaders: ${typeName("RequestHeaders")} & { [key: string]: string | number }`,
1446
- ` requestBody: ${typeName("RequestBody")}`,
1691
+ ` requestBody: ${$parameterBodies ? `${typeName("ParameterBodies")}[CONTENT_TYPE] | ` : "object | "}BodyInit`,
1447
1692
  ` responseBody: ${typeName("ResponseBodies")}[STATUS]`,
1448
- "}"
1449
- ].join("\n");
1693
+ "}",
1694
+ "",
1695
+ "/* @anchor:file:end */"
1696
+ ].filter(R15.isNotNil).join("\n");
1450
1697
  }
1451
1698
  function generateParameters(name, parameters, alias) {
1452
1699
  if (parameters.length === 0) {
@@ -1466,7 +1713,7 @@ function generateParameters(name, parameters, alias) {
1466
1713
  }
1467
1714
 
1468
1715
  // src/renderer/operation-request/index.ts
1469
- import * as R14 from "ramda";
1716
+ import * as R16 from "ramda";
1470
1717
  function errorToComment(err, mediaType) {
1471
1718
  const $err = String(err).split("\n").map(((line) => ` * ${line}`)).join("\n");
1472
1719
  return [
@@ -1476,23 +1723,8 @@ function errorToComment(err, mediaType) {
1476
1723
  " */"
1477
1724
  ].join("\n");
1478
1725
  }
1479
- async function operationRequestRenderer(operationDefinition, options) {
1480
- const { operation, operationId, method, pathname } = operationDefinition;
1481
- const { qs } = options;
1482
- if (!operation.responses) return "";
1483
- const typeName = typeNameFactory(operationDefinition);
1484
- const moduleName = operationDefinition.module.name;
1485
- const parameters = operation.parameters?.filter((p) => !JsonSchemaUtils.isRef(p)) || [];
1486
- const queryParameters = parameters.filter((p) => p.in === "query");
1487
- const headersParameters = parameters.filter((p) => p.in === "header");
1488
- const pathParameters = parameters.filter((p) => p.in === "path");
1489
- const $queryParameters = queryParameters.map((p) => {
1490
- const option = qs(p);
1491
- const $option = !option || R14.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1492
- return ` if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
1493
- }).concat("").join("\n");
1494
- const $headerParameters = headersParameters.map((p) => ` if (args && ${JSON.stringify(p.name)} in args) req.header(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}])`).concat("").join("\n");
1495
- const $pathParameters = pathParameters.map((p) => ` if (args && ${JSON.stringify(p.name)} in args) req.params(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}])`).concat("").join("\n");
1726
+ function requestBodyRenderer(operationDefinition, typeName) {
1727
+ const { operation } = operationDefinition;
1496
1728
  const requestBodyContent = operation.requestBody?.content || {};
1497
1729
  const $requestBody = Object.entries(requestBodyContent).map(([mediaType, mediaTypeObject]) => {
1498
1730
  if (!mediaTypeObject.schema) return;
@@ -1521,27 +1753,107 @@ async function operationRequestRenderer(operationDefinition, options) {
1521
1753
  } catch (err) {
1522
1754
  return indent(2, errorToComment(err, mediaType));
1523
1755
  }
1524
- }).filter(R14.isNotNil).join("\n");
1756
+ }).filter(R16.isNotNil).join("\n");
1757
+ return $requestBody;
1758
+ }
1759
+ function requestHeadersRenderer(operationDefinition, typeName) {
1760
+ const { operation } = operationDefinition;
1761
+ 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");
1762
+ return $headers;
1763
+ }
1764
+ function requestQueryRenderer(operationDefinition, qs, typeName) {
1765
+ const { operation } = operationDefinition;
1766
+ const $query = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "query").map((p) => {
1767
+ const option = qs(p);
1768
+ const $option = !option || R16.isEmpty(option) ? "" : `, ${JSON.stringify(option)}`;
1769
+ return ` if (args && ${JSON.stringify(p.name)} in args) req.query(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}]${$option})`;
1770
+ }).concat("").join("\n");
1771
+ return $query;
1772
+ }
1773
+ function requestPathParametersRenderer(operationDefinition, typeName) {
1774
+ const { operation } = operationDefinition;
1775
+ const $pathParameters = (operation.parameters || []).filter((p) => !JsonSchemaUtils.isRef(p)).filter((p) => p.in === "path").map((p) => ` if (args && ${JSON.stringify(p.name)} in args) req.params(${JSON.stringify(p.name)}, args[${JSON.stringify(p.name)}])`).concat("").join("\n");
1776
+ return $pathParameters;
1777
+ }
1778
+ function getRequestMediaTypes(operationDefinition) {
1779
+ const { operation } = operationDefinition;
1780
+ const requestBodyContent = operation.requestBody?.content || {};
1781
+ return Object.keys(requestBodyContent);
1782
+ }
1783
+ function mediaTypeRenderer(operationDefinition) {
1784
+ const mediaTypes = getRequestMediaTypes(operationDefinition);
1785
+ if (mediaTypes.length === 1 && !mediaTypes[0].endsWith("/*")) {
1786
+ return ` req.type("${mediaTypes[0]}")
1787
+ `;
1788
+ } else if (mediaTypes.some((mediaType) => mediaType === "*/*")) {
1789
+ } else if (mediaTypes.some((mediaType) => mediaType.endsWith("/*"))) {
1790
+ return ' if(args?.["content-type"]) req.type(args["content-type"])\n';
1791
+ } else if (mediaTypes.length > 1) {
1792
+ return ' if(args?.["content-type"]) req.type(args["content-type"])\n';
1793
+ }
1794
+ return "";
1795
+ }
1796
+ function operationDeclarationRenderer(operationDefinition, typeName) {
1797
+ const { operationId } = operationDefinition;
1798
+ const mediaTypes = getRequestMediaTypes(operationDefinition);
1799
+ if (mediaTypes.length === 0) {
1800
+ return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, never>>`;
1801
+ } else if (mediaTypes.length === 1) {
1802
+ return `function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS, ${JSON.stringify(mediaTypes[0])}>>`;
1803
+ } else if (mediaTypes.length > 1) {
1804
+ 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>>`;
1805
+ }
1806
+ throw new Error("[operationDeclarationRenderer] Unreachable");
1807
+ }
1808
+ async function operationRequestRenderer(operationDefinition, options) {
1809
+ const { operation, operationId, method, pathname } = operationDefinition;
1810
+ const { qs } = options;
1811
+ if (!operation.responses) return "";
1812
+ const typeName = typeNameFactory(operationDefinition);
1813
+ const moduleName = operationDefinition.module.name;
1814
+ const $queryParameters = requestQueryRenderer(operationDefinition, qs, typeName);
1815
+ const $headerParameters = requestHeadersRenderer(operationDefinition, typeName);
1816
+ const $pathParameters = requestPathParametersRenderer(operationDefinition, typeName);
1817
+ const $mediaType = mediaTypeRenderer(operationDefinition);
1818
+ const $requestBody = requestBodyRenderer(operationDefinition, typeName);
1819
+ const $operationDeclaration = operationDeclarationRenderer(operationDefinition, typeName);
1525
1820
  return [
1821
+ "/* @anchor:file:start */",
1822
+ "",
1526
1823
  `const moduleName = "${moduleName}"`,
1527
1824
  `const method = "${method}"`,
1528
1825
  `const pathname = "${pathname}"`,
1529
1826
  "",
1530
- `export function ${operationId}<STATUS extends keyof ${typeName("ResponseBodies")}>(args?: ${typeName("RequestParameters")}): Keq<Operation<STATUS>> {`,
1827
+ "/* @anchor:operation-declaration */",
1828
+ `export ${$operationDeclaration} {`,
1531
1829
  ` const req = request.post<${typeName("ResponseBodies")}[STATUS]>("${pathname}")`,
1532
1830
  " .option('module', { name: moduleName, pathname, method })",
1533
1831
  "",
1832
+ $mediaType || void 0,
1833
+ " /* @anchor:query:start */",
1534
1834
  $queryParameters || void 0,
1835
+ " /* @anchor:query:end */",
1836
+ "",
1837
+ " /* @anchor:headers:start */",
1535
1838
  $headerParameters || void 0,
1839
+ " /* @anchor:headers:end */",
1840
+ "",
1841
+ " /* @anchor:path-parameters:start */",
1536
1842
  $pathParameters || void 0,
1537
- $requestBody ? `${$requestBody}
1538
- ` : void 0,
1539
- " return req",
1843
+ " /* @anchor:path-parameters:end */",
1844
+ "",
1845
+ " /* @anchor:body:start */",
1846
+ $requestBody || void 0,
1847
+ " /* @anchor:body:end */",
1848
+ "",
1849
+ " /* @anchor:operation-return */",
1850
+ ` return req as ReturnType<typeof ${operationId}>`,
1540
1851
  "}",
1541
1852
  "",
1542
1853
  `${operationId}.pathname = pathname`,
1543
- `${operationId}.method = method`
1544
- ].filter(R14.isNotNil).join("\n");
1854
+ `${operationId}.method = method`,
1855
+ "/* @anchor:file:end */"
1856
+ ].filter(R16.isNotNil).join("\n");
1545
1857
  }
1546
1858
 
1547
1859
  // src/tasks/compile/utils/compile-operation-definition.ts
@@ -1572,7 +1884,7 @@ function genEntrypointFilepath2(moduleName) {
1572
1884
  ].join("/");
1573
1885
  }
1574
1886
  async function compileOperationDefinition(options) {
1575
- const { rc, requestArtifact, schemaArtifacts, operationDefinitions } = options;
1887
+ const { compiler, task, rc, requestArtifact, schemaArtifacts, operationDefinitions } = options;
1576
1888
  const alias = (name) => `${name}Schema`;
1577
1889
  const qs = (parameter) => {
1578
1890
  if (typeof rc.qs === "function") {
@@ -1606,7 +1918,7 @@ async function compileOperationDefinition(options) {
1606
1918
  content,
1607
1919
  extensionName: ".type.ts"
1608
1920
  });
1609
- typeArtifact.addDependence("keq", ["KeqOperation", "KeqQueryValue", "KeqParamValue"]);
1921
+ typeArtifact.addDependence("keq", ["KeqOperation", "KeqQueryInit", "KeqPathParameterInit"]);
1610
1922
  const dependentSchemaDefinitions = operationDefinition.getDependencies();
1611
1923
  for (const dependentSchemaDefinition of dependentSchemaDefinitions) {
1612
1924
  const dependentArtifact = schemaArtifacts.find(isArtifactCompiledBy(dependentSchemaDefinition));
@@ -1618,7 +1930,7 @@ async function compileOperationDefinition(options) {
1618
1930
  new DependencyIdentifier(dependentSchemaDefinition.name, alias(dependentSchemaDefinition.name))
1619
1931
  ]);
1620
1932
  }
1621
- return typeArtifact;
1933
+ return await compiler.hooks.afterCompileOperationType.promise(typeArtifact, operationDefinition, task);
1622
1934
  }
1623
1935
  async function createRequestArtifact(operationDefinition, typeArtifact) {
1624
1936
  const typeName = typeNameFactory(operationDefinition);
@@ -1645,13 +1957,13 @@ async function compileOperationDefinition(options) {
1645
1957
  [
1646
1958
  `${typeName("RequestQuery")}`,
1647
1959
  `${typeName("RequestHeaders")}`,
1648
- `${typeName("RequestBody")}`
1960
+ `${typeName("RequestBodies")}`
1649
1961
  ],
1650
1962
  { export: true }
1651
1963
  );
1652
- return artifact;
1964
+ return await compiler.hooks.afterCompileOperationRequest.promise(artifact, operationDefinition, task);
1653
1965
  }
1654
- const artifacts = R15.unnest(
1966
+ const artifacts = R17.unnest(
1655
1967
  await Promise.all(
1656
1968
  operationDefinitions.map(async (operationDefinition) => {
1657
1969
  const typeArtifact = await createTypeArtifact(operationDefinition);
@@ -1660,7 +1972,7 @@ async function compileOperationDefinition(options) {
1660
1972
  })
1661
1973
  )
1662
1974
  );
1663
- const operationDefinitionsGroupByModuleName = R15.groupBy(
1975
+ const operationDefinitionsGroupByModuleName = R17.groupBy(
1664
1976
  (operationDefinition) => operationDefinition.module.name,
1665
1977
  operationDefinitions
1666
1978
  );
@@ -1688,32 +2000,38 @@ async function compileOperationDefinition(options) {
1688
2000
  async function requestRenderer() {
1689
2001
  return [
1690
2002
  "import { KeqRequest } from 'keq'",
1691
- "export const request = new KeqRequest()"
2003
+ "",
2004
+ "/* @anchor:file:start */",
2005
+ "",
2006
+ "/* @anchor:request-declaration */",
2007
+ "export const request = new KeqRequest()",
2008
+ "",
2009
+ "/* @anchor:file:end"
1692
2010
  ].join("\n");
1693
2011
  }
1694
2012
 
1695
2013
  // src/tasks/compile/index.ts
1696
- function createCompileTask(options) {
2014
+ function main6(compiler) {
1697
2015
  return {
1698
- title: "Compile",
1699
- enabled: options?.enabled,
1700
- skip: options?.skip,
1701
2016
  task: async (context, task) => {
1702
2017
  if (!context.setup) throw new Error("Please run setup task first.");
1703
2018
  if (!context.shaken) throw new Error("Please run shaking task first.");
1704
2019
  const rc = context.setup.rc;
1705
2020
  const matcher = context.setup.matcher;
1706
2021
  const documents = context.shaken.documents.filter((document) => !matcher.isModuleIgnored(document.module));
1707
- const requestArtifact = new Artifact({
1708
- id: "request",
1709
- filepath: "request",
1710
- content: await requestRenderer(),
1711
- extensionName: ".ts"
1712
- });
2022
+ const requestArtifact = await compiler.hooks.afterCompileKeqRequest.promise(
2023
+ new Artifact({
2024
+ id: "request",
2025
+ filepath: "request",
2026
+ content: await requestRenderer(),
2027
+ extensionName: ".ts"
2028
+ }),
2029
+ task
2030
+ );
1713
2031
  const schemaDefinitions = documents.flatMap((document) => document.schemas);
1714
2032
  const operationDefinitions = documents.flatMap((document) => document.operations);
1715
- const schemaArtifacts = await compileSchemaDefinition({ schemaDefinitions });
1716
- const operationArtifacts = await compileOperationDefinition({ rc, operationDefinitions, schemaArtifacts, requestArtifact });
2033
+ const schemaArtifacts = await compileSchemaDefinition({ compiler, task, schemaDefinitions });
2034
+ const operationArtifacts = await compileOperationDefinition({ compiler, task, rc, operationDefinitions, schemaArtifacts, requestArtifact });
1717
2035
  const artifacts = [requestArtifact, ...schemaArtifacts, ...operationArtifacts];
1718
2036
  context.compiled = {
1719
2037
  artifacts
@@ -1721,6 +2039,24 @@ function createCompileTask(options) {
1721
2039
  }
1722
2040
  };
1723
2041
  }
2042
+ function createCompileTask(compiler, options) {
2043
+ return {
2044
+ title: "Compile",
2045
+ enabled: options?.enabled,
2046
+ skip: options?.skip,
2047
+ task: (context, task) => task.newListr(
2048
+ [
2049
+ main6(compiler),
2050
+ {
2051
+ task: (context2, task2) => compiler.hooks.afterCompile.promise(task2)
2052
+ }
2053
+ ],
2054
+ {
2055
+ concurrent: false
2056
+ }
2057
+ )
2058
+ };
2059
+ }
1724
2060
 
1725
2061
  // src/tasks/interactive/utils/select-operation-definitions.ts
1726
2062
  import { select } from "inquirer-select-pro";
@@ -1757,7 +2093,7 @@ function createInteractiveTask(options) {
1757
2093
  const documents = context.validated.documents;
1758
2094
  const operationDefinitions = documents.flatMap((document) => document.operations);
1759
2095
  const selectedOperationDefinitions = await selectOperationDefinitions(task, operationDefinitions);
1760
- if (options.override) {
2096
+ if (options.clear) {
1761
2097
  matcher.append({
1762
2098
  persist: false,
1763
2099
  ignore: true,
@@ -1768,7 +2104,7 @@ function createInteractiveTask(options) {
1768
2104
  }
1769
2105
  for (const op of selectedOperationDefinitions) {
1770
2106
  matcher.append({
1771
- persist: true,
2107
+ persist: !!options.persist,
1772
2108
  ignore: options.mode === "add",
1773
2109
  moduleName: op.module.name,
1774
2110
  operationMethod: op.method,
@@ -1779,38 +2115,161 @@ function createInteractiveTask(options) {
1779
2115
  };
1780
2116
  }
1781
2117
 
1782
- // src/tasks/index.ts
1783
- async function build(options) {
1784
- const tasks = new Listr(
1785
- [
1786
- createSetupTask(options),
1787
- createDownloadTask({ skipIgnoredModules: !options.interactive }),
1788
- createValidateTask(),
1789
- createInteractiveTask({ mode: "except", override: true, enabled: !!options.interactive }),
1790
- createShakingTask({ skipIgnoredModules: true, skipEmptyDocuments: true }),
1791
- createCompileTask(),
1792
- createPersistTask({ persistIgnore: false })
1793
- ],
1794
- {
1795
- concurrent: false,
1796
- renderer: "default",
1797
- rendererOptions: {
1798
- suffixSkips: true,
1799
- collapseSubtasks: false,
1800
- collapseErrors: false
2118
+ // src/compiler/intercepter/perfect-error-message.ts
2119
+ import * as R18 from "ramda";
2120
+ function perfectErrorMessage() {
2121
+ return {
2122
+ register: (tap) => {
2123
+ const fn = tap.fn;
2124
+ function prefix(err) {
2125
+ if (err instanceof Error) {
2126
+ err.message = `[Plugin: ${tap.name}] ${err.message}`;
2127
+ }
2128
+ }
2129
+ if (tap.type === "promise") {
2130
+ tap.fn = async (...args) => {
2131
+ try {
2132
+ return await fn(...args);
2133
+ } catch (err) {
2134
+ prefix(err);
2135
+ throw err;
2136
+ }
2137
+ };
2138
+ }
2139
+ if (tap.type === "sync") {
2140
+ tap.fn = (...args) => {
2141
+ try {
2142
+ return fn(...args);
2143
+ } catch (err) {
2144
+ prefix(err);
2145
+ throw err;
2146
+ }
2147
+ };
2148
+ }
2149
+ if (tap.type === "async") {
2150
+ tap.fn = (...args) => {
2151
+ const callback = R18.last(args);
2152
+ return fn(...R18.init(args), (err, result) => {
2153
+ prefix(err);
2154
+ return callback(err, result);
2155
+ });
2156
+ };
1801
2157
  }
2158
+ return tap;
1802
2159
  }
1803
- );
1804
- await tasks.run({});
2160
+ };
1805
2161
  }
1806
2162
 
1807
- // src/define-keq-config.ts
1808
- function defineKeqConfig(config) {
1809
- return config;
2163
+ // src/compiler/intercepter/print-information.ts
2164
+ function proxyTaskWrapper(pluginName, task) {
2165
+ return new Proxy(task, {
2166
+ set(target, prop2, value) {
2167
+ if (prop2 !== "output") {
2168
+ return Reflect.set(target, prop2, value);
2169
+ }
2170
+ target.output = `[Plugin: ${pluginName}] ${value}`;
2171
+ return true;
2172
+ }
2173
+ });
1810
2174
  }
2175
+ function printInformation(taskIndex) {
2176
+ return {
2177
+ register: (tap) => {
2178
+ const fn = tap.fn;
2179
+ if (tap.type === "promise") {
2180
+ tap.fn = (...args) => {
2181
+ const task = args[taskIndex];
2182
+ const proxyTask = proxyTaskWrapper(tap.name, task);
2183
+ args[taskIndex] = proxyTask;
2184
+ proxyTask.output = "Processing...";
2185
+ return fn(...args);
2186
+ };
2187
+ }
2188
+ if (tap.type === "sync") {
2189
+ tap.fn = (...args) => {
2190
+ const task = args[taskIndex];
2191
+ const proxyTask = proxyTaskWrapper(tap.name, task);
2192
+ args[taskIndex] = proxyTask;
2193
+ proxyTask.output = "Processing...";
2194
+ return fn(...args);
2195
+ };
2196
+ }
2197
+ if (tap.type === "async") {
2198
+ tap.fn = (...args) => {
2199
+ const task = args[taskIndex];
2200
+ const proxyTask = proxyTaskWrapper(tap.name, task);
2201
+ args[taskIndex] = proxyTask;
2202
+ proxyTask.output = "Processing...";
2203
+ return fn(...args);
2204
+ };
2205
+ }
2206
+ return tap;
2207
+ }
2208
+ };
2209
+ }
2210
+
2211
+ // src/compiler/compiler.ts
2212
+ var Compiler = class {
2213
+ constructor(options) {
2214
+ this.options = options;
2215
+ for (const hook of Object.values(this.hooks)) {
2216
+ hook.intercept(perfectErrorMessage());
2217
+ }
2218
+ this.hooks.afterSetup.intercept(printInformation(0));
2219
+ this.hooks.afterPersist.intercept(printInformation(0));
2220
+ }
2221
+ context = {};
2222
+ hooks = {
2223
+ // core
2224
+ afterSetup: new AsyncSeriesHook(["task"]),
2225
+ afterDownload: new AsyncSeriesHook(["task"]),
2226
+ afterValidate: new AsyncSeriesHook(["task"]),
2227
+ afterShaking: new AsyncSeriesHook(["task"]),
2228
+ afterCompile: new AsyncSeriesHook(["task"]),
2229
+ afterPersist: new AsyncSeriesHook(["task"]),
2230
+ done: new SyncHook(),
2231
+ // compile
2232
+ afterCompileKeqRequest: new AsyncSeriesWaterfallHook(["artifact", "task"]),
2233
+ afterCompileSchema: new AsyncSeriesWaterfallHook(["artifact", "schema", "task"]),
2234
+ afterCompileOperationType: new AsyncSeriesWaterfallHook(["artifact", "operation", "task"]),
2235
+ afterCompileOperationRequest: new AsyncSeriesWaterfallHook(["artifact", "operation", "task"])
2236
+ };
2237
+ async run() {
2238
+ const options = this.options;
2239
+ const tasks = new Listr(
2240
+ [
2241
+ createSetupTask(this, options),
2242
+ createDownloadTask(this, { skipIgnoredModules: !options.interactive }),
2243
+ createValidateTask(this, { enabled: !!options.build }),
2244
+ createInteractiveTask({ enabled: !!options.interactive, ...typeof options.interactive === "object" ? options.interactive : { mode: "except" } }),
2245
+ createShakingTask(this, { enabled: !!options.build, ...typeof options.build === "object" ? options.build.shaking : void 0 }),
2246
+ createCompileTask(this, { enabled: !!options.build }),
2247
+ createPersistTask(this, { enabled: !!options.build })
2248
+ ],
2249
+ {
2250
+ concurrent: false,
2251
+ renderer: "default",
2252
+ rendererOptions: {
2253
+ suffixSkips: true,
2254
+ collapseSubtasks: false,
2255
+ collapseErrors: false
2256
+ }
2257
+ }
2258
+ );
2259
+ await tasks.run(this.context);
2260
+ await this.hooks.done.promise();
2261
+ }
2262
+ };
1811
2263
  export {
2264
+ ApiDocument,
2265
+ ApiDocumentV3_1,
2266
+ Artifact,
2267
+ Compiler,
1812
2268
  FileNamingStyle,
1813
- build,
2269
+ ModuleDefinition,
2270
+ OperationDefinition,
2271
+ RuntimeConfig,
2272
+ SchemaDefinition,
1814
2273
  defineKeqConfig
1815
2274
  };
1816
2275
  //# sourceMappingURL=index.js.map