asphodelos 0.5.1 → 0.5.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/README.md CHANGED
@@ -292,6 +292,12 @@ const { data, error } = await client.users({ id: '1' }).get()
292
292
  The app is imported for its type only, so a browser bundle never pulls the server in. The project
293
293
  needs `@elysiajs/eden`, with `elysia` resolving to one copy.
294
294
 
295
+ A path key with a query string right after a path parameter, such as `/jobs/{id}?beta=true`, is
296
+ left out of the app, its types, the mock and the Eden wrappers, with a warning: Elysia reads `:id?`
297
+ as an optional parameter, which would hide the routes beside it (`/jobs/{id}/cancel`) from the
298
+ client. The hooks still reach it, with `fetch`. A module generated before keeps its handlers on
299
+ regeneration, so remove such a route from it by hand.
300
+
295
301
  ### Imports Between Generated Files
296
302
 
297
303
  Nothing is configured per file: how one generated file imports another follows from where the two
package/dist/cli.mjs CHANGED
@@ -4,7 +4,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
4
4
  import { Console, Effect, FileSystem, Option, Path, Ref, Stream } from "effect";
5
5
  import { CliError, Command, Flag } from "effect/cli";
6
6
  // package.json
7
- var version = "0.5.1";
7
+ var version = "0.5.2";
8
8
  var description = "Asphodelos is a code generator from OpenAPI to Elysia";
9
9
  // src/cli/index.ts
10
10
  const COMMAND_NAME = "asphodelos";
@@ -48,7 +48,7 @@ function runJobs(config) {
48
48
  const [{ parseOpenAPI }, { FormatOptions }, { cleanSplitOutputs, makeJob }] = yield* Effect.promise(() => Promise.all([
49
49
  import("./openapi-4yNuDK_n.mjs").then((n) => n.t),
50
50
  import("./format-DC258z--.mjs").then((n) => n.r),
51
- import("./shared-CQsoa7gV.mjs").then((n) => n.s)
51
+ import("./shared-Ch_ArUGQ.mjs").then((n) => n.s)
52
52
  ]));
53
53
  const jobs = makeJob(yield* parseOpenAPI(config.input), config);
54
54
  yield* cleanSplitOutputs(jobs);
@@ -86,7 +86,7 @@ function reportJobs(config, previous) {
86
86
  onFailure: (error) => Console.error(`❌ ${error.message}`),
87
87
  onSuccess: (report) => Console.log(report)
88
88
  }));
89
- const { outsideSources } = yield* Effect.promise(() => import("./shared-CQsoa7gV.mjs").then((n) => n.s));
89
+ const { outsideSources } = yield* Effect.promise(() => import("./shared-Ch_ArUGQ.mjs").then((n) => n.s));
90
90
  const inputDirectory = path.dirname(path.resolve(config.input));
91
91
  return {
92
92
  inputDirectory,
@@ -1875,6 +1875,23 @@ function renameOperationParams(operation, renames) {
1875
1875
  parameters
1876
1876
  };
1877
1877
  }
1878
+ const warnedQueryKeys = /* @__PURE__ */ new Set();
1879
+ /**
1880
+ * Whether a path key can become an Elysia route path.
1881
+ *
1882
+ * A key with a query string right after a `{param}` segment, such as `/jobs/{id}?beta=true`, cannot:
1883
+ * Elysia reads `:id?` as an optional parameter, so the route tree Eden types would lose the routes
1884
+ * beside it (`/jobs/{id}/cancel`). Such a key is left out of the app, its types, the mock and the
1885
+ * Eden wrappers, with a warning once per key; the hooks reach it with `fetch`.
1886
+ */
1887
+ function isElysiaRoutePath(pathKey) {
1888
+ if (!/\{[^/{}]+\}\?/u.test(pathKey)) return true;
1889
+ if (!warnedQueryKeys.has(pathKey)) {
1890
+ warnedQueryKeys.add(pathKey);
1891
+ console.warn(`[asphodelos] path '${pathKey}' has a query string after a path parameter, which Elysia reads as an optional parameter; it is left out of the app, its types, the mock and the Eden wrappers`);
1892
+ }
1893
+ return false;
1894
+ }
1878
1895
  // src/helper/openapi.ts
1879
1896
  /**
1880
1897
  * Generates prefix-only query-key getters (`get<Prefix>Key() => ['<prefix>']`)
@@ -2081,7 +2098,7 @@ function walk(path, resource, pathItem, api, canonical) {
2081
2098
  });
2082
2099
  }
2083
2100
  function operationsByResource(api) {
2084
- const canonical = canonicalParamNames([...Object.keys(api.paths ?? {}), ...Object.keys(api.webhooks ?? {}).map((event) => `/webhooks/${event}`)]);
2101
+ const canonical = canonicalParamNames([...Object.keys(api.paths ?? {}).filter(isElysiaRoutePath), ...Object.keys(api.webhooks ?? {}).map((event) => `/webhooks/${event}`)]);
2085
2102
  const grouped = /* @__PURE__ */ new Map();
