This repository has been archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
180 lines (172 loc) · 5.4 KB
/
index.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
const { faker } = require("@faker-js/faker");
const properCase = require("proper-case");
// List of all types of Fakers. We specify this explicitly since there is
// no easy way to filter out these from the other objects on the faker module.
const fakerTypes = [
"address",
"animal",
"color",
"commerce",
"company",
"database",
"datatype",
"date",
"finance",
"git",
"hacker",
"helpers",
"image",
"internet",
"lorem",
"mersenne",
"music",
"name",
"phone",
"random",
"science",
"system",
"vehicle",
"word",
];
// Creates the scaffolds for Enum options that Insomnia's Template Tags expect
populateFakerOptions = function (someArray) {
return someArray.sort().map(function (key) {
return {
displayName: properCase(key),
value: key,
};
});
};
// Creates Faker Sub Types and hides them so that they only show when the
// parent Type is selected
populateFakerSubOptions = function () {
return fakerTypes.map(function (fakerType) {
let objPropertyNames = Object.getOwnPropertyNames(faker[fakerType]).filter(
function (item) {
const funArray = ["length", "name"];
const objPropertyArray = Object.getOwnPropertyNames(
faker[fakerType][item]
);
return (
item != "faker" &&
objPropertyArray.length == funArray.length &&
objPropertyArray.every((v) => funArray.indexOf(v) >= 0)
);
}
);
const fakerTypeOptions = populateFakerOptions(objPropertyNames);
return {
displayName: properCase(fakerType),
type: "enum",
defaultValue: "",
options: fakerTypeOptions,
hide: (args) => fakerType != args[0].value,
};
});
};
populateFakerLocalizationOptions = function () {
return Object.keys(faker.locales)
.sort()
.map(function (locale) {
return {
displayName: locale,
value: locale,
};
});
};
// Actual Template Tags export that Insomnia expects
module.exports.templateTags = [
{
name: "faker",
displayName: "Faker",
description: "Generate Faker data in Insomnia",
args: [
{
displayName: "Type",
type: "enum",
options: populateFakerOptions(fakerTypes),
},
]
.concat(populateFakerSubOptions())
.concat([
{
displayName:
"(Optional) Modifier [see Faker docs for args usage; construct as JSON or list surrounded by square brackets]",
type: "string",
encoding: "base64",
description:
"Allows you to pass in a string that some types allow for more fine grained control over the output of the value. See https://github.com/faker-js/faker for more info.",
},
{
displayName: "Localization",
type: "enum",
options: populateFakerLocalizationOptions(),
defaultValue: "en",
},
]),
async run(_context, type, ...args) {
let returnValue;
// Since we dynamically generate the Faker Type Sub Options, we
// don't know which argument its stored at, so lets look it up
var fakerTypeIndex = fakerTypes.indexOf(type);
// Check to see if we have selected a Sub Type Value
var subTypeValue = args[fakerTypeIndex];
// If not, be sure to select the first value from the correct Faker Type
if (subTypeValue == "") {
subTypeValue = this.args[fakerTypeIndex + 1].options[0].value;
}
// Setup faker locale for i18n support
var fakerLocale = args.slice(-1)[0];
faker.locale = fakerLocale;
// Optional Format String
var optionalModifier = args.slice(-2)[0];
if (optionalModifier != "") {
try {
// Attempt to parse arguments as JSON object or list
// console.log(
// `Attempting to parse as JSON object`,
// JSON.parse(optionalModifier)
// );
const optionalModifierObj = JSON.parse(optionalModifier);
try {
// Try calling via .apply()
// .apply() takes second arg of array type, so format it as an Array if not already
const optionalModifierArray =
optionalModifierObj.constructor === Array
? optionalModifierObj
: Array(optionalModifierObj);
returnValue = faker[type][subTypeValue].apply(
null,
optionalModifierArray
);
} catch (_err) {
// Otherwise, try calling directly with JSON parsed objects as arguments
returnValue = faker[type][subTypeValue](optionalModifierObj);
}
} catch (_err) {
try {
// Attempt to parse as list of arguments
// console.log(
// `Attempting to parse as list / array`,
// optionalModifier.split(",")
// );
returnValue = faker[type][subTypeValue].apply(
null,
optionalModifier.split(",")
);
} catch (_err) {
// If none of that works, just send optional modifier argument as a string
returnValue = faker[type][subTypeValue](optionalModifier);
}
}
} else {
// Otherwise call out to Faker module without arguments
returnValue = faker[type][subTypeValue]();
}
if (typeof returnValue !== "string") {
returnValue = JSON.stringify(returnValue);
}
return returnValue;
},
},
];