@cap-js/cds-typer 0.9.0 → 0.10.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 +10 -1
- package/lib/file.js +24 -8
- package/lib/visitor.js +9 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
6
6
|
|
|
7
|
-
## Version 0.
|
|
7
|
+
## Version 0.10.1 - TBD
|
|
8
8
|
|
|
9
9
|
### Changed
|
|
10
10
|
|
|
@@ -12,6 +12,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
|
|
|
12
12
|
|
|
13
13
|
### Fixed
|
|
14
14
|
|
|
15
|
+
## Version 0.10.0 - 2023-09-21
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
- Actions and functions are now attached to a static `.actions` property of each generated class. This reflects the runtime behaviour better than the former way of generating instance methods
|
|
19
|
+
|
|
20
|
+
### Added
|
|
21
|
+
|
|
22
|
+
### Fixed
|
|
23
|
+
|
|
15
24
|
## Version 0.9.0 - 2023-09-08
|
|
16
25
|
|
|
17
26
|
### Changed
|
package/lib/file.js
CHANGED
|
@@ -127,18 +127,34 @@ class SourceFile extends File {
|
|
|
127
127
|
* @returns {string} - the stringified lambda
|
|
128
128
|
* @example
|
|
129
129
|
* ```js
|
|
130
|
-
*
|
|
131
|
-
* stringifyLambda({
|
|
132
|
-
* stringifyLambda({name: 'f', parameters: [['p','T']]
|
|
133
|
-
* stringifyLambda({name: 'f', parameters: [['p','T']], returns: 'number'
|
|
130
|
+
* // note: these samples are actually simplified! See below.
|
|
131
|
+
* stringifyLambda({parameters: [['p','T']]}) // f: { (p: T): any, ... }
|
|
132
|
+
* stringifyLambda({name: 'f', parameters: [['p','T']]}) // f: { (p: T) => any, ... }
|
|
133
|
+
* stringifyLambda({name: 'f', parameters: [['p','T']], returns: 'number'}) // f: { (p: T) => number, ... }
|
|
134
|
+
* stringifyLambda({name: 'f', parameters: [['p','T']], returns: 'number', initialiser: '_ => 42'}) // f: { (p: T): string = _ => 42, ... }
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* The generated string will not be just the signature of the function. Instead, it will be an object offering a callable signature.
|
|
138
|
+
* On top of that, it will also expose a property `__parameters`, which is an object reflecting the functions parameters.
|
|
139
|
+
* The reason for this is that the CDS runtime actually treats the function parameters as a named object. This can not be rectified via
|
|
140
|
+
* type magic, as parameter names do not exist on type level. So we can not use these names to reuse them as object properties.
|
|
141
|
+
* Instead, we generate this utility object for the runtime to use:
|
|
134
142
|
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```js
|
|
145
|
+
* stringifyLambda({name: 'f', parameters: [['p','T']], returns: 'number'}) // { (p: T): number, __parameters: { p: T } }
|
|
135
146
|
* ```
|
|
136
147
|
*/
|
|
137
|
-
static stringifyLambda({name, parameters=[], returns='any', initialiser}) {
|
|
138
|
-
const
|
|
139
|
-
const
|
|
148
|
+
static stringifyLambda({name, parameters=[], returns='any', initialiser, isStatic=false}) {
|
|
149
|
+
const parameterTypes = parameters.map(([n, t]) => `${n}: ${t}`).join(', ')
|
|
150
|
+
const callableSignature = `(${parameterTypes}): ${returns}`
|
|
151
|
+
let prefix = name ? `${name}: `: ''
|
|
152
|
+
if (prefix && isStatic) {
|
|
153
|
+
prefix = `static ${prefix}`
|
|
154
|
+
}
|
|
140
155
|
const suffix = initialiser ? ` = ${initialiser}` : ''
|
|
141
|
-
|
|
156
|
+
const lambda = `{ ${callableSignature}, __parameters: {${parameterTypes}}, __returns: ${returns} }`
|
|
157
|
+
return prefix + lambda + suffix
|
|
142
158
|
}
|
|
143
159
|
|
|
144
160
|
/**
|
package/lib/visitor.js
CHANGED
|
@@ -144,20 +144,25 @@ class Visitor {
|
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
buffer.add('static actions: {')
|
|
148
|
+
buffer.indent()
|
|
147
149
|
for (const [aname, action] of Object.entries(entity.actions ?? {})) {
|
|
148
150
|
buffer.add(
|
|
149
151
|
SourceFile.stringifyLambda({
|
|
150
152
|
name: aname,
|
|
151
153
|
parameters: this.#stringifyFunctionParams(action.params, file),
|
|
152
|
-
returns: action.returns ? this.resolver.resolveAndRequire(action.returns, file).typeName : 'any'
|
|
153
|
-
initialiser: `undefined as unknown as
|
|
154
|
+
returns: action.returns ? this.resolver.resolveAndRequire(action.returns, file).typeName : 'any'
|
|
155
|
+
//initialiser: `undefined as unknown as typeof ${clean}.${aname}`,
|
|
154
156
|
})
|
|
155
157
|
)
|
|
156
158
|
}
|
|
157
159
|
buffer.outdent()
|
|
158
|
-
buffer.add('}
|
|
160
|
+
buffer.add('}') // end of actions
|
|
161
|
+
|
|
162
|
+
buffer.outdent()
|
|
163
|
+
buffer.add('};') // end of generated class
|
|
159
164
|
buffer.outdent()
|
|
160
|
-
buffer.add('}')
|
|
165
|
+
buffer.add('}') // end of aspect
|
|
161
166
|
|
|
162
167
|
// CLASS WITH ADDED ASPECTS
|
|
163
168
|
file.addImport(baseDefinitions.path)
|