@cap-js/cds-typer 0.29.0 → 0.31.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 CHANGED
@@ -1,24 +1,46 @@
1
- # Change Log
1
+ # Changelog
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
- This project adheres to [Semantic Versioning](http://semver.org/).
5
- The format is based on [Keep a Changelog](http://keepachangelog.com/).
6
4
 
7
- ## Version 0.30.0 - TBD
5
+ ## [Unreleased]
8
6
 
9
- ## Version 0.29.0 - 2024-11-20
7
+ ### Added
8
+ ### Changed
9
+ ### Deprecated
10
+ ### Removed
11
+ ### Fixed
12
+ ### Security
13
+
14
+ ## [0.31.0] - 2024-12-16
15
+ ### Fixed
16
+ - type-referencing a property that is a key no longer breaks the referring property
17
+ - when targeting ESM, all imports within the generated types now add a `/index.js`-suffix to conform to modern module resolution mechanisms
18
+ - leaving `target_module_type` at `'auto'` now properly acts on a detected `"type":"module"`
19
+
20
+ ### Added
21
+ - cds aspects now generate a synthetic plural type too, to be used in `composition of many`
22
+
23
+ ## [0.30.0] - 2024-12-02
24
+
25
+ ### Changed
26
+ - [breaking] when running cds-typer in a CAP project, the default for the `outputDirectory` option will be `./@cds-models` instead of `./`. This default takes the lowest precedence after setting it in the project's `cds.env`, or explicitly as CLI argument.
27
+
28
+ ### Fixed
29
+ - cds-typer no longer ignores the selected `outputDirectory`, which would also cause an issue during build
30
+
31
+ ## [0.29.0] - 2024-11-20
10
32
  ### Added
11
- - [breaking] cds-typer now tries to automatically detect whether it has to generate ESM or CommonJS in the emitted _index.js_ files. This behaviour can be overridden via the `--targetModuleType` option. _If you rely on these generated index.js files to be CJS despite your project being of ESM type, you need to manually tell cds-typer to generate CJS files!_
33
+ - [breaking] cds-typer now tries to automatically detect whether it has to generate ESM or CommonJS in the emitted _index.js_ files. This behaviour can be overridden via the `--targetModuleType` option. _If you rely on these generated index.js files to be CJS despite your project being of ESM type, you need to manually tell cds-typer to generate CJS files!_
12
34
 
13
35
  ### Fixed
14
36
  - The static `.keys` property now properly reels in key types from inherited classes.
15
37
 
16
- ## Version 0.28.1 - 2024-11-07
38
+ ## [0.28.1] - 2024-11-07
17
39
  ### Fixed
18
40
  - `cds build` no longer fails on Windows with an `EINVAL` error.
19
41
  - `cds build` also supports custom model paths in `tsconfig.json` that do not end with `/index.ts`. This is the case for projects running with `tsx`.
20
42
 
21
- ## Version 0.28.0 - 24-10-24
43
+ ## [0.28.0] - 24-10-24
22
44
  ### Added
23
45
  - Schema definition for `cds.typer` options in `package.json` and `.cdsrc-*.json` files
24
46
  - Added a static `elements` property to all entities, which allows access to the `LinkedDefinitions` instance of an entity's elements
@@ -31,7 +53,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
31
53
  - Properly support mandatory (`not null`) action parameters with `array of` types
32
54
  - Static property `.drafts` is only create for entity classes that are actually draft enabled
33
55
 
34
- ## Version 0.27.0 - 2024-10-02
56
+ ## [0.27.0] - 2024-10-02
35
57
  ### Changed
36
58
  - Any configuration variable (via CLI or `cds.env`) can now be passed in snake_case in addition to camelCase
37
59
  - Action parameters are now generated as optional by default, which is how the runtime treats them. Mandatory parameters have to be marked as `not null` in CDS/CDL, or `notNull` in CSN.
@@ -41,7 +63,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
41
63
  - Fix a bug where cds-typer would produce redundant type declarations when the model contains an associations to another entity's property
42
64
  - Reintroduce default value `'.'` for `--outputDirectory`
43
65
 
44
- ## Version 0.26.0 - 2024-09-11
66
+ ## [0.26.0] - 2024-09-11
45
67
  ### Added
46
68
  - Added a static `.keys` property in all entities. That property is dictionary which holds all properties as keys that are marked as `key` in CDS
47
69
  - Added a CLI option `--useEntitiesProxy`. When set to `true`, all entities are wrapped into `Proxy` objects during runtime, allowing top level imports of entity types.
@@ -54,8 +76,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
54
76
  - Services now have their own export (named like the service itself). The current default export is not usable in some scenarios from CommonJS modules.
55
77
  - Enums and operation parameters can have doc comments
56
78
 
57
-
58
- ## Version 0.25.0 - 2024-08-13
79
+ ## [0.25.0] - 2024-08-13
59
80
  ### Added
60
81
  - Declaring a type alias on an enum in cds now also exports it on value level in the resulting type
61
82
 
@@ -66,7 +87,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
66
87
  - All properties are now preceeded with the `declare` modifier to pass strict tsconfigs using `useDefineForClassFields` or `noImplicitOverride`
67
88
  - The static `actions` property of generated classes now includes the types from all inherited classes to also suggest actions defined in a base entity/aspect/type.
68
89
 
69
- ## Version 0.24.0 - 2024-07-18
90
+ ## [0.24.0] - 2024-07-18
70
91
  ### Fixed
71
92
  - Suppressed an error that would incorrectly point out naming clashes when an entity was named in singular inflection in the model
72
93
  - CDS aspects now also generate a aspect-function in singular inflection, similar to how entities do
@@ -75,14 +96,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
75
96
  - Aspects generate named classes again so that tooltips will show more meaningful provenance for properties
76
97
  - The TypeScript task for `cds build` no longer looks for tsconfig.json to determine if the project has TS nature and instead checks the dependencies in the project's package.json for an occurrence of `typescript`
77
98
 
78
- ## Version 0.23.0 - 2024-07-04
99
+ ## [0.23.0] - 2024-07-04
79
100
 
80
101
  ### Fixed
81
102
  - Plurals no longer have `is_singular` attached in the resulting .js files
82
103
  - Properties are properly propagated beyond just one level of inheritance
83
104
 
84
-
85
- ## Version 0.22.0 - 2024-06-20
105
+ ## [0.22.0] - 2024-06-20
86
106
 
87
107
  ### Fixed
88
108
  - Fixed a bug where keys would sometimes inconsistently become nullable
@@ -90,16 +110,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
90
110
  ### Changed
91
111
  - Logging now internally uses `cds.log` and pipes output into the `cds-typer` logger, which can be configured via `cds.env` in addition to explicitly passing a `--logLevel` parameter to CLI. Users now have to use the levels defined in [`cds.log.levels`](https://cap.cloud.sap/docs/node.js/cds-log#log-levels). The formerly valid levels `WARNING`, `CRITICAL`, and `NONE` are now deprecated and automatically mapped to valid levels for now.
92
112
 
93
- ## Version 0.21.2 - 2024-06-06
113
+ ## [0.21.2] - 2024-06-06
94
114
  ### Fixed
95
115
  - The typescript build task will no longer attempt to run unless at least cds 8 is installed
96
116
 
97
- ## Version 0.21.1 - 2024-06-03
117
+ ## [0.21.1] - 2024-06-03
98
118
  ### Fixed
99
119
  - Added missing _cds-plugin.js_ to exported files to properly enable calling `cds build --for typescript`
100
120
 
101
-
102
- ## Version 0.21.0 - 2024-05-31
121
+ ## [0.21.0] - 2024-05-31
103
122
  ### Added
104
123
  - Added `IEEE754Compatible` flag which, when set to `true`, generates decimal fields as `(number | string)` instead of `number`. This flag will be removed in the long run
105
124
  - Added plugin to `cds build` TypeScript projects. Can be explicitly called using `cds build --for typescript`
@@ -113,15 +132,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
113
132
  - Entities ending with an "s" are no longer incorrectly truncated within `extends`-clauses
114
133
  - Entity names prefixed with their own namespace (e.g. `Name.Name`, `Name.NameAttachments`) are not stripped of their name prefix
115
134
 
116
- ## Version 0.20.2 - 2024-04-29
135
+ ## [0.20.2] - 2024-04-29
117
136
  ### Fixed
118
137
  - Referring to a property's type in a function/ action parameter no longer refers to the enclosing entity
119
138
 
120
- ## Version 0.20.1 - 2024-04-24
139
+ ## [0.20.1] - 2024-04-24
121
140
  ### Fixed
122
141
  - Void actions no longer crash the type generation process
123
142
 
124
- ## Version 0.20.0 - 2024-04-23
143
+ ## [0.20.0] - 2024-04-23
125
144
  ### Added
126
145
  - Types for actions and functions now expose a `.kind` property which holds the string `'function'` or `'action'` respectively
127
146
  - Added the `CdsDate`, `CdsDateTime`, `CdsTime`, `CdsTimestamp` types, which are each represented as a `string`.
@@ -137,32 +156,32 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
137
156
  - Aspects are now consistently named and called in their singular form
138
157
  - Only detect inflection clash if singular and plural share the same namespace. This also no longer reports `sap.common` as erroneous during type creation
139
158
 
140
- ## Version 0.19.0 - 2024-03-28
159
+ ## [0.19.0] - 2024-03-28
141
160
  ### Added
142
161
  - Support for `cds.Vector`, which will be represented as `string`
143
162
 
144
- ## Version 0.18.2 - 2024-03-21
163
+ ## [0.18.2] - 2024-03-21
145
164
  ### Fixed
146
165
  - Resolving `@sap/cds` will now look in the CWD first to ensure a consistent use the same CDS version across different setups
147
166
  - Types of function parameters starting with `cds.` are not automatically considered builtin anymore and receive a more thorough check against an allow-list
148
167
 
149
168
 
150
- ## Version 0.18.1 - 2024-03-13
169
+ ## [0.18.1] - 2024-03-13
151
170
  ### Fix
152
171
  - Remove faulty plural for CDS `type` definitions from the generated _index.js_ files
153
172
 
154
- ## Version 0.18.0 - 2024-03-12
173
+ ## [0.18.0] - 2024-03-12
155
174
  ### Added
156
175
  - Improved support for projections, including projections on inline definitions, and on views, as well as support for explicit exclusion and selection of properties
157
176
 
158
177
  ### Changed
159
178
  - [breaking] CDS `type` definitions will not be inflected. Whatever inflection you define them in will be assumed treated as a singular form and will not receive a plural form anymore
160
179
 
161
- ## Version 0.17.0 - 2024-03-05
180
+ ## [0.17.0] - 2024-03-05
162
181
  ### Fixed
163
182
  - Fixed a bug where refering to an externally defined enum via the `typeof` syntax would crash the type generation
164
183
 
165
- ## Version 0.16.0 - 2024-02-01
184
+ ## [0.16.0] - 2024-02-01
166
185
  ### Changed
167
186
  - Changed default log level from `NONE` to `ERROR`. See the doc to manually pass in another log level for cds-typer runs
168
187
  - Name collisions between automatically generated foreign key fields (`.…_ID`, `.…_code`, etc.) with explicitly named fields will now raise an error
@@ -171,7 +190,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
171
190
  ### Fixed
172
191
  - Externally defined enums can now be used as parameter types in actions
173
192
 
174
- ## Version 0.15.0 - 2023-12-21
193
+ ## [0.15.0] - 2023-12-21
175
194
  ### Added
176
195
  - Support for [scoped entities](https://cap.cloud.sap/docs/cds/cdl#scoped-names)
177
196
  - Support for [delimited identifiers](https://cap.cloud.sap/docs/cds/cdl#delimited-identifiers)
@@ -182,18 +201,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
182
201
  - Arrays of inline enum values can now be used as action parameters too. But they will only be represented by their enclosing type for now, i.e. `string`, `number`, etc.
183
202
  - Foreign keys of projection entities are now propagated as well
184
203
 
185
- ## Version 0.14.0 - 2023-12-13
204
+ ## [0.14.0] - 2023-12-13
186
205
  ### Added
187
206
  - Entities that are database views now also receive typings
188
207
 
189
- ## Version 0.13.0 - 2023-12-06
208
+ ## [0.13.0] - 2023-12-06
190
209
  ### Changed
191
210
  - Enums are now generated ecplicitly in the respective _index.js_ files and don't have to extract their values from the model at runtime anymore
192
211
 
193
212
  ### Added
194
213
  - The `excluding` clause in projections now actually excludes the specified properties in the generated types
195
214
 
196
- ## Version 0.12.0 - 2023-11-23
215
+ ## [0.12.0] - 2023-11-23
197
216
 
198
217
  ### Changed
199
218
  - Generate `cds.LargeBinary` as string, buffer, _or readable_ in the case of media content
@@ -204,7 +223,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
204
223
  ### Fixed
205
224
  - Now using names of enum values in generated _index.js_ files if no explicit value is present
206
225
 
207
- ## Version 0.11.1 - 2023-10-12
226
+ ## [0.11.1] - 2023-10-12
208
227
 
209
228
  ### Changed
210
229
 
@@ -212,7 +231,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
212
231
  ### Fixed
213
232
  - Fixed how service names are exported as default export
214
233
 
215
- ## Version 0.11.0 - 2023-10-10
234
+ ## [0.11.0] - 2023-10-10
216
235
 
217
236
  ### Changed
218
237
 
@@ -228,7 +247,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
228
247
  ### Removed
229
248
  - `compileFromCSN` is no longer part of the package's API
230
249
 
231
- ## Version 0.10.0 - 2023-09-21
250
+ ## [0.10.0] - 2023-09-21
232
251
 
233
252
  ### Changed
234
253
  - 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
@@ -237,7 +256,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
237
256
 
238
257
  ### Fixed
239
258
 
240
- ## Version 0.9.0 - 2023-09-08
259
+ ## [0.9.0] - 2023-09-08
241
260
 
242
261
  ### Changed
243
262
 
@@ -247,8 +266,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
247
266
  ### Fixed
248
267
  - Foreign keys are now propagated more than one level (think: `x_ID_ID_ID`)
249
268
 
250
-
251
- ## Version 0.8.0 - 2023-09-05
269
+ ## [0.8.0] - 2023-09-05
252
270
 
253
271
  ### Changed
254
272
 
@@ -258,8 +276,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
258
276
  - Foreign keys that are inherited via aspects are now also generated in addition to the resolved property (see 0.7.0)
259
277
  - Explicitly annotated `@singular` and `@plural` names are now properly used in generated _index.js_ files
260
278
 
261
-
262
- ## Version 0.7.0 - 2023-08-22
279
+ ## [0.7.0] - 2023-08-22
263
280
 
264
281
  ### Changed
265
282
 
@@ -268,7 +285,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
268
285
  - Foreign keys are now present in the generated types in addition to the resolved property
269
286
 
270
287
  ### Fixed
271
- ## Version 0.6.1 - 2023-08-10
288
+ ## [0.6.1] - 2023-08-10
272
289
 
273
290
  ### Changed
274
291
 
@@ -277,7 +294,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
277
294
  ### Fixed
278
295
  - Removed a warning about circular imports
279
296
 
280
- ## Version 0.6.0 - 2023-08-07
297
+ ## [0.6.0] - 2023-08-07
281
298
 
282
299
  ### Added
283
300
  - Support for `event` syntax
@@ -287,7 +304,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
287
304
  - Add leading underscore to appease `noUnusedParameters` in strict tsconfigs
288
305
  - No longer inflect `type` definitions when they are referenced within entities or other type definitions
289
306
 
290
- ## Version 0.5.0 - 2023-07-25
307
+ ## [0.5.0] - 2023-07-25
291
308
 
292
309
  ### Changed
293
310
  - Facilitate strict property checks. Note: `checkJs: true` must be present in the project's _jsconfig.json_ or _tsconfig.json_ respectively for this feature to become effective
@@ -299,12 +316,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
299
316
  - Generate `string` type for date-related types in CDS definitions
300
317
  - Generate `Buffer | string` type for the CDS type `LargeBinary`
301
318
 
302
-
303
- ## Version 0.4.0 - 2023-07-06
319
+ ## [0.4.0] - 2023-07-06
304
320
  ### Added
305
321
  - Support for enums when they are defined separately (not inline in the property type of an entity)
306
322
 
307
- ## Version 0.3.0 - 2023-06-26
323
+ ## [0.3.0] - 2023-06-26
308
324
  ### Added
309
325
  - Support `function` definitions (apart from `action`s)
310
326
  ### Changed
@@ -313,25 +329,25 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
313
329
  ### Fixed
314
330
  - Properly import CDS `type` definitions when they are referenced elsewhere
315
331
 
316
- ## Version 0.2.5-beta.1 - 2023-06-13
332
+ ## [0.2.5-beta.1] - 2023-06-13
317
333
 
318
334
  ### Changed
319
335
  - Bump version
320
336
 
321
- ## Version 0.2.4 - 2023-06-12
337
+ ## [0.2.4] - 2023-06-12
322
338
  - Enable use of annotated singular/ plural names in associations/ compositions
323
339
  - Rename package from `@sap/cds-dts-generator` to `@cap-js/cds-typer`
324
340
 
325
- ## Version 0.2.3 - 2023-05-17
341
+ ## [0.2.3] - 2023-05-17
326
342
  - Add missing library files
327
343
 
328
- ## Version 0.2.2 - 2023-05-17
344
+ ## [0.2.2] - 2023-05-17
329
345
  - Make class hierarchy flatter
330
346
 
331
- ## Version 0.2.1 - 2023-05-16
347
+ ## [0.2.1] - 2023-05-16
332
348
  - Add missing files
333
349
 
334
- ## Version 0.2.0 - 2023-05-15
350
+ ## [0.2.0] - 2023-05-15
335
351
  - use native Typescript AST in unit tests
336
352
  - add `propertiesOptional` flag
337
353
  - support flat, as well as nested inline declarations
@@ -342,9 +358,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
342
358
  - enable use of additional type libraries (HANA types available as first library)
343
359
  - provide proper JSDoc for all modules
344
360
  - export entity types for singular variants alongside plural types
345
- ## Version 0.1.1 - 2023-01-26
361
+
362
+ ## [0.1.1] - 2023-01-26
346
363
  - add TL;DR section to README
347
364
  - allow multiple positional arguments
348
365
 
349
- ## Version 0.1.0
366
+ ## [0.1.0] - 2023-01-01
350
367
  - initial code base
package/lib/cli.js CHANGED
@@ -142,6 +142,7 @@ const addCLIParamsToConfig = params => {
142
142
  }
143
143
  }
144
144
 
145
+ // when changing/ adding anything in here, make sure to adjust scripts/write-cds-typer-schema.js accordingly!
145
146
  const flags = enrichFlagSchema({
146
147
  outputDirectory: {
147
148
  desc: 'Root directory to write the generated files to.',
@@ -258,6 +259,9 @@ const prepareParameters = (/** @type {any[]} */ argv) => {
258
259
  }
259
260
 
260
261
  const main = async (/** @type {any[]} */ argv) => {
262
+ // when calling from CLI within a CAP project, make sure plugins (this includes cds-typer)
263
+ // are initialised and have their default values injected into cds.env
264
+ await cds.plugins
261
265
  const { positional } = prepareParameters(argv)
262
266
  compileFromFile(positional)
263
267
  }
@@ -53,7 +53,7 @@ export type DraftOf<T> = { new(...args: any[]): DraftEntity<T> }
53
53
  export type DraftsOf<T> = typeof Array<DraftEntity<T>>
54
54
 
55
55
  export type DeepRequired<T> = {
56
- [K in keyof T]: DeepRequired<T[K]>
56
+ [K in keyof T]: DeepRequired<Unkey<T[K]>>
57
57
  } & Exclude<Required<T>, null>;
58
58
 
59
59
  const key = Symbol('key') // to avoid .key showing up in IDE's auto-completion
@@ -63,6 +63,8 @@ export type KeysOf<T> = {
63
63
  [K in keyof T as NonNullable<T[K]> extends Key<unknown> ? K : never]-?: Key<{}> // T[K]
64
64
  }
65
65
 
66
+ export type Unkey<T> = T extends Key<infer U> ? U : T
67
+
66
68
  /**
67
69
  * Dates and timestamps are strings during runtime, so cds-typer represents them as such.
68
70
  */
package/lib/config.js CHANGED
@@ -1,3 +1,5 @@
1
+ const fs = require('node:fs')
2
+ const path = require('node:path')
1
3
  const cds = require('@sap/cds')
2
4
  const { camelToSnake, getProjectTargetType } = require('./util')
3
5
  const { LOG } = require('./logging')
@@ -32,29 +34,40 @@ const camelSnakeHybrid = target => {
32
34
  return proxy
33
35
  }
34
36
  class Config {
35
- static #defaults = {
36
- propertiesOptional: true,
37
- useEntitiesProxy: false,
38
- inlineDeclarations: 'flat',
39
- outputDirectory: '.',
40
- targetModuleType: 'auto'
37
+ /** @type {Record<string, unknown>} */
38
+ static #defaults = {}
39
+ static get defaults () {
40
+ // these are the exact defaults that are used when cds-typer is loaded as cds plugin
41
+ // which will shove the values into cds.env. When executed standalone, without cds environment,
42
+ // we need to load them from package.json ourselves.
43
+ if (Object.keys(Config.#defaults).length) return Config.#defaults
44
+ try {
45
+ const packageJsonPath = path.resolve(__dirname, path.join('..', 'package.json'))
46
+ const pjson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
47
+ Config.#defaults = pjson.cds.typer
48
+ } catch (e) {
49
+ LOG.error(`Failed to load default configuration from package.json: ${e}`)
50
+ }
51
+ return Config.#defaults
41
52
  }
42
53
 
43
54
  values = undefined
44
55
  proxy = undefined
45
56
 
46
57
  init () {
47
- this.values = {...Config.#defaults, ...(cds.env.typer ?? {})}
58
+ this.values = {...Config.defaults, ...(cds.env.typer ?? {})}
48
59
  this.proxy = camelSnakeHybrid(this.values)
49
- if (this.values.targetModuleType === 'auto') {
60
+ if (this.proxy.targetModuleType === 'auto') {
50
61
  const type = getProjectTargetType(cds.root)
62
+ let detectedType = 'cjs'
51
63
  if (type) {
52
64
  LOG.info(`automatically detected module type '${type}' in ${cds.root}`)
53
- this.values.targetModuleType = type
65
+ detectedType = type
54
66
  } else {
55
67
  LOG.warn(`target module type was set to 'auto', but could not detect module type in ${cds.root}. Falling back to cjs`)
56
- this.values.targetModuleType = 'cjs'
57
68
  }
69
+ this.values.targetModuleType = detectedType
70
+ this.proxy.targetModuleType = detectedType
58
71
  }
59
72
  }
60
73
 
package/lib/file.js CHANGED
@@ -10,7 +10,6 @@ const { proxyAccessFunction } = require('./components/javascript')
10
10
  const { createObjectOf, stringIdent } = require('./printers/wrappers')
11
11
  const { configuration } = require('./config')
12
12
  const { CJSPrinter, ESMPrinter } = require('./printers/javascript')
13
- const { getProjectTargetType } = require('./util')
14
13
 
15
14
  const AUTO_GEN_NOTE = '// This is an automatically generated file. Please do not change its contents manually!'
16
15
 
@@ -109,6 +108,9 @@ class Library extends File {
109
108
  * Source file containing several buffers.
110
109
  */
111
110
  class SourceFile extends File {
111
+ /** @type {import('../lib/printers/javascript').Printer | undefined} */
112
+ #jsPrinter
113
+
112
114
  /**
113
115
  * @param {string | Path} path - path to the file
114
116
  */
@@ -146,8 +148,10 @@ class SourceFile extends File {
146
148
  this.services = { buffer: new Buffer(), names: [] }
147
149
  /** @type {Record<string,string[]>} */
148
150
  this.entityProxies = {}
149
- /** @type {import('../lib/printers/javascript').Printer} */
150
- this.jsPrinter = configuration.targetModuleType === 'esm'
151
+ }
152
+
153
+ get jsPrinter () {
154
+ return this.#jsPrinter ??= configuration.targetModuleType === 'esm'
151
155
  ? new ESMPrinter()
152
156
  : new CJSPrinter()
153
157
  }
@@ -401,9 +405,13 @@ class SourceFile extends File {
401
405
  // currently only needed to extend cds.Service and would trigger unused-variable-errors in strict configs
402
406
  buffer.add('import cds from \'@sap/cds\'') // TODO should go to visitor#printService, but can't express this as Path
403
407
  }
408
+ const file = configuration.targetModuleType === 'esm'
409
+ ? '/index.js'
410
+ : ''
404
411
  for (const imp of Object.values(this.imports)) {
405
412
  if (!imp.isCwd(this.path.asDirectory())) {
406
- buffer.add(`import * as ${imp.asIdentifier()} from '${imp.asDirectory({relative: this.path.asDirectory()})}';`)
413
+ const from = imp.asDirectory({relative: this.path.asDirectory()}) + file
414
+ buffer.add(`import * as ${imp.asIdentifier()} from '${from}';`)
407
415
  }
408
416
  }
409
417
  buffer.blankLine()
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Forcefully overrides the .name property of a class.
3
+ * See: https://github.com/microsoft/TypeScript/issues/442
4
+ * @param {string} clazz - the class to override the name property for
5
+ * @param {string} content - the content to set the name property to
6
+ */
7
+ const overrideNameProperty = (clazz, content) => `Object.defineProperty(${clazz}, 'name', { value: '${content}' })`
8
+
1
9
  class JavaScriptPrinter {
2
10
  /**
3
11
  * @param {string} text - comment text
@@ -17,7 +25,7 @@ class JavaScriptPrinter {
17
25
  /**
18
26
  * @abstract
19
27
  * @param {string} alias - what the import should be known as within the importing file
20
- * @param {string} from - the package/ location to import from
28
+ * @param {string} from - the package/ location to import from
21
29
  * @returns {string}
22
30
  */
23
31
  // eslint-disable-next-line no-unused-vars
@@ -127,5 +135,6 @@ class CJSPrinter extends JavaScriptPrinter {
127
135
 
128
136
  module.exports = {
129
137
  CJSPrinter,
130
- ESMPrinter
138
+ ESMPrinter,
139
+ overrideNameProperty
131
140
  }
@@ -113,6 +113,14 @@ const createPromiseOf = t => `Promise<${t}>`
113
113
  */
114
114
  const deepRequire = (t, lookup = '') => `${base}.DeepRequired<${t}>${lookup}`
115
115
 
116
+ /**
117
+ * Wraps a type into an unkey type (removes they Key<> type).
118
+ * This is needed for type references, where Foo:id should result in `typeof id`, not `Key<typeof id>`.
119
+ * @param {string} t - the type to wrap.
120
+ * @returns {string}
121
+ */
122
+ const unkey = t => `${base}.Unkey<${t}>`
123
+
116
124
  /**
117
125
  * Puts a passed string in docstring format.
118
126
  * @param {string | undefined} doc - raw string to docify. May contain linebreaks.
@@ -150,5 +158,6 @@ module.exports = {
150
158
  createCompositionOfMany,
151
159
  deepRequire,
152
160
  docify,
153
- stringIdent
161
+ stringIdent,
162
+ unkey
154
163
  }
@@ -189,6 +189,8 @@ function asIdentifier ({info, wrapper = undefined, relative = undefined}) {
189
189
  : `${info.namespace.asIdentifier()}.${wrapped}`
190
190
  }
191
191
 
192
+ /** @typedef {EntityInfo} Info */
193
+
192
194
  module.exports = {
193
195
  EntityRepository,
194
196
  asIdentifier
package/lib/visitor.js CHANGED
@@ -8,7 +8,7 @@ const { SourceFile, FileRepository, Buffer, Path } = require('./file')
8
8
  const { FlatInlineDeclarationResolver, StructuredInlineDeclarationResolver } = require('./components/inline')
9
9
  const { Resolver } = require('./resolution/resolver')
10
10
  const { LOG } = require('./logging')
11
- const { docify, createPromiseOf, createUnionOf, createKeysOf, createElementsOf, stringIdent, createDraftsOf, createDraftOf, createIntersectionOf, createReturnTypeOf } = require('./printers/wrappers')
11
+ const { docify, createPromiseOf, createUnionOf, createKeysOf, createElementsOf, stringIdent, createDraftsOf, createDraftOf, createIntersectionOf } = require('./printers/wrappers')
12
12
  const { csnToEnumPairs, propertyToInlineEnumName, isInlineEnumType, stringifyEnumType } = require('./components/enum')
13
13
  const { isReferenceType } = require('./components/reference')
14
14
  const { empty } = require('./components/typescript')
@@ -18,6 +18,7 @@ const { last } = require('./components/identifier')
18
18
  const { getPropertyModifiers } = require('./components/property')
19
19
  const { configuration } = require('./config')
20
20
  const { createMember } = require('./components/class')
21
+ const { overrideNameProperty } = require('./printers/javascript')
21
22
 
22
23
  /** @typedef {import('./file').File} File */
23
24
  /** @typedef {import('./typedefs').visitor.Context} Context */
@@ -27,6 +28,7 @@ const { createMember } = require('./components/class')
27
28
  /** @typedef {import('./typedefs').resolver.EntityCSN} EntityCSN */
28
29
  /** @typedef {import('./typedefs').resolver.EnumCSN} EnumCSN */
29
30
 
31
+
30
32
  class Visitor {
31
33
  /**
32
34
  * Gathers all files that are supposed to be written to
@@ -354,13 +356,8 @@ class Visitor {
354
356
  * @param {EntityCSN} entity - the entity to print
355
357
  */
356
358
  #printEntity(fq, entity) {
357
- // static .name has to be defined more forcefully: https://github.com/microsoft/TypeScript/issues/442
358
- /**
359
- * @param {string} clazz - the class to override the name property for
360
- * @param {string} content - the content to set the name property to
361
- */
362
- const overrideNameProperty = (clazz, content) => `Object.defineProperty(${clazz}, 'name', { value: '${content}' })`
363
- const { namespace: ns, entityName: clean, inflection } = this.entityRepository.getByFqOrThrow(fq)
359
+ const info = this.entityRepository.getByFqOrThrow(fq)
360
+ const { namespace: ns, entityName: clean, inflection } = info
364
361
  const file = this.fileRepository.getNamespaceFile(ns)
365
362
  let { singular, plural } = inflection
366
363
 
@@ -408,24 +405,34 @@ class Visitor {
408
405
  buffer.add(`Object.defineProperty(${singular}, 'is_singular', { value: true })`)
409
406
 
410
407
  // PLURAL
411
-
412
408
  // types do not receive a plural
413
409
  if (!isType(entity)) {
414
- if (plural.includes('.')) {
415
- // Foo.text -> namespace Foo { class text { ... }}
416
- plural = last(plural)
417
- }
418
- // plural can not be a type alias to $singular[] but needs to be a proper class instead,
419
- // so it can get passed as value to CQL functions.
420
- const additionalProperties = this.#staticClassContents(fq, singular, true)
421
- additionalProperties.push('$count?: number')
422
- buffer.add(docify(entity.doc))
423
- buffer.add(`export class ${plural} extends Array<${singular}> {${additionalProperties.join('\n')}}`)
424
- buffer.add(overrideNameProperty(plural, entity.name))
410
+ this.#printPlural(info, buffer)
425
411
  }
426
412
  buffer.blankLine()
427
413
  }
428
414
 
415
+ /**
416
+ * @param {import('./resolution/entity').Info} info - the entity info
417
+ * @param {import('./file').Buffer} buffer - buffer to write the plural into
418
+ */
419
+ #printPlural(info, buffer) {
420
+ const { fullyQualifiedName: fq, csn: entity } = info
421
+ let { singular, plural } = info.inflection
422
+
423
+ if (plural.includes('.')) {
424
+ // Foo.text -> namespace Foo { class text { ... }}
425
+ plural = last(plural)
426
+ }
427
+ // plural can not be a type alias to $singular[] but needs to be a proper class instead,
428
+ // so it can get passed as value to CQL functions.
429
+ const additionalProperties = this.#staticClassContents(fq, singular, true)
430
+ additionalProperties.push('$count?: number')
431
+ buffer.add(docify(entity.doc))
432
+ buffer.add(`export class ${plural} extends Array<${singular}> {${additionalProperties.join('\n')}}`)
433
+ buffer.add(overrideNameProperty(plural, entity.name))
434
+ }
435
+
429
436
  /**
430
437
  * Stringifies function parameters in preparation of passing them to {@link SourceFile.stringifyLambda}.
431
438
  * Resolves all parameters to a pair of parameter name and name of the resolved type.
@@ -522,7 +529,8 @@ class Visitor {
522
529
  */
523
530
  #printAspect(fq, aspect) {
524
531
  LOG.debug(`Printing aspect ${fq}`)
525
- const { namespace, entityName, inflection } = this.entityRepository.getByFqOrThrow(fq)
532
+ const info = this.entityRepository.getByFqOrThrow(fq)
533
+ const { namespace, entityName, inflection } = info
526
534
  const file = this.fileRepository.getNamespaceFile(namespace)
527
535
  // aspects are technically classes and can therefore be added to the list of defined classes.
528
536
  // Still, when using them as mixins for a class, they need to already be defined.
@@ -530,6 +538,9 @@ class Visitor {
530
538
  file.addClass(entityName, fq)
531
539
  file.aspects.add(`// the following represents the CDS aspect '${entityName}'`)
532
540
  this.#aspectify(fq, aspect, file.aspects, { cleanName: inflection.singular })
541
+ if (!isType(aspect)) {
542
+ this.#printPlural(info, file.aspects)
543
+ }
533
544
  }
534
545
 
535
546
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/cds-typer",
3
- "version": "0.29.0",
3
+ "version": "0.31.0",
4
4
  "description": "Generates .ts files for a CDS model to receive code completion in VS Code",
5
5
  "main": "index.js",
6
6
  "repository": "github:cap-js/cds-typer",
@@ -63,6 +63,13 @@
63
63
  ]
64
64
  },
65
65
  "cds": {
66
+ "typer": {
67
+ "output_directory": "@cds-models",
68
+ "inline_declarations": "flat",
69
+ "target_module_type": "auto",
70
+ "properties_optional": true,
71
+ "use_entities_proxy": false
72
+ },
66
73
  "schema": {
67
74
  "buildTaskType": {
68
75
  "name": "typescript",