This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShopifyRequest.js
73 lines (60 loc) · 1.97 KB
/
ShopifyRequest.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
const Http = require("sf-core/net/http");
const ShopifyAuth = require("./ShopifyAuth");
const http = new Http();
require("./btoa.js");
const ShopifyRequest = function() {
var _method = "GET";
var _endpoint = null;
var _params = [];
this.exec = function(onSuccess) {
http.request(
{
headers: {
"Authorization": "Basic " + btoa(ShopifyAuth.getAPIKey() + ":" + ShopifyAuth.getPassword())
},
method: _method,
url: this.generateURL(),
onLoad: function(response) {
onSuccess(JSON.parse(response.body.toString()));
},
onError: function(error) {
console.log("ShopifyRequest: "+ error.message);
}
}
);
};
this.setMethod = function(method) {
_method = method;
};
this.setEndpoint = function(endpoint) {
_endpoint = endpoint;
};
this.addUriParam = function(param, value) {
_params.push({paramName: param, value: value});
};
this.generateURL = function() {
var url = this.generateBaseURL();
url += _endpoint;
for (var i = 0; i < _params.length; i++) {
url += (i == 0)? "?" : "&";
url += _params[i].paramName + "=" + (Array.isArray(_params[i].value)? _params[i].value.join(",") : _params[i].value);
}
return url;
};
this.generateBaseURL = function() {
return "https://" + ShopifyAuth.getStoreName() + ".myshopify.com/admin/";
};
this.limit = function(amount) {
this.addUriParam("limit", amount);
return this;
};
this.fields = function(fields) {
this.addUriParam("fields", fields);
return this;
};
this.collectionID = function(id) {
this.addUriParam("collection_id", id);
return this;
};
};
module.exports = ShopifyRequest;