asphodelos 0.2.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,15 +320,38 @@ 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
 
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.
354
+
253
355
  ### Infinite Query (`x-pagination`)
254
356
 
255
357
  Set `x-pagination: true` on a GET operation to generate infinite query hooks.
@@ -261,43 +363,32 @@ paths:
261
363
  x-pagination: true
262
364
  ```
263
365
 
264
- The paging rules go in a `pagination` argument:
265
-
266
- ```ts
267
- const items = useListItemsInfinite(undefined, {
268
- initialPageParam: 0,
269
- getNextPageParam: (lastPage) => lastPage.nextPage,
270
- buildInit: (pageParam) => ({ query: { page: String(pageParam) } }),
271
- })
272
- ```
273
-
274
- Vue Query takes only `buildInit` there, with `initialPageParam` / `getNextPageParam` in the third
275
- argument. SWR takes `buildInit(pageIndex, previousPage)` and stops when it returns `null`.
276
-
277
- ## Test & Mock Generation
278
-
279
- ### Test Generation
280
-
281
- Generates `bun:test` tests that call `app.handle(...)` on the real app: a success-status test per
282
- operation, plus `401` / `404` tests when the spec declares them. They start red against the empty
283
- handlers; re-running keeps your hand-written tests.
366
+ The paging rules go in a `pagination` argument, ahead of the options:
284
367
 
285
368
  ```ts
286
- export default defineConfig({
287
- input: 'openapi.yaml',
288
- test: {
289
- output: 'src/app.test.ts', // or `split: true` for modules/<resource>/index.test.ts
290
- 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
+ }),
291
377
  },
292
- })
378
+ { options: { query: { page: 0 } } },
379
+ )
293
380
  ```
294
381
 
295
- ### 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
296
387
 
297
388
  Generates a standalone Elysia server that answers every operation with a
298
389
  [`@faker-js/faker`](https://fakerjs.dev/) mock of its success response. Secured operations that
299
390
  declare a `401` answer it when the credential is missing, and path parameters answer a declared
300
- `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.
301
392
 
302
393
  ```ts
303
394
  export default defineConfig({
@@ -325,12 +416,25 @@ declare answers `500` with an `application/problem+json` body saying what is mis
325
416
 
326
417
  ## Full Config Reference
327
418
 
328
- With `split: true`, `output` is a directory (one file per entry + `index.ts` barrel); otherwise it
329
- is a single `.ts` file. `components.output` and the per-type components are mutually exclusive.
330
-
331
- A split directory belongs to the generator: every run empties its `.ts` files before refilling it,
332
- so an entry that leaves the document does not leave an orphaned file behind. Subdirectories, other
333
- 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.
334
438
 
335
439
  ```ts
336
440
  import { defineConfig } from 'asphodelos'
