This repository was archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (119 loc) · 3.64 KB
/
index.js
File metadata and controls
134 lines (119 loc) · 3.64 KB
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
/* eslint no-console: off */
const fetch = require('node-fetch');
let hcBackend;
function toJson(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response.json();
}
function handleFetchErrors(error, defaultMessage) {
let message = defaultMessage;
if (error.code === 'ECONNREFUSED') {
message = 'Is the human connection server running?';
}
throw Error(`${message}\nError: ${error.message}`);
}
async function findBySlug(models, slug) {
try {
const url = new URL(models, hcBackend);
url.searchParams.append('slug', slug);
const response = await fetch(url, {
headers: { 'Content-Type': 'application/json' },
});
const json = await toJson(response);
return json.data[0];
} catch (error) {
handleFetchErrors(error, `Cannot find a model of type ${models} by slug ${slug}`);
return null;
}
}
async function resolveSlugs(slugs) {
const result = {};
const { organization, categories } = slugs;
if (organization) {
const organizationModel = await findBySlug('organizations', organization);
if (!organizationModel) throw (Error(`Cannot find organization "${organization}"\n${Error('Not Found')}`));
// eslint-disable-next-line no-underscore-dangle
result.organizationId = organizationModel._id;
}
if (categories) {
result.categoryIds = [];
categories.forEach(async (category) => {
const categoryModel = await findBySlug('categories', category);
if (!categoryModel) throw (Error(`Cannot find category "${category}"\n${Error('Not Found')}`));
// eslint-disable-next-line no-underscore-dangle
result.categoryIds.push(categoryModel._id);
});
}
return result;
}
class User {
constructor(params) {
this.email = params.email;
this.password = params.password;
}
async login() {
const formData = {
email: this.email,
password: this.password,
strategy: 'local',
};
try {
const url = new URL('/authentication', hcBackend);
const response = await fetch(url, {
method: 'post',
body: JSON.stringify(formData),
headers: { 'Content-Type': 'application/json' },
});
const json = await toJson(response);
this.accessToken = json.accessToken;
return true;
} catch (error) {
handleFetchErrors(error, 'Cannot log in.');
}
return null;
}
async contribute(contribution, options) {
const contributionParams = contribution;
if (options && options.resolveSlugs) {
const additionalParams = await resolveSlugs(options.resolveSlugs);
Object.assign(contributionParams, additionalParams);
}
let method = 'post';
let url = new URL('/contributions', hcBackend);
if (options && options.slug) {
const existingContribution = await findBySlug('contributions', options.slug);
if (existingContribution) {
method = 'patch';
// eslint-disable-next-line no-underscore-dangle
url = new URL(`/contributions/${existingContribution._id}`, hcBackend);
} else {
contributionParams.slug = options.slug; // that's OK! Just create it with this slug.
}
}
await this.login();
try {
const response = await fetch(url, {
method,
body: JSON.stringify(contributionParams),
headers: {
Authorization: `Bearer ${this.accessToken}`,
'Content-Type': 'application/json',
},
});
const json = await toJson(response);
return json;
} catch (error) {
handleFetchErrors(error, 'Cannot create post.');
}
return null;
}
}
function connect(url) {
hcBackend = new URL(url);
}
module.exports = {
User,
connect,
};