-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (123 loc) · 4.54 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
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const request = require('request-promise');
const contentType = require('content-type');
class WidgetInstaller {
constructor(domain, login, apikey = null, needUpdate = 0) {
this.login = login;
this.apikey = apikey;
this.domain = domain;
this.needUpdate = needUpdate;
this._request = request.defaults({
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0',
'X-Requested-With': 'XMLHttpRequest'
},
jar: true,
transform(body, response) {
const ct = contentType.parse(response.headers['content-type']);
if (ct.type === 'application/json' || ct.type === 'text/json') {
return JSON.parse(body);
}
return body;
}
});
}
auth() {
return this._request.get(`https://${this.domain}.amocrm.ru/private/api/auth.php`, {
qs: {
'type': 'json',
'USER_LOGIN': this.login,
'USER_HASH': this.apikey
}
});
}
async createWidgetKey(code) {
const result = await this._request.post(`https://${this.domain}.amocrm.ru/ajax/settings/dev/`, {
formData: {action: 'create', code}
});
if ('error' in result) {
throw new Error(result.error);
} else {
return await this.findWidgetData(code);
}
}
async findWidgetData(code) {
const result = await this._request.get(`https://${this.domain}.amocrm.ru/ajax/settings/dev/`);
if ('error' in result) {
throw new Error(result.error);
} else {
const items = result.response.widgets.items;
for (let item of items) {
if (typeof item === 'object' && item.hasOwnProperty('code') && item.code === code) {
return [item['code'], item['secret_key']];
}
}
return [null, null];
}
}
async uploadWidget(archivePath, code, secretKey) {
const result = await this._request.post(`https://widgets.amocrm.ru/${this.domain}/upload/`, {
formData: {
' widget': {
value: fs.createReadStream(archivePath),
options: {
filename: 'widget.zip',
contentType: 'application/x-zip-compressed'
}
},
'secret': secretKey,
'widget': code,
'amouser': this.login,
'amohash': this.apikey,
'domain': `amocrm.ru`
}
});
const text = JSON.parse(result.replace(/^<script(.*)script>/, ''));
if ('error' in text) {
throw new Error(text.error)
}
}
async deleteWidget(code, secretKey) {
const result = await this._request.post(`https://widgets.amocrm.ru/${this.domain}/delete/`, {
formData: {
'secret': secretKey,
'widget': code,
'amouser': this.login,
'amohash': this.apikey,
}
});
const text = JSON.parse(result.replace(/^<script(.*)script>/, ''));
if ('error' in text) {
throw new Error(text.error)
}
}
static zipArchive(widgetFolder) {
return new Promise((resolve, reject) => {
const widgetPath = path.resolve('widget.zip');
const widgetFileStream = fs.createWriteStream(widgetPath);
const archive = archiver('zip', {
zlib: {level: 9}
});
widgetFileStream.on('close', () => {
resolve(widgetPath);
});
archive.on('error', (err) => {
reject(err);
});
archive.pipe(widgetFileStream);
archive.directory(path.resolve(widgetFolder), false);
archive.finalize();
});
}
static updateManifest(widgetFolder, code, secretKey) {
const manifestPath = path.resolve(widgetFolder, 'manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
manifest.widget.code = code;
manifest.widget.secret_key = secretKey;
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
return true;
}
}
module.exports = WidgetInstaller;