get-fake-api 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +634 -2
- package/data/auth.js +36 -0
- package/data/orders.js +137 -0
- package/data/posts.js +214 -0
- package/data/products.js +260 -0
- package/data/users.js +377 -0
- package/index.js +74 -0
- package/package.json +31 -4
- package/test.js +113 -0
package/data/users.js
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
const baseUsers = [
|
|
2
|
+
{
|
|
3
|
+
id: 1,
|
|
4
|
+
firstName: "Sarah",
|
|
5
|
+
lastName: "Connor",
|
|
6
|
+
fullName: "Sarah Connor",
|
|
7
|
+
username: "sconnor",
|
|
8
|
+
email: "sarah.connor@example.com",
|
|
9
|
+
gender: "female",
|
|
10
|
+
age: 29,
|
|
11
|
+
phone: "+1-202-555-0143",
|
|
12
|
+
avatar: "https://images.unsplash.com/photo-1494790108377-be9c29b29330?w=200&auto=format&fit=crop&q=80",
|
|
13
|
+
role: "developer",
|
|
14
|
+
company: {
|
|
15
|
+
name: "Cyberdyne Systems",
|
|
16
|
+
title: "Senior Full Stack Engineer",
|
|
17
|
+
department: "Engineering"
|
|
18
|
+
},
|
|
19
|
+
address: {
|
|
20
|
+
street: "742 Evergreen Terrace",
|
|
21
|
+
city: "Springfield",
|
|
22
|
+
state: "Oregon",
|
|
23
|
+
zipCode: "97477",
|
|
24
|
+
country: "United States"
|
|
25
|
+
},
|
|
26
|
+
website: "https://sarahconnor.dev"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
id: 2,
|
|
30
|
+
firstName: "John",
|
|
31
|
+
lastName: "Doe",
|
|
32
|
+
fullName: "John Doe",
|
|
33
|
+
username: "johndoe",
|
|
34
|
+
email: "john.doe@example.com",
|
|
35
|
+
gender: "male",
|
|
36
|
+
age: 34,
|
|
37
|
+
phone: "+1-202-555-0189",
|
|
38
|
+
avatar: "https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=200&auto=format&fit=crop&q=80",
|
|
39
|
+
role: "manager",
|
|
40
|
+
company: {
|
|
41
|
+
name: "Acme Corporation",
|
|
42
|
+
title: "Product Manager",
|
|
43
|
+
department: "Product"
|
|
44
|
+
},
|
|
45
|
+
address: {
|
|
46
|
+
street: "123 Market Street",
|
|
47
|
+
city: "San Francisco",
|
|
48
|
+
state: "California",
|
|
49
|
+
zipCode: "94105",
|
|
50
|
+
country: "United States"
|
|
51
|
+
},
|
|
52
|
+
website: "https://johndoe.me"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 3,
|
|
56
|
+
firstName: "Elena",
|
|
57
|
+
lastName: "Rostova",
|
|
58
|
+
fullName: "Elena Rostova",
|
|
59
|
+
username: "erostova",
|
|
60
|
+
email: "elena.rostova@example.com",
|
|
61
|
+
gender: "female",
|
|
62
|
+
age: 26,
|
|
63
|
+
phone: "+44-20-7946-0912",
|
|
64
|
+
avatar: "https://images.unsplash.com/photo-1573496359142-b8d87734a5a2?w=200&auto=format&fit=crop&q=80",
|
|
65
|
+
role: "designer",
|
|
66
|
+
company: {
|
|
67
|
+
name: "PixelCraft Studio",
|
|
68
|
+
title: "Lead UI/UX Designer",
|
|
69
|
+
department: "Design"
|
|
70
|
+
},
|
|
71
|
+
address: {
|
|
72
|
+
street: "45 Oxford Circus",
|
|
73
|
+
city: "London",
|
|
74
|
+
state: "Greater London",
|
|
75
|
+
zipCode: "W1D 1BS",
|
|
76
|
+
country: "United Kingdom"
|
|
77
|
+
},
|
|
78
|
+
website: "https://elenarostova.design"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 4,
|
|
82
|
+
firstName: "Liam",
|
|
83
|
+
lastName: "Chen",
|
|
84
|
+
fullName: "Liam Chen",
|
|
85
|
+
username: "liamc",
|
|
86
|
+
email: "liam.chen@example.com",
|
|
87
|
+
gender: "male",
|
|
88
|
+
age: 31,
|
|
89
|
+
phone: "+1-415-555-0199",
|
|
90
|
+
avatar: "https://images.unsplash.com/photo-1570295999919-56ceb5ecca61?w=200&auto=format&fit=crop&q=80",
|
|
91
|
+
role: "developer",
|
|
92
|
+
company: {
|
|
93
|
+
name: "Nexis Cloud",
|
|
94
|
+
title: "DevOps Architect",
|
|
95
|
+
department: "Infrastructure"
|
|
96
|
+
},
|
|
97
|
+
address: {
|
|
98
|
+
street: "88 King St E",
|
|
99
|
+
city: "Toronto",
|
|
100
|
+
state: "Ontario",
|
|
101
|
+
zipCode: "M5C 1G5",
|
|
102
|
+
country: "Canada"
|
|
103
|
+
},
|
|
104
|
+
website: "https://liamchen.io"
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
id: 5,
|
|
108
|
+
firstName: "Amina",
|
|
109
|
+
lastName: "Diallo",
|
|
110
|
+
fullName: "Amina Diallo",
|
|
111
|
+
username: "aminad",
|
|
112
|
+
email: "amina.diallo@example.com",
|
|
113
|
+
gender: "female",
|
|
114
|
+
age: 28,
|
|
115
|
+
phone: "+33-1-4268-5500",
|
|
116
|
+
avatar: "https://images.unsplash.com/photo-1580489944761-15a19d654956?w=200&auto=format&fit=crop&q=80",
|
|
117
|
+
role: "admin",
|
|
118
|
+
company: {
|
|
119
|
+
name: "Global Data Labs",
|
|
120
|
+
title: "Data Operations Lead",
|
|
121
|
+
department: "Operations"
|
|
122
|
+
},
|
|
123
|
+
address: {
|
|
124
|
+
street: "12 Rue de Rivoli",
|
|
125
|
+
city: "Paris",
|
|
126
|
+
state: "Île-de-France",
|
|
127
|
+
zipCode: "75001",
|
|
128
|
+
country: "France"
|
|
129
|
+
},
|
|
130
|
+
website: "https://aminadiallo.com"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
id: 6,
|
|
134
|
+
firstName: "Marcus",
|
|
135
|
+
lastName: "Vance",
|
|
136
|
+
fullName: "Marcus Vance",
|
|
137
|
+
username: "mvance",
|
|
138
|
+
email: "marcus.vance@example.com",
|
|
139
|
+
gender: "male",
|
|
140
|
+
age: 42,
|
|
141
|
+
phone: "+1-512-555-0165",
|
|
142
|
+
avatar: "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=200&auto=format&fit=crop&q=80",
|
|
143
|
+
role: "manager",
|
|
144
|
+
company: {
|
|
145
|
+
name: "Apex Ventures",
|
|
146
|
+
title: "VP of Product",
|
|
147
|
+
department: "Executive"
|
|
148
|
+
},
|
|
149
|
+
address: {
|
|
150
|
+
street: "500 Congress Ave",
|
|
151
|
+
city: "Austin",
|
|
152
|
+
state: "Texas",
|
|
153
|
+
zipCode: "78701",
|
|
154
|
+
country: "United States"
|
|
155
|
+
},
|
|
156
|
+
website: "https://marcusvance.biz"
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
id: 7,
|
|
160
|
+
firstName: "Chloe",
|
|
161
|
+
lastName: "Dubois",
|
|
162
|
+
fullName: "Chloe Dubois",
|
|
163
|
+
username: "cdubois",
|
|
164
|
+
email: "chloe.dubois@example.com",
|
|
165
|
+
gender: "female",
|
|
166
|
+
age: 24,
|
|
167
|
+
phone: "+1-604-555-0128",
|
|
168
|
+
avatar: "https://images.unsplash.com/photo-1534528741775-53994a69daeb?w=200&auto=format&fit=crop&q=80",
|
|
169
|
+
role: "editor",
|
|
170
|
+
company: {
|
|
171
|
+
name: "Chronicle Media",
|
|
172
|
+
title: "Content Strategist",
|
|
173
|
+
department: "Marketing"
|
|
174
|
+
},
|
|
175
|
+
address: {
|
|
176
|
+
street: "320 Granville St",
|
|
177
|
+
city: "Vancouver",
|
|
178
|
+
state: "British Columbia",
|
|
179
|
+
zipCode: "V6C 1S9",
|
|
180
|
+
country: "Canada"
|
|
181
|
+
},
|
|
182
|
+
website: "https://chloedubois.me"
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
id: 8,
|
|
186
|
+
firstName: "Arjun",
|
|
187
|
+
lastName: "Patel",
|
|
188
|
+
fullName: "Arjun Patel",
|
|
189
|
+
username: "arjunp",
|
|
190
|
+
email: "arjun.patel@example.com",
|
|
191
|
+
gender: "male",
|
|
192
|
+
age: 30,
|
|
193
|
+
phone: "+91-98200-12345",
|
|
194
|
+
avatar: "https://images.unsplash.com/photo-1500648767791-00dcc994a43e?w=200&auto=format&fit=crop&q=80",
|
|
195
|
+
role: "developer",
|
|
196
|
+
company: {
|
|
197
|
+
name: "InfiniTech Solutions",
|
|
198
|
+
title: "Cloud Solutions Architect",
|
|
199
|
+
department: "Engineering"
|
|
200
|
+
},
|
|
201
|
+
address: {
|
|
202
|
+
street: "14 Bandra Kurla Complex",
|
|
203
|
+
city: "Mumbai",
|
|
204
|
+
state: "Maharashtra",
|
|
205
|
+
zipCode: "400051",
|
|
206
|
+
country: "India"
|
|
207
|
+
},
|
|
208
|
+
website: "https://arjunpatel.tech"
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
id: 9,
|
|
212
|
+
firstName: "Sofia",
|
|
213
|
+
lastName: "Martinez",
|
|
214
|
+
fullName: "Sofia Martinez",
|
|
215
|
+
username: "smartinez",
|
|
216
|
+
email: "sofia.martinez@example.com",
|
|
217
|
+
gender: "female",
|
|
218
|
+
age: 33,
|
|
219
|
+
phone: "+34-91-123-4567",
|
|
220
|
+
avatar: "https://images.unsplash.com/photo-1544005313-94ddf0286df2?w=200&auto=format&fit=crop&q=80",
|
|
221
|
+
role: "user",
|
|
222
|
+
company: {
|
|
223
|
+
name: "Solaria Energy",
|
|
224
|
+
title: "Environmental Analyst",
|
|
225
|
+
department: "Sustainability"
|
|
226
|
+
},
|
|
227
|
+
address: {
|
|
228
|
+
street: "Paseo de la Castellana 80",
|
|
229
|
+
city: "Madrid",
|
|
230
|
+
state: "Madrid",
|
|
231
|
+
zipCode: "28046",
|
|
232
|
+
country: "Spain"
|
|
233
|
+
},
|
|
234
|
+
website: "https://sofiamartinez.es"
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
id: 10,
|
|
238
|
+
firstName: "David",
|
|
239
|
+
lastName: "Kim",
|
|
240
|
+
fullName: "David Kim",
|
|
241
|
+
username: "dkim",
|
|
242
|
+
email: "david.kim@example.com",
|
|
243
|
+
gender: "male",
|
|
244
|
+
age: 27,
|
|
245
|
+
phone: "+82-2-555-0134",
|
|
246
|
+
avatar: "https://images.unsplash.com/photo-1506794778202-cad84cf45f1d?w=200&auto=format&fit=crop&q=80",
|
|
247
|
+
role: "developer",
|
|
248
|
+
company: {
|
|
249
|
+
name: "Quantum Logic",
|
|
250
|
+
title: "Mobile App Developer",
|
|
251
|
+
department: "Mobile"
|
|
252
|
+
},
|
|
253
|
+
address: {
|
|
254
|
+
street: "123 Teheran-ro, Gangnam-gu",
|
|
255
|
+
city: "Seoul",
|
|
256
|
+
state: "Seoul",
|
|
257
|
+
zipCode: "06134",
|
|
258
|
+
country: "South Korea"
|
|
259
|
+
},
|
|
260
|
+
website: "https://davidkim.dev"
|
|
261
|
+
}
|
|
262
|
+
];
|
|
263
|
+
|
|
264
|
+
const firstNamesM = ["James", "Alexander", "Ethan", "Benjamin", "Lucas", "Noah", "Oliver", "William", "Daniel", "Henry"];
|
|
265
|
+
const firstNamesF = ["Emma", "Olivia", "Ava", "Isabella", "Sophia", "Mia", "Harper", "Evelyn", "Abigail", "Emily"];
|
|
266
|
+
const lastNames = ["Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis", "Rodriguez", "Martinez", "Hernandez", "Lopez", "Gonzalez", "Wilson", "Anderson"];
|
|
267
|
+
const roles = ["developer", "designer", "manager", "admin", "editor", "user"];
|
|
268
|
+
const companies = [
|
|
269
|
+
{ name: "Acme Corp", dept: "Product" },
|
|
270
|
+
{ name: "TechNova", dept: "Engineering" },
|
|
271
|
+
{ name: "Starlight Labs", dept: "Research" },
|
|
272
|
+
{ name: "CloudNine", dept: "Infrastructure" },
|
|
273
|
+
{ name: "Horizon Health", dept: "Healthcare" }
|
|
274
|
+
];
|
|
275
|
+
const cities = [
|
|
276
|
+
{ city: "New York", state: "NY", country: "United States", zip: "10001" },
|
|
277
|
+
{ city: "London", state: "Greater London", country: "United Kingdom", zip: "EC1A 1BB" },
|
|
278
|
+
{ city: "Berlin", state: "Berlin", country: "Germany", zip: "10115" },
|
|
279
|
+
{ city: "Tokyo", state: "Tokyo", country: "Japan", zip: "100-0001" },
|
|
280
|
+
{ city: "Sydney", state: "NSW", country: "Australia", zip: "2000" }
|
|
281
|
+
];
|
|
282
|
+
|
|
283
|
+
function generateUser(id) {
|
|
284
|
+
const isFemale = id % 2 !== 0;
|
|
285
|
+
const firstName = isFemale
|
|
286
|
+
? firstNamesF[(id - 1) % firstNamesF.length]
|
|
287
|
+
: firstNamesM[(id - 1) % firstNamesM.length];
|
|
288
|
+
const lastName = lastNames[(id * 3) % lastNames.length];
|
|
289
|
+
const username = `${firstName.toLowerCase()}${lastName.toLowerCase()}${id}`;
|
|
290
|
+
const gender = isFemale ? "female" : "male";
|
|
291
|
+
const role = roles[id % roles.length];
|
|
292
|
+
const comp = companies[id % companies.length];
|
|
293
|
+
const loc = cities[id % cities.length];
|
|
294
|
+
const age = 22 + (id * 7) % 38;
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
id,
|
|
298
|
+
firstName,
|
|
299
|
+
lastName,
|
|
300
|
+
fullName: `${firstName} ${lastName}`,
|
|
301
|
+
username,
|
|
302
|
+
email: `${username}@example.com`,
|
|
303
|
+
gender,
|
|
304
|
+
age,
|
|
305
|
+
phone: `+1-555-${String(1000 + (id * 123) % 9000)}`,
|
|
306
|
+
avatar: `https://images.unsplash.com/photo-${1500000000000 + id}?w=200&auto=format&fit=crop&q=80`,
|
|
307
|
+
role,
|
|
308
|
+
company: {
|
|
309
|
+
name: comp.name,
|
|
310
|
+
title: `${role.charAt(0).toUpperCase() + role.slice(1)} Specialist`,
|
|
311
|
+
department: comp.dept
|
|
312
|
+
},
|
|
313
|
+
address: {
|
|
314
|
+
street: `${100 + id * 5} Main Street`,
|
|
315
|
+
city: loc.city,
|
|
316
|
+
state: loc.state,
|
|
317
|
+
zipCode: loc.zip,
|
|
318
|
+
country: loc.country
|
|
319
|
+
},
|
|
320
|
+
website: `https://${username}.dev`
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function getUsers(optionsOrCount) {
|
|
325
|
+
let count = 10;
|
|
326
|
+
let filters = {};
|
|
327
|
+
|
|
328
|
+
if (typeof optionsOrCount === "number") {
|
|
329
|
+
count = optionsOrCount <= 0 ? 0 : optionsOrCount;
|
|
330
|
+
} else if (typeof optionsOrCount === "object" && optionsOrCount !== null) {
|
|
331
|
+
if (typeof optionsOrCount.count === "number") {
|
|
332
|
+
count = optionsOrCount.count <= 0 ? 0 : optionsOrCount.count;
|
|
333
|
+
} else if (typeof optionsOrCount.limit === "number") {
|
|
334
|
+
count = optionsOrCount.limit <= 0 ? 0 : optionsOrCount.limit;
|
|
335
|
+
}
|
|
336
|
+
filters = optionsOrCount;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const result = [];
|
|
340
|
+
const targetCount = count > 0 ? count : 0;
|
|
341
|
+
const poolSize = Math.max(targetCount, baseUsers.length);
|
|
342
|
+
|
|
343
|
+
for (let i = 0; i < poolSize; i++) {
|
|
344
|
+
const user = i < baseUsers.length ? baseUsers[i] : generateUser(i + 1);
|
|
345
|
+
|
|
346
|
+
if (filters.gender && user.gender !== filters.gender.toLowerCase()) continue;
|
|
347
|
+
if (filters.role && user.role !== filters.role.toLowerCase()) continue;
|
|
348
|
+
if (filters.company && !user.company.name.toLowerCase().includes(filters.company.toLowerCase())) continue;
|
|
349
|
+
if (filters.q) {
|
|
350
|
+
const q = filters.q.toLowerCase();
|
|
351
|
+
const match = user.fullName.toLowerCase().includes(q) ||
|
|
352
|
+
user.email.toLowerCase().includes(q) ||
|
|
353
|
+
user.username.toLowerCase().includes(q);
|
|
354
|
+
if (!match) continue;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
result.push(user);
|
|
358
|
+
if (result.length >= targetCount) break;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return result;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function getUserById(id) {
|
|
365
|
+
const numericId = Number(id);
|
|
366
|
+
if (!numericId || numericId <= 0) return null;
|
|
367
|
+
if (numericId <= baseUsers.length) {
|
|
368
|
+
return baseUsers.find(u => u.id === numericId) || null;
|
|
369
|
+
}
|
|
370
|
+
return generateUser(numericId);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
module.exports = {
|
|
374
|
+
getUsers,
|
|
375
|
+
getUserById,
|
|
376
|
+
baseUsers
|
|
377
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
const { getUsers, getUserById, baseUsers } = require("./data/users");
|
|
2
|
+
const { getProducts, getProductById, baseProducts } = require("./data/products");
|
|
3
|
+
const { getPosts, getPostById, getComments, getCommentsByPostId, basePosts, baseComments } = require("./data/posts");
|
|
4
|
+
const { getOrders, getOrderById, baseOrders } = require("./data/orders");
|
|
5
|
+
const { getAuthToken, getAuthSession } = require("./data/auth");
|
|
6
|
+
|
|
7
|
+
const fakeApi = {
|
|
8
|
+
async getUsers(options = {}) {
|
|
9
|
+
const delay = typeof options === "object" && options.delay ? options.delay : 200;
|
|
10
|
+
return new Promise((resolve) => setTimeout(() => resolve(getUsers(options)), delay));
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
async getUserById(id, delay = 150) {
|
|
14
|
+
return new Promise((resolve) => setTimeout(() => resolve(getUserById(id)), delay));
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
async getProducts(options = {}) {
|
|
18
|
+
const delay = typeof options === "object" && options.delay ? options.delay : 200;
|
|
19
|
+
return new Promise((resolve) => setTimeout(() => resolve(getProducts(options)), delay));
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
async getProductById(id, delay = 150) {
|
|
23
|
+
return new Promise((resolve) => setTimeout(() => resolve(getProductById(id)), delay));
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
async getPosts(options = {}) {
|
|
27
|
+
const delay = typeof options === "object" && options.delay ? options.delay : 200;
|
|
28
|
+
return new Promise((resolve) => setTimeout(() => resolve(getPosts(options)), delay));
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
async getComments(options = {}) {
|
|
32
|
+
const delay = typeof options === "object" && options.delay ? options.delay : 200;
|
|
33
|
+
return new Promise((resolve) => setTimeout(() => resolve(getComments(options)), delay));
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
async getOrders(options = {}) {
|
|
37
|
+
const delay = typeof options === "object" && options.delay ? options.delay : 200;
|
|
38
|
+
return new Promise((resolve) => setTimeout(() => resolve(getOrders(options)), delay));
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
async login(credentials = {}, delay = 300) {
|
|
42
|
+
return new Promise((resolve) => setTimeout(() => resolve(getAuthSession(baseUsers[0])), delay));
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const api = {
|
|
47
|
+
getUsers,
|
|
48
|
+
getUserById,
|
|
49
|
+
getProducts,
|
|
50
|
+
getProductById,
|
|
51
|
+
getPosts,
|
|
52
|
+
getPostById,
|
|
53
|
+
getComments,
|
|
54
|
+
getCommentsByPostId,
|
|
55
|
+
getOrders,
|
|
56
|
+
getOrderById,
|
|
57
|
+
getAuthToken,
|
|
58
|
+
getAuthSession,
|
|
59
|
+
fakeApi,
|
|
60
|
+
data: {
|
|
61
|
+
users: baseUsers,
|
|
62
|
+
products: baseProducts,
|
|
63
|
+
posts: basePosts,
|
|
64
|
+
comments: baseComments,
|
|
65
|
+
orders: baseOrders
|
|
66
|
+
},
|
|
67
|
+
getusers: getUsers,
|
|
68
|
+
getproducts: getProducts,
|
|
69
|
+
getposts: getPosts,
|
|
70
|
+
getcomments: getComments,
|
|
71
|
+
getorders: getOrders
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
module.exports = api;
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "get-fake-api",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Zero-dependency, offline mock data generator for developers to test, prototype, and build UI applications.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"mock",
|
|
11
|
+
"fake-api",
|
|
12
|
+
"fake-data",
|
|
13
|
+
"dummy-data",
|
|
14
|
+
"mock-data",
|
|
15
|
+
"testing",
|
|
16
|
+
"prototype",
|
|
17
|
+
"offline-api",
|
|
18
|
+
"users",
|
|
19
|
+
"products",
|
|
20
|
+
"posts",
|
|
21
|
+
"orders"
|
|
22
|
+
],
|
|
23
|
+
"author": "yashraj",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/yash-raj-01/get_fake_api.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/yash-raj-01/get_fake_api/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/yash-raj-01/get_fake_api#readme"
|
|
33
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const {
|
|
2
|
+
getUsers,
|
|
3
|
+
getUserById,
|
|
4
|
+
getProducts,
|
|
5
|
+
getProductById,
|
|
6
|
+
getPosts,
|
|
7
|
+
getPostById,
|
|
8
|
+
getComments,
|
|
9
|
+
getCommentsByPostId,
|
|
10
|
+
getOrders,
|
|
11
|
+
getOrderById,
|
|
12
|
+
getAuthToken,
|
|
13
|
+
getAuthSession,
|
|
14
|
+
fakeApi,
|
|
15
|
+
getusers,
|
|
16
|
+
getproducts
|
|
17
|
+
} = require("./index");
|
|
18
|
+
|
|
19
|
+
console.log("========================================");
|
|
20
|
+
console.log("🧪 Running get-fake-api Test Suite");
|
|
21
|
+
console.log("========================================\n");
|
|
22
|
+
|
|
23
|
+
let passed = 0;
|
|
24
|
+
let failed = 0;
|
|
25
|
+
|
|
26
|
+
function assert(condition, message) {
|
|
27
|
+
if (condition) {
|
|
28
|
+
console.log(` ✅ PASS: ${message}`);
|
|
29
|
+
passed++;
|
|
30
|
+
} else {
|
|
31
|
+
console.error(` ❌ FAIL: ${message}`);
|
|
32
|
+
failed++;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
console.log("▶ Testing Users Module:");
|
|
37
|
+
const usersDefault = getUsers();
|
|
38
|
+
assert(usersDefault.length === 10, "getUsers() returns default 10 users");
|
|
39
|
+
assert(typeof usersDefault[0].email === "string" && usersDefault[0].email.includes("@"), "User has valid email");
|
|
40
|
+
assert(typeof usersDefault[0].company.name === "string", "User has nested company");
|
|
41
|
+
|
|
42
|
+
const users5 = getUsers(5);
|
|
43
|
+
assert(users5.length === 5, "getUsers(5) returns exactly 5 users");
|
|
44
|
+
|
|
45
|
+
const femaleUsers = getUsers({ gender: "female", count: 3 });
|
|
46
|
+
assert(femaleUsers.length === 3 && femaleUsers.every(u => u.gender === "female"), "Filtered female users successfully");
|
|
47
|
+
|
|
48
|
+
const userById = getUserById(2);
|
|
49
|
+
assert(userById && userById.id === 2 && userById.fullName === "John Doe", "getUserById(2) finds correct user");
|
|
50
|
+
|
|
51
|
+
const userSearch = getUsers({ q: "Sarah" });
|
|
52
|
+
assert(userSearch.length > 0 && userSearch[0].firstName === "Sarah", "Search query q='Sarah' finds matching user");
|
|
53
|
+
|
|
54
|
+
console.log("\n▶ Testing Products Module:");
|
|
55
|
+
const productsDefault = getProducts();
|
|
56
|
+
assert(productsDefault.length === 10, "getProducts() returns default 10 products");
|
|
57
|
+
assert(typeof productsDefault[0].price === "number", "Product price is numeric");
|
|
58
|
+
|
|
59
|
+
const electronics = getProducts({ category: "electronics" });
|
|
60
|
+
assert(electronics.length > 0 && electronics.every(p => p.category === "electronics"), "Filter by category works");
|
|
61
|
+
|
|
62
|
+
const productById = getProductById(1);
|
|
63
|
+
assert(productById && productById.id === 1, "getProductById(1) returns product");
|
|
64
|
+
|
|
65
|
+
const sortedProducts = getProducts({ sortField: "price", sortOrder: "desc", count: 5 });
|
|
66
|
+
assert(sortedProducts[0].price >= sortedProducts[1].price, "Sorting products by price descending works");
|
|
67
|
+
|
|
68
|
+
console.log("\n▶ Testing Posts & Comments Module:");
|
|
69
|
+
const posts = getPosts(3);
|
|
70
|
+
assert(posts.length === 3, "getPosts(3) returns 3 posts");
|
|
71
|
+
assert(Array.isArray(posts[0].tags), "Post contains tags array");
|
|
72
|
+
|
|
73
|
+
const postById = getPostById(1);
|
|
74
|
+
assert(postById && postById.id === 1, "getPostById(1) returns post");
|
|
75
|
+
|
|
76
|
+
const comments = getComments(4);
|
|
77
|
+
assert(comments.length === 4, "getComments(4) returns 4 comments");
|
|
78
|
+
|
|
79
|
+
const postComments = getCommentsByPostId(1);
|
|
80
|
+
assert(postComments.length > 0 && postComments.every(c => c.postId === 1), "getCommentsByPostId(1) returns filtered comments");
|
|
81
|
+
|
|
82
|
+
console.log("\n▶ Testing Orders Module:");
|
|
83
|
+
const orders = getOrders(2);
|
|
84
|
+
assert(orders.length === 2, "getOrders(2) returns 2 orders");
|
|
85
|
+
assert(Array.isArray(orders[0].items) && orders[0].items.length > 0, "Order has items");
|
|
86
|
+
|
|
87
|
+
const orderById = getOrderById(1);
|
|
88
|
+
assert(orderById && orderById.orderNumber === "ORD-2026-9041", "getOrderById(1) returns order with orderNumber");
|
|
89
|
+
|
|
90
|
+
console.log("\n▶ Testing Auth Module:");
|
|
91
|
+
const token = getAuthToken();
|
|
92
|
+
assert(typeof token === "string" && token.split(".").length === 3, "getAuthToken() returns valid JWT format");
|
|
93
|
+
|
|
94
|
+
const session = getAuthSession();
|
|
95
|
+
assert(session.accessToken && session.user && session.tokenType === "Bearer", "getAuthSession() returns full auth session");
|
|
96
|
+
|
|
97
|
+
console.log("\n▶ Testing Backward Compatibility Aliases:");
|
|
98
|
+
assert(typeof getusers === "function", "Lowercase alias getusers is present");
|
|
99
|
+
assert(typeof getproducts === "function", "Lowercase alias getproducts is present");
|
|
100
|
+
|
|
101
|
+
console.log("\n▶ Testing Async fakeApi Client:");
|
|
102
|
+
fakeApi.getUsers({ count: 2, delay: 50 }).then((asyncUsers) => {
|
|
103
|
+
assert(asyncUsers.length === 2, "fakeApi.getUsers({ count: 2 }) returns via Promise");
|
|
104
|
+
|
|
105
|
+
console.log("\n========================================");
|
|
106
|
+
if (failed === 0) {
|
|
107
|
+
console.log(`🎉 ALL TESTS PASSED (${passed}/${passed})`);
|
|
108
|
+
} else {
|
|
109
|
+
console.error(`💥 SOME TESTS FAILED (${failed} failed, ${passed} passed)`);
|
|
110
|
+
process.exitCode = 1;
|
|
111
|
+
}
|
|
112
|
+
console.log("========================================\n");
|
|
113
|
+
});
|