-
Notifications
You must be signed in to change notification settings - Fork 13
/
.hygen.js
54 lines (51 loc) · 2.35 KB
/
.hygen.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
module.exports = {
helpers: {
/**Check if a field or option is collective (contains fields or options).*/
isCollective: (entity) =>
entity.type === "collection" ||
entity.type === "fixedCollection" ||
entity.type === "multiOptions" ||
entity.type === "options",
/**Format a string as a class name, uppercase for each initial and no whitespace.*/
classify: (name) => name.replace(/\s/g, ""),
/**Format a string as a lowercase single word, or a lowercase first word and uppercase initial + lowercase rest for following words.*/
camelify: (input) => {
const isSingleWord = input.split(" ").length === 1;
const uppercaseInitialLowercaseRest = (input) =>
input[0].toUpperCase() + input.slice(1).toLowerCase();
if (isSingleWord) return input.toLowerCase();
// multi-word
let result = [];
input.split(" ").forEach((word, index) => {
index === 0
? result.push(word.toLowerCase())
: result.push(uppercaseInitialLowercaseRest(word));
});
return result.join("");
},
/**Check if operations array has a Get All operation.*/
hasGetAll: (operations) =>
operations.some((operation) => operation.name === "Get All"),
/**Check if a particular operation is a GET request.*/
isRetrieval: (operation) => operation.requestMethod === "GET",
/**Check if a particular operation has any associated additional fields.*/
hasAdditionalFields: (operation) =>
operation.fields.filter((field) => field.name === "Additional Fields"),
/**Create the credential string for based on auth type.*/
getCredentialsString: (name, auth) =>
name + (auth === "OAuth2" ? "OAuth2" : "") + "Api",
/**Check if the endpoint has an extractable variable.*/
hasEndpointVariable: (endpoint) => endpoint.split("").includes(":"),
/**Extract the variable from the endpoint.*/
getVariableFromEndpoint: (endpoint) => endpoint.match(/:(.*)(\/)?/)[1],
/**Reformat endpoint from colon version to string-interpolated version.*/
fixEndpoint: (endpoint) => {
const routeParameter = endpoint.match(/:(.*)(\/)?/)[1];
return endpoint.replace(
":" + routeParameter,
"${" + routeParameter + "}"
);
},
hasNumericalLimits: (field) => field.numericalLimits,
},
};