UNPKG

@omnigraph/odata

Version:
70 lines (69 loc) 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTypeNameFromRef = getTypeNameFromRef; const pascal_case_1 = require("pascal-case"); function getNamespaceFromTypeRef({ typeRef, namespaces }) { let namespace = ''; namespaces?.forEach(el => { if (typeRef.startsWith(el) && el.length > namespace.length && // It can be deeper namespace !typeRef.replace(el + '.', '').includes('.') // Typename cannot have `.` ) { namespace = el; } }); return namespace; } const SCALARS = new Map([ ['Edm.Binary', 'String'], ['Edm.Stream', 'String'], ['Edm.String', 'String'], ['Edm.Int16', 'Int'], ['Edm.Byte', 'Byte'], ['Edm.Int32', 'Int'], ['Edm.Int64', 'BigInt'], ['Edm.Double', 'Float'], ['Edm.Boolean', 'Boolean'], ['Edm.Guid', 'GUID'], ['Edm.DateTimeOffset', 'DateTime'], ['Edm.Date', 'Date'], ['Edm.TimeOfDay', 'String'], ['Edm.Single', 'Float'], ['Edm.Duration', 'ISO8601Duration'], ['Edm.Decimal', 'Float'], ['Edm.SByte', 'Byte'], ['Edm.GeographyPoint', 'String'], ]); function getTypeNameFromRef({ typeRef, isInput, isRequired, aliasNamespaceMap, multipleSchemas, namespaces, isEnumType, }) { const typeRefArr = typeRef.split('Collection('); const arrayDepth = typeRefArr.length; let actualTypeRef = typeRefArr.join('').split(')').join(''); const typeNamespace = getNamespaceFromTypeRef({ typeRef: actualTypeRef, namespaces, }); if (aliasNamespaceMap.has(typeNamespace)) { const alias = aliasNamespaceMap.get(typeNamespace); actualTypeRef = actualTypeRef.replace(typeNamespace, alias); } const actualTypeRefArr = actualTypeRef.split('.'); const typeName = multipleSchemas ? (0, pascal_case_1.pascalCase)(actualTypeRefArr.join('_')) : actualTypeRefArr[actualTypeRefArr.length - 1]; let realTypeName = typeName; if (SCALARS.has(actualTypeRef)) { realTypeName = SCALARS.get(actualTypeRef); } else if (isEnumType(typeName)) { realTypeName = typeName; } else if (isInput) { realTypeName += 'Input'; } const fakeEmptyArr = new Array(arrayDepth); realTypeName = fakeEmptyArr.join('[') + realTypeName + fakeEmptyArr.join(']'); if (isRequired) { realTypeName += '!'; } return realTypeName; }