-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
167 lines (150 loc) · 4.41 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
/**
* Magento 2 Promise API library
* File: index.js
* We need to define "use strict", so we're doing it here.
*/
"use strict";
/**
* Continue with the main functionality.
*/
const qs = require('qs');
const request = require('request');
const deepmerge = require('deepmerge');
const defaultOptions = {
url: null,
store: 'default',
userAgent: 'Fabacus Magento 2 Library',
authentication: {
login: {
type: 'admin',
username: undefined,
password: undefined
},
integration: {
consumer_key: undefined,
consumer_secret: undefined,
access_token: undefined,
access_token_secret: undefined
}
}
}
class MagentoTwo {
constructor(url, options) {
if(typeof url != 'string') {
options = url;
url = options.url;
}
options.url = url;
this.baseUrl = url;
this.options = deepmerge(defaultOptions, options);
this.rootPath = 'rest/'+this.options.store;
this.authKey = false;
}
init() {
return new Promise((resolve, reject) => {
this._initHelpers();
if(this.options.authentication.login.username) {
let path;
if(this.options.authentication.login.type == 'admin') {
path = '/V1/integration/admin/token'
} else {
path = '/V1/integration/customer/token'
}
this.post(path, {username: this.options.authentication.login.username, password: this.options.authentication.login.password})
.then(token => {
this.authKey = token;
resolve(this);
})
.catch(e => {
reject(e);
});
} else if(this.options.authentication.integration.access_token) {
this.authKey = this.options.authentication.integration.access_token;
}
resolve(this);
});
}
_initHelpers() {
this.module = require('./lib/module')(this);
this.bundleProduct = require('./lib/bundleProduct')(this);
this.configProduct = require('./lib/configProduct')(this);
this.catalog = require('./lib/catalog')(this);
this.sales = require('./lib/sales')(this);
this.customers = require('./lib/customers')(this);
}
get(path, params) {
return this._send(path, 'GET', params);
}
delete(path, params) {
return this._send(path, 'DELETE', params);
}
post(path, params, data) {
if(!data) {
data = params;
params = null;
}
return this._send(path, 'POST', params, data);
}
put(path, params, data) {
if(!data) {
data = params;
params = null;
}
return this._send(path, 'PUT', params, data);
}
_send(url, method, params, data, options) {
let uri = [this.baseUrl,this.rootPath,url].join('/').replace(/\/\//g,'/').replace(':/', '://');
//check if there's any missing parameters
let missingFields = uri.match(/(\{[a-zA-Z0-9_]+\})/g)
if(missingFields && missingFields.length > 0) {
return Promise.reject('URL missing parameters: '+missingFields.join(", "));
}
let headers = {}
headers['User-Agent'] = this.options.userAgent;
headers['Content-Type'] = 'application/json';
if(this.authKey) {
headers.Authorization = 'Bearer '+this.authKey;
}
return new Promise((resolve, reject) => {
request({
uri: uri,
method: method,
headers: headers,
rejectUnauthorized: false,
qs: params,
body: JSON.stringify(data)
}, (err, response, body) => {
if(err) reject(err);
let returnValue = body;
try {
returnValue = JSON.parse(returnValue);
} catch(e) {
return reject(e);
}
//success if the code is a 200 type
if(response.statusCode >= 200 && response.statusCode < 300) {
return resolve(returnValue);
}
//otherwise fail it :)
if(returnValue.parameters) {
reject(this._format(returnValue.message, returnValue.parameters, '%{key}'));
} else {
reject(returnValue.message);
}
})
})
}
_format(url, replace, replaceTemplate) {
replaceTemplate = replaceTemplate || '{{key}}';
for(let key of Object.keys(replace)) {
let replaceVal = replace[key];
let replaceKey = replaceTemplate.replace('{key}', key);
if(typeof replaceVal == 'undefined') {
throw new Error('Missing url parameter: '+replaceKey);
}
url = url.replace(replaceKey, replace[key]);
}
return url;
}
}
module.exports = MagentoTwo;