@ic-reactor/codegen 0.1.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.
@@ -0,0 +1,237 @@
1
+ /**
2
+ * Shared types for IC Reactor code generation
3
+ */
4
+ /**
5
+ * Information about a canister method extracted from a .did file
6
+ */
7
+ interface MethodInfo {
8
+ /** Method name as defined in the Candid interface */
9
+ name: string;
10
+ /** Whether this method is a query or update (mutation) */
11
+ type: "query" | "mutation";
12
+ /** Whether the method accepts arguments */
13
+ hasArgs: boolean;
14
+ /** Human-readable description of the arguments */
15
+ argsDescription?: string;
16
+ /** Human-readable description of the return type */
17
+ returnDescription?: string;
18
+ }
19
+ /**
20
+ * Configuration for a single canister
21
+ */
22
+ interface CanisterConfig {
23
+ /** Name of the canister (used for variable naming) */
24
+ name?: string;
25
+ /** Path to the .did file */
26
+ didFile: string;
27
+ /** Output directory (default: ./src/canisters/<name>) */
28
+ outDir?: string;
29
+ /** Use DisplayReactor for automatic type transformations (default: true) */
30
+ useDisplayReactor?: boolean;
31
+ /**
32
+ * Path to import ClientManager from (relative to generated file).
33
+ * The file at this path should export: { clientManager: ClientManager }
34
+ * Default: "../../lib/client"
35
+ */
36
+ clientManagerPath?: string;
37
+ /** Custom canister ID (optional, uses environment by default) */
38
+ canisterId?: string;
39
+ }
40
+ /**
41
+ * Hook generation type
42
+ */
43
+ type HookType = "query" | "suspenseQuery" | "infiniteQuery" | "suspenseInfiniteQuery" | "mutation";
44
+ /**
45
+ * Options for generating a hook file
46
+ */
47
+ interface GeneratorOptions {
48
+ canisterName: string;
49
+ methodName: string;
50
+ methodType: "query" | "mutation";
51
+ hasArgs: boolean;
52
+ outDir: string;
53
+ }
54
+ /**
55
+ * Options for reactor file generation
56
+ */
57
+ interface ReactorGeneratorOptions {
58
+ canisterName: string;
59
+ canisterConfig: CanisterConfig;
60
+ /** Global default clientManagerPath */
61
+ globalClientManagerPath?: string;
62
+ /** Whether declarations have been generated */
63
+ hasDeclarations?: boolean;
64
+ /** Whether to generate advanced per-method hooks (default: false) */
65
+ advanced?: boolean;
66
+ /** DID content (required when advanced=true) */
67
+ didContent?: string;
68
+ }
69
+
70
+ /**
71
+ * Naming utilities for code generation
72
+ *
73
+ * Uses the `change-case` library for base transformations,
74
+ * with domain-specific helpers for IC Reactor patterns.
75
+ */
76
+ /**
77
+ * Convert string to PascalCase
78
+ * @example toPascalCase("get_message") → "GetMessage"
79
+ * @example toPascalCase("my-canister") → "MyCanister"
80
+ */
81
+ declare function toPascalCase(str: string): string;
82
+ /**
83
+ * Convert string to camelCase
84
+ * @example toCamelCase("get_message") → "getMessage"
85
+ * @example toCamelCase("my-canister") → "myCanister"
86
+ */
87
+ declare function toCamelCase(str: string): string;
88
+ /**
89
+ * Generate hook file name
90
+ * @example getHookFileName("get_message", "query") → "getMessageQuery.ts"
91
+ */
92
+ declare function getHookFileName(methodName: string, hookType: string): string;
93
+ /**
94
+ * Generate hook export name
95
+ * @example getHookExportName("get_message", "query") → "getMessageQuery"
96
+ */
97
+ declare function getHookExportName(methodName: string, hookType: string): string;
98
+ /**
99
+ * Generate React hook name (with use prefix)
100
+ * @example getReactHookName("get_message", "query") → "useGetMessageQuery"
101
+ */
102
+ declare function getReactHookName(methodName: string, hookType: string): string;
103
+ /**
104
+ * Generate reactor variable name
105
+ * @example getReactorName("backend") → "backendReactor"
106
+ */
107
+ declare function getReactorName(canisterName: string): string;
108
+ /**
109
+ * Generate service type name
110
+ * @example getServiceTypeName("backend") → "BackendService"
111
+ */
112
+ declare function getServiceTypeName(canisterName: string): string;
113
+
114
+ /**
115
+ * DID file parser
116
+ *
117
+ * Extracts method information from Candid interface definition files.
118
+ * Based on the CLI's parser implementation (with comment stripping).
119
+ */
120
+
121
+ /**
122
+ * Parse a .did file and extract method information
123
+ */
124
+ declare function parseDIDFile(didFilePath: string): MethodInfo[];
125
+ /**
126
+ * Extract methods from DID content
127
+ *
128
+ * Handles formats like:
129
+ * - `name : (args) -> (result)`
130
+ * - `name : (args) -> (result) query`
131
+ * - `name : (args) -> (result) composite_query`
132
+ * - `name : func (args) -> (result)`
133
+ */
134
+ declare function extractMethods(didContent: string): MethodInfo[];
135
+ /**
136
+ * Get methods by type
137
+ */
138
+ declare function getMethodsByType(methods: MethodInfo[], type: "query" | "mutation"): MethodInfo[];
139
+ /**
140
+ * Format method info for display
141
+ */
142
+ declare function formatMethodForDisplay(method: MethodInfo): string;
143
+
144
+ /**
145
+ * Bindgen utilities
146
+ *
147
+ * Generates TypeScript declarations from Candid files using @icp-sdk/bindgen.
148
+ */
149
+ interface BindgenOptions {
150
+ /** Path to the .did file */
151
+ didFile: string;
152
+ /** Output directory for generated declarations */
153
+ outDir: string;
154
+ /** Canister name (used for naming) */
155
+ canisterName: string;
156
+ }
157
+ interface BindgenResult {
158
+ success: boolean;
159
+ declarationsDir: string;
160
+ error?: string;
161
+ }
162
+ /**
163
+ * Generate TypeScript declarations from a Candid file
164
+ *
165
+ * This creates:
166
+ * - declarations/<canisterName>.did.ts - IDL factory and types
167
+ */
168
+ declare function generateDeclarations(options: BindgenOptions): Promise<BindgenResult>;
169
+ /**
170
+ * Check if declarations already exist for a canister
171
+ */
172
+ declare function declarationsExist(outDir: string, canisterName: string): boolean;
173
+ /**
174
+ * Save a Candid source to a file (for use with fetch command)
175
+ */
176
+ declare function saveCandidFile(candidSource: string, outDir: string, canisterName: string): string;
177
+
178
+ /**
179
+ * Reactor file template generator
180
+ *
181
+ * Generates the reactor instance file for a canister.
182
+ * Supports simple mode (generic hooks) and advanced mode (per-method typed hooks).
183
+ */
184
+
185
+ /**
186
+ * Generate the reactor file content
187
+ */
188
+ declare function generateReactorFile(options: ReactorGeneratorOptions): string;
189
+
190
+ /**
191
+ * Query hook template generator
192
+ *
193
+ * Generates createQuery/createQueryFactory-based hooks for canister query methods.
194
+ */
195
+
196
+ interface QueryHookOptions {
197
+ canisterName: string;
198
+ method: MethodInfo;
199
+ type?: HookType;
200
+ }
201
+ /**
202
+ * Generate a query hook file content
203
+ */
204
+ declare function generateQueryHook(options: QueryHookOptions): string;
205
+
206
+ /**
207
+ * Mutation hook template generator
208
+ *
209
+ * Generates createMutation-based hooks for canister update methods.
210
+ */
211
+
212
+ interface MutationHookOptions {
213
+ canisterName: string;
214
+ method: MethodInfo;
215
+ }
216
+ /**
217
+ * Generate a mutation hook file content
218
+ */
219
+ declare function generateMutationHook(options: MutationHookOptions): string;
220
+
221
+ /**
222
+ * Infinite Query hook template generator
223
+ *
224
+ * Generates createInfiniteQuery-based hooks for paginated canister methods.
225
+ */
226
+
227
+ interface InfiniteQueryHookOptions {
228
+ canisterName: string;
229
+ method: MethodInfo;
230
+ type?: HookType;
231
+ }
232
+ /**
233
+ * Generate an infinite query hook file content
234
+ */
235
+ declare function generateInfiniteQueryHook(options: InfiniteQueryHookOptions): string;
236
+
237
+ export { type BindgenOptions, type BindgenResult, type CanisterConfig, type GeneratorOptions, type HookType, type InfiniteQueryHookOptions, type MethodInfo, type MutationHookOptions, type QueryHookOptions, type ReactorGeneratorOptions, declarationsExist, extractMethods, formatMethodForDisplay, generateDeclarations, generateInfiniteQueryHook, generateMutationHook, generateQueryHook, generateReactorFile, getHookExportName, getHookFileName, getMethodsByType, getReactHookName, getReactorName, getServiceTypeName, parseDIDFile, saveCandidFile, toCamelCase, toPascalCase };
@@ -0,0 +1,237 @@
1
+ /**
2
+ * Shared types for IC Reactor code generation
3
+ */
4
+ /**
5
+ * Information about a canister method extracted from a .did file
6
+ */
7
+ interface MethodInfo {
8
+ /** Method name as defined in the Candid interface */
9
+ name: string;
10
+ /** Whether this method is a query or update (mutation) */
11
+ type: "query" | "mutation";
12
+ /** Whether the method accepts arguments */
13
+ hasArgs: boolean;
14
+ /** Human-readable description of the arguments */
15
+ argsDescription?: string;
16
+ /** Human-readable description of the return type */
17
+ returnDescription?: string;
18
+ }
19
+ /**
20
+ * Configuration for a single canister
21
+ */
22
+ interface CanisterConfig {
23
+ /** Name of the canister (used for variable naming) */
24
+ name?: string;
25
+ /** Path to the .did file */
26
+ didFile: string;
27
+ /** Output directory (default: ./src/canisters/<name>) */
28
+ outDir?: string;
29
+ /** Use DisplayReactor for automatic type transformations (default: true) */
30
+ useDisplayReactor?: boolean;
31
+ /**
32
+ * Path to import ClientManager from (relative to generated file).
33
+ * The file at this path should export: { clientManager: ClientManager }
34
+ * Default: "../../lib/client"
35
+ */
36
+ clientManagerPath?: string;
37
+ /** Custom canister ID (optional, uses environment by default) */
38
+ canisterId?: string;
39
+ }
40
+ /**
41
+ * Hook generation type
42
+ */
43
+ type HookType = "query" | "suspenseQuery" | "infiniteQuery" | "suspenseInfiniteQuery" | "mutation";
44
+ /**
45
+ * Options for generating a hook file
46
+ */
47
+ interface GeneratorOptions {
48
+ canisterName: string;
49
+ methodName: string;
50
+ methodType: "query" | "mutation";
51
+ hasArgs: boolean;
52
+ outDir: string;
53
+ }
54
+ /**
55
+ * Options for reactor file generation
56
+ */
57
+ interface ReactorGeneratorOptions {
58
+ canisterName: string;
59
+ canisterConfig: CanisterConfig;
60
+ /** Global default clientManagerPath */
61
+ globalClientManagerPath?: string;
62
+ /** Whether declarations have been generated */
63
+ hasDeclarations?: boolean;
64
+ /** Whether to generate advanced per-method hooks (default: false) */
65
+ advanced?: boolean;
66
+ /** DID content (required when advanced=true) */
67
+ didContent?: string;
68
+ }
69
+
70
+ /**
71
+ * Naming utilities for code generation
72
+ *
73
+ * Uses the `change-case` library for base transformations,
74
+ * with domain-specific helpers for IC Reactor patterns.
75
+ */
76
+ /**
77
+ * Convert string to PascalCase
78
+ * @example toPascalCase("get_message") → "GetMessage"
79
+ * @example toPascalCase("my-canister") → "MyCanister"
80
+ */
81
+ declare function toPascalCase(str: string): string;
82
+ /**
83
+ * Convert string to camelCase
84
+ * @example toCamelCase("get_message") → "getMessage"
85
+ * @example toCamelCase("my-canister") → "myCanister"
86
+ */
87
+ declare function toCamelCase(str: string): string;
88
+ /**
89
+ * Generate hook file name
90
+ * @example getHookFileName("get_message", "query") → "getMessageQuery.ts"
91
+ */
92
+ declare function getHookFileName(methodName: string, hookType: string): string;
93
+ /**
94
+ * Generate hook export name
95
+ * @example getHookExportName("get_message", "query") → "getMessageQuery"
96
+ */
97
+ declare function getHookExportName(methodName: string, hookType: string): string;
98
+ /**
99
+ * Generate React hook name (with use prefix)
100
+ * @example getReactHookName("get_message", "query") → "useGetMessageQuery"
101
+ */
102
+ declare function getReactHookName(methodName: string, hookType: string): string;
103
+ /**
104
+ * Generate reactor variable name
105
+ * @example getReactorName("backend") → "backendReactor"
106
+ */
107
+ declare function getReactorName(canisterName: string): string;
108
+ /**
109
+ * Generate service type name
110
+ * @example getServiceTypeName("backend") → "BackendService"
111
+ */
112
+ declare function getServiceTypeName(canisterName: string): string;
113
+
114
+ /**
115
+ * DID file parser
116
+ *
117
+ * Extracts method information from Candid interface definition files.
118
+ * Based on the CLI's parser implementation (with comment stripping).
119
+ */
120
+
121
+ /**
122
+ * Parse a .did file and extract method information
123
+ */
124
+ declare function parseDIDFile(didFilePath: string): MethodInfo[];
125
+ /**
126
+ * Extract methods from DID content
127
+ *
128
+ * Handles formats like:
129
+ * - `name : (args) -> (result)`
130
+ * - `name : (args) -> (result) query`
131
+ * - `name : (args) -> (result) composite_query`
132
+ * - `name : func (args) -> (result)`
133
+ */
134
+ declare function extractMethods(didContent: string): MethodInfo[];
135
+ /**
136
+ * Get methods by type
137
+ */
138
+ declare function getMethodsByType(methods: MethodInfo[], type: "query" | "mutation"): MethodInfo[];
139
+ /**
140
+ * Format method info for display
141
+ */
142
+ declare function formatMethodForDisplay(method: MethodInfo): string;
143
+
144
+ /**
145
+ * Bindgen utilities
146
+ *
147
+ * Generates TypeScript declarations from Candid files using @icp-sdk/bindgen.
148
+ */
149
+ interface BindgenOptions {
150
+ /** Path to the .did file */
151
+ didFile: string;
152
+ /** Output directory for generated declarations */
153
+ outDir: string;
154
+ /** Canister name (used for naming) */
155
+ canisterName: string;
156
+ }
157
+ interface BindgenResult {
158
+ success: boolean;
159
+ declarationsDir: string;
160
+ error?: string;
161
+ }
162
+ /**
163
+ * Generate TypeScript declarations from a Candid file
164
+ *
165
+ * This creates:
166
+ * - declarations/<canisterName>.did.ts - IDL factory and types
167
+ */
168
+ declare function generateDeclarations(options: BindgenOptions): Promise<BindgenResult>;
169
+ /**
170
+ * Check if declarations already exist for a canister
171
+ */
172
+ declare function declarationsExist(outDir: string, canisterName: string): boolean;
173
+ /**
174
+ * Save a Candid source to a file (for use with fetch command)
175
+ */
176
+ declare function saveCandidFile(candidSource: string, outDir: string, canisterName: string): string;
177
+
178
+ /**
179
+ * Reactor file template generator
180
+ *
181
+ * Generates the reactor instance file for a canister.
182
+ * Supports simple mode (generic hooks) and advanced mode (per-method typed hooks).
183
+ */
184
+
185
+ /**
186
+ * Generate the reactor file content
187
+ */
188
+ declare function generateReactorFile(options: ReactorGeneratorOptions): string;
189
+
190
+ /**
191
+ * Query hook template generator
192
+ *
193
+ * Generates createQuery/createQueryFactory-based hooks for canister query methods.
194
+ */
195
+
196
+ interface QueryHookOptions {
197
+ canisterName: string;
198
+ method: MethodInfo;
199
+ type?: HookType;
200
+ }
201
+ /**
202
+ * Generate a query hook file content
203
+ */
204
+ declare function generateQueryHook(options: QueryHookOptions): string;
205
+
206
+ /**
207
+ * Mutation hook template generator
208
+ *
209
+ * Generates createMutation-based hooks for canister update methods.
210
+ */
211
+
212
+ interface MutationHookOptions {
213
+ canisterName: string;
214
+ method: MethodInfo;
215
+ }
216
+ /**
217
+ * Generate a mutation hook file content
218
+ */
219
+ declare function generateMutationHook(options: MutationHookOptions): string;
220
+
221
+ /**
222
+ * Infinite Query hook template generator
223
+ *
224
+ * Generates createInfiniteQuery-based hooks for paginated canister methods.
225
+ */
226
+
227
+ interface InfiniteQueryHookOptions {
228
+ canisterName: string;
229
+ method: MethodInfo;
230
+ type?: HookType;
231
+ }
232
+ /**
233
+ * Generate an infinite query hook file content
234
+ */
235
+ declare function generateInfiniteQueryHook(options: InfiniteQueryHookOptions): string;
236
+
237
+ export { type BindgenOptions, type BindgenResult, type CanisterConfig, type GeneratorOptions, type HookType, type InfiniteQueryHookOptions, type MethodInfo, type MutationHookOptions, type QueryHookOptions, type ReactorGeneratorOptions, declarationsExist, extractMethods, formatMethodForDisplay, generateDeclarations, generateInfiniteQueryHook, generateMutationHook, generateQueryHook, generateReactorFile, getHookExportName, getHookFileName, getMethodsByType, getReactHookName, getReactorName, getServiceTypeName, parseDIDFile, saveCandidFile, toCamelCase, toPascalCase };