@skmtc/core 0.0.794 → 0.0.796

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.
@@ -1,35 +1,348 @@
1
1
  import type { TypeSystemObject, TypeSystemValue, TypeSystemVoid } from '../types/TypeSystem.js';
2
2
  import type { Definition } from '../dsl/Definition.js';
3
3
  import { List } from './List.js';
4
+ /**
5
+ * Constructor arguments for {@link FunctionParameter}.
6
+ */
4
7
  type FunctionParameterArgs = {
8
+ /** Optional parameter name (if undefined and destructure is true, uses destructured syntax) */
5
9
  name?: string;
10
+ /** The type definition for the parameter */
6
11
  typeDefinition: Definition<TypeSystemObject | TypeSystemVoid>;
12
+ /** Whether to use destructured parameter syntax */
7
13
  destructure?: boolean;
14
+ /** Whether the parameter is required (affects optional marker) */
8
15
  required?: boolean;
16
+ /** Whether to skip empty properties in destructured parameters */
9
17
  skipEmpty?: boolean;
10
18
  };
19
+ /**
20
+ * Union type representing the different parameter configurations.
21
+ */
11
22
  type ParameterProperties = VoidParameter | DestructuredParameter | RegularParameter;
23
+ /**
24
+ * Represents a void parameter (no parameters).
25
+ */
12
26
  type VoidParameter = {
13
27
  type: 'void';
14
28
  };
29
+ /**
30
+ * Represents a destructured object parameter.
31
+ */
15
32
  type DestructuredParameter = {
16
33
  type: 'destructured';
17
34
  typeDefinition: Definition<TypeSystemObject>;
18
35
  required: true;
19
36
  };
37
+ /**
38
+ * Represents a regular named parameter.
39
+ */
20
40
  type RegularParameter = {
21
41
  type: 'regular';
22
42
  name: string;
23
43
  typeDefinition: Definition<TypeSystemValue>;
24
44
  required: boolean;
25
45
  };
