forked from svenheden/csharp-models-to-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
142 lines (111 loc) · 3.45 KB
/
converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const path = require('path');
const flatten = arr => arr.reduce((a, b) => a.concat(b), []);
const collectionRegex = /^(?:I?List|IReadOnlyList|IEnumerable|ICollection|HashSet)<([\w\d\[\]]+)>\?*$/;
const dictionaryRegex = /^I?Dictionary<([\w\d]+),\s?([\w\d]+)>\?*$/;
const defaultTypeTranslations = {
int: 'number',
double: 'number',
float: 'number',
Int32: 'number',
Int64: 'number',
short: 'number',
long: 'number',
decimal: 'number',
bool: 'boolean',
DateTime: 'string',
DateTimeOffset: 'string',
Guid: 'string',
dynamic: 'any',
object: 'any'
};
const createConverter = config => {
const typeTranslations = Object.assign(
{},
defaultTypeTranslations,
config.customTypeTranslations
);
const convert = json => {
const content = json.map(file => {
const filename = path.relative(process.cwd(), file.FileName);
return flatten([
...file.Models.map(model => convertModel(model)),
...file.Enums.map(enum_ => convertEnum(enum_, filename))
]).join('\n');
});
const filteredContent = content.filter(x => x.length > 0);
return filteredContent.join('\n');
};
const convertModel = model => {
const rows = [];
const members = [...model.Fields, ...model.Properties];
let baseClasses = '';
if (!config.ignoreBaseTypes) {
baseClasses = model.BaseClasses;
} else {
if (model.BaseClasses) {
let resultBaseClasses = [];
for (const value of model.BaseClasses.split(',')) {
const baseClass = value.trim();
if (!config.ignoreBaseTypes.includes(baseClass)) {
resultBaseClasses.push(baseClass);
}
}
baseClasses = resultBaseClasses.join(', ');
}
}
const resultBaseClasses = baseClasses
? ` extends ${model.BaseClasses}`
: '';
if (members.length > 0) {
rows.push(`export interface ${model.ModelName}${resultBaseClasses} {`);
members.forEach(member => {
rows.push(convertProperty(member));
});
rows.push(`}\n`);
}
return rows;
};
const convertEnum = enum_ => {
const rows = [];
rows.push(`export enum ${enum_.Identifier} {`);
enum_.Items.forEach(item => {
rows.push(`\t${item.Name} = ${item.Value},`);
});
rows.push(`}\n`);
return rows;
};
const convertProperty = property => {
const optional = property.Type.endsWith('?');
const collection = property.Type.match(collectionRegex);
const dictionary = property.Type.match(dictionaryRegex);
const identifier = convertIdentifier(
optional
? `${property.Identifier.split(' ')[0]}?`
: property.Identifier.split(' ')[0]
);
let type;
if (collection) {
type = `${convertType(collection[1])}[]`;
} else if (dictionary) {
type = `{ [index: ${convertType(dictionary[1])}]: ${convertType(
dictionary[2]
)} }`;
} else {
type = convertType(
optional
? property.Type.slice(0, property.Type.length - 1)
: property.Type
);
}
const deprecationMark = property.IsObsolete ? `\t/** @deprecated */\n` : ``
return `${deprecationMark}\t${identifier}: ${type}`;
};
const convertIdentifier = identifier =>
config.camelCase
? identifier[0].toLowerCase() + identifier.substring(1)
: identifier;
const convertType = type =>
type in typeTranslations ? typeTranslations[type] : type;
return convert;
};
module.exports = createConverter;