ts-procedures 5.7.0 → 5.7.1

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.
Files changed (36) hide show
  1. package/README.md +185 -0
  2. package/agent_config/claude-code/skills/guide/api-reference.md +200 -0
  3. package/agent_config/claude-code/skills/guide/patterns.md +108 -0
  4. package/agent_config/copilot/copilot-instructions.md +87 -0
  5. package/agent_config/cursor/cursorrules +87 -0
  6. package/build/implementations/http/doc-registry.test.js +27 -1
  7. package/build/implementations/http/doc-registry.test.js.map +1 -1
  8. package/build/implementations/http/express-rpc/index.js +1 -0
  9. package/build/implementations/http/express-rpc/index.js.map +1 -1
  10. package/build/implementations/http/express-rpc/index.test.js +1 -1
  11. package/build/implementations/http/express-rpc/index.test.js.map +1 -1
  12. package/build/implementations/http/hono-api/index.js +2 -0
  13. package/build/implementations/http/hono-api/index.js.map +1 -1
  14. package/build/implementations/http/hono-api/index.test.js +9 -0
  15. package/build/implementations/http/hono-api/index.test.js.map +1 -1
  16. package/build/implementations/http/hono-rpc/index.js +1 -0
  17. package/build/implementations/http/hono-rpc/index.js.map +1 -1
  18. package/build/implementations/http/hono-rpc/index.test.js +1 -1
  19. package/build/implementations/http/hono-rpc/index.test.js.map +1 -1
  20. package/build/implementations/http/hono-stream/index.js +17 -1
  21. package/build/implementations/http/hono-stream/index.js.map +1 -1
  22. package/build/implementations/http/hono-stream/index.test.js +61 -0
  23. package/build/implementations/http/hono-stream/index.test.js.map +1 -1
  24. package/build/implementations/http/hono-stream/types.d.ts +4 -13
  25. package/build/implementations/types.d.ts +5 -0
  26. package/build/index.js +8 -1
  27. package/build/index.js.map +1 -1
  28. package/package.json +21 -3
  29. package/src/client/call.ts +74 -0
  30. package/src/client/errors.ts +43 -0
  31. package/src/client/fetch-adapter.ts +191 -0
  32. package/src/client/hooks.ts +65 -0
  33. package/src/client/index.ts +121 -0
  34. package/src/client/request-builder.ts +73 -0
  35. package/src/client/stream.ts +164 -0
  36. package/src/client/types.ts +103 -0
