This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathendpoints.js
188 lines (155 loc) · 4.9 KB
/
endpoints.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const fetch = require('isomorphic-fetch');
const R = require('ramda');
const fs = require('fs');
const path = require('path');
const Progress = require('progress');
const pascalCase = require('to-pascal-case');
const S = require('string');
const Handlebars = require('handlebars');
const ENDOINTS_URL ='http://procore-api-documentation-staging.s3-website-us-east-1.amazonaws.com';
const notEmpty = R.compose(
R.not,
R.isEmpty
);
const endpointTemplatePath = path.join(
__dirname,
'endpoint.template'
);
const requiredField = R.ifElse(
R.identity,
() => '',
() => '?'
);
const typescriptType = type => {
switch(type) {
case 'integer':
return 'number';
default:
return type;
}
}
Handlebars.registerHelper(
'interface',
R.reduce(
(memo, { name, required, type }) =>
memo.concat(`${name}${requiredField(required)}: ${typescriptType(type)};\n`),
''
)
);
Handlebars.registerHelper(
'args',
R.ifElse(
R.isEmpty,
R.identity,
R.compose(
R.join(', '),
R.pluck('name')
)
)
);
const isProductionGroup = R.compose(
R.equals('production'),
R.prop('highest_support_level')
);
const removeNonProductionGroups = (groups) => new Promise(
(resolve, reject) => {
resolve(R.filter(isProductionGroup, groups))
}
);
const isProductionEndpoint = R.compose(
R.equals('production'),
R.prop('support_level')
);
const removeNonProductionEndpoints = (endpoints) => new Promise(
(resolve, reject) => {
resolve(R.filter(isProductionEndpoint, endpoints))
}
);
const REGEX_INVALID_SYMBOLS = /\.|\(|\)|'|"/g;
function fromNameToCamelized(nameString) {
return S(nameString.toLowerCase().replace(REGEX_INVALID_SYMBOLS, '')).camelize().s;
}
function fromNameToPascal(nameString) {
return pascalCase(nameString);
}
function fromNameToStub(nameString) {
return nameString
.toLowerCase()
.trim()
.replace(/ |\//g, '-')
.replace(REGEX_INVALID_SYMBOLS, '');
}
const endpointCommand = (to, { destination, index }) => {
return fetch(`${ENDOINTS_URL}/master/groups.json`)
.then((res) => {
return res.json().catch((err) => {
err.endpoint = endpointName;
err.reason = 'parsing JSON';
throw err;
});
})
.then(removeNonProductionGroups)
.then((groups) => {
const bar = new Progress(':bar :percent', { total: groups.length });
const libPath = path.join(process.cwd(), to);
const libIndexPath = path.join(libPath, index);
const endpointsFolderPath = path.join(libPath, destination);
if (!fs.existsSync(endpointsFolderPath)) {
fs.mkdirSync(endpointsFolderPath);
}
return Promise.all(
groups.map(({ name }) => {
const endpointNameStub = fromNameToStub(name);
const endpointUrl = `${ENDOINTS_URL}/master/${endpointNameStub}.json`;
return fetch(endpointUrl)
.then((res) => res.json())
.then(removeNonProductionEndpoints)
.then(([{ path: endpointPath, path_params, query_params }]) => {
fs.readFile(endpointTemplatePath, 'utf8', (err, data) => {
const camelizedEndpointName = fromNameToCamelized(name);
const pascalCaseEndpointName = fromNameToPascal(name);
const params = R.when(
R.compose(
R.not,
R.contains('id'),
R.pluck('name')
),
R.concat([{ name: "id", type: "integer" }])
)(path_params);
const config = {
params,
name: camelizedEndpointName,
interfaceName: pascalCaseEndpointName,
definitions: params,
path: endpointPath
};
if (err) throw err;
template = Handlebars.compile(data)
file = template(config);
return fs.writeFile(path.join(endpointsFolderPath, `${endpointNameStub}.ts`), file, () => {
if (err) throw err;
fs.appendFileSync(libIndexPath, `export { default as ${camelizedEndpointName} } from './${destination}/${endpointNameStub}'\n`)
bar.tick();
});
});
})
.catch((err) => {
err.endpoint = name;
err.reason = 'Fetch';
console.error(`Failed fetch for endpoint name "${name}" with URL ${endpointUrl}`);
throw err;
});
})
)
.catch((err) => {
if (err.endpoint && err.reason) {
console.error(`Failed to fetch and parse JSON for endpoint: ${err.endpoint} failed at step: ${err.reason}`);
}
throw err;
})
})
}
endpointCommand.fromNameToCamelized = fromNameToCamelized;
endpointCommand.fromNameToPascal = fromNameToPascal;
endpointCommand.fromNameToStub = fromNameToStub;
module.exports= endpointCommand;