@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.
@@ -330,7 +330,7 @@ function formatMs(ms) {
330
330
  }
331
331
  //#endregion
332
332
  //#region package.json
333
- var version = "5.7.0";
333
+ var version = "5.8.0";
334
334
  //#endregion
335
335
  //#region src/constants.ts
336
336
  /**
@@ -443,6 +443,10 @@ const diagnosticCode = {
443
443
  */
444
444
  invalidPluginOptions: "KUBB_INVALID_PLUGIN_OPTIONS",
445
445
  /**
446
+ * An extension's options fail its options `schema`.
447
+ */
448
+ invalidOptions: "KUBB_INVALID_OPTIONS",
449
+ /**
446
450
  * A post-generate command (`output.postGenerate`) exited with a failure.
447
451
  */
448
452
  postGenerateFailed: "KUBB_POST_GENERATE_FAILED",
@@ -650,6 +654,11 @@ const diagnosticCatalog = {
650
654
  cause: "A plugin was configured with options that cannot be honored, for example `output.mode: 'file'` paired with a `group` option.",
651
655
  fix: "Fix the plugin options. A single-file output has nothing to group, so remove the `group` option or use `output.mode: 'directory'`."
652
656
  },
657
+ [diagnosticCode.invalidOptions]: {
658
+ title: "Invalid options",
659
+ cause: "A plugin declares an options `schema`, and the options passed to it in kubb.config.ts do not match it.",
660
+ fix: "Fix each option the message lists by its path, such as `enum.type`. The plugin documentation lists the accepted values."
661
+ },
653
662
  [diagnosticCode.postGenerateFailed]: {
654
663
  title: "Post-generate command failed",
655
664
  cause: "A post-generate command (`output.postGenerate`) exited with a non-zero status.",
@@ -924,10 +933,15 @@ var Diagnostics = class Diagnostics {
924
933
  };
925
934
  //#endregion
926
935
  //#region src/composables.ts
936
+ const kubbContextKey = Symbol.for("kubb.context");
937
+ const host = globalThis;
927
938
  /**
928
939
  * Holds the setup context of the plugin whose `setup` or `kubb:plugin:setup` is running. `KubbDriver` sets it for each plugin.
940
+ *
941
+ * The store lives on `globalThis`, so the ESM and CommonJS builds of `@kubb/core` share one. A plugin that a
942
+ * CommonJS config loads would otherwise read an empty store and fail with `KUBB_NO_CONTEXT`.
929
943
  */
930
- const kubbContext = new AsyncLocalStorage();
944
+ const kubbContext = host[kubbContextKey] ??= new AsyncLocalStorage();
931
945
  function requireContext(caller) {
932
946
  const ctx = kubbContext.getStore();
933
947
  if (ctx) return ctx;
@@ -985,7 +999,7 @@ function injectFile(file) {
985
999
  requireContext("injectFile").injectFile(file);
986
1000
  }
987
1001
  /**
988
- * Returns the options the user passed to the plugin being set up, like `ctx.options`.
1002
+ * Returns the options of the plugin being set up, parsed by its options `schema` when it has one, like `ctx.options`.
989
1003
  */
990
1004
  function useOptions() {
991
1005
  return requireContext("useOptions").options;
@@ -1081,12 +1095,49 @@ function checkDependencies({ name, dependencies, plugins }) {
1081
1095
  });
1082
1096
  }
1083
1097
  }
