@kubb/core 5.7.0 → 5.8.0

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.
@@ -356,7 +356,7 @@ function formatMs(ms) {
356
356
  }
357
357
  //#endregion
358
358
  //#region package.json
359
- var version = "5.7.0";
359
+ var version = "5.8.0";
360
360
  //#endregion
361
361
  //#region src/constants.ts
362
362
  /**
@@ -469,6 +469,10 @@ const diagnosticCode = {
469
469
  */
470
470
  invalidPluginOptions: "KUBB_INVALID_PLUGIN_OPTIONS",
471
471
  /**
472
+ * An extension's options fail its options `schema`.
473
+ */
474
+ invalidOptions: "KUBB_INVALID_OPTIONS",
475
+ /**
472
476
  * A post-generate command (`output.postGenerate`) exited with a failure.
473
477
  */
474
478
  postGenerateFailed: "KUBB_POST_GENERATE_FAILED",
@@ -676,6 +680,11 @@ const diagnosticCatalog = {
676
680
  cause: "A plugin was configured with options that cannot be honored, for example `output.mode: 'file'` paired with a `group` option.",
677
681
  fix: "Fix the plugin options. A single-file output has nothing to group, so remove the `group` option or use `output.mode: 'directory'`."
678
682
  },
683
+ [diagnosticCode.invalidOptions]: {
684
+ title: "Invalid options",
685
+ cause: "A plugin declares an options `schema`, and the options passed to it in kubb.config.ts do not match it.",
686
+ fix: "Fix each option the message lists by its path, such as `enum.type`. The plugin documentation lists the accepted values."
687
+ },
679
688
  [diagnosticCode.postGenerateFailed]: {
680
689
  title: "Post-generate command failed",
681
690
  cause: "A post-generate command (`output.postGenerate`) exited with a non-zero status.",
@@ -950,10 +959,15 @@ var Diagnostics = class Diagnostics {
950
959
  };
951
960
  //#endregion
952
961
  //#region src/composables.ts
962
+ const kubbContextKey = Symbol.for("kubb.context");
963
+ const host = globalThis;
953
964
  /**
954
965
  * Holds the setup context of the plugin whose `setup` or `kubb:plugin:setup` is running. `KubbDriver` sets it for each plugin.
966
+ *
967
+ * The store lives on `globalThis`, so the ESM and CommonJS builds of `@kubb/core` share one. A plugin that a
968
+ * CommonJS config loads would otherwise read an empty store and fail with `KUBB_NO_CONTEXT`.
955
969
  */
956
- const kubbContext = new node_async_hooks.AsyncLocalStorage();
970
+ const kubbContext = host[kubbContextKey] ??= new node_async_hooks.AsyncLocalStorage();
957
971
  function requireContext(caller) {
958
972
  const ctx = kubbContext.getStore();
959
973
  if (ctx) return ctx;
@@ -1011,7 +1025,7 @@ function injectFile(file) {
1011
1025
  requireContext("injectFile").injectFile(file);
1012
1026
  }
1013
1027
  /**
1014
- * Returns the options the user passed to the plugin being set up, like `ctx.options`.
1028
+ * Returns the options of the plugin being set up, parsed by its options `schema` when it has one, like `ctx.options`.
1015
1029
  */
1016
1030
  function useOptions() {
1017
1031
  return requireContext("useOptions").options;
@@ -1107,12 +1121,49 @@ function checkDependencies({ name, dependencies, plugins }) {
1107
1121
  });
1108
1122
  }
1109
1123
  }