@@ -342,74 +446,78 @@ export default defineConfig({
342
446
  prefix: '/api/v3', // new Elysia({ prefix })
343
447
  port: '3000',
344
448
  integration: false, // true: no .listen(), a host framework owns the server
345
- 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
346
451
  readonly: false, // wrap top-level schemas in t.Readonly(...)
347
452
  // format: {}, // oxfmt FormatConfig
348
453
 
349
- // `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.
350
457
  components: {
351
458
  // output: 'src/components.ts', // single-file mode
459
+ // package: '@packages/components',
352
460
 
353
461
  schemas: {
354
462
  output: 'src/components/schemas',
355
463
  split: true,
356
- import: '../schemas',
357
464
  exportTypes: true,
465
+ // package: '@packages/schemas',
358
466
  },
359
467
  responses: {
360
468
  output: 'src/components/responses',
361
469
  split: true,
362
- import: '../responses',
363
470
  exportTypes: true,
471
+ // package: '@packages/responses',
364
472
  },
365
473
  parameters: {
366
474
  output: 'src/components/parameters',
367
475
  split: true,
368
- import: '../parameters',
369
476
  exportTypes: true,
477
+ // package: '@packages/parameters',
370
478
  },
371
479
  requestBodies: {
372
480
  output: 'src/components/requestBodies',
373
481
  split: true,
374
- import: '../requestBodies',
375
482
  exportTypes: true,
483
+ // package: '@packages/requestBodies',
376
484
  },
377
485
  headers: {
378
486
  output: 'src/components/headers',
379
487
  split: true,
380
- import: '../headers',
381
488
  exportTypes: true,
489
+ // package: '@packages/headers',
382
490
  },
383
491
  mediaTypes: {
384
492
  output: 'src/components/mediaTypes',
385
493
  split: true,
386
- import: '../mediaTypes',
387
494
  exportTypes: true,
495
+ // package: '@packages/mediaTypes',
388
496
  },
389
497
  examples: {
390
498
  output: 'src/components/examples',
391
499
  split: true,
392
- import: '../examples',
500
+ // package: '@packages/examples',
393
501
  },
394
502
  securitySchemes: {
395
503
  output: 'src/components/securitySchemes',
396
504
  split: true,
397
- import: '../securitySchemes',
505
+ // package: '@packages/securitySchemes',
398
506
  },
399
507
  links: {
400
508
  output: 'src/components/links',
401
509
  split: true,
402
- import: '../links',
510
+ // package: '@packages/links',
403
511
  },
404
512
  callbacks: {
405
513
  output: 'src/components/callbacks',
406
514
  split: true,
407
- import: '../callbacks',
515
+ // package: '@packages/callbacks',
408
516
  },
409
517
  pathItems: {
410
518
  output: 'src/components/pathItems',
411
519
  split: true,
412
- import: '../pathItems',
520
+ // package: '@packages/pathItems',
413
521
  },
414
522
  },
415
523
 
@@ -417,16 +525,17 @@ export default defineConfig({
417
525
  output: 'src/types.ts',
418
526
  },
419
527
 
420
- eden: {
421
- output: 'src/eden.ts',
422
- import: './lib',
423
- client: 'client',
424
- 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
425
534
  },
426
535
 
427
- test: {
428
- split: true, // false: a single file at `output`
429
- 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',
430
539
  },
431
540
 
432
541
  mock: {
@@ -440,46 +549,25 @@ export default defineConfig({
440
549
  },
441
550
 
442
551
  swr: {
443
- output: 'src/swr',
444
- import: '../lib',
445
- split: true,
446
- client: 'client',
552
+ output: 'src/swr.ts',
447
553
  },
448
554
  'tanstack-query': {
449
- output: 'src/tanstack-query',
450
- import: '../lib',
451
- split: true,
452
- client: 'client',
555
+ output: 'src/tanstack-query.ts',
453
556
  },
454
557
  'preact-query': {
455
- output: 'src/preact-query',
456
- import: '../lib',
457
- split: true,
458
- client: 'client',
558
+ output: 'src/preact-query.ts',
459
559
  },
460
560
  'solid-query': {
461
- output: 'src/solid-query',
462
- import: '../lib',
463
- split: true,
464
- client: 'client',
561
+ output: 'src/solid-query.ts',
465
562
  },
466
563
  'vue-query': {
467
- output: 'src/vue-query',
468
- import: '../lib',
469
- split: true,
470
- client: 'client',
564
+ output: 'src/vue-query.ts',
471
565
  },
472
566
  'svelte-query': {
473
- output: 'src/svelte-query',
474
- import: '../lib',
475
- split: true,
476
- client: 'client',
567
+ output: 'src/svelte-query.ts',
477
568
  },
478
569
  'angular-query': {
479
- output: 'src/angular-query',
480
- import: '../lib',
481
- split: true,
482
- client: 'client',
570
+ output: 'src/angular-query.ts',
483
571
  },
484
572
  })
485
573
  ```
@@ -582,8 +670,6 @@ bun run test:clients # build, then compile and run the generated hooks under t
582
670
  bun run test:pack # build, pack, and install the tarball with npm into an empty project
583
671
  ```
584
672
 
585
- Coding agents: start with [AGENTS.md](https://github.com/nakita628/asphodelos/blob/main/AGENTS.md).
586
-
587
673
  ## License
588
674
 
589
675
  Distributed under the MIT License. See [LICENSE](https://github.com/nakita628/asphodelos?tab=MIT-1-ov-file) for more information.