1084
- function defineExtension({ kind, meta, build }) {
1085
- const factory = (options) => build(options ?? {});
1086
- return Object.assign(factory, meta ? {
1087
- kind,
1088
- meta
1089
- } : { kind });
1098
+ function formatIssuePath(path) {
1099
+ return (path ?? []).map((segment) => String(typeof segment === "object" ? segment.key : segment)).join(".");
1100
+ }
1101
+ /**
1102
+ * Validates an extension's raw options against its `schema` and returns the parsed value, with the schema's defaults.
1103
+ * Throws `KUBB_INVALID_OPTIONS`, naming the extension and each issue path, when the options are invalid.
1104
+ * Refuses an async validator, because extension factories run synchronously while the config loads.
1105
+ */
1106
+ function parseExtensionOptions({ kind, meta, schema, input }) {
1107
+ const validated = schema["~standard"].validate(input);
1108
+ if (typeof validated.then === "function") {
1109
+ Promise.resolve(validated).catch(() => void 0);
1110
+ throw new Error(`The options schema of ${kind} "${meta.name}" validates asynchronously. Use a synchronous schema, because Kubb validates options while the config loads.`);
1111
+ }
1112
+ const result = validated;
1113
+ if (!result.issues) return result.value;
1114
+ const issues = result.issues.map((issue) => {
1115
+ const path = formatIssuePath(issue.path);
1116
+ return path ? `\`${path}\`: ${issue.message}` : issue.message;
1117
+ });
1118
+ throw new Diagnostics.Error({
1119
+ code: Diagnostics.code.invalidOptions,
1120
+ severity: "error",
1121
+ message: `Invalid options for ${kind} "${meta.name}". ${issues.join("; ")}`,
1122
+ help: `Fix the listed options of "${meta.name}" in kubb.config.ts. Its documentation lists the accepted values.`,
1123
+ location: { kind: "config" },
1124
+ ...kind === "plugin" && { plugin: meta.name }
1125
+ });
1126
+ }
1127
+ function defineExtension({ kind, meta, schema, build }) {
1128
+ const factory = (raw) => {
1129
+ const options = raw ?? {};
1130
+ return build({
1131
+ options,
1132
+ parsedOptions: schema && meta ? parseExtensionOptions({
1133
+ kind,
1134
+ meta,
1135
+ schema,
1136
+ input: options
1137
+ }) : options
1138
+ });
1139
+ };
1140
+ return Object.assign(factory, { kind }, meta && { meta }, schema && { schema });
1090
1141
  }
1091
1142
  //#endregion
1092
1143
  //#region src/definePlugin.ts
@@ -1131,7 +1182,7 @@ function assertPluginOption({ plugin, option, value, allowed }) {
1131
1182
  function definePlugin(input) {
1132
1183
  if (typeof input === "function") return defineExtension({
1133
1184
  kind: "plugin",
1134
- build: (options) => {
1185
+ build: ({ options }) => {
1135
1186
  const plugin = input(options);
1136
1187
  return {
1137
1188
  ...plugin,
@@ -1139,14 +1190,16 @@ function definePlugin(input) {
1139
1190
  };
1140
1191
  }
1141
1192
  });
1142
- const { meta, dependencies, enforce, hooks = {}, setup } = input;
1193
+ const { meta, schema, dependencies, enforce, hooks = {}, setup } = input;
1143
1194
  return defineExtension({
1144
1195
  kind: "plugin",
1145
1196
  meta,
1146
- build: (options) => ({
1197
+ schema,
1198
+ build: ({ options, parsedOptions }) => ({
1147
1199
  name: meta.name,
1148
1200
  meta,
1149
1201
  options,
1202
+ ...schema && { parsedOptions },
1150
1203
  dependencies,
1151
1204
  enforce,
1152
1205
  hooks,
@@ -1765,7 +1818,7 @@ var Hookable = class {
1765
1818
  }
1766
1819
  };
1767
1820
  //#endregion
1768
- //#region \0@oxc-project+runtime@0.148.0/helpers/esm/usingCtx.js
1821
+ //#region \0@oxc-project+runtime@0.153.0/helpers/esm/usingCtx.js
1769
1822
  function _usingCtx() {
1770
1823
  var r = "function" == typeof SuppressedError ? SuppressedError : function(r, e) {
1771
1824
  var n = Error();
@@ -2200,14 +2253,15 @@ var KubbDriver = class {
2200
2253
  const normalized = this.#sortPlugins(this.config.plugins.map((rawPlugin) => {
2201
2254
  if (rawPlugin.meta) checkCompatibility(rawPlugin.meta);
2202
2255
  const meta = resolveMeta(rawPlugin);
2256
+ const options = rawPlugin.parsedOptions ?? rawPlugin.options;
2203
2257
  return {
2204
2258
  name: meta.name,
2205
2259
  meta,
2206
- dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(rawPlugin.options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2260
+ dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2207
2261
  enforce: rawPlugin.enforce,
2208
2262
  hooks: rawPlugin.hooks,
2209
2263
  setup: rawPlugin.setup,
2210
- options: rawPlugin.options ?? defaultPluginOptions(),
2264
+ options: options ?? defaultPluginOptions(),
2211
2265
  resolver: createResolver({ pluginName: meta.name })
2212
2266
  };
2213
2267
  }));
@@ -2781,4 +2835,4 @@ function pluginNotFoundError({ pluginName, requiredBy }) {
2781
2835
  //#endregion
2782
2836
  export { clean as A, useConfig as C, formatMs as D, Diagnostics as E, write as F, toError as I, camelCase as L, isPathInside as M, read as N, getElapsedMs as O, toPosixPath as P, tryUseKubb as S, useOptions as T, kubbContext as _, _usingCtx as a, setOptions as b, Resolver as c, definePlugin as d, defineExtension as f, injectFile as g, addMacro as h, FileManager as i, exists as j, inParallel as k, getInputKind as l, addGenerator as m, pluginNotFoundError as n, Hookable as o, resolveMeta as p, createNodeCache as r, createResolver as s, KubbDriver as t, assertPluginOption as u, requirePlugin as v, useKubb as w, setResolver as x, setMacros as y };
2783
2837
 
2784
- //# sourceMappingURL=KubbDriver-DRobnIcp.js.map
2838
+ //# sourceMappingURL=KubbDriver-BrltQ0nL.js.map