46
+ /**
47
+ * Generates TypeScript function parameters with advanced handling for different parameter patterns.
48
+ *
49
+ * `FunctionParameter` is a sophisticated utility for generating TypeScript function parameter
50
+ * syntax that handles multiple parameter patterns including named parameters, destructured
51
+ * object parameters, and void parameters. It provides intelligent type handling and can
52
+ * generate both the parameter declaration and the parameter usage patterns.
53
+ *
54
+ * This class is essential for generating type-safe function signatures in API clients,
55
+ * utility functions, and other generated code where parameters need to be handled
56
+ * consistently and safely.
57
+ *
58
+ * ## Supported Parameter Patterns
59
+ *
60
+ * - **Named Parameters**: Traditional `paramName: Type` syntax
61
+ * - **Destructured Parameters**: `{ prop1, prop2 }: ObjectType` syntax
62
+ * - **Void Parameters**: No parameters when dealing with void types
63
+ * - **Optional Parameters**: Automatic handling of optional parameter markers
64
+ * - **Empty Filtering**: Option to skip empty properties in destructured objects
65
+ *
66
+ * @example Named parameter generation
67
+ * ```typescript
68
+ * import { FunctionParameter, Definition, Identifier } from '@skmtc/core';
69
+ *
70
+ * const userIdParam = new FunctionParameter({
71
+ * name: 'userId',
72
+ * typeDefinition: new Definition({
73
+ * context: generateContext,
74
+ * identifier: Identifier.createType('string'),
75
+ * value: { type: 'string' }
76
+ * }),
77
+ * required: true
78
+ * });
79
+ *
80
+ * console.log(userIdParam.toString()); // 'userId: string'
81
+ * console.log(userIdParam.toInbound()); // 'userId'
82
+ * ```
83
+ *
84
+ * @example Destructured parameter generation
85
+ * ```typescript
86
+ * const destructuredParam = new FunctionParameter({
87
+ * typeDefinition: new Definition({
88
+ * context: generateContext,
89
+ * identifier: Identifier.createType('UserParams'),
90
+ * value: {
91
+ * type: 'object',
92
+ * objectProperties: {
93
+ * properties: {
94
+ * name: { type: 'string' },
95
+ * email: { type: 'string' },
96
+ * age: { type: 'number' }
97
+ * }
98
+ * }
99
+ * }
100
+ * }),
101
+ * destructure: true,
102
+ * required: true
103
+ * });
104
+ *
105
+ * console.log(destructuredParam.toString());
106
+ * // '{ name, email, age }: UserParams'
107
+ *
108
+ * console.log(destructuredParam.toInbound());
109
+ * // '{ name, email, age }'
110
+ * ```
111
+ *
112
+ * @example Optional parameter handling
113
+ * ```typescript
114
+ * const optionalParam = new FunctionParameter({
115
+ * name: 'options',
116
+ * typeDefinition: optionsTypeDefinition,
117
+ * required: false
118
+ * });
119
+ *
120
+ * console.log(optionalParam.toString()); // 'options?: OptionsType'
121
+ * ```
122
+ *
123
+ * @example Void parameter handling
124
+ * ```typescript
125
+ * const voidParam = new FunctionParameter({
126
+ * typeDefinition: new Definition({
127
+ * context: generateContext,
128
+ * identifier: Identifier.createType('void'),
129
+ * value: { type: 'void' }
130
+ * })
131
+ * });
132
+ *
133
+ * console.log(voidParam.toString()); // ''
134
+ * console.log(voidParam.toInbound()); // ''
135
+ * ```
136
+ *
137
+ * @example Property checking
138
+ * ```typescript
139
+ * const objectParam = new FunctionParameter({
140
+ * typeDefinition: userTypeDefinition,
141
+ * destructure: true,
142
+ * required: true
143
+ * });
144
+ *
145
+ * if (objectParam.hasProperty('email')) {
146
+ * // Handle email property access
147
+ * console.log('Parameter includes email property');
148
+ * }
149
+ *
150
+ * const propertyList = objectParam.toPropertyList();
151
+ * console.log(propertyList.toString()); // 'name, email, age'
152
+ * ```
153
+ *
154
+ * @example API method generation with parameters
155
+ * ```typescript
156
+ * class ApiMethodGenerator {
157
+ * generateMethod(
158
+ * methodName: string,
159
+ * pathParams?: FunctionParameter,
160
+ * bodyParam?: FunctionParameter
161
+ * ) {
162
+ * const parameters = [pathParams, bodyParam]
163
+ * .filter(p => p && p.toString())
164
+ * .map(p => p!.toString())
165
+ * .join(', ');
166
+ *
167
+ * const pathInbound = pathParams?.toInbound() || '';
168
+ * const bodyInbound = bodyParam?.toInbound() || '';
169
+ *
170
+ * return `
171
+ * async ${methodName}(${parameters}) {
172
+ * const path = this.buildPath(${pathInbound});
173
+ * return this.request('POST', path, ${bodyInbound});
174
+ * }`;
175
+ * }
176
+ * }
177
+ * ```
178
+ *
179
+ * @example Empty property filtering
180
+ * ```typescript
181
+ * const filteredParam = new FunctionParameter({
182
+ * typeDefinition: sparseObjectDefinition,
183
+ * destructure: true,
184
+ * required: true,
185
+ * skipEmpty: true // Skip properties with empty types
186
+ * });
187
+ *
188
+ * // Only non-empty properties will be included in destructuring
189
+ * console.log(filteredParam.toString());
190
+ * // '{ validProp1, validProp2 }: FilteredType' (empty props omitted)
191
+ * ```
192
+ */
26
193
  export declare class FunctionParameter {
194
+ /** The internal parameter configuration determining how the parameter is generated */
27
195
  properties: ParameterProperties;
196
+ /** Whether to skip empty properties in destructured parameters */
28
197
  skipEmpty?: boolean;
198
+ /**
199
+ * Creates a new FunctionParameter instance with the specified configuration.
200
+ *
201
+ * The constructor analyzes the provided type definition and configuration options
202
+ * to determine the appropriate parameter pattern (void, regular, or destructured).
203
+ * It handles the complex logic of parameter type determination and validation.
204
+ *
205
+ * @param args - Configuration options for the function parameter
206
+ *
207
+ * @throws {Error} When the parameter configuration is invalid
208
+ *
209
+ * @example
210
+ * ```typescript
211
+ * const param = new FunctionParameter({
212
+ * name: 'userData',
213
+ * typeDefinition: userTypeDefinition,
214
+ * required: true
215
+ * });
216
+ * ```
217
+ */
29
218
  constructor(args: FunctionParameterArgs);
219
+ /**
220
+ * Checks if the parameter has a specific property (for object-based parameters).
221
+ *
222
+ * This method determines whether a given property name exists within the parameter's
223
+ * type structure. It's useful for conditional code generation based on available
224
+ * properties and only returns true for object-based parameters (regular object
225
+ * parameters or destructured parameters).
226
+ *
227
+ * @param name - The property name to check for
228
+ * @returns True if the property exists in the parameter's type structure
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * const userParam = new FunctionParameter({
233
+ * typeDefinition: userTypeDefinition, // has properties: name, email, age
234
+ * destructure: true,
235
+ * required: true
236
+ * });
237
+ *
238
+ * if (userParam.hasProperty('email')) {
239
+ * console.log('Parameter includes email property');
240
+ * }
241
+ *
242
+ * if (userParam.hasProperty('nonexistent')) {
243
+ * // This won't execute
244
+ * }
245
+ * ```
246
+ */
30
247
  hasProperty(name: string): boolean;
248
+ /**
249
+ * Generates a List of property names from the parameter.
250
+ *
251
+ * This method extracts property names from object-based parameters and returns
252
+ * them as a List instance. For regular parameters, it returns the parameter name,
253
+ * for destructured parameters it returns all object properties, and for void
254
+ * parameters it returns an empty list.
255
+ *
256
+ * @returns List containing the relevant property or parameter names
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * const destructuredParam = new FunctionParameter({
261
+ * typeDefinition: userTypeDefinition, // has properties: name, email, age
262
+ * destructure: true,
263
+ * required: true
264
+ * });
265
+ *
266
+ * const propertyList = destructuredParam.toPropertyList();
267
+ * console.log(propertyList.toString()); // 'name, email, age'
268
+ *
269
+ * const regularParam = new FunctionParameter({
270
+ * name: 'userData',
271
+ * typeDefinition: userTypeDefinition,
272
+ * required: true
273
+ * });
274
+ *
275
+ * const regularList = regularParam.toPropertyList();
276
+ * console.log(regularList.toString()); // 'userData'
277
+ * ```
278
+ */
31
279
  toPropertyList(): List;
280
+ /**
281
+ * Generates the inbound parameter syntax for function calls.
282
+ *
283
+ * This method produces the syntax used when calling a function with this parameter.
284
+ * For regular parameters, it returns the parameter name. For destructured parameters,
285
+ * it returns the destructured object syntax. For void parameters, it returns an
286
+ * empty string.
287
+ *
288
+ * @returns The inbound parameter syntax string
289
+ *
290
+ * @example
291
+ * ```typescript
292
+ * const regularParam = new FunctionParameter({
293
+ * name: 'userId',
294
+ * typeDefinition: stringTypeDefinition,
295
+ * required: true
296
+ * });
297
+ *
298
+ * console.log(regularParam.toInbound()); // 'userId'
299
+ *
300
+ * const destructuredParam = new FunctionParameter({
301
+ * typeDefinition: userTypeDefinition,
302
+ * destructure: true,
303
+ * required: true
304
+ * });
305
+ *
306
+ * console.log(destructuredParam.toInbound()); // '{ name, email, age }'
307
+ * ```
308
+ */
32
309
  toInbound(): string;
310
+ /**
311
+ * Generates the complete function parameter declaration syntax.
312
+ *
313
+ * This method produces the full parameter declaration that appears in a function
314
+ * signature, including the parameter name, type annotation, and optional markers.
315
+ * It handles all parameter patterns supported by the class.
316
+ *
317
+ * @returns The complete parameter declaration string
318
+ *
319
+ * @example
320
+ * ```typescript
321
+ * const requiredParam = new FunctionParameter({
322
+ * name: 'userId',
323
+ * typeDefinition: stringTypeDefinition,
324
+ * required: true
325
+ * });
326
+ *
327
+ * console.log(requiredParam.toString()); // 'userId: string'
328
+ *
329
+ * const optionalParam = new FunctionParameter({
330
+ * name: 'options',
331
+ * typeDefinition: optionsTypeDefinition,
332
+ * required: false
333
+ * });
334
+ *
335
+ * console.log(optionalParam.toString()); // 'options?: OptionsType'
336
+ *
337
+ * const destructuredParam = new FunctionParameter({
338
+ * typeDefinition: userTypeDefinition,
339
+ * destructure: true,
340
+ * required: true
341
+ * });
342
+ *
343
+ * console.log(destructuredParam.toString()); // '{ name, email, age }: UserType'
344
+ * ```
345
+ */
33
346
  toString(): string;
34
347
  }
35
348
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"FunctionParameter.d.ts","sourceRoot":"","sources":["../../src/typescript/FunctionParameter.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAC/F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACtD,OAAO,EAAE,IAAI,EAAwB,MAAM,WAAW,CAAA;AAItD,KAAK,qBAAqB,GAAG;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,cAAc,EAAE,UAAU,CAAC,gBAAgB,GAAG,cAAc,CAAC,CAAA;IAC7D,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB,CAAA;AAED,KAAK,mBAAmB,GAAG,aAAa,GAAG,qBAAqB,GAAG,gBAAgB,CAAA;AAEnF,KAAK,aAAa,GAAG;IACnB,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED,KAAK,qBAAqB,GAAG;IAC3B,IAAI,EAAE,cAAc,CAAA;IACpB,cAAc,EAAE,UAAU,CAAC,gBAAgB,CAAC,CAAA;IAC5C,QAAQ,EAAE,IAAI,CAAA;CACf,CAAA;AAED,KAAK,gBAAgB,GAAG;IACtB,IAAI,EAAE,SAAS,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ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