@dudousxd/nestjs-codegen 0.2.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/CHANGELOG.md +77 -0
- package/LICENSE +21 -0
- package/bin/nestjs-codegen.js +8 -0
- package/dist/cli/main.cjs +5044 -0
- package/dist/cli/main.cjs.map +1 -0
- package/dist/cli/main.d.cts +10 -0
- package/dist/cli/main.d.ts +10 -0
- package/dist/cli/main.js +5024 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/extension/index.cjs +35 -0
- package/dist/extension/index.cjs.map +1 -0
- package/dist/extension/index.d.cts +2 -0
- package/dist/extension/index.d.ts +2 -0
- package/dist/extension/index.js +8 -0
- package/dist/extension/index.js.map +1 -0
- package/dist/index-BwIRjOQA.d.cts +509 -0
- package/dist/index-BwIRjOQA.d.ts +509 -0
- package/dist/index.cjs +3975 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +128 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +3938 -0
- package/dist/index.js.map +1 -0
- package/dist/nest/index.cjs +3941 -0
- package/dist/nest/index.cjs.map +1 -0
- package/dist/nest/index.d.cts +67 -0
- package/dist/nest/index.d.ts +67 -0
- package/dist/nest/index.js +3919 -0
- package/dist/nest/index.js.map +1 -0
- package/package.json +81 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { U as UserConfig, R as ResolvedConfig, a as RouteDescriptor, V as ValidationAdapter, S as SchemaModule, b as ResolvedFormsConfig, C as CodegenExtension, E as ExtensionContext } from './index-BwIRjOQA.cjs';
|
|
2
|
+
export { A as AdapterUsage, c as ContractDescriptor, d as ContractSource, e as ControllerRef, N as NumberCheck, f as RenderContext, g as RenderedModule, h as SchemaNode, i as ScopeConfig, j as StringCheck, T as TypeRef, k as ValidationOption, r as resolveAdapter } from './index-BwIRjOQA.cjs';
|
|
3
|
+
import { ClassDeclaration, SourceFile, Project } from 'ts-morph';
|
|
4
|
+
|
|
5
|
+
declare function defineConfig(c: UserConfig): UserConfig;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resolve a {@link UserConfig} (e.g. `NestjsCodegenModule.forRoot()` options) into a
|
|
9
|
+
* fully-defaulted {@link ResolvedConfig} — without reading a config file. Used by the
|
|
10
|
+
* Nest module and any programmatic caller that already holds the config in memory.
|
|
11
|
+
*
|
|
12
|
+
* @param userConfig the raw user config (forRoot options minus module-only fields)
|
|
13
|
+
* @param cwd project root used to resolve globs / outDir. Defaults to `process.cwd()`.
|
|
14
|
+
*/
|
|
15
|
+
declare function resolveConfig(userConfig: UserConfig, cwd?: string): ResolvedConfig;
|
|
16
|
+
declare function loadConfig(cwd?: string): Promise<ResolvedConfig>;
|
|
17
|
+
|
|
18
|
+
declare class ConfigError extends Error {
|
|
19
|
+
constructor(message: string, options?: ErrorOptions);
|
|
20
|
+
}
|
|
21
|
+
declare class CodegenError extends Error {
|
|
22
|
+
constructor(message: string, options?: ErrorOptions);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Run one full codegen pass: discover pages, emit pages.d.ts, components.json, index.d.ts.
|
|
27
|
+
* Route discovery is deliberately skipped — it requires spawning a Nest app and is
|
|
28
|
+
* not appropriate for the hot path of a file watcher.
|
|
29
|
+
*
|
|
30
|
+
* Optionally accepts pre-discovered routes (e.g. from a full generate + route-discovery pass).
|
|
31
|
+
* When routes are present, emits routes.ts.
|
|
32
|
+
* When routes with contracts are present, also emits api.ts.
|
|
33
|
+
*/
|
|
34
|
+
declare function generate(config: ResolvedConfig, inputRoutes?: RouteDescriptor[]): Promise<void>;
|
|
35
|
+
|
|
36
|
+
interface Watcher {
|
|
37
|
+
close(): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Start two chokidar watchers:
|
|
41
|
+
*
|
|
42
|
+
* 1. **Pages watcher** (`config.pages.glob`, 150 ms debounce) — runs `generate(config)` on
|
|
43
|
+
* any page file change, regenerating `pages.d.ts` and the cache manifest.
|
|
44
|
+
*
|
|
45
|
+
* 2. **Contracts watcher** (`config.contracts.glob`, configurable debounce — default 500 ms) —
|
|
46
|
+
* re-runs static AST route discovery via ts-morph, then re-emits `routes.ts` and (when
|
|
47
|
+
* contracts are present) `api.ts` + `index.d.ts`.
|
|
48
|
+
*
|
|
49
|
+
* Both watchers share a single lock file in `config.codegen.outDir`. If another live process
|
|
50
|
+
* already holds the lock, logs a warning and returns a no-op watcher.
|
|
51
|
+
*/
|
|
52
|
+
declare function watch(config: ResolvedConfig, onChange?: () => void): Promise<Watcher>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Try to acquire an exclusive lock for a watcher in `outDir`.
|
|
56
|
+
*
|
|
57
|
+
* Uses O_CREAT | O_EXCL (via 'wx' flag) for atomic file creation to prevent
|
|
58
|
+
* TOCTOU race conditions between concurrent processes.
|
|
59
|
+
*
|
|
60
|
+
* Returns `{ release }` on success.
|
|
61
|
+
* Returns `null` if another live process already holds the lock.
|
|
62
|
+
*/
|
|
63
|
+
declare function acquireLock(outDir: string): Promise<{
|
|
64
|
+
release: () => Promise<void>;
|
|
65
|
+
} | null>;
|
|
66
|
+
|
|
67
|
+
declare const zodAdapter: ValidationAdapter;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Pure-AST translation of class-validator-decorated DTO classes into the neutral
|
|
71
|
+
* {@link SchemaModule} IR. Reads decorator names + literal args via ts-morph — it
|
|
72
|
+
* never imports class-validator at runtime. A `ValidationAdapter` renders the IR.
|
|
73
|
+
*
|
|
74
|
+
* This is the structural successor of `dto-to-zod.ts`: same control flow,
|
|
75
|
+
* recursion guard, and warnings, but it emits `SchemaNode` values instead of zod
|
|
76
|
+
* text. The zod adapter reproduces the previous output byte-for-byte.
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
declare function extractSchemaFromDto(classDecl: ClassDeclaration, sourceFile: SourceFile, project: Project): SchemaModule;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Emits `forms.ts` into `outDir` — re-exported (Path A) or inlined/synthesized
|
|
83
|
+
* (Path A inline / Path B) zod schemas per validatable route, plus a
|
|
84
|
+
* `formSchemas` name→schema map.
|
|
85
|
+
*
|
|
86
|
+
* Returns `true` when a `forms.ts` was written (drives the index export).
|
|
87
|
+
*/
|
|
88
|
+
declare function emitForms(routes: RouteDescriptor[], outDir: string, config?: ResolvedFormsConfig, adapter?: ValidationAdapter): Promise<boolean>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Emits `api.ts` into `outDir` for all routes that carry a `.contract`.
|
|
92
|
+
*
|
|
93
|
+
* By default each leaf is a bare typed-fetch callable. Registered extensions shape the
|
|
94
|
+
* output: an `apiClientLayer` (e.g. `@dudousxd/nestjs-codegen-tanstack`) turns leaves into
|
|
95
|
+
* handles; an `apiTransport` changes how requests are issued; `apiMembers` add handle
|
|
96
|
+
* members; `apiHeader` contributes top-level imports/statements.
|
|
97
|
+
*/
|
|
98
|
+
interface ApiEmitOptions {
|
|
99
|
+
fetcherImportPath?: string;
|
|
100
|
+
/** Registered extensions. Their api.ts hooks (transport/layer/members/header) are applied. */
|
|
101
|
+
extensions?: CodegenExtension[];
|
|
102
|
+
/** Shared extension context (from `generate()`). When omitted, a minimal one is built from routes. */
|
|
103
|
+
ctx?: ExtensionContext;
|
|
104
|
+
}
|
|
105
|
+
declare function emitApi(routes: RouteDescriptor[], outDir: string, opts?: ApiEmitOptions): Promise<void>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Emits `routes.ts` into `outDir` with:
|
|
109
|
+
* - A `ROUTES` constant mapping name → path
|
|
110
|
+
* - A `RouteName` union type of all route names
|
|
111
|
+
* - A `RouteParams<K>` mapped type using template-literal `infer` to extract `:param` segments
|
|
112
|
+
* - A runtime `route(name, params?)` helper that interpolates path params
|
|
113
|
+
*/
|
|
114
|
+
declare function emitRoutes(routes: RouteDescriptor[], outDir: string): Promise<void>;
|
|
115
|
+
|
|
116
|
+
interface FastDiscoveryOptions {
|
|
117
|
+
/** Absolute path to the project root. */
|
|
118
|
+
cwd: string;
|
|
119
|
+
/** Controllers glob, e.g. 'src/**\/*.controller.ts' */
|
|
120
|
+
glob: string;
|
|
121
|
+
/** Optional tsconfig.json path; default 'tsconfig.json' in cwd */
|
|
122
|
+
tsconfig?: string;
|
|
123
|
+
}
|
|
124
|
+
declare function discoverContractsFast(opts: FastDiscoveryOptions): Promise<RouteDescriptor[]>;
|
|
125
|
+
|
|
126
|
+
declare const VERSION = "0.2.0";
|
|
127
|
+
|
|
128
|
+
export { CodegenError, ConfigError, type FastDiscoveryOptions, ResolvedConfig, RouteDescriptor, SchemaModule, UserConfig, VERSION, ValidationAdapter, type Watcher, acquireLock, defineConfig, discoverContractsFast, emitApi, emitForms, emitRoutes, extractSchemaFromDto, generate, loadConfig, resolveConfig, watch, zodAdapter };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { U as UserConfig, R as ResolvedConfig, a as RouteDescriptor, V as ValidationAdapter, S as SchemaModule, b as ResolvedFormsConfig, C as CodegenExtension, E as ExtensionContext } from './index-BwIRjOQA.js';
|
|
2
|
+
export { A as AdapterUsage, c as ContractDescriptor, d as ContractSource, e as ControllerRef, N as NumberCheck, f as RenderContext, g as RenderedModule, h as SchemaNode, i as ScopeConfig, j as StringCheck, T as TypeRef, k as ValidationOption, r as resolveAdapter } from './index-BwIRjOQA.js';
|
|
3
|
+
import { ClassDeclaration, SourceFile, Project } from 'ts-morph';
|
|
4
|
+
|
|
5
|
+
declare function defineConfig(c: UserConfig): UserConfig;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resolve a {@link UserConfig} (e.g. `NestjsCodegenModule.forRoot()` options) into a
|
|
9
|
+
* fully-defaulted {@link ResolvedConfig} — without reading a config file. Used by the
|
|
10
|
+
* Nest module and any programmatic caller that already holds the config in memory.
|
|
11
|
+
*
|
|
12
|
+
* @param userConfig the raw user config (forRoot options minus module-only fields)
|
|
13
|
+
* @param cwd project root used to resolve globs / outDir. Defaults to `process.cwd()`.
|
|
14
|
+
*/
|
|
15
|
+
declare function resolveConfig(userConfig: UserConfig, cwd?: string): ResolvedConfig;
|
|
16
|
+
declare function loadConfig(cwd?: string): Promise<ResolvedConfig>;
|
|
17
|
+
|
|
18
|
+
declare class ConfigError extends Error {
|
|
19
|
+
constructor(message: string, options?: ErrorOptions);
|
|
20
|
+
}
|
|
21
|
+
declare class CodegenError extends Error {
|
|
22
|
+
constructor(message: string, options?: ErrorOptions);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Run one full codegen pass: discover pages, emit pages.d.ts, components.json, index.d.ts.
|
|
27
|
+
* Route discovery is deliberately skipped — it requires spawning a Nest app and is
|
|
28
|
+
* not appropriate for the hot path of a file watcher.
|
|
29
|
+
*
|
|
30
|
+
* Optionally accepts pre-discovered routes (e.g. from a full generate + route-discovery pass).
|
|
31
|
+
* When routes are present, emits routes.ts.
|
|
32
|
+
* When routes with contracts are present, also emits api.ts.
|
|
33
|
+
*/
|
|
34
|
+
declare function generate(config: ResolvedConfig, inputRoutes?: RouteDescriptor[]): Promise<void>;
|
|
35
|
+
|
|
36
|
+
interface Watcher {
|
|
37
|
+
close(): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Start two chokidar watchers:
|
|
41
|
+
*
|
|
42
|
+
* 1. **Pages watcher** (`config.pages.glob`, 150 ms debounce) — runs `generate(config)` on
|
|
43
|
+
* any page file change, regenerating `pages.d.ts` and the cache manifest.
|
|
44
|
+
*
|
|
45
|
+
* 2. **Contracts watcher** (`config.contracts.glob`, configurable debounce — default 500 ms) —
|
|
46
|
+
* re-runs static AST route discovery via ts-morph, then re-emits `routes.ts` and (when
|
|
47
|
+
* contracts are present) `api.ts` + `index.d.ts`.
|
|
48
|
+
*
|
|
49
|
+
* Both watchers share a single lock file in `config.codegen.outDir`. If another live process
|
|
50
|
+
* already holds the lock, logs a warning and returns a no-op watcher.
|
|
51
|
+
*/
|
|
52
|
+
declare function watch(config: ResolvedConfig, onChange?: () => void): Promise<Watcher>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Try to acquire an exclusive lock for a watcher in `outDir`.
|
|
56
|
+
*
|
|
57
|
+
* Uses O_CREAT | O_EXCL (via 'wx' flag) for atomic file creation to prevent
|
|
58
|
+
* TOCTOU race conditions between concurrent processes.
|
|
59
|
+
*
|
|
60
|
+
* Returns `{ release }` on success.
|
|
61
|
+
* Returns `null` if another live process already holds the lock.
|
|
62
|
+
*/
|
|
63
|
+
declare function acquireLock(outDir: string): Promise<{
|
|
64
|
+
release: () => Promise<void>;
|
|
65
|
+
} | null>;
|
|
66
|
+
|
|
67
|
+
declare const zodAdapter: ValidationAdapter;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Pure-AST translation of class-validator-decorated DTO classes into the neutral
|
|
71
|
+
* {@link SchemaModule} IR. Reads decorator names + literal args via ts-morph — it
|
|
72
|
+
* never imports class-validator at runtime. A `ValidationAdapter` renders the IR.
|
|
73
|
+
*
|
|
74
|
+
* This is the structural successor of `dto-to-zod.ts`: same control flow,
|
|
75
|
+
* recursion guard, and warnings, but it emits `SchemaNode` values instead of zod
|
|
76
|
+
* text. The zod adapter reproduces the previous output byte-for-byte.
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
declare function extractSchemaFromDto(classDecl: ClassDeclaration, sourceFile: SourceFile, project: Project): SchemaModule;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Emits `forms.ts` into `outDir` — re-exported (Path A) or inlined/synthesized
|
|
83
|
+
* (Path A inline / Path B) zod schemas per validatable route, plus a
|
|
84
|
+
* `formSchemas` name→schema map.
|
|
85
|
+
*
|
|
86
|
+
* Returns `true` when a `forms.ts` was written (drives the index export).
|
|
87
|
+
*/
|
|
88
|
+
declare function emitForms(routes: RouteDescriptor[], outDir: string, config?: ResolvedFormsConfig, adapter?: ValidationAdapter): Promise<boolean>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Emits `api.ts` into `outDir` for all routes that carry a `.contract`.
|
|
92
|
+
*
|
|
93
|
+
* By default each leaf is a bare typed-fetch callable. Registered extensions shape the
|
|
94
|
+
* output: an `apiClientLayer` (e.g. `@dudousxd/nestjs-codegen-tanstack`) turns leaves into
|
|
95
|
+
* handles; an `apiTransport` changes how requests are issued; `apiMembers` add handle
|
|
96
|
+
* members; `apiHeader` contributes top-level imports/statements.
|
|
97
|
+
*/
|
|
98
|
+
interface ApiEmitOptions {
|
|
99
|
+
fetcherImportPath?: string;
|
|
100
|
+
/** Registered extensions. Their api.ts hooks (transport/layer/members/header) are applied. */
|
|
101
|
+
extensions?: CodegenExtension[];
|
|
102
|
+
/** Shared extension context (from `generate()`). When omitted, a minimal one is built from routes. */
|
|
103
|
+
ctx?: ExtensionContext;
|
|
104
|
+
}
|
|
105
|
+
declare function emitApi(routes: RouteDescriptor[], outDir: string, opts?: ApiEmitOptions): Promise<void>;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Emits `routes.ts` into `outDir` with:
|
|
109
|
+
* - A `ROUTES` constant mapping name → path
|
|
110
|
+
* - A `RouteName` union type of all route names
|
|
111
|
+
* - A `RouteParams<K>` mapped type using template-literal `infer` to extract `:param` segments
|
|
112
|
+
* - A runtime `route(name, params?)` helper that interpolates path params
|
|
113
|
+
*/
|
|
114
|
+
declare function emitRoutes(routes: RouteDescriptor[], outDir: string): Promise<void>;
|
|
115
|
+
|
|
116
|
+
interface FastDiscoveryOptions {
|
|
117
|
+
/** Absolute path to the project root. */
|
|
118
|
+
cwd: string;
|
|
119
|
+
/** Controllers glob, e.g. 'src/**\/*.controller.ts' */
|
|
120
|
+
glob: string;
|
|
121
|
+
/** Optional tsconfig.json path; default 'tsconfig.json' in cwd */
|
|
122
|
+
tsconfig?: string;
|
|
123
|
+
}
|
|
124
|
+
declare function discoverContractsFast(opts: FastDiscoveryOptions): Promise<RouteDescriptor[]>;
|
|
125
|
+
|
|
126
|
+
declare const VERSION = "0.2.0";
|
|
127
|
+
|
|
128
|
+
export { CodegenError, ConfigError, type FastDiscoveryOptions, ResolvedConfig, RouteDescriptor, SchemaModule, UserConfig, VERSION, ValidationAdapter, type Watcher, acquireLock, defineConfig, discoverContractsFast, emitApi, emitForms, emitRoutes, extractSchemaFromDto, generate, loadConfig, resolveConfig, watch, zodAdapter };
|