From 54389a1f1de865c1cef9812f6b4b39ff72a55326 Mon Sep 17 00:00:00 2001 From: Daniel O'Grady <103028279+daogrady@users.noreply.github.com> Date: Thu, 16 Jan 2025 06:30:02 +0100 Subject: [PATCH] Export service as unnamed class (#454) Co-authored-by: Christian Georgi --- CHANGELOG.md | 2 ++ lib/file.js | 4 ---- lib/visitor.js | 11 ++++++++--- test/unit/files/actions/model.ts | 8 +++----- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 522f3cc8..e38ca7fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/file.js b/lib/file.js index ce35733e..0e46ab7c 100644 --- a/lib/file.js +++ b/lib/file.js @@ -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' : '' diff --git a/lib/visitor.js b/lib/visitor.js index ccce7580..e189878b 100644 --- a/lib/visitor.js +++ b/lib/visitor.js @@ -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) } diff --git a/test/unit/files/actions/model.ts b/test/unit/files/actions/model.ts index 0fd71537..13aa02f8 100644 --- a/test/unit/files/actions/model.ts +++ b/test/unit/files/actions/model.ts @@ -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'; @@ -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