farrow-api
Advanced tools
Comparing version
@@ -15,2 +15,5 @@ "use strict"; | ||
if ((0, formatter_1.isNamedFormatType)(input) && input.name) { | ||
if (input.namespace) { | ||
return "".concat(input.namespace, ".").concat(input.name); | ||
} | ||
return input.name; | ||
@@ -132,4 +135,5 @@ } | ||
} | ||
if (exportSet.has(typeName)) { | ||
throw new Error("Duplicate Object/Struct type name: ".concat(typeName)); | ||
var exportKey = formatType.namespace ? "".concat(formatType.namespace, ".").concat(typeName) : typeName; | ||
if (exportSet.has(exportKey)) { | ||
throw new Error("Duplicate Object/Struct type name: ".concat(typeName, " with namespace: ").concat(formatType.namespace)); | ||
} | ||
@@ -143,2 +147,7 @@ exportSet.add(typeName); | ||
var expression = formatType.itemTypes.map(function (itemType) { return (0, exports.getFieldType)(itemType.typeId, formatResult.types); }); | ||
var exportKey = formatType.namespace ? "".concat(formatType.namespace, ".").concat(typeName) : typeName; | ||
if (exportSet.has(exportKey)) { | ||
throw new Error("Duplicate Union type name: ".concat(typeName, " with namespace: ").concat(formatType.namespace)); | ||
} | ||
exportSet.add(typeName); | ||
var source_2 = "\n/**\n * @label ".concat(typeName, "\n*/\nexport type ").concat(typeName, " =\n").concat(applyIndentForEachLine(expression.map(function (item) { return "| ".concat(item); }).join('\n'), 2), "\n "); | ||
@@ -150,2 +159,7 @@ return source_2.trim(); | ||
var expression = formatType.itemTypes.map(function (itemType) { return (0, exports.getFieldType)(itemType.typeId, formatResult.types); }); | ||
var exportKey = formatType.namespace ? "".concat(formatType.namespace, ".").concat(typeName) : typeName; | ||
if (exportSet.has(exportKey)) { | ||
throw new Error("Duplicate Intersect type name: ".concat(typeName, " with namespace: ").concat(formatType.namespace)); | ||
} | ||
exportSet.add(typeName); | ||
var source_3 = "\n/**\n * @label ".concat(typeName, "\n*/\nexport type ").concat(typeName, " =\n").concat(applyIndentForEachLine(expression.map(function (item) { return "& ".concat(item); }).join('\n'), 2), "\n"); | ||
@@ -157,2 +171,7 @@ return source_3.trim(); | ||
var expression = formatType.itemTypes.map(function (itemType) { return (0, exports.getFieldType)(itemType.typeId, formatResult.types); }); | ||
var exportKey = formatType.namespace ? "".concat(formatType.namespace, ".").concat(typeName) : typeName; | ||
if (exportSet.has(exportKey)) { | ||
throw new Error("Duplicate Tuple type name: ".concat(typeName, " with namespace: ").concat(formatType.namespace)); | ||
} | ||
exportSet.add(typeName); | ||
return "\n/**\n * @label ".concat(typeName, "\n*/\nexport type ").concat(typeName, " = [\n").concat(applyIndentForEachLine(expression.join(',\n'), 2), "\n]\n").trim(); | ||
@@ -173,3 +192,49 @@ } | ||
}; | ||
var typeDeclarations = tslib_1.__spreadArray([exports.JSON_TYPE_DECLARATION], tslib_1.__read(handleTypeDeclarations(formatResult.types)), false); | ||
var collectNamespace = function (formatTypes) { | ||
var _a; | ||
var namespaceMap = new Map(); | ||
for (var key in formatTypes) { | ||
var formatType = formatTypes[key]; | ||
if ((0, formatter_1.isNamedFormatType)(formatType)) { | ||
var namespace = (_a = formatType.namespace) !== null && _a !== void 0 ? _a : ''; | ||
var types = namespaceMap.get(namespace); | ||
if (!types) { | ||
types = {}; | ||
namespaceMap.set(namespace, types); | ||
} | ||
types[key] = formatType; | ||
} | ||
} | ||
return namespaceMap; | ||
}; | ||
var handleNamespaceDeclaration = function (namespace, formatTypes) { | ||
var formattedTypes = handleTypeDeclarations(formatTypes); | ||
if (!namespace) { | ||
return formattedTypes.join('\n\n'); | ||
} | ||
return "\nexport namespace ".concat(namespace, " {\n").concat(applyIndentForEachLine(formattedTypes.join('\n\n'), 2), "\n}\n").trim(); | ||
}; | ||
var handleNamespaceDeclarations = function (namespaceMap) { | ||
var e_1, _a; | ||
var result = []; | ||
try { | ||
for (var namespaceMap_1 = tslib_1.__values(namespaceMap), namespaceMap_1_1 = namespaceMap_1.next(); !namespaceMap_1_1.done; namespaceMap_1_1 = namespaceMap_1.next()) { | ||
var _b = tslib_1.__read(namespaceMap_1_1.value, 2), namespace = _b[0], types = _b[1]; | ||
var formattedNamespace = handleNamespaceDeclaration(namespace, types); | ||
if (formattedNamespace) { | ||
result.push(formattedNamespace); | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (namespaceMap_1_1 && !namespaceMap_1_1.done && (_a = namespaceMap_1.return)) _a.call(namespaceMap_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return result; | ||
}; | ||
var namespaceMap = collectNamespace(formatResult.types); | ||
var typeDeclarations = tslib_1.__spreadArray([exports.JSON_TYPE_DECLARATION], tslib_1.__read(handleNamespaceDeclarations(namespaceMap)), false); | ||
var variableDeclarations = []; | ||
@@ -176,0 +241,0 @@ if ((options === null || options === void 0 ? void 0 : options.apiClient) !== false) { |
@@ -125,15 +125,19 @@ "use strict"; | ||
var controvertObjectType = function (input) { | ||
var _a; | ||
var fields = controvertFieldsType(input.fields); | ||
return /** @class */ (function (_super) { | ||
tslib_1.__extends(Obj, _super); | ||
function Obj() { | ||
var _this = _super.call(this) || this; | ||
for (var name in fields) { | ||
// @ts-ignore | ||
_this[name] = fields[name]; | ||
return _a = /** @class */ (function (_super) { | ||
tslib_1.__extends(Obj, _super); | ||
function Obj() { | ||
var _this = _super.call(this) || this; | ||
for (var name in fields) { | ||
// @ts-ignore | ||
_this[name] = fields[name]; | ||
} | ||
return _this; | ||
} | ||
return _this; | ||
} | ||
return Obj; | ||
}(farrow_schema_1.ObjectType)); | ||
return Obj; | ||
}(farrow_schema_1.ObjectType)), | ||
_a.displayName = input.name, | ||
_a.namespace = input.namespace, | ||
_a; | ||
}; | ||
@@ -159,3 +163,6 @@ var controvertFieldsType = function (input) { | ||
}); | ||
return farrow_schema_1.Union.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(items), false)); | ||
var UnionType = farrow_schema_1.Union.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(items), false)); | ||
UnionType.displayName = input.name; | ||
UnionType.namespace = input.namespace; | ||
return UnionType; | ||
}; | ||
@@ -167,7 +174,13 @@ var controvertIntersectType = function (input) { | ||
}); | ||
return farrow_schema_1.Intersect.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(items), false)); | ||
var IntersectType = farrow_schema_1.Intersect.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(items), false)); | ||
IntersectType.displayName = input.name; | ||
IntersectType.namespace = input.namespace; | ||
return IntersectType; | ||
}; | ||
var controvertStructType = function (input) { | ||
var fields = controvertFieldsType(input.fields); | ||
return (0, farrow_schema_1.Struct)(fields); | ||
var StructType = (0, farrow_schema_1.Struct)(fields); | ||
StructType.displayName = input.name; | ||
StructType.namespace = input.namespace; | ||
return StructType; | ||
}; | ||
@@ -187,3 +200,6 @@ var controvertRecordType = function (input) { | ||
}); | ||
return farrow_schema_1.Tuple.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(items), false)); | ||
var TupleType = farrow_schema_1.Tuple.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(items), false)); | ||
TupleType.displayName = input.name; | ||
TupleType.namespace = input.namespace; | ||
return TupleType; | ||
}; | ||
@@ -190,0 +206,0 @@ var controvertLiteralType = function (input) { |
{ | ||
"name": "farrow-api", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "A type-friendly BFF framework", | ||
@@ -30,3 +30,3 @@ "main": "dist/index.js", | ||
"farrow-pipeline": "^2.3.0", | ||
"farrow-schema": "^2.3.0", | ||
"farrow-schema": "^2.3.1", | ||
"tslib": "^2.5.0" | ||
@@ -33,0 +33,0 @@ }, |
@@ -13,2 +13,5 @@ import { FormatFields, FormatType, FormatTypes, isNamedFormatType } from 'farrow-schema/formatter' | ||
if (isNamedFormatType(input) && input.name) { | ||
if (input.namespace) { | ||
return `${input.namespace}.${input.name}` | ||
} | ||
return input.name | ||
@@ -192,4 +195,6 @@ } | ||
if (exportSet.has(typeName)) { | ||
throw new Error(`Duplicate Object/Struct type name: ${typeName}`) | ||
const exportKey = formatType.namespace ? `${formatType.namespace}.${typeName}` : typeName | ||
if (exportSet.has(exportKey)) { | ||
throw new Error(`Duplicate Object/Struct type name: ${typeName} with namespace: ${formatType.namespace}`) | ||
} | ||
@@ -213,2 +218,11 @@ | ||
const expression = formatType.itemTypes.map((itemType) => getFieldType(itemType.typeId, formatResult.types)) | ||
const exportKey = formatType.namespace ? `${formatType.namespace}.${typeName}` : typeName | ||
if (exportSet.has(exportKey)) { | ||
throw new Error(`Duplicate Union type name: ${typeName} with namespace: ${formatType.namespace}`) | ||
} | ||
exportSet.add(typeName) | ||
const source = ` | ||
@@ -228,2 +242,11 @@ /** | ||
const expression = formatType.itemTypes.map((itemType) => getFieldType(itemType.typeId, formatResult.types)) | ||
const exportKey = formatType.namespace ? `${formatType.namespace}.${typeName}` : typeName | ||
if (exportSet.has(exportKey)) { | ||
throw new Error(`Duplicate Intersect type name: ${typeName} with namespace: ${formatType.namespace}`) | ||
} | ||
exportSet.add(typeName) | ||
const source = ` | ||
@@ -244,2 +267,10 @@ /** | ||
const exportKey = formatType.namespace ? `${formatType.namespace}.${typeName}` : typeName | ||
if (exportSet.has(exportKey)) { | ||
throw new Error(`Duplicate Tuple type name: ${typeName} with namespace: ${formatType.namespace}`) | ||
} | ||
exportSet.add(typeName) | ||
return ` | ||
@@ -270,4 +301,57 @@ /** | ||
const typeDeclarations = [JSON_TYPE_DECLARATION, ...handleTypeDeclarations(formatResult.types)] | ||
type NamespaceMap = Map<string, FormatTypes> | ||
const collectNamespace = (formatTypes: FormatTypes) => { | ||
const namespaceMap: NamespaceMap = new Map() | ||
for (const key in formatTypes) { | ||
const formatType = formatTypes[key] | ||
if (isNamedFormatType(formatType)) { | ||
const namespace = formatType.namespace ?? '' | ||
let types = namespaceMap.get(namespace) | ||
if (!types) { | ||
types = {} | ||
namespaceMap.set(namespace, types) | ||
} | ||
types[key] = formatType | ||
} | ||
} | ||
return namespaceMap | ||
} | ||
const handleNamespaceDeclaration = (namespace: string, formatTypes: FormatTypes) => { | ||
const formattedTypes = handleTypeDeclarations(formatTypes) | ||
if (!namespace) { | ||
return formattedTypes.join('\n\n') | ||
} | ||
return ` | ||
export namespace ${namespace} { | ||
${applyIndentForEachLine(formattedTypes.join('\n\n'), 2)} | ||
} | ||
`.trim() | ||
} | ||
const handleNamespaceDeclarations = (namespaceMap: NamespaceMap) => { | ||
const result = [] as string[] | ||
for (const [namespace, types] of namespaceMap) { | ||
const formattedNamespace = handleNamespaceDeclaration(namespace, types) | ||
if (formattedNamespace) { | ||
result.push(formattedNamespace) | ||
} | ||
} | ||
return result | ||
} | ||
const namespaceMap = collectNamespace(formatResult.types) | ||
const typeDeclarations = [JSON_TYPE_DECLARATION, ...handleNamespaceDeclarations(namespaceMap)] | ||
const variableDeclarations: string[] = [] | ||
@@ -274,0 +358,0 @@ |
@@ -183,2 +183,4 @@ import { | ||
return class Obj extends ObjectType { | ||
static displayName = input.name | ||
static namespace = input.namespace | ||
constructor() { | ||
@@ -215,3 +217,8 @@ super() | ||
const items: SchemaCtorInput[] = input.itemTypes.map(({ typeId }) => findType(typeId)) | ||
return Union(...items) | ||
const UnionType = Union(...items) | ||
UnionType.displayName = input.name | ||
UnionType.namespace = input.namespace | ||
return UnionType | ||
} | ||
@@ -221,3 +228,8 @@ | ||
const items: SchemaCtorInput[] = input.itemTypes.map(({ typeId }) => findType(typeId)) | ||
return Intersect(...items) | ||
const IntersectType = Intersect(...items) | ||
IntersectType.displayName = input.name | ||
IntersectType.namespace = input.namespace | ||
return IntersectType | ||
} | ||
@@ -227,3 +239,8 @@ | ||
const fields = controvertFieldsType(input.fields) | ||
return Struct(fields) | ||
const StructType = Struct(fields) | ||
StructType.displayName = input.name | ||
StructType.namespace = input.namespace | ||
return StructType | ||
} | ||
@@ -243,3 +260,8 @@ | ||
const items: SchemaCtorInput[] = input.itemTypes.map(({ typeId }) => findType(typeId)) | ||
return Tuple(...items) | ||
const TupleType = Tuple(...items) | ||
TupleType.displayName = input.name | ||
TupleType.namespace = input.namespace | ||
return TupleType | ||
} | ||
@@ -246,0 +268,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
82086
13.71%1540
11.03%Updated