@cap-js/ord 1.4.3 → 1.4.4
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/lib/constants.js +37 -0
- package/lib/protocol-resolver.js +134 -0
- package/lib/templates.js +45 -63
- package/package.json +4 -1
package/lib/constants.js
CHANGED
|
@@ -117,6 +117,39 @@ const SEM_VERSION_REGEX =
|
|
|
117
117
|
|
|
118
118
|
const MCP_CUSTOM_TYPE = "sap:mcp-server-card:v0";
|
|
119
119
|
|
|
120
|
+
// ORD apiProtocol values
|
|
121
|
+
const ORD_API_PROTOCOL = Object.freeze({
|
|
122
|
+
ODATA_V4: "odata-v4",
|
|
123
|
+
ODATA_V2: "odata-v2",
|
|
124
|
+
REST: "rest",
|
|
125
|
+
GRAPHQL: "graphql",
|
|
126
|
+
SAP_INA: "sap-ina-api-v1",
|
|
127
|
+
SAP_DATA_SUBSCRIPTION: "sap.dp:data-subscription-api:v1",
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Mapping from CAP protocol kind to ORD apiProtocol
|
|
131
|
+
// CAP may return 'odata', 'odata-v4', 'rest', etc.
|
|
132
|
+
const CAP_TO_ORD_PROTOCOL_MAP = Object.freeze({
|
|
133
|
+
"odata": ORD_API_PROTOCOL.ODATA_V4,
|
|
134
|
+
"odata-v4": ORD_API_PROTOCOL.ODATA_V4,
|
|
135
|
+
"odata-v2": ORD_API_PROTOCOL.ODATA_V2,
|
|
136
|
+
"rest": ORD_API_PROTOCOL.REST,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Protocols that ORD supports but CAP doesn't recognize (endpoints4 returns [])
|
|
140
|
+
// These need special handling in the ORD plugin
|
|
141
|
+
const ORD_ONLY_PROTOCOLS = Object.freeze({
|
|
142
|
+
"ina": {
|
|
143
|
+
apiProtocol: ORD_API_PROTOCOL.SAP_INA,
|
|
144
|
+
hasEntryPoints: false,
|
|
145
|
+
hasResourceDefinitions: false,
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// Protocols that the ORD plugin cannot currently generate definitions for
|
|
150
|
+
// GraphQL is supported by ORD spec, but plugin can't emit graphql-sdl yet
|
|
151
|
+
const PLUGIN_UNSUPPORTED_PROTOCOLS = Object.freeze(["graphql"]);
|
|
152
|
+
|
|
120
153
|
// CF mTLS Error Reasons
|
|
121
154
|
const CF_MTLS_ERROR_REASON = Object.freeze({
|
|
122
155
|
NO_HEADERS: "NO_HEADERS",
|
|
@@ -146,6 +179,7 @@ module.exports = {
|
|
|
146
179
|
BASIC_AUTH_HEADER_KEY,
|
|
147
180
|
BUILD_DEFAULT_PATH,
|
|
148
181
|
BLOCKED_SERVICE_NAME,
|
|
182
|
+
CAP_TO_ORD_PROTOCOL_MAP,
|
|
149
183
|
CDS_ELEMENT_KIND,
|
|
150
184
|
CF_MTLS_HEADERS,
|
|
151
185
|
COMPILER_TYPES,
|
|
@@ -160,12 +194,15 @@ module.exports = {
|
|
|
160
194
|
OPEN_RESOURCE_DISCOVERY_VERSION,
|
|
161
195
|
OPENAPI_SERVERS_ANNOTATION,
|
|
162
196
|
ORD_ACCESS_STRATEGY,
|
|
197
|
+
ORD_API_PROTOCOL,
|
|
163
198
|
ORD_DOCUMENT_FILE_NAME,
|
|
164
199
|
ORD_EXTENSIONS_PREFIX,
|
|
165
200
|
ORD_ODM_ENTITY_NAME_ANNOTATION,
|
|
166
201
|
ORD_EXISTING_PRODUCT_PROPERTY,
|
|
202
|
+
ORD_ONLY_PROTOCOLS,
|
|
167
203
|
ORD_RESOURCE_TYPE,
|
|
168
204
|
ORD_SERVICE_NAME,
|
|
205
|
+
PLUGIN_UNSUPPORTED_PROTOCOLS,
|
|
169
206
|
RESOURCE_VISIBILITY,
|
|
170
207
|
ALLOWED_VISIBILITY,
|
|
171
208
|
IMPLEMENTATIONSTANDARD_VERSIONS,
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
const cds = require("@sap/cds");
|
|
2
|
+
const {
|
|
3
|
+
CAP_TO_ORD_PROTOCOL_MAP,
|
|
4
|
+
ORD_ONLY_PROTOCOLS,
|
|
5
|
+
ORD_API_PROTOCOL,
|
|
6
|
+
PLUGIN_UNSUPPORTED_PROTOCOLS,
|
|
7
|
+
} = require("./constants");
|
|
8
|
+
const Logger = require("./logger");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Gets CAP endpoints for a service using CDS endpoints4().
|
|
12
|
+
*
|
|
13
|
+
* @param {string} serviceName The service name.
|
|
14
|
+
* @param {Object} srvDefinition The service definition object.
|
|
15
|
+
* @returns {Array} Raw endpoints from CDS.
|
|
16
|
+
*/
|
|
17
|
+
function _getCapEndpoints(serviceName, srvDefinition) {
|
|
18
|
+
const srvObj = { name: serviceName, definition: srvDefinition };
|
|
19
|
+
return cds.service.protocols.endpoints4(srvObj);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Reads the explicit @protocol annotation from service definition.
|
|
24
|
+
*
|
|
25
|
+
* @param {Object} srvDefinition The service definition object.
|
|
26
|
+
* @returns {string|null} Protocol name, or null if not explicitly set.
|
|
27
|
+
*/
|
|
28
|
+
function _getExplicitProtocol(srvDefinition) {
|
|
29
|
+
const protocol = srvDefinition["@protocol"];
|
|
30
|
+
if (!protocol) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return Array.isArray(protocol) ? protocol[0] : protocol;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Resolves protocol for ORD API Resource generation.
|
|
38
|
+
*
|
|
39
|
+
* Design Principles:
|
|
40
|
+
* - explicit protocol is the "master switch" for all decisions
|
|
41
|
+
* - Rule A: Explicit protocol + empty endpoints → don't fallback to OData
|
|
42
|
+
* - Rule B: Only fallback to OData when no explicit protocol
|
|
43
|
+
* - Rule C: Never produce [null] in entryPoints
|
|
44
|
+
*
|
|
45
|
+
* @param {string} serviceName The service name.
|
|
46
|
+
* @param {Object} srvDefinition The service definition object.
|
|
47
|
+
* @param {Object} options Configuration options.
|
|
48
|
+
* @param {Function} options.isPrimaryDataProduct Strategy function to check if service is primary data product.
|
|
49
|
+
* @returns {Array} Array with single {apiProtocol, entryPoints, hasResourceDefinitions} object, or empty array.
|
|
50
|
+
*/
|
|
51
|
+
function resolveApiResourceProtocol(serviceName, srvDefinition, options = {}) {
|
|
52
|
+
const { isPrimaryDataProduct = () => false } = options;
|
|
53
|
+
|
|
54
|
+
// 1. Primary Data Product - early return
|
|
55
|
+
if (isPrimaryDataProduct(srvDefinition)) {
|
|
56
|
+
return [
|
|
57
|
+
{
|
|
58
|
+
apiProtocol: ORD_API_PROTOCOL.SAP_DATA_SUBSCRIPTION,
|
|
59
|
+
entryPoints: [],
|
|
60
|
+
hasResourceDefinitions: true,
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const explicit = _getExplicitProtocol(srvDefinition);
|
|
66
|
+
|
|
67
|
+
// 2. Handle explicit protocol
|
|
68
|
+
if (explicit) {
|
|
69
|
+
// 2a. Check if it's an ORD-only protocol (e.g., INA)
|
|
70
|
+
if (ORD_ONLY_PROTOCOLS[explicit]) {
|
|
71
|
+
const config = ORD_ONLY_PROTOCOLS[explicit];
|
|
72
|
+
const path = config.hasEntryPoints ? cds.service.protocols.path4(srvDefinition) : null;
|
|
73
|
+
return [
|
|
74
|
+
{
|
|
75
|
+
apiProtocol: config.apiProtocol,
|
|
76
|
+
entryPoints: path ? [path] : [],
|
|
77
|
+
hasResourceDefinitions: config.hasResourceDefinitions,
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 2b. Check if it's a plugin-unsupported protocol
|
|
83
|
+
if (PLUGIN_UNSUPPORTED_PROTOCOLS.includes(explicit)) {
|
|
84
|
+
Logger.warn(
|
|
85
|
+
`Protocol '${explicit}' is supported by ORD but this plugin cannot generate its resource definitions yet.`,
|
|
86
|
+
);
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 3. Try to resolve from CAP endpoints
|
|
92
|
+
const capEndpoints = _getCapEndpoints(serviceName, srvDefinition);
|
|
93
|
+
for (const endpoint of capEndpoints) {
|
|
94
|
+
if (PLUGIN_UNSUPPORTED_PROTOCOLS.includes(endpoint.kind)) {
|
|
95
|
+
Logger.warn(
|
|
96
|
+
`Protocol '${endpoint.kind}' is supported by ORD but this plugin cannot generate its resource definitions yet.`,
|
|
97
|
+
);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const apiProtocol = CAP_TO_ORD_PROTOCOL_MAP[endpoint.kind] ?? endpoint.kind;
|
|
102
|
+
if (apiProtocol) {
|
|
103
|
+
return [
|
|
104
|
+
{
|
|
105
|
+
apiProtocol,
|
|
106
|
+
entryPoints: endpoint.path ? [endpoint.path] : [],
|
|
107
|
+
hasResourceDefinitions: true,
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 4. Handle explicit protocol with no CAP endpoint (Rule A)
|
|
114
|
+
if (explicit) {
|
|
115
|
+
Logger.warn(`Unknown protocol '${explicit}' is not supported, skipping service '${serviceName}'.`);
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 5. No explicit protocol and no CAP endpoint - fallback to OData (Rule B)
|
|
120
|
+
const path = cds.service.protocols.path4(srvDefinition);
|
|
121
|
+
return [
|
|
122
|
+
{
|
|
123
|
+
apiProtocol: ORD_API_PROTOCOL.ODATA_V4,
|
|
124
|
+
entryPoints: path ? [path] : [],
|
|
125
|
+
hasResourceDefinitions: true,
|
|
126
|
+
},
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = {
|
|
131
|
+
resolveApiResourceProtocol,
|
|
132
|
+
// Exported for testing
|
|
133
|
+
_getExplicitProtocol,
|
|
134
|
+
};
|
package/lib/templates.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const cds = require("@sap/cds");
|
|
2
1
|
const { hasSAPPolicyLevel } = require("./utils");
|
|
3
2
|
const { isMCPPluginReady } = require("./mcpAdapter");
|
|
4
3
|
const defaults = require("./defaults");
|
|
@@ -21,9 +20,11 @@ const {
|
|
|
21
20
|
SHORT_DESCRIPTION_PREFIX,
|
|
22
21
|
CONTENT_MERGE_KEY,
|
|
23
22
|
CDS_ELEMENT_KIND,
|
|
23
|
+
ORD_API_PROTOCOL,
|
|
24
24
|
} = require("./constants");
|
|
25
25
|
const Logger = require("./logger");
|
|
26
26
|
const { ensureAccessStrategies } = require("./access-strategies");
|
|
27
|
+
const { resolveApiResourceProtocol } = require("./protocol-resolver");
|
|
27
28
|
|
|
28
29
|
function unflatten(flattedObject) {
|
|
29
30
|
let result = {};
|
|
@@ -44,42 +45,6 @@ function readORDExtensions(model) {
|
|
|
44
45
|
return unflatten(ordExtensions);
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
/**
|
|
48
|
-
* Reads the service definition and returns an array of entryPoint paths.
|
|
49
|
-
*
|
|
50
|
-
* @param {string} srv The service definition name.
|
|
51
|
-
* @param {Object} srvDefinition The service definition object.
|
|
52
|
-
* @returns {Array} An array containing paths and it's kind.
|
|
53
|
-
*/
|
|
54
|
-
const _generatePaths = (srv, srvDefinition) => {
|
|
55
|
-
const srvObj = { name: srv, definition: srvDefinition };
|
|
56
|
-
const protocols = cds.service.protocols;
|
|
57
|
-
|
|
58
|
-
const paths = protocols.endpoints4(srvObj);
|
|
59
|
-
|
|
60
|
-
//TODO: check graphql replication in paths object and re-visit logic
|
|
61
|
-
//removing instances of graphql protocol from paths
|
|
62
|
-
for (var index = paths.length - 1; index >= 0; index--) {
|
|
63
|
-
if (paths[index].kind === "graphql") {
|
|
64
|
-
Logger.warn("Graphql protocol is not supported.");
|
|
65
|
-
paths.splice(index, 1);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
//putting OData as default in case of non-supported protocol
|
|
70
|
-
if (paths.length === 0) {
|
|
71
|
-
if (isPrimaryDataProductService(srvDefinition)) {
|
|
72
|
-
// Data product services use REST protocol, not OData
|
|
73
|
-
paths.push({ kind: "rest", path: protocols.path4(srvDefinition) });
|
|
74
|
-
} else {
|
|
75
|
-
srvDefinition["@odata"] = true;
|
|
76
|
-
paths.push({ kind: "odata", path: protocols.path4(srvDefinition) });
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return paths;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
48
|
/**
|
|
84
49
|
* This is a template function to create item of entityTypeMappings array.
|
|
85
50
|
*
|
|
@@ -321,9 +286,17 @@ const createAPIResourceTemplate = (serviceName, serviceDefinition, appConfig, pa
|
|
|
321
286
|
const visibility = _handleVisibility(ordExtensions, serviceDefinition, appConfig.env?.defaultVisibility);
|
|
322
287
|
const packageId = _getPackageID(appConfig.ordNamespace, packageIds, ORD_RESOURCE_TYPE.api, visibility);
|
|
323
288
|
|
|
324
|
-
const
|
|
289
|
+
const protocolResults = resolveApiResourceProtocol(serviceName, serviceDefinition, {
|
|
290
|
+
isPrimaryDataProduct: isPrimaryDataProductService,
|
|
291
|
+
});
|
|
325
292
|
const apiResources = [];
|
|
326
293
|
|
|
294
|
+
// If no protocols were generated, skip this service
|
|
295
|
+
if (protocolResults.length === 0) {
|
|
296
|
+
Logger.info(`No supported protocols for service '${serviceName}', skipping API resource generation.`);
|
|
297
|
+
return apiResources;
|
|
298
|
+
}
|
|
299
|
+
|
|
327
300
|
// Handle version suffix extraction for primary data product services
|
|
328
301
|
let cleanServiceName,
|
|
329
302
|
version,
|
|
@@ -352,16 +325,36 @@ const createAPIResourceTemplate = (serviceName, serviceDefinition, appConfig, pa
|
|
|
352
325
|
|
|
353
326
|
const ordId = `${appConfig.ordNamespace}:apiResource:${cleanServiceName}:${version}`;
|
|
354
327
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
if (
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
328
|
+
protocolResults.forEach((protocolResult) => {
|
|
329
|
+
const { apiProtocol, entryPoints, hasResourceDefinitions } = protocolResult;
|
|
330
|
+
|
|
331
|
+
// Build resource definitions based on protocol
|
|
332
|
+
let resourceDefinitions = [];
|
|
333
|
+
if (hasResourceDefinitions) {
|
|
334
|
+
if (apiProtocol === ORD_API_PROTOCOL.SAP_DATA_SUBSCRIPTION) {
|
|
335
|
+
// Data product services use CSN
|
|
336
|
+
resourceDefinitions = [
|
|
337
|
+
_getResourceDefinition(
|
|
338
|
+
"sap-csn-interop-effective-v1",
|
|
339
|
+
"json",
|
|
340
|
+
ordId,
|
|
341
|
+
serviceName,
|
|
342
|
+
"csn.json",
|
|
343
|
+
accessStrategies,
|
|
344
|
+
),
|
|
345
|
+
];
|
|
346
|
+
} else if (apiProtocol === ORD_API_PROTOCOL.REST) {
|
|
347
|
+
// REST only has OpenAPI, no EDMX
|
|
348
|
+
resourceDefinitions = [
|
|
349
|
+
_getResourceDefinition("openapi-v3", "json", ordId, serviceName, "oas3.json", accessStrategies),
|
|
350
|
+
];
|
|
351
|
+
} else {
|
|
352
|
+
// OData and others have both OpenAPI and EDMX
|
|
353
|
+
resourceDefinitions = [
|
|
354
|
+
_getResourceDefinition("openapi-v3", "json", ordId, serviceName, "oas3.json", accessStrategies),
|
|
355
|
+
_getResourceDefinition("edmx", "xml", ordId, serviceName, "edmx", accessStrategies),
|
|
356
|
+
];
|
|
357
|
+
}
|
|
365
358
|
}
|
|
366
359
|
|
|
367
360
|
const entityTypeMappings = _getEntityTypeMappings(serviceDefinition);
|
|
@@ -378,9 +371,9 @@ const createAPIResourceTemplate = (serviceName, serviceDefinition, appConfig, pa
|
|
|
378
371
|
partOfPackage: packageId,
|
|
379
372
|
partOfGroups: [_getGroupID(serviceDefinition, defaults.groupTypeId, appConfig)],
|
|
380
373
|
releaseStatus: "active",
|
|
381
|
-
apiProtocol
|
|
382
|
-
resourceDefinitions
|
|
383
|
-
entryPoints
|
|
374
|
+
apiProtocol,
|
|
375
|
+
resourceDefinitions,
|
|
376
|
+
entryPoints,
|
|
384
377
|
extensible: {
|
|
385
378
|
supported: "no",
|
|
386
379
|
},
|
|
@@ -389,24 +382,13 @@ const createAPIResourceTemplate = (serviceName, serviceDefinition, appConfig, pa
|
|
|
389
382
|
...ordExtensions,
|
|
390
383
|
};
|
|
391
384
|
|
|
385
|
+
// Special handling for data product services
|
|
392
386
|
if (isPrimaryDataProductService(serviceDefinition)) {
|
|
393
|
-
obj.apiProtocol = "sap.dp:data-subscription-api:v1";
|
|
394
387
|
obj.direction = "outbound";
|
|
395
|
-
obj.entryPoints = [];
|
|
396
388
|
if (extracted) {
|
|
397
389
|
// Overwrite partOfGroups
|
|
398
390
|
obj.partOfGroups = [`${defaults.groupTypeId}:${appConfig.ordNamespace}:${cleanServiceName}`];
|
|
399
391
|
}
|
|
400
|
-
obj.resourceDefinitions = [
|
|
401
|
-
_getResourceDefinition(
|
|
402
|
-
"sap-csn-interop-effective-v1",
|
|
403
|
-
"json",
|
|
404
|
-
ordId,
|
|
405
|
-
serviceName,
|
|
406
|
-
"csn.json",
|
|
407
|
-
accessStrategies,
|
|
408
|
-
),
|
|
409
|
-
];
|
|
410
392
|
}
|
|
411
393
|
|
|
412
394
|
if (obj.visibility !== RESOURCE_VISIBILITY.private) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cap-js/ord",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "CAP Plugin for generating ORD document.",
|
|
5
5
|
"repository": "cap-js/ord",
|
|
6
6
|
"author": "SAP SE (https://www.sap.com)",
|
|
@@ -49,6 +49,9 @@
|
|
|
49
49
|
"engines": {
|
|
50
50
|
"node": ">=18 <23"
|
|
51
51
|
},
|
|
52
|
+
"overrides": {
|
|
53
|
+
"@sap/cds-compiler": "6.6.2"
|
|
54
|
+
},
|
|
52
55
|
"cds": {
|
|
53
56
|
"requires": {
|
|
54
57
|
"SAP ORD Service": {
|