-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoltin.js
157 lines (126 loc) · 4.06 KB
/
moltin.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
'use strict';
const fs = require('fs');
const request = require('request-promise-native');
const MoltinGateway = require('@moltin/sdk').gateway;
const Moltin = MoltinGateway({
client_id: process.env.MOLTIN_CLIENT_ID,
client_secret: process.env.MOLTIN_CLIENT_SECRET
});
/*
Exposing the authenticate-if-needed logic
*/
const authenticate = async function(storage) {
const expired =
!storage.get('mtoken') || Date.now().toString() >= storage.get('mexpires');
return expired ? await Moltin.Authenticate() : undefined;
};
/*
Augmenting JS SDK with the CreateRelationships function for categories.
The buildRelationshipData is not publicly exposed through the API
so we will expect resources to be of the following shape:
[{ type: 'type', id: 'id'}]
We also expect the type to be already pluralized (e.g. categories vs. category).
We also need this "raw" version on Products to create variation relations.
The resource is called /variations and the type is product-variation.
The buildRelationshipData helper cannot handle it.
*/
const relate = function(id, type, resources) {
return this.request.send(
`${this.endpoint}/${id}/relationships/${type}`,
'POST',
resources
);
};
Moltin.Categories.CreateRelationships = relate;
Moltin.Products.CreateRelationshipsRaw = relate;
/*
Recursively delete all records to clean up the catalog
*/
const removeAll = function() {
const clean = async () => {
const { data, meta } = await this.All();
const total = meta && meta.results ? meta.results.all : data.length;
const current = meta && meta.results ? meta.results.total : data.length;
console.log('Processing the first %s of %s total', current, total);
for (let item of data) {
console.log(
'Requesting a delete of %s - %s',
item.name || item.code,
item.id
);
try {
await this.Delete(item.id);
} catch (error) {
if (Array.isArray(error)) {
error.forEach(console.error);
} else {
console.error(error);
}
}
}
return total > current ? await clean() : undefined;
};
return clean();
};
Moltin.Categories.RemoveAll = removeAll;
Moltin.Products.RemoveAll = removeAll;
Moltin.Currencies.RemoveAll = removeAll;
/*
Adding missing API wrappers. Reusing the existing protypes and instance variables.
*/
Moltin.Variations = Object.setPrototypeOf(
Object.assign({}, Moltin.Products),
Moltin.Products
);
Moltin.Variations.endpoint = 'variations';
Moltin.Files = Object.setPrototypeOf(
Object.assign({}, Moltin.Products),
Moltin.Products
);
Moltin.Files.endpoint = 'files';
/*
Need to overwrite Create() to stream binary files
The JS SDK can only send JSON payload
*/
Moltin.Files.Create = async function(file) {
const { config, storage } = this.request;
await authenticate(storage);
const url = `${config.protocol}://${config.host}/${config.version}`;
const response = await request({
uri: `${url}/${this.endpoint}`,
method: 'POST',
headers: {
Authorization: `Bearer: ${storage.get('mtoken')}`,
'Content-Type': 'multipart/form-data',
'X-MOLTIN-SDK-LANGUAGE': config.sdk.language,
'X-MOLTIN-SDK-VERSION': config.sdk.version
},
formData: {
public: 'true',
file_name: file.replace(/.+\//, ''),
file: fs.createReadStream(file)
}
});
return JSON.parse(response);
};
Moltin.Files.RemoveAll = removeAll;
/*
Varitions options and options modifiers require parent's context.
*/
Moltin.Variations.Options = function(variationId) {
const options = Object.setPrototypeOf(Object.assign({}, this), this);
options.endpoint = `variations/${variationId}/variation-options`;
options.Modifiers = function(optionId) {
const modifiers = Object.setPrototypeOf(Object.assign({}, this), this);
modifiers.endpoint = `variations/${variationId}/variation-options/${optionId}/product-modifiers`;
return modifiers;
};
return options;
};
/*
Expose the /build endpoint
*/
Moltin.Products.Build = function(id) {
return this.request.send(`${this.endpoint}/${id}/build`, 'POST');
};
module.exports = Moltin;