1110
- function defineExtension({ kind, meta, build }) {
1111
- const factory = (options) => build(options ?? {});
1112
- return Object.assign(factory, meta ? {
1113
- kind,
1114
- meta
1115
- } : { kind });
1124
+ function formatIssuePath(path) {
1125
+ return (path ?? []).map((segment) => String(typeof segment === "object" ? segment.key : segment)).join(".");
1126
+ }
1127
+ /**
1128
+ * Validates an extension's raw options against its `schema` and returns the parsed value, with the schema's defaults.
1129
+ * Throws `KUBB_INVALID_OPTIONS`, naming the extension and each issue path, when the options are invalid.
1130
+ * Refuses an async validator, because extension factories run synchronously while the config loads.
1131
+ */
1132
+ function parseExtensionOptions({ kind, meta, schema, input }) {
1133
+ const validated = schema["~standard"].validate(input);
1134
+ if (typeof validated.then === "function") {
1135
+ Promise.resolve(validated).catch(() => void 0);
1136
+ throw new Error(`The options schema of ${kind} "${meta.name}" validates asynchronously. Use a synchronous schema, because Kubb validates options while the config loads.`);
1137
+ }
1138
+ const result = validated;
1139
+ if (!result.issues) return result.value;
1140
+ const issues = result.issues.map((issue) => {
1141
+ const path = formatIssuePath(issue.path);
1142
+ return path ? `\`${path}\`: ${issue.message}` : issue.message;
1143
+ });
1144
+ throw new Diagnostics.Error({
1145
+ code: Diagnostics.code.invalidOptions,
1146
+ severity: "error",
1147
+ message: `Invalid options for ${kind} "${meta.name}". ${issues.join("; ")}`,
1148
+ help: `Fix the listed options of "${meta.name}" in kubb.config.ts. Its documentation lists the accepted values.`,
1149
+ location: { kind: "config" },
1150
+ ...kind === "plugin" && { plugin: meta.name }
1151
+ });
1152
+ }
1153
+ function defineExtension({ kind, meta, schema, build }) {
1154
+ const factory = (raw) => {
1155
+ const options = raw ?? {};
1156
+ return build({
1157
+ options,
1158
+ parsedOptions: schema && meta ? parseExtensionOptions({
1159
+ kind,
1160
+ meta,
1161
+ schema,
1162
+ input: options
1163
+ }) : options
1164
+ });
1165
+ };
1166
+ return Object.assign(factory, { kind }, meta && { meta }, schema && { schema });
1116
1167
  }
1117
1168
  //#endregion
1118
1169
  //#region src/definePlugin.ts
@@ -1157,7 +1208,7 @@ function assertPluginOption({ plugin, option, value, allowed }) {
1157
1208
  function definePlugin(input) {
1158
1209
  if (typeof input === "function") return defineExtension({
1159
1210
  kind: "plugin",
1160
- build: (options) => {
1211
+ build: ({ options }) => {
1161
1212
  const plugin = input(options);
1162
1213
  return {
1163
1214
  ...plugin,
@@ -1165,14 +1216,16 @@ function definePlugin(input) {
1165
1216
  };
1166
1217
  }
1167
1218
  });
1168
- const { meta, dependencies, enforce, hooks = {}, setup } = input;
1219
+ const { meta, schema, dependencies, enforce, hooks = {}, setup } = input;
1169
1220
  return defineExtension({
1170
1221
  kind: "plugin",
1171
1222
  meta,
1172
- build: (options) => ({
1223
+ schema,
1224
+ build: ({ options, parsedOptions }) => ({
1173
1225
  name: meta.name,
1174
1226
  meta,
1175
1227
  options,
1228
+ ...schema && { parsedOptions },
1176
1229
  dependencies,
1177
1230
  enforce,
1178
1231
  hooks,
@@ -1791,7 +1844,7 @@ var Hookable = class {
1791
1844
  }
1792
1845
  };
1793
1846
  //#endregion
1794
- //#region \0@oxc-project+runtime@0.148.0/helpers/esm/usingCtx.js
1847
+ //#region \0@oxc-project+runtime@0.153.0/helpers/esm/usingCtx.js
1795
1848
  function _usingCtx() {
1796
1849
  var r = "function" == typeof SuppressedError ? SuppressedError : function(r, e) {
1797
1850
  var n = Error();
@@ -2226,14 +2279,15 @@ var KubbDriver = class {
2226
2279
  const normalized = this.#sortPlugins(this.config.plugins.map((rawPlugin) => {
2227
2280
  if (rawPlugin.meta) checkCompatibility(rawPlugin.meta);
2228
2281
  const meta = resolveMeta(rawPlugin);
2282
+ const options = rawPlugin.parsedOptions ?? rawPlugin.options;
2229
2283
  return {
2230
2284
  name: meta.name,
2231
2285
  meta,
2232
- dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(rawPlugin.options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2286
+ dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2233
2287
  enforce: rawPlugin.enforce,
2234
2288
  hooks: rawPlugin.hooks,
2235
2289
  setup: rawPlugin.setup,
2236
- options: rawPlugin.options ?? defaultPluginOptions(),
2290
+ options: options ?? defaultPluginOptions(),
2237
2291
  resolver: createResolver({ pluginName: meta.name })
2238
2292
  };
2239
2293
  }));
@@ -3040,4 +3094,4 @@ Object.defineProperty(exports, "write", {
3040
3094
  }
3041
3095
  });
3042
3096
 
3043
- //# sourceMappingURL=KubbDriver-BcoHguLM.cjs.map
3097
+ //# sourceMappingURL=KubbDriver-DXDTb9_c.cjs.map