package/README.md CHANGED
@@ -880,6 +880,191 @@ import type { RPCConfig, RPCHttpRouteDoc, StreamHttpRouteDoc, StreamMode, APICon
880
880
  // Hono API (REST-style)
881
881
  import { HonoAPIAppBuilder } from 'ts-procedures/hono-api'
882
882
  import type { APIConfig, APIHttpRouteDoc, APIInput, HttpMethod, QueryParser } from 'ts-procedures/hono-api'
883
+
884
+ // Client Runtime
885
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
886
+ import type { ClientAdapter, ClientHooks, TypedStream, ClientInstance } from 'ts-procedures/client'
887
+
888
+ // Code Generation
889
+ import { generateClient } from 'ts-procedures/codegen'
890
+ ```
891
+
892
+ ## Client Code Generation
893
+
894
+ ts-procedures can generate type-safe client SDKs directly from your server's `DocRegistry` output. Generated files include TypeScript types and callable functions for every registered procedure, organized by scope — no manual type duplication required.
895
+
896
+ ### Quick Start
897
+
898
+ **Step 1 — Serve your docs endpoint:**
899
+
900
+ ```typescript
901
+ app.get('/docs', (c) => c.json(docs.toJSON()))
902
+ ```
903
+
904
+ **Step 2 — Generate the client:**
905
+
906
+ ```bash
907
+ npx ts-procedures-codegen --url http://localhost:3000/docs --out ./src/generated/api
908
+ ```
909
+
910
+ **Step 3 — Use the client:**
911
+
912
+ ```typescript
913
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
914
+ import { createScopeBindings } from './generated/api'
915
+
916
+ const client = createClient({
917
+ adapter: createFetchAdapter(),
918
+ basePath: 'http://localhost:3000',
919
+ scopes: createScopeBindings,
920
+ hooks: {
921
+ onBeforeRequest(ctx) {
922
+ ctx.request.headers = { ...ctx.request.headers, Authorization: `Bearer ${getToken()}` }
923
+ return ctx
924
+ },
925
+ },
926
+ })
927
+
928
+ // Fully typed — params and response inferred from server schemas
929
+ const user = await client.users.GetUser({ pathParams: { id: '123' } })
930
+ ```
931
+
932
+ ### Generated File Structure
933
+
934
+ Running the codegen command produces one file per scope, plus shared error types and a barrel export:
935
+
936
+ ```
937
+ generated/
938
+ users.ts # Types + callables for "users" scope
939
+ billing.ts # Types + callables for "billing" scope
940
+ notifications.ts # Types + callables for stream procedures
941
+ _errors.ts # Typed error classes + ProcedureErrorUnion
942
+ index.ts # Barrel exports + createScopeBindings
943
+ ```
944
+
945
+ ### CLI Reference
946
+
947
+ | Flag | Description | Required |
948
+ |------|-------------|----------|
949
+ | `--url <url>` | Fetch DocEnvelope from URL | One of `--url` or `--file` |
950
+ | `--file <path>` | Read DocEnvelope from JSON file | One of `--url` or `--file` |
951
+ | `--out <dir>` | Output directory | Yes |
952
+ | `--watch` | Poll for changes and regenerate | No |
953
+ | `--interval <ms>` | Watch poll interval (default: 3000) | No |
954
+ | `--dry-run` | Preview without writing files | No |
955
+ | `--client-import-path <path>` | Override import path (default: `ts-procedures/client`) | No |
956
+ | `--namespace-types` | Wrap types in nested TypeScript namespaces per scope/route | No |
957
+ | `--config <path>` | Path to config file (default: `ts-procedures-codegen.config.json`) | No |
958
+ | `--enum-style <union\|enum>` | TypeScript enum style (requires `--namespace-types`) | No |
959
+ | `--depluralize` | Singularize array item type names (requires `--namespace-types`) | No |
960
+ | `--array-item-naming <value>` | Postfix for array item type names (requires `--namespace-types`) | No |
961
+ | `--uncountable-words <list>` | Comma-separated words to skip singularization (requires `--namespace-types`) | No |
962
+ | `--jsdoc` | Emit JSDoc comments from JSON Schema descriptions (requires `--namespace-types`) | No |
963
+ | `--self-contained` | Emit `_types.ts` and `_client.ts` — no runtime dependency on `ts-procedures` | No |
964
+
965
+ > **Note:** ajsc formatting options (`--enum-style enum`, `--depluralize`, `--array-item-naming`, `--uncountable-words`, `--jsdoc`) only take effect with `--namespace-types`. In flat mode, all types are inlined and these options have no effect.
966
+ >
967
+ > You can also use a `ts-procedures-codegen.config.json` file in your project root instead of CLI flags. CLI flags override config values.
968
+
969
+ ### Adapter Interface
970
+
971
+ The client requires an adapter that handles the actual HTTP transport. A built-in fetch adapter is included, and you can implement your own for any HTTP library:
972
+
973
+ ```typescript
974
+ import { createFetchAdapter } from 'ts-procedures/client'
975
+
976
+ // Use the built-in fetch adapter
977
+ const adapter = createFetchAdapter({ headers: { 'X-API-Key': 'my-key' } })
978
+
979
+ // Or implement your own (e.g., for axios)
980
+ const axiosAdapter: ClientAdapter = {
981
+ async request({ url, method, headers, body }) {
982
+ const res = await axios({ url, method, headers, data: body })
983
+ return { status: res.status, headers: res.headers, body: res.data }
984
+ },
985
+ async stream({ url, method, headers, body }) {
986
+ // Return AsyncIterable of SSE events
987
+ },
988
+ }
989
+ ```
990
+
991
+ ### Hooks
992
+
993
+ Hooks let you intercept requests and responses globally or per-procedure call. Global hooks apply to every call made through the client instance; per-procedure hooks override or extend them for a single invocation.
994
+
995
+ ```typescript
996
+ // Global hooks (apply to all calls)
997
+ const client = createClient({
998
+ adapter,
999
+ basePath: 'http://localhost:3000',
1000
+ scopes: createScopeBindings,
1001
+ hooks: {
1002
+ onBeforeRequest(ctx) { /* add auth headers */ return ctx },
1003
+ onAfterResponse(ctx) { /* handle errors, logging */ },
1004
+ onError(ctx) { /* error reporting */ },
1005
+ },
1006
+ })
1007
+
1008
+ // Per-procedure hook override
1009
+ await client.users.GetUser({ pathParams: { id: '123' } }, {
1010
+ onAfterResponse(ctx) {
1011
+ const rateLimit = ctx.response.headers['x-rate-limit-remaining']
1012
+ },
1013
+ })
1014
+ ```
1015
+
1016
+ ### Streaming
1017
+
1018
+ Stream procedures return a `TypedStream` — an async iterable for yield values, with a `.result` promise for the final return value:
1019
+
1020
+ ```typescript
1021
+ const stream = client.events.WatchNotifications({ filter: 'all' })
1022
+
1023
+ for await (const event of stream) {
1024
+ console.log(event) // typed as WatchNotificationsYield
1025
+ }
1026
+
1027
+ const result = await stream.result // typed as WatchNotificationsReturn
1028
+ ```
1029
+
1030
+ ### Programmatic API
1031
+
1032
+ For build pipelines or custom tooling, `generateClient` can be called directly without the CLI:
1033
+
1034
+ ```typescript
1035
+ import { generateClient } from 'ts-procedures/codegen'
1036
+
1037
+ await generateClient({
1038
+ url: 'http://localhost:3000/docs',
1039
+ outDir: './src/generated/api',
1040
+ clientImportPath: '@my-app/procedures-client', // optional
1041
+ namespaceTypes: true, // optional — wrap types in nested namespaces
1042
+ selfContained: true, // optional — emit _types.ts + _client.ts (no ts-procedures runtime dep)
1043
+ dryRun: false, // optional
1044
+ ajsc: { // optional — ajsc TypescriptConverter options
1045
+ enumStyle: 'union',
1046
+ depluralize: true,
1047
+ arrayItemNaming: 'Item',
1048
+ uncountableWords: ['criteria'],
1049
+ },
1050
+ })
1051
+ ```
1052
+
1053
+ ### Self-Contained Mode
1054
+
1055
+ With `--self-contained`, the generated output includes two additional files in the output directory:
1056
+
1057
+ - **`_types.ts`** — All client type definitions (`ClientInstance`, `TypedStream`, `ProcedureCallOptions`, hooks, adapters, descriptors)
1058
+ - **`_client.ts`** — Full client runtime: `createClient`, `createFetchAdapter`, hook pipeline, and error classes (`ClientRequestError`, `ClientPathParamError`, `ClientStreamError`)
1059
+
1060
+ All generated scope files and `index.ts` import from `./_types` instead of `ts-procedures/client`, so app consumers can import everything from the generated directory without needing `ts-procedures` as a runtime dependency. `ts-procedures` becomes a devDependency only.
1061
+
1062
+ ```typescript
1063
+ // Without --self-contained (default)
1064
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
1065
+
1066
+ // With --self-contained
1067
+ import { createClient, createFetchAdapter } from './generated/_client'
883
1068
  ```
884
1069
 
885
1070
  ## AI Agent Setup
@@ -751,6 +751,206 @@ type AnyHttpRouteDoc = RPCHttpRouteDoc | APIHttpRouteDoc | StreamHttpRouteDoc
751
751
 
752
752
  ---
753
753
 
754
+ ## createClient(config)
755
+
756
+ Creates a typed client instance bound to generated scope bindings.
757
+
758
+ ```typescript
759
+ function createClient<TScopes>(config: {
760
+ adapter: ClientAdapter
761
+ basePath: string
762
+ scopes: (client: ClientInstance) => TScopes
763
+ hooks?: ClientHooks
764
+ }): TScopes
765
+ ```
766
+
767
+ ### Parameters
768
+
769
+ - `config.adapter` — Transport adapter implementing `ClientAdapter`. Use `createFetchAdapter()` for fetch-based transport.
770
+ - `config.basePath` — Base URL prepended to all request paths (e.g., `'http://localhost:3000'`).
771
+ - `config.scopes` — Factory function that receives a raw `ClientInstance` and returns the typed scope object. Typically the `createScopeBindings` export from generated code.
772
+ - `config.hooks` — Optional global hooks applied to every call. Can be overridden per call.
773
+
774
+ ### Return Value
775
+
776
+ Returns the result of `config.scopes(clientInstance)` — a typed object where each key is a scope and each value is a record of callable procedure functions.
777
+
778
+ ### ClientHooks
779
+
780
+ ```typescript
781
+ interface ClientHooks {
782
+ onBeforeRequest?: (ctx: { request: ClientRequest }) => { request: ClientRequest } | void
783
+ onAfterResponse?: (ctx: { request: ClientRequest; response: ClientResponse }) => void
784
+ }
785
+ ```
786
+
787
+ ### Example
788
+
789
+ ```typescript
790
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
791
+ import { createScopeBindings } from './generated/api'
792
+
793
+ const client = createClient({
794
+ adapter: createFetchAdapter(),
795
+ basePath: 'http://localhost:3000',
796
+ scopes: createScopeBindings,
797
+ hooks: {
798
+ onBeforeRequest(ctx) {
799
+ ctx.request.headers = { ...ctx.request.headers, Authorization: `Bearer ${getToken()}` }
800
+ return ctx
801
+ },
802
+ },
803
+ })
804
+
805
+ const user = await client.users.GetUser({ pathParams: { id: '123' } })
806
+ ```
807
+
808
+ ---
809
+
810
+ ## createFetchAdapter()
811
+
812
+ Creates a `ClientAdapter` using the global `fetch` API.
813
+
814
+ ```typescript
815
+ function createFetchAdapter(options?: {
816
+ fetch?: typeof globalThis.fetch
817
+ }): ClientAdapter
818
+ ```
819
+
820
+ ### Parameters
821
+
822
+ - `options.fetch` — Optional custom fetch implementation. Defaults to `globalThis.fetch`.
823
+
824
+ ### Return Value
825
+
826
+ A `ClientAdapter` that supports both request/response and SSE streaming.
827
+
828
+ ### Example
829
+
830
+ ```typescript
831
+ import { createFetchAdapter } from 'ts-procedures/client'
832
+
833
+ // Default (uses globalThis.fetch)
834
+ const adapter = createFetchAdapter()
835
+
836
+ // Custom fetch (e.g., node-fetch, undici)
837
+ const adapter = createFetchAdapter({ fetch: customFetch })
838
+ ```
839
+
840
+ ---
841
+
842
+ ## generateClient(options)
843
+
844
+ Build-time CLI and programmatic API for generating typed client files from a `DocEnvelope`.
845
+
846
+ ```typescript
847
+ // CLI usage (preferred)
848
+ // npx ts-procedures-codegen --url <url> --out <dir> [--namespace-types] [--dry-run]
849
+
850
+ // Programmatic
851
+ async function generateClient(options: {
852
+ url?: string
853
+ file?: string
854
+ envelope?: DocEnvelope
855
+ outDir: string
856
+ ajsc?: AjscOptions
857
+ clientImportPath?: string
858
+ dryRun?: boolean
859
+ namespaceTypes?: boolean
860
+ selfContained?: boolean
861
+ }): Promise<GeneratedFile[]>
862
+ ```
863
+
864
+ ### CLI Options
865
+
866
+ | Flag | Description |
867
+ |------|-------------|
868
+ | `--url <url>` | URL to fetch the `DocEnvelope` JSON from (e.g., `http://localhost:3000/docs`) |
869
+ | `--file <path>` | Read DocEnvelope from a local JSON file |
870
+ | `--out <dir>` | Output directory for generated files |
871
+ | `--watch` | Poll for changes and regenerate |
872
+ | `--interval <ms>` | Watch poll interval (default: 3000) |
873
+ | `--dry-run` | Preview without writing files |
874
+ | `--namespace-types` | Wrap types in nested TS namespaces (`Scope.Route.Params`) |
875
+ | `--config <path>` | Path to config file (default: `ts-procedures-codegen.config.json`) |
876
+ | `--client-import-path <path>` | Override import path (default: `ts-procedures/client`) |
877
+ | `--enum-style <union\|enum>` | TypeScript enum style (requires `--namespace-types`) |
878
+ | `--depluralize` | Singularize array item type names (requires `--namespace-types`) |
879
+ | `--array-item-naming <value>` | Postfix for array item type names (requires `--namespace-types`) |
880
+ | `--uncountable-words <list>` | Comma-separated words to skip singularization (requires `--namespace-types`) |
881
+ | `--jsdoc` | Emit JSDoc comments from schema descriptions (requires `--namespace-types`) |
882
+ | `--self-contained` | Emit `_types.ts` and `_client.ts` in output dir — no runtime dependency on `ts-procedures` |
883
+
884
+ ### Generated Output
885
+
886
+ - One `.ts` file per scope (e.g., `users.ts`, `events.ts`)
887
+ - A root `index.ts` exporting `createScopeBindings`
888
+ - Each scope file exports fully-typed callable functions and TypeScript types
889
+ - With `--namespace-types`: types are wrapped in `export namespace Scope { export namespace Route { ... } }`
890
+
891
+ ### Example
892
+
893
+ ```typescript
894
+ // CLI
895
+ // npx ts-procedures-codegen --url http://localhost:3000/docs --out ./src/generated/api --namespace-types
896
+
897
+ // Programmatic
898
+ import { generateClient } from 'ts-procedures/codegen'
899
+
900
+ await generateClient({
901
+ url: 'http://localhost:3000/docs',
902
+ outDir: './src/generated/api',
903
+ namespaceTypes: true,
904
+ selfContained: true, // emit _types.ts + _client.ts (no runtime dep)
905
+ ajsc: { depluralize: true, arrayItemNaming: 'Item' },
906
+ })
907
+ ```
908
+
909
+ ---
910
+
911
+ ## TypedStream\<TYield, TReturn\>
912
+
913
+ Async iterable returned by stream procedure calls on the generated client. Yields typed values and exposes a `result` promise for the stream's final return value.
914
+
915
+ ```typescript
916
+ interface TypedStream<TYield, TReturn> extends AsyncIterable<TYield> {
917
+ result: Promise<TReturn>
918
+ [Symbol.asyncIterator](): AsyncIterator<TYield>
919
+ }
920
+ ```
921
+
922
+ ### Type Parameters
923
+
924
+ - `TYield` — Type of each yielded value (from `schema.yieldType`)
925
+ - `TReturn` — Type of the stream's return value (from `schema.returnType`). Sent as `event: 'return'` SSE message by `HonoStreamAppBuilder`.
926
+
927
+ ### Key Behavior
928
+
929
+ - Iterating with `for await...of` yields each SSE data event as `TYield`
930
+ - `stream.result` resolves when the stream closes with the final return value
931
+ - If the stream has no return value, `TReturn` is `void` and `result` resolves to `undefined`
932
+
933
+ ### Example
934
+
935
+ ```typescript
936
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
937
+ import { createScopeBindings } from './generated/api'
938
+
939
+ const client = createClient({ adapter: createFetchAdapter(), basePath: '...', scopes: createScopeBindings })
940
+
941
+ const stream = client.events.WatchNotifications({ filter: 'all' })
942
+
943
+ // Consume yielded values
944
+ for await (const event of stream) {
945
+ console.log(event) // Typed as WatchNotificationsYield
946
+ }
947
+
948
+ // Access return value after iteration completes
949
+ const summary = await stream.result // Typed as WatchNotificationsReturn
950
+ ```
951
+
952
+ ---
953
+
754
954
  ## Stack Utilities
755
955
 
756
956
  ### captureDefinitionInfo()
@@ -758,3 +758,111 @@ onRequestStart → factoryContext() → params validation
758
758
  → onStreamStart → handler yields → onStreamEnd → onRequestEnd
759
759
  → onMidStreamError (if throw) → onStreamEnd → onRequestEnd
760
760
  ```
761
+
762
+ ---
763
+
764
+ ## Client Code Generation Setup
765
+
766
+ ```typescript
767
+ import { DocRegistry } from 'ts-procedures/http-docs'
768
+
769
+ // Server: serve docs endpoint
770
+ const docs = new DocRegistry({
771
+ basePath: '/api',
772
+ headers: [{ name: 'Authorization', description: 'Bearer token' }],
773
+ errors: DocRegistry.defaultErrors(),
774
+ })
775
+ .from(rpcBuilder)
776
+ .from(apiBuilder)
777
+ .from(streamBuilder)
778
+
779
+ app.get('/docs', (c) => c.json(docs.toJSON()))
780
+
781
+ // Then generate client:
782
+ // npx ts-procedures-codegen --url http://localhost:3000/docs --out ./src/generated/api
783
+ // Optional: --namespace-types --self-contained --depluralize --enum-style union
784
+ ```
785
+
786
+ ---
787
+
788
+ ## Type-Safe Client Usage
789
+
790
+ ```typescript
791
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
792
+ import { createScopeBindings } from './generated/api'
793
+
794
+ const client = createClient({
795
+ adapter: createFetchAdapter(),
796
+ basePath: 'http://localhost:3000',
797
+ scopes: createScopeBindings,
798
+ hooks: {
799
+ onBeforeRequest(ctx) {
800
+ ctx.request.headers = {
801
+ ...ctx.request.headers,
802
+ Authorization: `Bearer ${getToken()}`,
803
+ }
804
+ return ctx
805
+ },
806
+ onAfterResponse(ctx) {
807
+ if (ctx.response.status === 401) {
808
+ redirect('/login')
809
+ }
810
+ },
811
+ },
812
+ })
813
+
814
+ // Fully typed — params and response inferred from server schemas
815
+ const user = await client.users.GetUser({ pathParams: { id: '123' } })
816
+ ```
817
+
818
+ ---
819
+
820
+ ## Streaming Client Usage
821
+
822
+ ```typescript
823
+ const stream = client.events.WatchNotifications({ filter: 'all' })
824
+
825
+ for await (const event of stream) {
826
+ console.log(event) // Typed as WatchNotificationsYield
827
+ }
828
+
829
+ // Access the stream's return value
830
+ const result = await stream.result // Typed as WatchNotificationsReturn
831
+ ```
832
+
833
+ ---
834
+
835
+ ## Per-Procedure Hook Override
836
+
837
+ ```typescript
838
+ // Override hooks for a specific call
839
+ await client.users.GetUser({ pathParams: { id: '123' } }, {
840
+ onAfterResponse(ctx) {
841
+ const rateLimit = ctx.response.headers['x-rate-limit-remaining']
842
+ console.log(`Rate limit remaining: ${rateLimit}`)
843
+ },
844
+ })
845
+ ```
846
+
847
+ ---
848
+
849
+ ## Custom Client Adapter (Axios)
850
+
851
+ ```typescript
852
+ import type { ClientAdapter } from 'ts-procedures/client'
853
+ import axios from 'axios'
854
+
855
+ const axiosAdapter: ClientAdapter = {
856
+ async request({ url, method, headers, body, signal }) {
857
+ const res = await axios({ url, method, headers, data: body, signal })
858
+ return {
859
+ status: res.status,
860
+ headers: Object.fromEntries(Object.entries(res.headers).filter(([, v]) => typeof v === 'string')) as Record<string, string>,
861
+ body: res.data,
862
+ }
863
+ },
864
+ async stream() {
865
+ throw new Error('Streaming not supported with axios adapter')
866
+ },
867
+ }
868
+ ```
@@ -292,3 +292,90 @@ describe('GetUser', () => {
292
292
  - `scope` can be string or string[] (joined as path segments)
293
293
  - Procedure name auto-converted to kebab-case
294
294
  - Stream routes support both GET (query params) and POST (JSON body)
295
+
296
+ ## Client Code Generation
297
+
298
+ Generate type-safe client SDKs from a running DocRegistry endpoint.
299
+
300
+ ### Imports
301
+
302
+ ```typescript
303
+ // Client Runtime
304
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
305
+ import type { ClientAdapter, ClientHooks, TypedStream, ClientInstance } from 'ts-procedures/client'
306
+
307
+ // Code Generation (build-time only)
308
+ import { generateClient } from 'ts-procedures/codegen'
309
+ ```
310
+
311
+ ### CLI
312
+
313
+ ```bash
314
+ npx ts-procedures-codegen --url http://localhost:3000/docs --out ./src/generated/api
315
+ # Optional flags: --namespace-types --jsdoc --depluralize --enum-style union
316
+ # --array-item-naming Item --uncountable-words criteria,alumni
317
+ # --dry-run --watch --client-import-path @my-app/client
318
+ # --config ./codegen.config.json --self-contained
319
+ ```
320
+
321
+ Generates one `.ts` file per scope plus a root `index.ts` exporting `createScopeBindings`.
322
+ Use `--namespace-types` to wrap types in nested TS namespaces (`Scope.Route.Params` instead of flat `RouteParams`).
323
+ Note: ajsc formatting options (`--enum-style enum`, `--depluralize`, `--jsdoc`, etc.) only take effect with `--namespace-types`.
324
+ Supports config file: `ts-procedures-codegen.config.json` (auto-loaded from CWD) or `--config <path>`.
325
+
326
+ ### createClient Example
327
+
328
+ ```typescript
329
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
330
+ import { createScopeBindings } from './generated/api'
331
+
332
+ const client = createClient({
333
+ adapter: createFetchAdapter(),
334
+ basePath: 'http://localhost:3000',
335
+ scopes: createScopeBindings,
336
+ hooks: {
337
+ onBeforeRequest(ctx) {
338
+ ctx.request.headers = { ...ctx.request.headers, Authorization: `Bearer ${getToken()}` }
339
+ return ctx
340
+ },
341
+ onAfterResponse(ctx) {
342
+ if (ctx.response.status === 401) redirect('/login')
343
+ },
344
+ },
345
+ })
346
+
347
+ // Fully typed — params and response inferred from server schemas
348
+ const user = await client.users.GetUser({ pathParams: { id: '123' } })
349
+
350
+ // Per-call hook override
351
+ await client.users.GetUser({ pathParams: { id: '123' } }, {
352
+ onAfterResponse(ctx) {
353
+ console.log(ctx.response.headers['x-rate-limit-remaining'])
354
+ },
355
+ })
356
+ ```
357
+
358
+ ### Hook Types
359
+
360
+ ```typescript
361
+ interface ClientHooks {
362
+ onBeforeRequest?: (ctx: { request: ClientRequest }) => { request: ClientRequest } | void
363
+ onAfterResponse?: (ctx: { request: ClientRequest; response: ClientResponse }) => void
364
+ }
365
+ ```
366
+
367
+ ### TypedStream Usage
368
+
369
+ ```typescript
370
+ const stream = client.events.WatchNotifications({ filter: 'all' })
371
+
372
+ for await (const event of stream) {
373
+ console.log(event) // Typed as WatchNotificationsYield
374
+ }
375
+
376
+ const result = await stream.result // Typed as WatchNotificationsReturn
377
+ ```
378
+
379
+ - `TypedStream<TYield, TReturn>` extends `AsyncIterable<TYield>`
380
+ - `stream.result` resolves with the final return value sent as `event: 'return'` SSE message
381
+ - Stream procedures that return `void` have `result` resolve to `undefined`
@@ -292,3 +292,90 @@ describe('GetUser', () => {
292
292
  - `scope` can be string or string[] (joined as path segments)
293
293
  - Procedure name auto-converted to kebab-case
294
294
  - Stream routes support both GET (query params) and POST (JSON body)
295
+
296
+ ## Client Code Generation
297
+
298
+ Generate type-safe client SDKs from a running DocRegistry endpoint.
299
+
300
+ ### Imports
301
+
302
+ ```typescript
303
+ // Client Runtime
304
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
305
+ import type { ClientAdapter, ClientHooks, TypedStream, ClientInstance } from 'ts-procedures/client'
306
+
307
+ // Code Generation (build-time only)
308
+ import { generateClient } from 'ts-procedures/codegen'
309
+ ```
310
+
311
+ ### CLI
312
+
313
+ ```bash
314
+ npx ts-procedures-codegen --url http://localhost:3000/docs --out ./src/generated/api
315
+ # Optional flags: --namespace-types --jsdoc --depluralize --enum-style union
316
+ # --array-item-naming Item --uncountable-words criteria,alumni
317
+ # --dry-run --watch --client-import-path @my-app/client
318
+ # --config ./codegen.config.json --self-contained
319
+ ```
320
+
321
+ Generates one `.ts` file per scope plus a root `index.ts` exporting `createScopeBindings`.
322
+ Use `--namespace-types` to wrap types in nested TS namespaces (`Scope.Route.Params` instead of flat `RouteParams`).
323
+ Note: ajsc formatting options (`--enum-style enum`, `--depluralize`, `--jsdoc`, etc.) only take effect with `--namespace-types`.
324
+ Supports config file: `ts-procedures-codegen.config.json` (auto-loaded from CWD) or `--config <path>`.
325
+
326
+ ### createClient Example
327
+
328
+ ```typescript
329
+ import { createClient, createFetchAdapter } from 'ts-procedures/client'
330
+ import { createScopeBindings } from './generated/api'
331
+
332
+ const client = createClient({
333
+ adapter: createFetchAdapter(),
334
+ basePath: 'http://localhost:3000',
335
+ scopes: createScopeBindings,
336
+ hooks: {
337
+ onBeforeRequest(ctx) {
338
+ ctx.request.headers = { ...ctx.request.headers, Authorization: `Bearer ${getToken()}` }
339
+ return ctx
340
+ },
341
+ onAfterResponse(ctx) {
342
+ if (ctx.response.status === 401) redirect('/login')
343
+ },
344
+ },
345
+ })
346
+
347
+ // Fully typed — params and response inferred from server schemas
348
+ const user = await client.users.GetUser({ pathParams: { id: '123' } })
349
+
350
+ // Per-call hook override
351
+ await client.users.GetUser({ pathParams: { id: '123' } }, {
352
+ onAfterResponse(ctx) {
353
+ console.log(ctx.response.headers['x-rate-limit-remaining'])
354
+ },
355
+ })
356
+ ```
357
+
358
+ ### Hook Types
359
+
360
+ ```typescript
361
+ interface ClientHooks {
362
+ onBeforeRequest?: (ctx: { request: ClientRequest }) => { request: ClientRequest } | void
363
+ onAfterResponse?: (ctx: { request: ClientRequest; response: ClientResponse }) => void
364
+ }
365
+ ```
366
+
367
+ ### TypedStream Usage
368
+
369
+ ```typescript
370
+ const stream = client.events.WatchNotifications({ filter: 'all' })
371
+
372
+ for await (const event of stream) {
373
+ console.log(event) // Typed as WatchNotificationsYield
374
+ }
375
+
376
+ const result = await stream.result // Typed as WatchNotificationsReturn
377
+ ```
378
+
379
+ - `TypedStream<TYield, TReturn>` extends `AsyncIterable<TYield>`
380
+ - `stream.result` resolves with the final return value sent as `event: 'return'` SSE message
381
+ - Stream procedures that return `void` have `result` resolve to `undefined`