-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.mjs
179 lines (150 loc) · 5.31 KB
/
index.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
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
168
169
170
171
172
173
174
175
176
177
178
179
"use strict";
const _axios = require("axios");
const _crypto = require("crypto");
const _oAuth = require('oauth-1.0a');
const _url = require('url');
const _transform = require("./lib/transform");
const _parser = require("./lib/parser");
export default class MagentoRestApi {
constructor(options) {
options = options || {};
if (!(options.url)) {
throw new Error('URL is required');
}
if (!(options.consumerKey)) {
throw new Error('consumerKey is required');
}
if (!(options.consumerSecret)) {
throw new Error('consumerSecret is required');
}
if (!(options.accessToken)) {
throw new Error('accessToken is required');
}
if (!(options.tokenSecret)) {
throw new Error('tokenSecret is required');
}
this.clientVersion = require('./package.json').version;
this._setDefaults(options);
}
_setDefaults(opt) {
this.url = opt.url;
this.consumerKey = opt.consumerKey;
this.consumerSecret = opt.consumerSecret;
this.accessToken = opt.accessToken;
this.tokenSecret = opt.tokenSecret;
this.endpointType = opt.type || 'V1';
this.shaVersion = opt.sha || 1;
this.timeout = opt.timeout;
this.axiosConfig = opt.axiosConfig || {};
this.isSsl = /^https:\/\//i.test(this.url);
}
_normalizeQueryString(url) {
if (!(url.includes('?'))) {
return url;
}
let query = _url.parse(url, true).query;
let params = Object.keys(query);
let queryString = '';
for (let i in params) {
if (queryString.length) {
queryString += '&';
}
queryString += encodeURIComponent(params[i]).replace(/%5B/g, '[').replace(/%5D/g, ']');
queryString += '=';
queryString += encodeURIComponent(query[params[i]]);
}
return url.split('?')[0] + '?' + queryString;
}
_getSHAType() {
if (this.shaVersion === 256) {
return {
"signature": "HMAC-SHA256",
"hash": "sha256"
}
} else if (this.shaVersion === 1) {
return {
"signature": "HMAC-SHA1",
"hash": "sha1"
}
} else {
throw new Error('Invalid SHA Version Specified.');
}
}
_getOAuth(data) {
let sha = this._getSHAType();
let oauth = _oAuth({
consumer: {
key: this.consumerKey,
secret: this.consumerSecret
},
signature_method: sha.signature,
hash_function(base_string, key) {
return _crypto
.createHmac(sha.hash, key)
.update(base_string)
.digest('base64')
}
});
let token = {
key: this.accessToken,
secret: this.tokenSecret
}
return oauth.toHeader(oauth.authorize(data, token));
}
_formURL(endpoint) {
let accessibleUrl = this.url.slice(-1) === '/' ? this.url : this.url + '/';
if (!this.isSsl) {
endpoint = this._normalizeQueryString(endpoint);
}
return accessibleUrl + 'rest/' + this.endpointType + '/' + endpoint;
}
_formRequest(method, endpoint, data) {
let request_data = {
method: method,
url: this._formURL(endpoint),
body: data
}
let options = {
method: method,
url: request_data.url,
timeout: this.timeout,
headers: {
'Authorization': this._getOAuth(request_data).Authorization,
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: request_data.body
}
if (typeof process !== "undefined" && Object.prototype.toString.call(process) === "[object process]") {
options.headers["User-Agent"] = "Magento REST API - JS Client/" + this.clientVersion;
}
options = { ...options, ...this.axiosConfig };
return _axios(options);
}
_searchTranslate(params) {
let paramObjKeys = Object.keys(params);
let antiTrigger = ["filter_groups", "filterGroups", "sort_orders", "sortOrders", "page_size", "pageSize", "current_page", "currentPage"];
let parserTrigger = paramObjKeys.filter(val => antiTrigger.includes(val));
if (parserTrigger.length === 0) {
params = _parser(params);
}
return _transform(params);
}
get(endpoint, params) {
if (params && Object.keys(params).length > 0) {
endpoint += '?' + this._searchTranslate(params);
} else {
endpoint += '?searchCriteria='
}
return this._formRequest('GET', endpoint);
}
post(endpoint, data) {
return this._formRequest('POST', endpoint, data);
}
put(endpoint, data) {
return this._formRequest('PUT', endpoint, data);
}
delete(endpoint) {
return this._formRequest('DELETE', endpoint);
}
}