@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
112 lines (105 loc) • 4.3 kB
JavaScript
let cds = require('../cds');
let { path } = cds.utils;
let common = require('./common');
let odata = require('./odata');
let openapi = require('./openapi');
let asyncapi = require('./asyncapi');
let messages = require('./message').getMessages();
module.exports = Object.assign(cds_import, {
from: {
edmx: cds_import_from_edmx,
openapi: cds_import_from_openapi,
asyncapi: cds_import_from_asyncapi
}
});
/**
* This is the import API function to generate CSN from XML/EDMX files.
* @param {string} filepath
* - a single valid filepath
* @param {{} | {keepNamespace:_keepNamespace, includeNamespaces:_includeNamespaces...}} options
* - an options object for importing OData files specifying the keepNamespace, includeNamespaces options
* - Default values for OData V2: {keepNamespace: false, includeNamespaces:"edmx,m,sap"}
* - Default values for OData V4: {keepNamespace: false, includeNamespaces:"edmx"}
* - optional parameters
* - other import options not supported
* @returns {object} CSN
*/
async function cds_import(filepath, options = { keepNamespace: false }) {
// identifies the file and calls the respective api
let fileType = filepath && await common.identifyFile(filepath);
let csn;
switch (fileType) {
case "edmx":
csn = await cds_import_from_edmx(filepath, options);
break;
case "openapi":
csn = await cds_import_from_openapi(filepath, options);
break;
case "asyncapi":
csn = await cds_import_from_asyncapi(filepath, options);
break;
default:
throw new Error(messages.INVALID_INPUT_FILE);
}
return csn;
}
/**
* This is the import API function to generate CSN from XML/EDMX files.
* @param {string} filepath
* - a single valid filepath
* @param {{} | {keepNamespace:_keepNamespace, includeNamespaces:_includeNamespaces...}} options
* - an options object specifying the keepNamespace, includeNamespaces options on how CSN is to be generated
* - Default values for OData V2: {keepNamespace: false, includeNamespaces:"edmx,m,sap"}
* - Default values for OData V4: {keepNamespace: false, includeNamespaces:"edmx"}
* - optional parameters
* - other import options not supported
* @returns {object} CSN
*/
async function cds_import_from_edmx(filepath, options = { keepNamespace: false }) {
// validates and reads the input file
filepath = await common.isValidInputFile(path.resolve(process.cwd(), filepath));
const src = await common.getContentFromFile(filepath);
// takes the api specific options and reads it from env
let odataOptions = { include_namespaces: options.includeNamespaces, keep_namespace: options.keepNamespace};
if (cds.env.import) {
common.readEnvVariables(odataOptions, false);
}
// generates csn
odataOptions.odataVersion = await common.getVersion(src);
odataOptions.include_all_namespaces = false;
odataOptions.namespaces = [];
if (odataOptions.include_namespaces) odata.getNamespaces(odataOptions);
let csn = await odata.edmx2csn(src, !odataOptions.keep_namespace && filepath, odataOptions);
// reading the file version
options.odataVersion = odataOptions.odataVersion;
// return the csn
return csn;
}
/**
* This is the import API function to generate CSN from openapi files.
* @param {string} filepath
* - a single valid filepath
* @returns {object} CSN
*/
async function cds_import_from_openapi(filepath, options = {}) {
// validates the input file
filepath = await common.isValidInputFile(path.resolve(process.cwd(), filepath));
const src = await common.getContentFromFile(filepath);
let csn = openapi.openAPI2csn(src);
options.inputFileKind = 'rest';
return csn;
}
/**
* This is the import API function to generate CSN from asyncapi files.
* @param {string} filepath
* - a single valid filepath
* @returns {object} CSN
*/
async function cds_import_from_asyncapi(filepath, options = {}) {
// validates the input file
filepath = await common.isValidInputFile(path.resolve(process.cwd(), filepath));
const src = await common.getContentFromFile(filepath);
let csn = asyncapi.asyncapi2csn(src);
options.inputFileKind = 'odata';
return csn;
}