Skip to content

Commit

Permalink
Export service as unnamed class (#454)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Georgi <[email protected]>
  • Loading branch information
daogrady and chgeo authored Jan 16, 2025
1 parent 031a586 commit 54389a1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ All notable changes to this project will be documented in this file.

### Changed
- prefixed builtin types like `Promise` and `Record` with `globalThis.`, to allow using names of builtin types for entities without collisions
- default export class representing the service itself is now exported without name
- bumped peer-dependency to `@cap-js/cds-types` to `>=0.9`

### Deprecated
### Removed
### Fixed
Expand Down
4 changes: 0 additions & 4 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,6 @@ class SourceFile extends File {
*/
getImports() {
const buffer = new Buffer()
if (this.services.names.length) {
// currently only needed to extend cds.Service and would trigger unused-variable-errors in strict configs
buffer.add('import cds from \'@sap/cds\'') // TODO should go to visitor#printService, but can't express this as Path
}
const file = configuration.targetModuleType === 'esm'
? '/index.js'
: ''
Expand Down
11 changes: 8 additions & 3 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,18 @@ class Visitor {

docify(service.doc).forEach(d => { buffer.add(d) })
// file.addImport(new Path(['cds'], '')) TODO make sap/cds import work
buffer.addIndentedBlock(`export class ${serviceNameSimple} extends cds.Service {`, () => {
buffer.addIndentedBlock('export default class {', () => {
Object.entries(service.operations ?? {}).forEach(([name, {doc}]) => {
buffer.add(docify(doc))
buffer.add(`declare ${name}: typeof ${name}`)
buffer.add(createMember({
name,
type: `typeof ${name}`,
isStatic: true,
isReadonly: true,
isDeclare: true,
}))
})
}, '}')
buffer.add(`export default ${serviceNameSimple}`)
buffer.blankLine()
file.addService(service.name)
}
Expand Down
8 changes: 3 additions & 5 deletions test/unit/files/actions/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ import {
getOneExternalType,
} from '#cds-models/actions_test/S'

import S2_default from '#cds-models/actions_test/S2'
import { S2 } from '#cds-models/actions_test/S2'
S2_default === S2
import S2 from '#cds-models/actions_test/S2'

import { S as S_} from '#cds-models/actions_test/S'
import S_ from '#cds-models/actions_test/S'

import { ExternalType, ExternalType2 } from '#cds-models/elsewhere'
import { ExternalInRoot } from '#cds-models';
Expand Down Expand Up @@ -63,7 +61,7 @@ export class S extends cds.ApplicationService { override async init(){
this.on(free4, req => { return { extType2:1 } satisfies ExternalType2 })

// calling actions
const s2:S2 = await cds.connect.to(S2)
const s2 = await cds.connect.to(S2)
await s2.a1({p1: '', p2: [ { extType2: 1 } ]}) satisfies ExternalType | null
await s2.a1('', [ { extType2: 1 } ]) satisfies ExternalType | null
await s2.a2({p1: '', p3: 1}) satisfies ExternalType | null
Expand Down

0 comments on commit 54389a1

Please sign in to comment.