Skip to content

Commit

Permalink
Merge pull request #25 from cap-js/default-namespace
Browse files Browse the repository at this point in the history
change default namespace
  • Loading branch information
sidakphull authored Jun 5, 2024
2 parents 61e2602 + 3795be2 commit a2cd748
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 1.0.2

### Changed

- Changed default namespace from `sap.app` to `customer.<package-name>`

## Version 1.0.1 - 14.05.2024

### Changed
Expand Down
1 change: 0 additions & 1 deletion lib/compile/config.json

This file was deleted.

19 changes: 17 additions & 2 deletions lib/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const getMessageTraits = require('./components/messageTraits');
const { definitionsToMessages, nestedAnnotation } = require('./components/messages');
const getChannels = require('./channels');
const cds = require('@sap/cds/lib');
const { join } = require('path');
const messages = require("./message");
const defaultConfig = require("./config.json")
const presetMapping = {
'event_spec_version': 'x-sap-event-spec-version',
'event_source': 'x-sap-event-source',
Expand All @@ -19,9 +19,20 @@ module.exports = function processor(csn, options = {}) {
envConf = cds.env.export.asyncapi;
}
if (!cds.env?.export?.asyncapi?.application_namespace) {
envConf["application_namespace"] = defaultConfig.application_namespace;
const packageJson = require(join(cds.root,'package.json'));
const appName = packageJson.name.replace(/\s/g, "-");
envConf["application_namespace"] = `customer.${appName}`
console.info(messages.APPLICATION_NAMESPACE);
}

if (cds.env.ord?.namespace) {
const ordNamespace = _getNamespace(cds.env.ord.namespace);
const asyncapiNamespace = _getNamespace(envConf.application_namespace);
if (ordNamespace !== asyncapiNamespace) {
console.warn(messages.NAMESPACE_MISMATCH)
}
}

_serviceErrorHandling(csn, options);

const applicationNamespace = envConf.application_namespace;
Expand Down Expand Up @@ -249,3 +260,7 @@ function* _iterate(services, csn, applicationNamespace, presets) {
yield [generatedAsyncAPI, { file: key }];
}
}

function _getNamespace(fullNamespace) {
return fullNamespace.split('.').slice(0, 2).join('.');
}
1 change: 1 addition & 0 deletions lib/compile/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const errorMessages = {
APPLICATION_NAMESPACE: 'Default application namespace is used.',
NAMESPACE_MISMATCH: 'ORD and AsyncAPI namespaces should be same.',
TITLE_VERSION_MERGED: 'Preset for Title and Version info needs to be added when merged flag is used.',
NO_EVENTS: 'No events found in the service.',
TITLE_VERSION_ANNOTATIONS: 'Title and Version info annotations needs to be added to the service(s).',
Expand Down
2 changes: 1 addition & 1 deletion test/lib/compile/asyncapiMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ describe('asyncapi export: presets and annotations', () => {
cds.env.export.asyncapi = {};
const csn = cds.compile.to.csn(inputCDS);
const generatedAsyncAPI = toAsyncAPI(csn);
expect(generatedAsyncAPI).toHaveProperty('x-sap-application-namespace','sap.app')
expect(generatedAsyncAPI).toHaveProperty('x-sap-application-namespace','customer.@cap-js/asyncapi')
});
});

0 comments on commit a2cd748

Please sign in to comment.