asphodelos 0.0.1 → 0.3.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.
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  - TypeBox component bundles (schemas, responses, parameters, …)
11
11
  - Eden Treaty wrappers and a self-contained `App` type
12
12
  - Client library hooks (SWR, TanStack Query, Preact Query, Solid Query, Vue Query, Svelte Query, Angular Query)
13
- - `bun:test` tests and a mock server
13
+ - A mock server
14
14
 
15
15
  Asphodelos targets the [Bun](https://bun.sh/) runtime.
16
16
 
@@ -22,15 +22,9 @@ Asphodelos targets the [Bun](https://bun.sh/) runtime.
22
22
  bun add -D asphodelos
23
23
  ```
24
24
 
25
- ### CLI
26
-
27
- ```bash
28
- bunx asphodelos path/to/input.{yaml,json,tsp} -o path/to/output.ts
29
- ```
30
-
31
25
  ### Configuration File
32
26
 
33
- Create `asphodelos.config.ts`:
27
+ The CLI runs the config file in the working directory. Create `asphodelos.config.ts`:
34
28
 
35
29
  ```ts
36
30
  import { defineConfig } from 'asphodelos'
@@ -51,18 +45,14 @@ bunx asphodelos
51
45
 
52
46
  ```text
53
47
  DESCRIPTION
54
- Generate Elysia code from OpenAPI or TypeSpec
48
+ Asphodelos is a code generator from OpenAPI to Elysia
55
49
 
56
50
  USAGE
57
- asphodelos [flags] [<input>]
58
-
59
- ARGUMENTS
60
- input input.{yaml,json,tsp} OpenAPI (.yaml, .json) or TypeSpec (.tsp) document to generate from (optional)
51
+ asphodelos [flags]
61
52
 
62
53
  FLAGS
63
- --output, -o output.ts TypeScript file the generated app is written to
64
- --config, -c file Config file to run (default: ./asphodelos.config.ts)
65
- --watch, -w Rerun the config on every change to its documents or itself
54
+ --config, -c file Config file to run (default: ./asphodelos.config.ts)
55
+ --watch, -w Rerun the config on every change to its documents or itself
66
56
 
67
57
  GLOBAL FLAGS
68
58
  --help, -h Show help information
@@ -72,9 +62,6 @@ GLOBAL FLAGS
72
62
  --log-level <all|trace|debug|info|warn|warning|error|fatal|none> Sets the minimum log level (choices: all, trace, debug, info, warn, warning, error, fatal, none)
73
63
 
74
64
  EXAMPLES
75
- # Generate a single app from one document
76
- asphodelos openapi.yaml -o src/index.ts
77
-
78
65
  # Run every generator declared in ./asphodelos.config.ts
79
66
  asphodelos
80
67
 
@@ -85,8 +72,8 @@ EXAMPLES
85
72
  asphodelos --watch
86
73
  ```
87
74
 
88
- With an `<input>` the CLI generates one app and ignores any config file. With no `<input>` it
89
- runs the config file.
75
+ The CLI runs the config file, `asphodelos.config.ts` in the working directory or the one
76
+ `--config` names, and nothing else: every input and output is declared there.
90
77
 
91
78
  ### Watch Mode
92
79
 
@@ -95,7 +82,10 @@ bunx asphodelos --watch
95
82
  ```
96
83
 
97
84
  Reruns the config on every change to the input documents or to the config itself, and keeps
98
- watching when a run fails. It cannot be combined with `<input>` / `--output`.
85
+ watching when a run fails. The whole directory of the input document is watched, so TypeSpec
86
+ imports and `$ref` files beside it trigger a rerun too — and so do the files a `$ref` or an import
87
+ reaches outside that directory. An input directory that is removed and recreated, or that does not
88
+ exist yet, is picked up when it appears.
99
89
 
100
90
  ### Example
101
91
 
@@ -193,14 +183,15 @@ export default defineConfig({
193
183
  })
194
184
  ```
195
185
 
196
- - **What it watches**: `asphodelos.config.ts`, and every `.yaml` / `.json` / `.tsp` in the
197
- directory of `input` — a `$ref` or a TypeSpec import can reach a sibling file.
186
+ - **What it watches**: `asphodelos.config.ts`, every `.yaml` / `.json` / `.tsp` in the directory
187
+ of `input` — a `$ref` or a TypeSpec import can reach a sibling file — and every file the document
188
+ reads from outside that directory.
198
189
  - **When it regenerates**: a config save always regenerates. A document save regenerates only when
199
190
  the documents' contents changed, or a generated file has gone missing; a save with nothing new in
200
191
  it is skipped.
201
192
  - **When the browser reloads**: only when a generated file actually changed.
202
193
  - **Cleanup**: an output that the config or the document no longer produces is removed. The app
203
- entry, `modules/` and the generated tests are never removed, because they hold your code.
194
+ entry and `modules/` are never removed, because they hold your code.
204
195
  - A config that fails to load is reported and the previous one stays in effect; the next save
205
196
  retries, so a typo never needs a restart. Every run is queued, so two never overlap.
206
197
 
@@ -218,19 +209,107 @@ export default defineConfig({
218
209
  })
219
210
  ```
220
211
 
221
- ### Wrapper Functions
212
+ ### Generated Client
222
213
 
223
- Generate one wrapper per operation over a Treaty client you supply.
214
+ Generate the Treaty client itself, typed by the app entry. Its address comes from the
215
+ environment: `baseUrl` names the variable, and the generated file asserts it is set.
224
216
 
225
217
  ```ts
226
218
  export default defineConfig({
227
219
  input: 'openapi.yaml',
228
- eden: {
229
- output: 'src/eden.ts',
230
- import: './lib', // module exporting `client` = treaty<App>(...)
231
- client: 'client',
232
- docs: true, // JSDoc above each wrapper
220
+ output: 'src/index.ts',
221
+ client: { output: 'src/lib/client.ts', baseUrl: { env: 'VITE_API_URL' } },
222
+ })
223
+ ```
224
+
225
+ ```ts
226
+ // src/lib/client.ts
227
+ import { treaty } from '@elysiajs/eden'
228
+ import type { app } from '../index'
229
+
230
+ const origin = import.meta.env.VITE_API_URL!
231
+
232
+ export const client = treaty<typeof app>(origin)
233
+ ```
234
+
235
+ `source: 'process.env'` reads `process.env` instead, for code Node.js or Bun runs:
236
+
237
+ ```ts
238
+ client: { output: 'src/lib/client.ts', baseUrl: { env: 'API_URL', source: 'process.env' } }
239
+ // const origin = process.env.API_URL!
240
+ ```
241
+
242
+ A value a module exports is used as written — `value` is the export followed by what is read
243
+ from it, and the module answers for it being there:
244
+
245
+ ```ts
246
+ client: { output: 'src/lib/client.ts', baseUrl: { import: '@/env', value: 'env.API_URL' } }
247
+ // import { env } from '@/env'
248
+ // const origin = env.API_URL
249
+ ```
250
+
251
+ `sameOrigin: true` is for an app a host framework such as TanStack Start or Next.js serves beside
252
+ its pages. In a browser the client sends to the page's own origin, so there is no CORS to
253
+ configure; the variable is read only without a window, for a server render or a loader. Needs the
254
+ DOM lib.
255
+
256
+ ```ts
257
+ client: { output: 'src/lib/client.ts', baseUrl: { env: 'API_URL', source: 'process.env' }, sameOrigin: true }
258
+ // const origin = typeof window === 'undefined' ? process.env.API_URL! : window.location.origin
259
+ ```
260
+
261
+ The `index.ts` beside the client re-exports it, so it is imported as `./lib` — by `eden`, by the
262
+ hooks, and by your own code:
263
+
264
+ ```ts
265
+ import { client } from './lib'
266
+
267
+ const { data, error } = await client.users({ id: '1' }).get()
268
+ ```
269
+
270
+ The app is imported for its type only, so a browser bundle never pulls the server in. The project
271
+ needs `@elysiajs/eden`, with `elysia` resolving to one copy.
272
+
273
+ ### Imports Between Generated Files
274
+
275
+ Nothing is configured per file: how one generated file imports another follows from where the two
276
+ are written.
277
+
278
+ - In the same package, relatively — `./lib`, `../components/schemas`.
279
+ - Under the app entry's directory with `pathAlias: '@/'`, through the alias — `@/lib`,
280
+ `@/components/schemas`.
281
+ - In different packages (each with its own `package.json`), by the name the output is published
282
+ as: the top-level `package` for the app entry, `client.package` for the client, and `package` on
283
+ each component section (`components.schemas.package`, `components.responses.package`, …), or on
284
+ `components` in single-file mode. A relative path never crosses a package.
285
+
286
+ ```ts
287
+ // apps/elysia/asphodelos.config.ts — the app, the client and the hooks in three packages
288
+ export default defineConfig({
289
+ input: 'openapi.yaml',
290
+ output: 'src/index.ts',
291
+ package: '@packages/elysia', // the client imports `app` from here
292
+ client: {
293
+ output: '../eden/src/client.ts',
294
+ baseUrl: { env: 'VITE_API_URL' },
295
+ package: '@packages/eden', // the hooks import it from here
233
296
  },
297
+ 'tanstack-query': { output: '../react/src/api/hooks.ts' },
298
+ })
299
+ ```
300
+
301
+ `@packages/elysia` exports `app` from its entry, `@packages/eden` exports the `index.ts` beside the client,
302
+ and `elysia` resolves to one copy across the workspace.
303
+
304
+ ### Wrapper Functions
305
+
306
+ Generate one wrapper per operation over the generated client.
307
+
308
+ ```ts
309
+ export default defineConfig({
310
+ input: 'openapi.yaml',
311
+ client: { output: 'src/lib/client.ts', baseUrl: { env: 'VITE_API_URL' } },
312
+ eden: { output: 'src/eden.ts' },
234
313
  })
235
314
  ```
236
315
 
@@ -241,18 +320,37 @@ Supported: SWR, TanStack Query, Preact Query, Solid Query, Vue Query, Svelte Que
241
320
  ```ts
242
321
  export default defineConfig({
243
322
  input: 'openapi.yaml',
244
- 'tanstack-query': {
245
- output: './src/tanstack-query',
246
- import: '../lib',
247
- split: true,
248
- client: 'client',
249
- },
323
+ client: { output: 'src/lib/client.ts', baseUrl: { env: 'VITE_API_URL' } },
324
+ 'tanstack-query': { output: './src/tanstack-query.ts' },
250
325
  })
251
326
  ```
252
327
 
253
- TanStack-family mutations also get a `<operation>MutationOptions()` factory, and SWR queries a
254
- `useImmutable<Operation>` hook. The generated hooks are compiled and run against the real
255
- libraries in [`test/`](test/README.md).
328
+ The hooks are written into one file and import the generated client, so a `client` block comes
329
+ with them.
330
+
331
+ Every operation is named by its method and path: a GET on `/users/{id}` becomes `useUsersId`, with
332
+ `getUsersIdQueryKey` and `getUsersIdQueryOptions` beside it; a POST on `/users` becomes
333
+ `usePostUsers`, with `getPostUsersMutationKey` and `getPostUsersMutationOptions`. Path parameters
334
+ come first, then one `options` object, then the library's own trailing argument (a `QueryClient`,
335
+ or Angular's inject options):
336
+
337
+ ```ts
338
+ const user = useUsersId(
339
+ { id: '1' },
340
+ {
341
+ query: { staleTime: 1_000 }, // the library's options, minus the key and the query function
342
+ options: { headers: { 'x-trace': 'a' } }, // the client's request options
343
+ },
344
+ )
345
+
346
+ const create = usePostUsers({ mutation: { onSuccess: () => invalidate() } })
347
+ create.mutate({ body: { name: 'Alice' } })
348
+ ```
349
+
350
+ A query's `options` are part of its key, headers excluded; a mutation honors a `mutationKey` of
351
+ your own over the generated one. SWR hooks take `{ swr, options }` instead, where `swr` adds
352
+ `swrKey` (replaces the generated key) and `enabled` (a `false` turns the key into `null`), and
353
+ return the key they used beside SWR's result.
256
354
 
257
355
  ### Infinite Query (`x-pagination`)
258
356
 
@@ -265,43 +363,32 @@ paths:
265
363
  x-pagination: true
266
364
  ```
267
365
 
268
- The paging rules go in a `pagination` argument:
269
-
270
- ```ts
271
- const items = useListItemsInfinite(undefined, {
272
- initialPageParam: 0,
273
- getNextPageParam: (lastPage) => lastPage.nextPage,
274
- buildInit: (pageParam) => ({ query: { page: String(pageParam) } }),
275
- })
276
- ```
277
-
278
- Vue Query takes only `buildInit` there, with `initialPageParam` / `getNextPageParam` in the third
279
- argument. SWR takes `buildInit(pageIndex, previousPage)` and stops when it returns `null`.
280
-
281
- ## Test & Mock Generation
282
-
283
- ### Test Generation
284
-
285
- Generates `bun:test` tests that call `app.handle(...)` on the real app: a success-status test per
286
- operation, plus `401` / `404` tests when the spec declares them. They start red against the empty
287
- handlers; re-running keeps your hand-written tests.
366
+ The paging rules go in a `pagination` argument, ahead of the options:
288
367
 
289
368
  ```ts
290
- export default defineConfig({
291
- input: 'openapi.yaml',
292
- test: {
293
- output: 'src/app.test.ts', // or `split: true` for modules/<resource>/index.test.ts
294
- pathAlias: '@/', // optional: import the app through a tsconfig alias
369
+ const items = useInfiniteItems(
370
+ {
371
+ initialPageParam: 0,
372
+ getNextPageParam: (lastPage) => lastPage.nextPage,
373
+ getRequestArgs: (options, pageParam) => ({
374
+ ...options,
375
+ query: { ...options.query, page: Number(pageParam) },
376
+ }),
295
377
  },
296
- })
378
+ { options: { query: { page: 0 } } },
379
+ )
297
380
  ```
298
381
 
299
- ### Mock Server Generation
382
+ Vue Query takes only `getRequestArgs` there, with `initialPageParam` / `getNextPageParam` in
383
+ `options.query`. SWR takes `pagination.getRequestArgs(options, index)` inside its options object,
384
+ and a `swr.swrKey` loader that returns `null` stops the paging.
385
+
386
+ ## Mock Server Generation
300
387
 
301
388
  Generates a standalone Elysia server that answers every operation with a
302
389
  [`@faker-js/faker`](https://fakerjs.dev/) mock of its success response. Secured operations that
303
390
  declare a `401` answer it when the credential is missing, and path parameters answer a declared
304
- `404` for the same sentinel values the generated tests send.
391
+ `404` for a sentinel value the schema accepts but no record is likely to carry.
305
392
 
306
393
  ```ts
307
394
  export default defineConfig({
@@ -329,12 +416,25 @@ declare answers `500` with an `application/problem+json` body saying what is mis
329
416
 
330
417
  ## Full Config Reference
331
418
 
332
- With `split: true`, `output` is a directory (one file per entry + `index.ts` barrel); otherwise it
333
- is a single `.ts` file. `components.output` and the per-type components are mutually exclusive.
334
-
335
- A split directory belongs to the generator: every run empties its `.ts` files before refilling it,
336
- so an entry that leaves the document does not leave an orphaned file behind. Subdirectories, other
337
- files and the single-file outputs of other generators are left alone.
419
+ Every generator is opted in by adding its section. `defineConfig` types the config — a value of
420
+ the wrong shape, an option that was removed, or hooks without the `client` block are refused as
421
+ you type — and the CLI checks the rest when it runs:
422
+
423
+ - A key the config does not know is an error, not a typo that is dropped.
424
+ - Every generator needs its own `output`. Two generators writing to one path is an error.
425
+ - `components.output` (one file) and the per-type `components.*` sections are mutually exclusive.
426
+ - A component section with `split: true` writes one file per entry plus an `index.ts` barrel into a
427
+ directory; otherwise `output` is a single `.ts` file (a directory stands for its `index.ts`).
428
+ - A split directory belongs to the generator: every run empties its `.ts` files before refilling
429
+ it, so an entry that leaves the document does not leave an orphaned file behind. Subdirectories,
430
+ other files and the single-file outputs of other generators are left alone.
431
+ - Imports between generated files are worked out from where they are written; see
432
+ [Imports Between Generated Files](#imports-between-generated-files). `package` names are
433
+ module specifiers.
434
+ - `prefix` must start with `/`.
435
+ - `eden` and the hooks import the generated client, so they need the `client` block.
436
+ - The hooks (`swr`, `tanstack-query`, …) are always one file; `split` is no longer an option
437
+ there, and neither is the `test` generator, `eden.docs`, or `import` and `client` on a section.
338
438
 
339
439
  ```ts
340
440
  import { defineConfig } from 'asphodelos'
@@ -346,74 +446,78 @@ export default defineConfig({
346
446
  prefix: '/api/v3', // new Elysia({ prefix })
347
447
  port: '3000',
348
448
  integration: false, // true: no .listen(), a host framework owns the server
349
- pathAlias: false, // true: `@/` imports between generated files
449
+ // pathAlias: '@/', // import prefix for the app entry's directory: `@/index`, `@/lib`
450
+ // package: '@packages/elysia', // the app's package name, for a client in another package
350
451
  readonly: false, // wrap top-level schemas in t.Readonly(...)
351
452
  // format: {}, // oxfmt FormatConfig
352
453
 
353
- // `exportTypes` adds `Static<typeof XSchema>` aliases.
454
+ // `exportTypes` adds `Static<typeof XSchema>` aliases. Every section, and `components` in
455
+ // single-file mode, takes a `package`: the name other packages import it by when it is written
456
+ // into a package of its own.
354
457
  components: {
355
458
  // output: 'src/components.ts', // single-file mode
459
+ // package: '@packages/components',
356
460
 
357
461
  schemas: {
358
462
  output: 'src/components/schemas',
359
463
  split: true,
360
- import: '../schemas',
361
464
  exportTypes: true,
465
+ // package: '@packages/schemas',
362
466
  },
363
467
  responses: {
364
468
  output: 'src/components/responses',
365
469
  split: true,
366
- import: '../responses',
367
470
  exportTypes: true,
471
+ // package: '@packages/responses',
368
472
  },
369
473
  parameters: {
370
474
  output: 'src/components/parameters',
371
475
  split: true,
372
- import: '../parameters',
373
476
  exportTypes: true,
477
+ // package: '@packages/parameters',
374
478
  },
375
479
  requestBodies: {
376
480
  output: 'src/components/requestBodies',
377
481
  split: true,
378
- import: '../requestBodies',
379
482
  exportTypes: true,
483
+ // package: '@packages/requestBodies',
380
484
  },
381
485
  headers: {
382
486
  output: 'src/components/headers',
383
487
  split: true,
384
- import: '../headers',
385
488
  exportTypes: true,
489
+ // package: '@packages/headers',
386
490
  },
387
491
  mediaTypes: {
388
492
  output: 'src/components/mediaTypes',
389
493
  split: true,
390
- import: '../mediaTypes',
391
494
  exportTypes: true,
495
+ // package: '@packages/mediaTypes',
392
496
  },
393
497
  examples: {
394
498
  output: 'src/components/examples',
395
499
  split: true,
396
- import: '../examples',
500
+ // package: '@packages/examples',
397
501
  },
398
502
  securitySchemes: {
399
503
  output: 'src/components/securitySchemes',
400
504
  split: true,
401
- import: '../securitySchemes',
505
+ // package: '@packages/securitySchemes',
402
506
  },
403
507
  links: {
404
508
  output: 'src/components/links',
405
509
  split: true,
406
- import: '../links',
510
+ // package: '@packages/links',
407
511
  },
408
512
  callbacks: {
409
513
  output: 'src/components/callbacks',
410
514
  split: true,
411
- import: '../callbacks',
515
+ // package: '@packages/callbacks',
412
516
  },
413
517
  pathItems: {
414
518
  output: 'src/components/pathItems',
415
519
  split: true,
416
- import: '../pathItems',
520
+ // package: '@packages/pathItems',
417
521
  },
418
522
  },
419
523
 
@@ -421,16 +525,17 @@ export default defineConfig({
421
525
  output: 'src/types.ts',
422
526
  },
423
527
 
424
- eden: {
425
- output: 'src/eden.ts',
426
- import: './lib',
427
- client: 'client',
428
- docs: false,
528
+ client: {
529
+ output: 'src/lib/client.ts', // re-exported by src/lib/index.ts
530
+ // package: '@packages/eden', // what other packages import the client by
531
+ baseUrl: { env: 'VITE_API_URL', source: 'import.meta.env' }, // import.meta.env.VITE_API_URL!
532
+ // baseUrl: { import: '@/env', value: 'env.API_URL' }, // a value a module exports
533
+ sameOrigin: false, // true: a browser uses window.location.origin, baseUrl is for the rest
429
534
  },
430
535
 
431
- test: {
432
- split: true, // false: a single file at `output`
433
- pathAlias: '@/',
536
+ // eden and the hooks import the generated client, so the `client` block above comes with them.
537
+ eden: {
538
+ output: 'src/eden.ts',
434
539
  },
435
540
 
436
541
  mock: {
@@ -444,46 +549,25 @@ export default defineConfig({
444
549
  },
445
550
 
446
551
  swr: {
447
- output: 'src/swr',
448
- import: '../lib',
449
- split: true,
450
- client: 'client',
552
+ output: 'src/swr.ts',
451
553
  },
452
554
  'tanstack-query': {
453
- output: 'src/tanstack-query',
454
- import: '../lib',
455
- split: true,
456
- client: 'client',
555
+ output: 'src/tanstack-query.ts',
457
556
  },
458
557
  'preact-query': {
459
- output: 'src/preact-query',
460
- import: '../lib',
461
- split: true,
462
- client: 'client',
558
+ output: 'src/preact-query.ts',
463
559
  },
464
560
  'solid-query': {
465
- output: 'src/solid-query',
466
- import: '../lib',
467
- split: true,
468
- client: 'client',
561
+ output: 'src/solid-query.ts',
469
562
  },
470
563
  'vue-query': {
471
- output: 'src/vue-query',
472
- import: '../lib',
473
- split: true,
474
- client: 'client',
564
+ output: 'src/vue-query.ts',
475
565
  },
476
566
  'svelte-query': {
477
- output: 'src/svelte-query',
478
- import: '../lib',
479
- split: true,
480
- client: 'client',
567
+ output: 'src/svelte-query.ts',
481
568
  },
482
569
  'angular-query': {
483
- output: 'src/angular-query',
484
- import: '../lib',
485
- split: true,
486
- client: 'client',
570
+ output: 'src/angular-query.ts',
487
571
  },
488
572
  })
489
573
  ```
@@ -511,7 +595,7 @@ name:
511
595
  | String | `x-minLength-message`, `x-maxLength-message`, `x-pattern-message`, `x-length-message` |
512
596
  | Array | `x-minItems-message`, `x-maxItems-message`, `x-uniqueItems-message`, `x-contains-message`, `x-minContains-message`, `x-maxContains-message`, `x-prefixItems-message`, `x-items-message` |
513
597
  | Object | `x-minProperties-message`, `x-maxProperties-message`, `x-additionalProperties-message`†, `x-propertyNames-message`, `x-patternProperties-message`†, `x-dependentRequired-message`, `x-dependentSchemas-message`, `x-properties-message`†, `x-unevaluatedProperties-message`†, `x-unevaluatedItems-message`† |
514
- | Combinators | `x-allOf-message`‡, `x-anyOf-message`, `x-oneOf-message`, `x-not-message`, `x-implication-message` |
598
+ | Composition | `x-allOf-message`‡, `x-anyOf-message`, `x-oneOf-message`, `x-not-message`, `x-implication-message` |
515
599
  | Conditional | `x-if-message`†, `x-then-message`†, `x-else-message`† |
516
600
 
517
601
  - † Kept on the schema, but Elysia 1.4 cannot route a 422 to it.
@@ -586,8 +670,6 @@ bun run test:clients # build, then compile and run the generated hooks under t
586
670
  bun run test:pack # build, pack, and install the tarball with npm into an empty project
587
671
  ```
588
672
 
589
- Coding agents: start with [AGENTS.md](AGENTS.md).
590
-
591
673
  ## License
592
674
 
593
675
  Distributed under the MIT License. See [LICENSE](https://github.com/nakita628/asphodelos?tab=MIT-1-ov-file) for more information.