Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating entities directly from odata service $metadata endpoint (csdl as xml) #20

Open
buchatsky opened this issue Sep 17, 2019 · 0 comments

Comments

@buchatsky
Copy link

I've written a tsgen-odata.js script which consumes a csdl xml directly from $metadata endpoint and then calls tsgen-core.ts for entity generation. It uses datajs library to parse metadata as you do in breeze.js

var argv = require('yargs').argv;
var tsGenCore = require('./tsgen-core');
var fs = require('fs');
var doRequest = require('request');
require('datajs');
var xmldom = require('xmldom');
global.DOMParser = xmldom.DOMParser;

var metaPath = './tools/metadata.json';

try {
    //var metaUrl = 'http://localhost:51163/odata/$metadata';
    var metaUrl = argv.i || argv.input;

    OData.defaultHttpClient = {
        request: function(request, success, error) {
            doRequest({
                url: request.requestUri,
                headers: request.headers
            }, (err, resp, body) => {
                var response;
                if (!err && resp.statusCode >= 200 && resp.statusCode <= 299) {
                    response = { requestUri: request.requestUri, statusCode: resp.statusCode, statusText: resp.statusText, headers: resp.headers, body: body };
                    success(response);
                } else {
                    error({ message: "HTTP request failed", request: request, response: response });
                }
            });
        }
    };

    OData.read({
            requestUri: metaUrl,
            // headers: { "Accept": "application/json"}
            headers: { 'Accept': 'application/*; odata.metadata=full'}
        },
        function (data) {
            // data.dataServices.schema is an array of schemas. with properties of
            // entityContainer[], association[], entityType[], and namespace.
            if (!data || !data.dataServices) {
                var error = new Error("Metadata query failed for: " + url);
                return deferred.reject(error);
            }
            var csdlMetadata = data.dataServices;
            //            
            var jsonStr = JSON.stringify(csdlMetadata);
            fs.writeFile(metaPath, jsonStr, (err) => {
                if (err) throw err;

                var config = argvToConfig();
                tsGenCore.generate(config);
            });
        },
        function (error) {
            var message = "Metadata query failed; " + (error.message || "");
            console.log('Unexpected error occurred: ' + message);
        },
        OData.metadataHandler
    );

} catch (e) {
    console.log('Unexpected error occurred: ' + e.stack);
}

function argvToConfig() {
    var config = {
        inputFileName: metaPath,//argv.i || argv.input,
        outputFolder: argv.o || argv.outputFolder,
        sourceFilesFolder: argv.s || argv.sourceFiles,
        baseClassName: argv.b || argv.baseClass,
        camelCase: !!(argv.c || argv.camelCase),
        kebabCaseFileNames: !!(argv.k || argv.kebabCaseFileNames),
        useEnumTypes: !!(argv.e || argv.useEnumTypes)
    };
    return config;
}

You may treat this as a feature request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant