forked from BenoitAverty/realworld-remix.run
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-fake-data.mjs
executable file
·72 lines (59 loc) · 1.81 KB
/
generate-fake-data.mjs
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
import faker from "faker";
import { fetch } from "@remix-run/node";
const API_URL = "https://stw74y7a42.execute-api.eu-west-1.amazonaws.com/dev/api";
// const API_URL = "http://localhost:3000/api";
const tags = ["javascript", "react", "remix", "java", "spring", "aws", "devfest", "gdg", "zenika"];
async function createUser() {
const user = {
username: faker.name.firstName(),
email: faker.internet.email(),
password: "realworld",
};
const settings = {
bio: faker.lorem.sentence(),
image: faker.internet.avatar(),
};
const responseRegister = await fetch(API_URL + "/users", {
method: "POST",
body: JSON.stringify({ user }),
headers: {
"Content-Type": "application/json",
},
});
const token = (await responseRegister.json()).user.token;
console.log("Created user " + user.username + ", he has token " + token)
await fetch(API_URL + "/user", {
method: "PUT",
body: JSON.stringify({ user: settings }),
headers: {
"Content-Type": "application/json",
"Authorization": "Token " + token,
},
});
return token;
}
function createArticle(token) {
return fetch(API_URL + "/articles", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Token " + token,
},
body: JSON.stringify({
article: {
title: faker.lorem.sentence(),
description: faker.lorem.sentences(2),
body: faker.lorem.paragraphs(),
tagList: tags.sort(() => Math.random() - 0.5).slice(0, Math.floor(Math.random()*tags.length)),
},
}),
});
}
async function createArticlesSet() {
const token = await createUser();
const articlesCount = Math.random()*10 + 5;
for(let i = 0; i < articlesCount ; ++i) {
await createArticle(token)
}
}
createArticlesSet().then(() => console.log("OK !"))