UNPKG

angular-odata

Version:

Client side OData typescript library for Angular

150 lines 7.66 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.apigen = apigen; const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const workspace_1 = require("@schematics/angular/utility/workspace"); const parse_name_1 = require("@schematics/angular/utility/parse-name"); const parser_1 = require("./metadata/parser"); const utils_1 = require("./utils"); const module_1 = require("./angular/module"); const api_config_1 = require("./angular/api-config"); const enum_1 = require("./angular/enum"); const base_1 = require("./angular/base"); const entity_1 = require("./angular/entity"); const service_1 = require("./angular/service"); const utils = { toTypescriptType: utils_1.toTypescriptType, }; function apigen(options) { return (tree, context) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(tree); if (!options.project) { options.project = workspace.projects.keys().next().value; } const project = workspace.projects.get(options.project); if (!project) { throw new schematics_1.SchematicsException(`Invalid project name: ${options.project}`); } if (options.path === undefined) { options.path = yield (0, workspace_1.createDefaultPath)(tree, options.project); } const parsedPath = (0, parse_name_1.parseName)(options.path, options.name); options.name = parsedPath.name; options.path = parsedPath.path; const modulePath = options.path + '/' + core_1.strings.dasherize(options.name); return fetch(options.metadata) .then((resp) => resp.text()) .then((data) => new parser_1.ODataMetadataParser(data).metadata()) .then((meta) => { var _a, _b, _c, _d, _e, _f; options.creation = new Date(); options.serviceRootUrl = options.metadata.substring(0, options.metadata.length - 9); options.version = meta.Version; const metadata = new base_1.Metadata(options, meta); const module = new module_1.Module(options); const config = new api_config_1.ApiConfig(options); const index = new base_1.Index(options); index.addDependency(module); index.addDependency(config); const sources = [metadata, index, module, config]; for (let s of meta.Schemas) { const namespace = s.Namespace; // Enum for (const enumType of (_a = s.EnumType) !== null && _a !== void 0 ? _a : []) { const enu = new enum_1.Enum(options, enumType); index.addDependency(enu); sources.push(enu); } // Entity for (let entityType of (_b = s.EntityType) !== null && _b !== void 0 ? _b : []) { const entity = new entity_1.Entity(options, entityType); index.addDependency(entity); sources.push(entity); } // Complex for (let complexType of (_c = s.ComplexType) !== null && _c !== void 0 ? _c : []) { const entity = new entity_1.Entity(options, complexType); index.addDependency(entity); sources.push(entity); } // Container for (let entityContainer of (_d = s.EntityContainer) !== null && _d !== void 0 ? _d : []) { const service = new service_1.Service(options, entityContainer); module.addService(service); index.addDependency(service); sources.push(service); for (let entitySet of (_e = entityContainer.EntitySet) !== null && _e !== void 0 ? _e : []) { const service = new service_1.Service(options, entitySet); module.addService(service); index.addDependency(service); sources.push(service); } for (let singleton of (_f = entityContainer.Singleton) !== null && _f !== void 0 ? _f : []) { const service = new service_1.Service(options, singleton); module.addService(service); index.addDependency(service); sources.push(service); } } } const functions = meta .functions() .reduce((callables, f) => { const callable = callables.find((c) => c.name() == f.Name); if (callable !== undefined) { callable.addOverload(f); } else { callables.push(new base_1.Callable(f)); } return callables; }, []); const actions = meta .actions() .reduce((callables, a) => { const callable = callables.find((c) => c.name() == a.Name); if (callable !== undefined) { callable.addOverload(a); } else { callables.push(new base_1.Callable(a)); } return callables; }, []); [...sources] .filter((s) => s instanceof service_1.Service) .forEach((s) => { s.addCallables(functions.filter((f) => { var _a; return f.isBound() && ((_a = f.bindingParameter()) === null || _a === void 0 ? void 0 : _a.Type) === s.entityType(); })); s.addCallables(actions.filter((f) => { var _a; return f.isBound() && ((_a = f.bindingParameter()) === null || _a === void 0 ? void 0 : _a.Type) === s.entityType(); })); }); [...sources].forEach((s) => { for (let t of s.importTypes()) { s.addDependencies(sources.filter((s) => s.fullName() === t)); } s.cleanImportedNames(); }); return (0, schematics_1.chain)(sources .map((s) => (0, schematics_1.apply)(s.template(), [ (0, schematics_1.template)(Object.assign(Object.assign(Object.assign({ name: s.name(), fileName: s.fileName(), fullName: s.fullName(), imports: s.imports(), }, s.variables()), core_1.strings), utils)), (0, schematics_1.move)((0, core_1.normalize)(`${modulePath}/${s.directory()}`)), ])) .reduce((rules, s) => [...rules, (0, schematics_1.mergeWith)(s, schematics_1.MergeStrategy.Overwrite)], [])); }); }); } //# sourceMappingURL=index.js.map