@itrocks/parameter-name 0.0.3 → 0.0.4
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 +133 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -7,3 +7,136 @@
|
|
|
7
7
|
# parameter-name
|
|
8
8
|
|
|
9
9
|
Runtime parameter name reflection from TypeScript declaration files.
|
|
10
|
+
|
|
11
|
+
*This documentation was written by an artificial intelligence and may contain errors or approximations.
|
|
12
|
+
It has not yet been fully reviewed by a human. If anything seems unclear or incomplete,
|
|
13
|
+
please feel free to contact the author of this package.*
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @itrocks/parameter-name
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
`@itrocks/parameter-name` provides a single function
|
|
24
|
+
`parameterNamesFromFile(fileName, className, methodName)` that reads the
|
|
25
|
+
corresponding TypeScript declaration file (`.d.ts`) and returns the list
|
|
26
|
+
of parameter names for a given class constructor or method.
|
|
27
|
+
|
|
28
|
+
It is typically used by other `@itrocks/*` packages to implement
|
|
29
|
+
decorators that need to know the **names** of constructor parameters at
|
|
30
|
+
runtime (for example to bind data, build forms, or perform dependency
|
|
31
|
+
injection), but you can also call it directly in your own code.
|
|
32
|
+
|
|
33
|
+
> **Important:** the function expects a `.d.ts` file to exist next to the
|
|
34
|
+
> runtime file, with the same base name. For example, if
|
|
35
|
+
> `fileName` is `/dist/user.js`, then a matching
|
|
36
|
+
> `/dist/user.d.ts` file must be present.
|
|
37
|
+
|
|
38
|
+
### Minimal example
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { parameterNamesFromFile } from '@itrocks/parameter-name'
|
|
42
|
+
|
|
43
|
+
const parameterNames = parameterNamesFromFile(
|
|
44
|
+
'/absolute/path/to/dist/user.js',
|
|
45
|
+
'User',
|
|
46
|
+
'constructor'
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
// e.g. ['id', 'email', 'displayName']
|
|
50
|
+
console.log(parameterNames)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
With the following declaration in the TypeScript `.d.ts` file:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
declare class User {
|
|
57
|
+
constructor(id: number, email: string, displayName?: string)
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
the call above will return `['id', 'email', 'displayName']`.
|
|
62
|
+
|
|
63
|
+
### Complete example with runtime resolution
|
|
64
|
+
|
|
65
|
+
In a more realistic scenario, you compute the file name from the
|
|
66
|
+
location of the running module and use the parameter names inside your
|
|
67
|
+
own reflection or binding logic.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import path from 'node:path'
|
|
71
|
+
import { fileURLToPath } from 'node:url'
|
|
72
|
+
import { parameterNamesFromFile } from '@itrocks/parameter-name'
|
|
73
|
+
|
|
74
|
+
// Example class defined in this module
|
|
75
|
+
export class User {
|
|
76
|
+
constructor(id: number, email: string, isAdmin = false) {}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parameterNamesOfUserConstructor(): string[] {
|
|
80
|
+
// Compute the compiled file path (e.g. dist/user.js)
|
|
81
|
+
const fileName = fileURLToPath(import.meta.url)
|
|
82
|
+
|
|
83
|
+
return parameterNamesFromFile(
|
|
84
|
+
fileName,
|
|
85
|
+
'User',
|
|
86
|
+
'constructor'
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const names = parameterNamesOfUserConstructor()
|
|
91
|
+
// ['id', 'email', 'isAdmin']
|
|
92
|
+
console.log('User constructor parameters:', names)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
You can then reuse `names` to automatically map user input, define
|
|
96
|
+
validation rules, or build a debug/logging helper that displays argument
|
|
97
|
+
names alongside their values.
|
|
98
|
+
|
|
99
|
+
## API
|
|
100
|
+
|
|
101
|
+
### `function parameterNamesFromFile(fileName: string, className: string, methodName: string): string[]`
|
|
102
|
+
|
|
103
|
+
Reads the TypeScript declaration file (`.d.ts`) corresponding to the
|
|
104
|
+
given `fileName` and extracts the parameter names of a specific class
|
|
105
|
+
constructor or method.
|
|
106
|
+
|
|
107
|
+
#### Parameters
|
|
108
|
+
|
|
109
|
+
- `fileName` – path to the compiled JavaScript file whose declaration
|
|
110
|
+
file you want to inspect. The function automatically replaces the
|
|
111
|
+
extension with `.d.ts` and reads that file. A matching declaration file
|
|
112
|
+
must exist.
|
|
113
|
+
- `className` – the exact name of the class as it appears in the
|
|
114
|
+
declaration file.
|
|
115
|
+
- `methodName` – name of the method whose parameters you want to read.
|
|
116
|
+
Use `'constructor'` to retrieve constructor parameter names.
|
|
117
|
+
|
|
118
|
+
#### Return value
|
|
119
|
+
|
|
120
|
+
- `string[]` – ordered list of parameter names. If a parameter does not
|
|
121
|
+
have a simple identifier name (for example a destructured parameter),
|
|
122
|
+
its entry in the array will be an empty string.
|
|
123
|
+
|
|
124
|
+
#### Notes and limitations
|
|
125
|
+
|
|
126
|
+
- Only **class declarations** are supported; free functions are not
|
|
127
|
+
inspected.
|
|
128
|
+
- The package relies on `.d.ts` files generated by the TypeScript
|
|
129
|
+
compiler. If those files are missing or out of sync with the runtime
|
|
130
|
+
code, the result may be incomplete or empty.
|
|
131
|
+
|
|
132
|
+
## Typical use cases
|
|
133
|
+
|
|
134
|
+
- Implement decorators that infer constructor parameter names at runtime
|
|
135
|
+
to bind values or perform dependency injection.
|
|
136
|
+
- Generate forms or validation schemas by matching parameter names with
|
|
137
|
+
metadata or user input fields.
|
|
138
|
+
- Build debugging helpers that log both parameter names and values when
|
|
139
|
+
methods or constructors are called.
|
|
140
|
+
- Create small reflection utilities in other `@itrocks/*` packages or in
|
|
141
|
+
your own framework code, without having to manually parse TypeScript
|
|
142
|
+
declaration files yourself.
|
package/package.json
CHANGED
|
@@ -29,8 +29,11 @@
|
|
|
29
29
|
"keywords": [
|
|
30
30
|
"backend",
|
|
31
31
|
"constructor",
|
|
32
|
+
"decorator",
|
|
33
|
+
"dts",
|
|
32
34
|
"function",
|
|
33
35
|
"it.rocks",
|
|
36
|
+
"metadata",
|
|
34
37
|
"method",
|
|
35
38
|
"name",
|
|
36
39
|
"parameter",
|
|
@@ -38,6 +41,7 @@
|
|
|
38
41
|
"parse",
|
|
39
42
|
"parsing",
|
|
40
43
|
"reflection",
|
|
44
|
+
"typescript",
|
|
41
45
|
"utility"
|
|
42
46
|
],
|
|
43
47
|
"license": "ISC",
|
|
@@ -52,5 +56,5 @@
|
|
|
52
56
|
"build:esm": "tsc -p tsconfig.esm.json"
|
|
53
57
|
},
|
|
54
58
|
"types": "./esm/parameter-name.d.ts",
|
|
55
|
-
"version": "0.0.
|
|
59
|
+
"version": "0.0.4"
|
|
56
60
|
}
|