2086
2103
  const collect = (resource, item) => {
2087
2104
  const bucket = grouped.get(resource) ?? [];
@@ -2089,7 +2106,7 @@ function operationsByResource(api) {
2089
2106
  grouped.set(resource, bucket);
2090
2107
  };
2091
2108
  for (const [path, pathItem] of Object.entries(api.paths ?? {})) {
2092
- if (!pathItem) continue;
2109
+ if (!pathItem || !isElysiaRoutePath(path)) continue;
2093
2110
  for (const [resource, route] of walk(path, resourceName(path), pathItem, api, canonical)) collect(resource, route);
2094
2111
  }
2095
2112
  for (const [event, pathItem] of Object.entries(api.webhooks ?? {})) {
@@ -2394,7 +2411,7 @@ function eden(openAPI, output, importPath, client, basePath) {
2394
2411
  const prefix = basePath && basePath !== "/" ? basePath : "";
2395
2412
  const operations = [];
2396
2413
  for (const [pathStr, pathItem] of Object.entries(openAPI.paths ?? {})) {
2397
- if (!pathItem) continue;
2414
+ if (!pathItem || !isElysiaRoutePath(pathStr)) continue;
2398
2415
  for (const method of HTTP_METHODS) {
2399
2416
  const operation = pathItem[method];
2400
2417
  if (!operation) continue;
@@ -2832,7 +2849,8 @@ function fetchTarget(fullPath, method, operation) {
2832
2849
  const index = names.indexOf(n);
2833
2850
  return `\${encodeURIComponent(${exprs[index] ?? params[index]})}`;
2834
2851
  });
2835
- return hasQuery ? `\`\${${ORIGIN}}${inner}\${search.size ? \`?\${search}\` : ''}\`` : `\`\${${ORIGIN}}${inner}\``;
2852
+ const separator = inner.includes("?") ? "&" : "?";
2853
+ return hasQuery ? `\`\${${ORIGIN}}${inner}\${search.size ? \`${separator}\${search}\` : ''}\`` : `\`\${${ORIGIN}}${inner}\``;
2836
2854
  };
2837
2855
  const search = (options) => hasQuery ? `const search=new URLSearchParams();for(const[k,v]of Object.entries(${options}?.query??{})){if(v===undefined)continue;if(Array.isArray(v)){for(const x of v){search.append(k,String(x))}}else{search.set(k,String(v))}}` : "";
2838
2856
  return {
@@ -3982,7 +4000,7 @@ function makeMock(spec, options = {}) {
3982
4000
  };
3983
4001
  const components = spec.components;
3984
4002
  const routes = Object.entries(spec.paths ?? {}).flatMap(([path, pathItem]) => {
3985
- if (!pathItem) return [];
4003
+ if (!pathItem || !isElysiaRoutePath(path)) return [];
3986
4004
  return HTTP_METHODS.flatMap((method) => {
3987
4005
  const operation = pathItem[method];
3988
4006
  if (!operation) return [];
@@ -4219,7 +4237,7 @@ function makeRoutes(openAPI, prefix) {
4219
4237
  const out = [];
4220
4238
  const prefixKeys = prefix && prefix !== "/" ? pathToKeys(prefix) : [];
4221
4239
  for (const [pathStr, pathItem] of Object.entries(openAPI.paths ?? {})) {
4222
- if (!pathItem) continue;
4240
+ if (!pathItem || !isElysiaRoutePath(pathStr)) continue;
4223
4241
  const keys = [...prefixKeys, ...pathToKeys(pathStr)];
4224
4242
  for (const method of HTTP_METHODS) {
4225
4243
  const operation = pathItem[method];
@@ -4271,8 +4289,8 @@ function collectRefs(openAPI) {
4271
4289
  }
4272
4290
  if ("schema" in p) enqueue(parameterSchema(p));
4273
4291
  };
4274
- for (const [, pathItem] of Object.entries(openAPI.paths ?? {})) {
4275
- if (!pathItem) continue;
4292
+ for (const [pathStr, pathItem] of Object.entries(openAPI.paths ?? {})) {
4293
+ if (!pathItem || !isElysiaRoutePath(pathStr)) continue;
4276
4294
  for (const method of HTTP_METHODS) {
4277
4295
  const op = pathItem[method];
4278
4296
  if (!op) continue;
@@ -1,6 +1,6 @@
1
1
  import { INPUT_EXTENSIONS, parseConfig } from "../index.mjs";
2
2
  import { t as FormatOptions } from "../format-DC258z--.mjs";
3
- import { a as makeJob, c as fileSystemLayer, i as jobTargets, n as isInsideDirectory, o as outsideSources, r as isUserCodeJob, t as cleanSplitOutputs } from "../shared-CQsoa7gV.mjs";
3
+ import { a as makeJob, c as fileSystemLayer, i as jobTargets, n as isInsideDirectory, o as outsideSources, r as isUserCodeJob, t as cleanSplitOutputs } from "../shared-Ch_ArUGQ.mjs";
4
4
  import { n as parseOpenAPI } from "../openapi-4yNuDK_n.mjs";
5
5
  import { Effect, FileSystem, Result } from "effect";
6
6
  import path from "node:path";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asphodelos",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Asphodelos is a code generator from OpenAPI to Elysia",
5
5
  "keywords": [
6
6
  "elysia",