asphodelos 0.3.0 → 0.3.2

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.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Effect, FileSystem, Schema, SchemaIssue, SchemaTransformation } from "effect";
2
2
  import { posix, resolve } from "node:path";
3
3
  import { pathToFileURL } from "node:url";
4
- //#region src/config/index.ts
4
+ // src/config/index.ts
5
5
  /** OpenAPI 3.x Components Object kinds, in declaration / config-field order. */
6
6
  const COMPONENT_KINDS = [
7
7
  "schemas",
@@ -53,17 +53,28 @@ var ConfigError = class extends Schema.TaggedError()("ConfigError", {
53
53
  * say which extensions are meant.
54
54
  */
55
55
  const TypeScriptPathSchema = Schema.declare(Schema.is(Schema.TemplateLiteral([Schema.String, ".ts"])), { message: "must be .ts file" });
56
- const InputSchema = Schema.declare(Schema.is(Schema.TemplateLiteral([Schema.String, Schema.Literals([
56
+ /** Extensions an input document is recognized by, and that a change has to carry to be worth regenerating for. */
57
+ const INPUT_EXTENSIONS = [
57
58
  ".yaml",
59
+ ".yml",
58
60
  ".json",
59
61
  ".tsp"
60
- ])])), { message: "must be .yaml | .json | .tsp" }).annotate({
62
+ ];
63
+ /**
64
+ * An input document: a path by its extension, or an `http(s)` URL whatever its path (a
65
+ * document served without an extension is OpenAPI by content).
66
+ */
67
+ function isInputPath(v) {
68
+ return typeof v === "string" && (INPUT_EXTENSIONS.some((extension) => v.endsWith(extension)) || /^https?:\/\//u.test(v));
69
+ }
70
+ const InputSchema = Schema.declare(isInputPath, { message: "must be .yaml | .yml | .json | .tsp, or an http(s) URL" }).annotate({
61
71
  title: "Input document",
62
- description: "OpenAPI or TypeSpec entry document that every generator reads.",
72
+ description: "OpenAPI or TypeSpec entry document that every generator reads: a file by its extension, or an http(s) URL.",
63
73
  examples: [
64
74
  "openapi.yaml",
65
75
  "./spec/openapi.json",
66
- "./spec/main.tsp"
76
+ "./spec/main.tsp",
77
+ "https://example.com/openapi"
67
78
  ]
68
79
  });
69
80
  /** Milliseconds, bounded so a mock cannot be configured to hang a request. */
@@ -293,7 +304,7 @@ const ConfigSchema = Schema.Struct({
293
304
  description: "Prefix the generated app is mounted on, emitted as `new Elysia({ prefix })`. Elysia wants the leading slash.",
294
305
  examples: ["/api", "/api/v1"]
295
306
  })),
296
- format: Schema.optionalKey(Schema.declare((u) => typeof u === "object" && u !== null, { message: "must be an oxfmt config object" }).annotate({
307
+ format: Schema.optionalKey(Schema.declare((value) => typeof value === "object" && value !== null, { message: "must be an oxfmt config object" }).annotate({
297
308
  title: "Formatter options",
298
309
  description: "oxfmt `FormatConfig` applied to every generated file. Defaults to printWidth 100, single quotes, no semicolons.",
299
310
  examples: [{
@@ -444,10 +455,10 @@ const ConfigSchema = Schema.Struct({
444
455
  seen.set(key, field);
445
456
  }
446
457
  return true;
447
- }, { message: "every generator needs its own output path" }), Schema.makeFilter((v) => {
458
+ }, { description: "every generator needs its own output path" }), Schema.makeFilter((v) => {
448
459
  const [first] = [...v.eden ? ["eden"] : [], ...HOOK_KINDS.filter((kind) => v[kind] !== void 0)];
449
460
  return v.client === void 0 && first !== void 0 ? `${first} needs the top-level client it imports: add client: { output }.` : true;
450
- }, { message: "every file that calls the client needs the client to be generated" })).annotate({
461
+ }, { description: "every file that calls the client needs the client to be generated" })).annotate({
451
462
  title: "asphodelos config",
452
463
  description: "Everything `asphodelos` generates from one OpenAPI or TypeSpec document. Only `input` is required; the app entry is always written, and each remaining field opts one generator in."
453
464
  });
@@ -467,6 +478,13 @@ function parseConfig(config) {
467
478
  }));
468
479
  }
469
480
  const reloads = { count: 0 };
481
+ /** Imports a module by specifier, a failure turned into a `ConfigError`. */
482
+ function importModule(specifier) {
483
+ return Effect.tryPromise({
484
+ try: () => import(specifier),
485
+ catch: (error) => new ConfigError({ message: error instanceof Error ? error.message : String(error) })
486
+ });
487
+ }
470
488
  /**
471
489
  * Imports the config module, bypassing the loader cache when asked.
472
490
  *
@@ -481,10 +499,6 @@ const reloads = { count: 0 };
481
499
  */
482
500
  function importConfigModule(abs, reload) {
483
501
  return Effect.gen(function* () {
484
- const importModule = (specifier) => Effect.tryPromise({
485
- try: () => import(specifier),
486
- catch: (error) => new ConfigError({ message: error instanceof Error ? error.message : String(error) })
487
- });
488
502
  if (!reload) return yield* importModule(pathToFileURL(abs).href);
489
503
  const fs = yield* FileSystem.FileSystem;
490
504
  const copy = resolve(abs, "..", `.asphodelos.config.${String(process.pid)}.${String(reloads.count += 1)}.ts`);
@@ -524,5 +538,4 @@ function readConfig(configPath, reload = false) {
524
538
  function defineConfig(config) {
525
539
  return config;
526
540
  }
527
- //#endregion
528
- export { COMPONENT_KINDS, ConfigError, HOOK_KINDS, defineConfig, parseConfig, readConfig };
541
+ export { COMPONENT_KINDS, ConfigError, HOOK_KINDS, INPUT_EXTENSIONS, defineConfig, parseConfig, readConfig };
@@ -1,10 +1,14 @@
1
- import { t as __exportAll } from "./rolldown-runtime-wcPFST8Q.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-D8jQpDG7.mjs";
2
2
  import { Data, Effect } from "effect";
3
3
  import path from "node:path";
4
4
  import SwaggerParser from "@apidevtools/swagger-parser";
5
- import { NodeHost, compile } from "@typespec/compiler";
6
- import { getOpenAPI3 } from "@typespec/openapi3";
7
- //#region src/openapi/index.ts
5
+ // src/openapi/index.ts
6
+ /**
7
+ * Uses `bundle()` to resolve external $refs while preserving internal ones, so a split document
8
+ * folds into one whose components are still referenced by name.
9
+ *
10
+ * @see https://apitools.dev/swagger-parser/docs/
11
+ */
8
12
  var openapi_exports = /* @__PURE__ */ __exportAll({ parseOpenAPI: () => parseOpenAPI });
9
13
  /** The document could not be read, compiled or parsed into an OpenAPI object. */
10
14
  var OpenAPIError = class extends Data.TaggedError("OpenAPIError") {};
@@ -12,15 +16,19 @@ function parseOpenAPI(input) {
12
16
  return Effect.tryPromise({
13
17
  try: async () => {
14
18
  if (!input.endsWith(".tsp")) return await SwaggerParser.bundle(input);
19
+ const [{ compile, NodeHost }, { getOpenAPI3 }] = await Promise.all([import("@typespec/compiler"), import("@typespec/openapi3")]);
15
20
  const program = await compile(NodeHost, path.resolve(input), { noEmit: true });
16
- if (program.diagnostics.length > 0) throw new Error(`TypeSpec compile failed:\n${program.diagnostics.map((d) => d.message).join("\n")}`);
21
+ if (program.diagnostics.length > 0) throw new Error(`TypeSpec compile failed:\n${program.diagnostics.map((diagnostic) => diagnostic.message).join("\n")}`);
17
22
  const [record] = await getOpenAPI3(program);
18
23
  const document = record && ("document" in record ? record.document : record.versions[0]?.document);
19
24
  if (!document) throw new Error(`TypeSpec emitted no OpenAPI document: ${input}`);
20
25
  return document;
21
26
  },
22
- catch: (error) => new OpenAPIError({ message: error instanceof Error ? error.message : String(error) })
27
+ catch: (error) => {
28
+ if (!(error instanceof Error)) return new OpenAPIError({ message: String(error) });
29
+ const code = "ioErrorCode" in error ? error.ioErrorCode : void 0;
30
+ return new OpenAPIError({ message: (!error.message.trim() || error.message.trimEnd().endsWith(":")) && typeof code === "string" && code ? `${error.message}${code}` : error.message });
31
+ }
23
32
  });
24
33
  }
25
- //#endregion
26
34
  export { parseOpenAPI as n, openapi_exports as t };
@@ -1,4 +1,4 @@
1
- //#region \0rolldown/runtime.js
1
+ // \0rolldown/runtime.js
2
2
  var __defProp = Object.defineProperty;
3
3
  var __exportAll = (all, no_symbols) => {
4
4
  let target = {};
@@ -9,5 +9,4 @@ var __exportAll = (all, no_symbols) => {
9
9
  if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
10
  return target;
11
11
  };
12
- //#endregion
13
12
  export { __exportAll as t };