-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopify-product.js
61 lines (57 loc) · 1.68 KB
/
shopify-product.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
const request = require('request')
const assert = require('assert')
const C = require('./constants')
const config = {}
exports.init = (shopifyConfig) => {
assert.ok(shopifyConfig)
config.store = shopifyConfig.store
config.username = shopifyConfig.username
config.password = shopifyConfig.password
config.sharedSecret = shopifyConfig.sharedSecret
config.webhookKey = shopifyConfig.webhookKey
config.adminUrl = shopifyConfig.adminUrl
config.returnArray = false
if (shopifyConfig.returnArray) {
config.returnArray = shopifyConfig.returnArray
}
}
/**
* Returns all the products from the current store
*/
exports.products = () => {
const url = `${config.adminUrl}/${C.URL_PRODUCTS}${C.URL_JSON_FORMAT}`
return new Promise(
(resolve, reject) => {
request(url, (error, response, body) => {
if (error) { reject(error) }
const json = JSON.parse(body)
if (C.HTTP_STATUS_OK === response.statusCode) {
if (config.returnArray) { resolve(json.products) }
resolve(json)
} else {
reject(json)
}
}).auth(config.username, config.password, true)
}
)
}
/**
* Returns a product, given the shopify id
* @todo: array to string for id
*/
exports.product = (id) => {
assert.ok(id)
return new Promise(
(resolve, reject) => {
request(`${config.adminUrl}/${C.URL_PRODUCTS}${C.URL_JSON_FORMAT}?ids=${id}`, (error, response, body) => {
if (error) { reject(error) }
const json = JSON.parse(body)
if (C.HTTP_STATUS_OK === response.statusCode) {
resolve(json)
} else {
reject(json)
}
}).auth(config.username, config.password, true)
}
)
}